send email endpoint in laaproductionweb

This commit is contained in:
Stoyan Zlatev
2024-01-17 15:15:50 +01:00
parent 95565a0add
commit 1ed57bae10
45 changed files with 727 additions and 862 deletions
@@ -35,7 +35,7 @@
}
catch (Exception e)
{
this.NotifyStateChanged(e);
this.NotifyStateChanged($"--- {e}");
}
base.Close();
@@ -51,7 +51,7 @@
}
catch (Exception e)
{
this.NotifyStateChanged(e);
this.NotifyStateChanged($"--- {e}");
}
}
@@ -59,13 +59,13 @@
{
try
{
this.buffer.Clear();
this.waitHandle.Set();
this.NotifyStateChanged($"--- start listening");
}
catch (Exception e)
{
this.NotifyStateChanged(e);
this.NotifyStateChanged($"--- {e}");
}
}
@@ -75,18 +75,15 @@
{
this.waitHandle.Reset();
this.buffer.Clear();
this.NotifyStateChanged($"--- stop listening");
}
catch (Exception e)
{
this.NotifyStateChanged(e);
this.NotifyStateChanged($"--- {e}");
}
}
protected void WriteEncoded(params byte[] bytes)
{
this.EnsureOpened();
try
{
if (this.encoder.TryEncode(bytes, out byte[] message))
@@ -98,101 +95,69 @@
}
catch (Exception e)
{
this.NotifyStateChanged(e);
this.NotifyStateChanged($"--- {e}");
}
}
protected void WritePlain(params byte[] bytes)
{
this.EnsureOpened();
try
{
this.Write(bytes, 0, bytes.Length);
}
catch (Exception e)
{
this.NotifyStateChanged(e);
}
}
private void EnsureOpened()
{
if (this.serialPort is null)
{
this.Open();
}
else if (!this.serialPort.IsOpen)
{
this.Close();
this.Open();
this.NotifyStateChanged($"--- {e}");
}
}
private void ReadingTaskHandler()
{
while (true)
while (this.IsOpen)
{
this.waitHandle.Wait();
try
{
var size = this.serialPort.BytesToRead;
var length = this.BytesToRead;
if (size > 0)
if (length > 0)
{
var buffer = new byte[length];
this.Read(buffer, 0, length);
this.buffer.AddRange(buffer);
var data = this.buffer.ToArray();
if (this.encoder.TryDecode(data, out byte[] message, out int start, out int _))
{
var buffer = new byte[size];
this.DataReceived?.Invoke(message);
this.serialPort.Read(buffer, 0, size);
this.buffer.AddRange(buffer);
this.NotifyStateChanged($"<<< {BitConverter.ToString(message)}");
}
var data = this.buffer.ToArray();
if (this.encoder.TryDecode(data, out byte[] message, out int start, out int _))
{
this.DataReceived?.Invoke(message);
this.NotifyStateChanged($"<<< {BitConverter.ToString(message)}");
}
if (start < 0)
{
this.buffer.Clear();
}
if (start < 0)
{
this.buffer.Clear();
}
}
catch (Exception e)
{
this.NotifyStateChanged(e);
}
}
}
private void StartReading()
{
this.cancellationTokenSource = new CancellationTokenSource();
this.cancellationToken = this.cancellationTokenSource.Token;
this.readingTask = Task.Factory
.StartNew(this.ReadingTaskHandler, this.cancellationToken);
this.readingTask = Task.Factory.StartNew(this.ReadingTaskHandler, this.cancellationToken);
this.readingTask.ContinueWith(task =>
{
if (task.Exception != null)
{
this.NotifyStateChanged(task.Exception);
this.NotifyStateChanged($"--- {task.Exception}");
}
this.StartReading();
});
}
private void Write(byte[] bytes, int start, int length)
{
var _lock = new object();
lock (_lock)
{
this.serialPort.Write(bytes, 0, length);
}
}
}
}
@@ -23,6 +23,32 @@
public event StateChanged StateChanged;
public int BytesToRead
{
get
{
if (this.IsOpen)
{
return this.serialPort.BytesToRead;
}
return default(int);
}
}
public bool IsOpen
{
get
{
if (this.serialPort is null)
{
return false;
}
return this.serialPort.IsOpen;
}
}
public virtual void Close()
{
try
@@ -33,12 +59,12 @@
this.serialPort.Dispose();
this.serialPort = null;
this.NotifyStateChanged($"--- {this} - disconnected.");
this.NotifyStateChanged($"--- Disconnected: {this}");
}
}
catch (Exception e)
{
this.NotifyStateChanged(e);
this.NotifyStateChanged($"--- {e}");
}
}
@@ -58,11 +84,11 @@
this.serialPort.Open();
this.NotifyStateChanged($"--- {this} - connected.");
this.NotifyStateChanged($"--- Connected: {this}");
}
catch (Exception e)
{
this.NotifyStateChanged(e);
this.NotifyStateChanged($"--- {e}");
}
}
@@ -92,5 +118,20 @@
{
this.StateChanged?.Invoke(state);
}
protected void Read(byte[] bytes, int start, int length)
{
this.serialPort.Read(bytes, start, length);
}
protected void Write(byte[] bytes, int start, int length)
{
var _lock = new object();
lock (_lock)
{
this.serialPort.Write(bytes, 0, length);
}
}
}
}
+27
View File
@@ -1,6 +1,33 @@
namespace OmniPlus
{
using OmniPlus.Features.Enums;
public class OmniMeter
{
const string NA = "n/a";
private OmniConnection connection;
public void ConnectIrda(string portName)
{
if (this.connection != null)
{
this.connection.Close();
}
this.connection = new OmniIrdaConnection(portName);
this.connection.Open();
}
public void ConnectUniPro(string portName)
{
if (this.connection != null)
{
this.connection.Close();
}
this.connection = new OmniUniProConnection(portName);
this.connection.Open();
}
}
}