This commit is contained in:
Roland Drabesch
2025-02-04 10:11:59 +01:00
297 changed files with 7078 additions and 13993 deletions
@@ -1,32 +1,54 @@
namespace Common.Hardware.Ports
{
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public static class SerialDataExtensions
{
private const int ByteSize = 8;
private const int SByteSize = 16;
private const int Int16Size = 16;
private const int UInt16Size = 16;
private const int Int32Size = 32;
private const int UInt32Size = 32;
private const int Int64Size = 64;
private const int UInt64Size = 64;
public static byte[] GetBitsBE(this byte value)
{
var bytes = new byte[8];
var bits = new byte[ByteSize];
for (int i = 7, x = 0; i >= 0; i--, x++)
{
bytes[x] = (byte)((value >> i) % 2);
bits[x] = (byte)((value >> i) & 1);
}
return bytes;
return bits;
}
public static byte[] GetBitsLE(this byte value)
{
var bytes = new byte[8];
var bits = new byte[ByteSize];
for (var i = 0; i < 8; i++)
for (var i = 0; i < ByteSize; i++)
{
bytes[i] = (byte)((value >> i) % 2);
bits[i] = (byte)((value >> i) & 1);
}
return bytes;
return bits;
}
public static byte[] GetBitsLE(this ushort value)
{
var bits = new byte[UInt16Size];
for (var i = 0; i < UInt16Size; i++)
{
bits[i] = (byte)((value >> i) & 1);
}
return bits;
}
public static byte GetByteLE(this byte[] bits)
@@ -38,7 +60,7 @@
var value = default(byte);
for (var i = 0; i < 8; i++)
for (var i = 0; i < ByteSize; i++)
{
value ^= (byte)(bits[i] << i);
}
@@ -417,6 +439,27 @@
return value;
}
public static ushort ToUIint16LEFromBits(this byte[] bits)
{
const int size = 16;
if (bits is null)
{
bits = new byte[size];
}
Array.Resize(ref bits, size);
ushort value = 0;
for (var i = 0; i < size; i++)
{
value ^= (ushort)(bits[i] << i);
}
return value;
}
public static uint ToUInt32LE(this byte[] bytes, int start = 0)
{
var value = default(uint);
@@ -21,13 +21,14 @@
this.COMPort = name;
this.readAwaiter = new ManualResetEventSlim();
this.closeAwaiter = new ManualResetEventSlim();
var sp = new SysSerialPort(this.COMPort, baudRate, parity, dataBits, stopBits);
serialPorts.AddOrUpdate(
key: this.COMPort,
addValue: sp,
updateValueFactory: (_1, _2) => sp);
serialPorts.AddOrUpdate(this.COMPort, sp, (key, value) =>
{
value?.Close();
return sp;
});
}
public event Action Connected;
@@ -36,9 +37,7 @@
public string COMPort { get; }
public bool IsOpen
=> serialPorts.TryGetValue(this.COMPort, out var sp)
&& sp.IsOpen;
public bool IsOpen => serialPorts.TryGetValue(this.COMPort, out var sp) && sp.IsOpen;
public void Close()
{
@@ -47,7 +46,7 @@
try
{
sp.Close();
this.closeAwaiter.Wait(3000);
this.closeAwaiter.Wait(5000);
this.closeAwaiter.Reset();
sp.DataReceived -= this.SP_DataReceived;
@@ -82,7 +81,7 @@
public IEnumerable<byte[]> ReadBytes()
{
var sp = this.GetOpenSP();
var sp = this.GetOpenedSP();
var size = sp.ReadBufferSize;
var buffer = new byte[size];
var length = 0;
@@ -109,12 +108,14 @@
yield return buffer;
}
sp.Close();
}
}
public IEnumerable<string> ReadLines()
{
var sp = this.GetOpenSP();
var sp = this.GetOpenedSP();
var text = default(string);
using (sp)
@@ -139,15 +140,17 @@
}
}
public void RemoveConnection()
public void Remove()
{
this.Close();
serialPorts.TryRemove(this.COMPort, out _);
if (serialPorts.TryRemove(this.COMPort, out var sp))
{
sp.Close();
}
}
public void Write(byte[] bytes)
{
var sp = this.GetOpenSP();
var sp = this.GetOpenedSP();
try
{
@@ -161,7 +164,7 @@
public void Write(string text)
{
var sp = this.GetOpenSP();
var sp = this.GetOpenedSP();
try
{
@@ -173,7 +176,7 @@
}
}
private SysSerialPort GetOpenSP()
private SysSerialPort GetOpenedSP()
{
if (!serialPorts.TryGetValue(this.COMPort, out var sp))
{
@@ -182,6 +185,7 @@
if (!sp.IsOpen)
{
this.Close();
this.Open();
}
@@ -115,6 +115,8 @@
private void SerialPort_Connected()
{
this.IsConnected = true;
this.cancellationTokenSource = new CancellationTokenSource();
this.cancellationToken = this.cancellationTokenSource.Token;
@@ -130,6 +132,9 @@
private void SerialPort_Disconnected()
{
this.IsActivated = false;
this.IsConnected = false;
try
{
this.cancellationTokenSource?.Cancel();
@@ -158,26 +163,20 @@
private void StartProcessingTasksAsync()
{
this.IsActivated = true;
this.processingTask = Task.Factory.StartNew(() =>
{
var manualResetEventSlim = new ManualResetEventSlim();
var addresses = default(ICollection<uint>);
while (!this.cancellationToken.IsCancellationRequested)
{
// addresses = this.GetAddressesInPamPool();
foreach (var tasksQueue in this.tasks.Values.ToArray())
{
if (tasksQueue.TryPeek(out var task) && task.State == SIRTTaskState.Pending)
{
// if (addresses.Count < 5 && !addresses.Any(x => x == task.RequestAddress) && !addresses.Any(x => x == task.ResponseAddress))
{
this.serialPort.Write(task.Start());
this.Logging?.Invoke(task.ToString());
// addresses.Add(task.RequestAddress);
}
this.serialPort.Write(task.Start());
this.Logging?.Invoke(task.ToString());
}
manualResetEventSlim.Wait(100);
@@ -28,6 +28,10 @@
public int Frequency { get; protected set; }
public bool IsConnected { get; protected set; }
public bool IsActivated { get; protected set; }
public void Close()
{
this.serialPort.Close();
@@ -84,6 +88,6 @@
}
public void RemoveConnection()
=> this.serialPort.RemoveConnection();
=> this.serialPort.Remove();
}
}
@@ -7,84 +7,95 @@
public class SIRTFactory
{
private readonly ConcurrentDictionary<string, SIRTBroadcaster> connections
private readonly ConcurrentDictionary<string, SIRTBroadcaster> broadcasters
= new ConcurrentDictionary<string, SIRTBroadcaster>();
public event Action<string> Logging;
public void CloseConnection(string portName)
{
if (this.connections.TryGetValue(portName, out var connection))
if (this.broadcasters.TryGetValue(portName, out var broadcaster))
{
connection.Close();
broadcaster.Logging -= this.NotifyMessage;
broadcaster.Close();
}
}
public void CloseConnections()
{
foreach (var connection in this.GetConnections())
foreach (var broadcaster in this.GetConnections())
{
connection.Close();
broadcaster.Logging -= this.NotifyMessage;
broadcaster.Close();
}
}
public void DequeueTasks(uint address)
{
foreach (var connection in this.GetConnections())
foreach (var broadcaster in this.GetConnections())
{
connection.DequeueTasks(address);
broadcaster.DequeueTasks(address);
}
}
public void OpenConnections()
{
foreach (var connection in this.GetConnections())
foreach (var broadcaster in this.GetConnections())
{
connection.Open();
broadcaster.Logging += this.NotifyMessage;
broadcaster.Open();
}
}
public void ConnectSIRT(string portName, Action<SIRTBroadcaster> callback)
{
if (!this.connections.TryGetValue(portName, out var broadcaster))
if (!this.broadcasters.TryGetValue(portName, out var broadcaster))
{
broadcaster = new SIRTBroadcaster(portName);
broadcaster.Logging += message => this.Logging?.Invoke(message);
broadcaster.Logging += this.NotifyMessage;
if (!this.connections.TryAdd(portName, broadcaster))
if (!this.broadcasters.TryAdd(portName, broadcaster))
{
broadcaster = null;
}
}
callback(broadcaster);
broadcaster?.Close();
broadcaster?.Open();
if (broadcaster?.IsActivated == false)
{
broadcaster?.Close();
broadcaster?.Open();
}
}
public void EnqueueTask(SIRTTask task)
{
foreach (var connection in this.GetConnections())
foreach (var broadcaster in this.GetConnections())
{
connection.EnqueueTask(task);
broadcaster.EnqueueTask(task);
}
}
public IEnumerable<string> GetPortNames()
=> this.connections.Keys.ToArray();
=> this.broadcasters.Keys.ToArray();
public void RemoveConnections()
{
foreach (var key in this.connections.Keys.ToArray())
foreach (var key in this.broadcasters.Keys.ToArray())
{
if (this.connections.TryRemove(key, out var broadcaster))
if (this.broadcasters.TryRemove(key, out var broadcaster))
{
broadcaster.Logging -= this.NotifyMessage;
broadcaster.RemoveConnection();
}
}
}
private void NotifyMessage(string message)
=> this.Logging?.Invoke(message);
private IEnumerable<SIRTBroadcaster> GetConnections()
=> this.connections.Values.ToArray();
=> this.broadcasters.Values.ToArray();
}
}
@@ -100,14 +100,10 @@
private EregisterProgrammingParameters GetProgrammingParameters()
{
var alarms = new AlarmsMask(this.SEMI.AlarmActiveInformation);
var dataLogContent = new ChangeDataLoggerSettings
{
LogContent = this.SEMI.LogContent
};
var fdrLogContent = new ChangeDataLoggerSettings
{
LogContent = this.SEMI.FixedDateReadingContent
};
var dataLogContent = new ChangeDataLoggerSettings();
dataLogContent.SetLogContent(this.SEMI.LogContent);
var fdrLogContent = new ChangeDataLoggerSettings();
fdrLogContent.SetLogContent(this.SEMI.FixedDateReadingContent);
var parameters = new EregisterProgrammingParameters
{
@@ -207,7 +207,7 @@
if (authLevel != AuthLevel.O)
{
var _bytes = ((long)authLevel).GetBytesLE();
var _bytes = ((ulong)authLevel).GetBytesLE();
Array.Resize(ref bytes, 6);
Array.Copy(_bytes, 0, bytes, 0, 6);
@@ -4,96 +4,106 @@
public class ChangeDataLoggerSettings : Pam
{
private readonly byte[] contentBits = new byte[16];
public ChangeDataLoggerSettings() : base(5, ChangeDataLoggerSettings)
{
}
public ushort TimeInterval { get; set; }
public ushort LogContent { get; set; }
public byte ForwardCounter
public ushort LogContent => this.contentBits.ToUIint16LEFromBits();
public bool ForwardCounter
{
get => (byte)(this.LogContent >> 12 & 1);
set => this.LogContent ^= (ushort)(value << 12);
get => this.contentBits[12] == 1;
set => this.contentBits[12] = (byte)(value ? 1 : 0);
}
public byte TimeOfMinimumFlow
public bool TimeOfMinimumFlow
{
get => (byte)(this.LogContent >> 11 & 1);
set => this.LogContent ^= (ushort)(value << 11);
get => this.contentBits[11] == 1;
set => this.contentBits[11] = (byte)(value ? 1 : 0);
}
public byte MinimumFlow
public bool MinimumFlow
{
get => (byte)(this.LogContent >> 10 & 1);
set => this.LogContent ^= (ushort)(value << 10);
get => this.contentBits[10] == 1;
set => this.contentBits[10] = (byte)(value ? 1 : 0);
}
public byte TimeOfBrokenPipe
public bool TimeOfBrokenPipe
{
get => (byte)(this.LogContent >> 9 & 1);
set => this.LogContent ^= (ushort)(value << 9);
get => this.contentBits[9] == 1;
set => this.contentBits[9] = (byte)(value ? 1 : 0);
}
public byte BrokenPipeFlow
public bool BrokenPipeFlow
{
get => (byte)(this.LogContent >> 8 & 1);
set => this.LogContent ^= (ushort)(value << 8);
get => this.contentBits[8] == 1;
set => this.contentBits[8] = (byte)(value ? 1 : 0);
}
public byte CurrentFlow
public bool CurrentFlow
{
get => (byte)(this.LogContent >> 7 & 1);
set => this.LogContent ^= (ushort)(value << 7);
get => this.contentBits[7] == 1;
set => this.contentBits[7] = (byte)(value ? 1 : 0);
}
public byte TimeOfPeakFlow
public bool TimeOfPeakFlow
{
get => (byte)(this.LogContent >> 6 & 1);
set => this.LogContent ^= (ushort)(value << 6);
get => this.contentBits[6] == 1;
set => this.contentBits[6] = (byte)(value ? 1 : 0);
}
public byte PeakFlow
public bool PeakFlow
{
get => (byte)(this.LogContent >> 5 & 1);
set => this.LogContent ^= (ushort)(value << 5);
get => this.contentBits[5] == 1;
set => this.contentBits[5] = (byte)(value ? 1 : 0);
}
public byte TimeOfMaximumFlow
public bool TimeOfMaximumFlow
{
get => (byte)(this.LogContent >> 4 & 1);
set => this.LogContent ^= (ushort)(value << 4);
get => this.contentBits[4] == 1;
set => this.contentBits[4] = (byte)(value ? 1 : 0);
}
public byte MaximumFlow
public bool MaximumFlow
{
get => (byte)(this.LogContent >> 3 & 1);
set => this.LogContent ^= (ushort)(value << 3);
get => this.contentBits[3] == 1;
set => this.contentBits[3] = (byte)(value ? 1 : 0);
}
public byte BackwardVolume
public bool BackwardVolume
{
get => (byte)(this.LogContent >> 2 & 1);
set => this.LogContent ^= (ushort)(value << 2);
get => this.contentBits[2] == 1;
set => this.contentBits[2] = (byte)(value ? 1 : 0);
}
public byte Counter
public bool Counter
{
get => (byte)(this.LogContent >> 1 & 1);
set => this.LogContent ^= (ushort)(value << 1);
get => this.contentBits[1] == 1;
set => this.contentBits[1] = (byte)(value ? 1 : 0);
}
public byte AlarmState
public bool AlarmState
{
get => (byte)(this.LogContent >> 0 & 1);
set => this.LogContent ^= (ushort)(value << 0);
get => this.contentBits[0] == 1;
set => this.contentBits[0] = (byte)(value ? 1 : 0);
}
public void SetLogContent(ushort value)
=> value.GetBitsLE()
.CopyTo(this.contentBits, 0);
public override byte[] ToByteArray()
{
this.TimeInterval.GetBytesBE().CopyTo(this.bytes, 1);
this.LogContent.GetBytesBE().CopyTo(this.bytes, 3);
this.TimeInterval
.GetBytesBE()
.CopyTo(this.bytes, 1);
this.LogContent
.GetBytesBE()
.CopyTo(this.bytes, 3);
return base.ToByteArray();
}
@@ -4,6 +4,8 @@
public class ChangeFixedDateReadingSettings : Pam
{
private readonly byte[] contentBits = new byte[16];
public ChangeFixedDateReadingSettings() : base(4, ChangeFixedDateReadingSettings)
{
}
@@ -14,88 +16,97 @@
set => this.bytes[1] = value;
}
public ushort LogContent
public ushort LogContent => this.contentBits.ToUIint16LEFromBits();
public bool ForwardCounter
{
get => this.bytes.ToUInt16BE(2);
set => value.GetBytesBE().CopyTo(this.bytes, 2);
get => this.contentBits[12] == 1;
set => this.contentBits[12] = (byte)(value ? 1 : 0);
}
public byte ForwardCounter
public bool TimeOfMinimumFlow
{
get => (byte)(this.LogContent >> 12 & 1);
set => this.LogContent ^= (ushort)(value << 12);
get => this.contentBits[11] == 1;
set => this.contentBits[11] = (byte)(value ? 1 : 0);
}
public byte TimeOfMinimumFlow
public bool MinimumFlow
{
get => (byte)(this.LogContent >> 11 & 1);
set => this.LogContent ^= (ushort)(value << 11);
get => this.contentBits[10] == 1;
set => this.contentBits[10] = (byte)(value ? 1 : 0);
}
public byte MinimumFlow
public bool TimeOfBrokenPipe
{
get => (byte)(this.LogContent >> 10 & 1);
set => this.LogContent ^= (ushort)(value << 10);
get => this.contentBits[9] == 1;
set => this.contentBits[9] = (byte)(value ? 1 : 0);
}
public byte TimeOfBrokenPipe
public bool BrokenPipeFlow
{
get => (byte)(this.LogContent >> 9 & 1);
set => this.LogContent ^= (ushort)(value << 9);
get => this.contentBits[8] == 1;
set => this.contentBits[8] = (byte)(value ? 1 : 0);
}
public byte BrokenPipeFlow
public bool CurrentFlow
{
get => (byte)(this.LogContent >> 8 & 1);
set => this.LogContent ^= (ushort)(value << 8);
get => this.contentBits[7] == 1;
set => this.contentBits[7] = (byte)(value ? 1 : 0);
}
public byte CurrentFlow
public bool TimeOfPeakFlow
{
get => (byte)(this.LogContent >> 7 & 1);
set => this.LogContent ^= (ushort)(value << 7);
get => this.contentBits[6] == 1;
set => this.contentBits[6] = (byte)(value ? 1 : 0);
}
public byte TimeOfPeakFlow
public bool PeakFlow
{
get => (byte)(this.LogContent >> 6 & 1);
set => this.LogContent ^= (ushort)(value << 6);
get => this.contentBits[5] == 1;
set => this.contentBits[5] = (byte)(value ? 1 : 0);
}
public byte PeakFlow
public bool TimeOfMaximumFlow
{
get => (byte)(this.LogContent >> 5 & 1);
set => this.LogContent ^= (ushort)(value << 5);
get => this.contentBits[4] == 1;
set => this.contentBits[4] = (byte)(value ? 1 : 0);
}
public byte TimeOfMaximumFlow
public bool MaximumFlow
{
get => (byte)(this.LogContent >> 4 & 1);
set => this.LogContent ^= (ushort)(value << 4);
get => this.contentBits[3] == 1;
set => this.contentBits[3] = (byte)(value ? 1 : 0);
}
public byte MaximumFlow
public bool BackwardVolume
{
get => (byte)(this.LogContent >> 3 & 1);
set => this.LogContent ^= (ushort)(value << 3);
get => this.contentBits[2] == 1;
set => this.contentBits[2] = (byte)(value ? 1 : 0);
}
public byte BackwardVolume
public bool Counter
{
get => (byte)(this.LogContent >> 2 & 1);
set => this.LogContent ^= (ushort)(value << 2);
get => this.contentBits[1] == 1;
set => this.contentBits[1] = (byte)(value ? 1 : 0);
}
public byte Counter
public bool AlarmState
{
get => (byte)(this.LogContent >> 1 & 1);
set => this.LogContent ^= (ushort)(value << 1);
get => this.contentBits[0] == 1;
set => this.contentBits[0] = (byte)(value ? 1 : 0);
}
public byte AlarmState
public void SetLogContent(ushort value)
=> value.GetBitsLE()
.CopyTo(this.contentBits, 0);
public override byte[] ToByteArray()
{
get => (byte)(this.LogContent >> 0 & 1);
set => this.LogContent ^= (ushort)(value << 0);
this.LogContent
.GetBytesBE()
.CopyTo(this.bytes, 3);
return base.ToByteArray();
}
}
}
@@ -34,7 +34,9 @@
public string HexKey
{
get => BitConverter.ToString(this.Key).Replace("-", string.Empty);
get => BitConverter
.ToString(this.Key)
.Replace("-", string.Empty);
set => this.Key = value.GetEncryptionKey();
}
}
@@ -5,6 +5,8 @@
protected DebugCmd(byte code) : base(3, GetDebug)
=> this.bytes[2] = code;
public override string CmdHex => $"{base.CmdHex}-{this.bytes[2]:X2}";
public static implicit operator byte[](DebugCmd cmd) => cmd.bytes;
/// <summary>
@@ -12,6 +12,8 @@
this.bytes[0] = cmd;
}
public virtual string CmdHex => $"{this.bytes[0]:X2}";
public Pam Append(Pam pam)
{
var appendBytes = pam.ToByteArray();
@@ -123,85 +123,85 @@
public ushort Data_Logging_Period { get; set; }
[CompareProperty]
public byte Data_Logging_Forward_Counter { get; set; }
public bool Data_Logging_Forward_Counter { get; set; }
[CompareProperty]
public byte Data_Logging_Time_Of_Minimum_Flow { get; set; }
public bool Data_Logging_Time_Of_Minimum_Flow { get; set; }
[CompareProperty]
public byte Data_Logging_Minimum_Flow { get; set; }
public bool Data_Logging_Minimum_Flow { get; set; }
[CompareProperty]
public byte Data_Logging_Time_Of_Broken_Pipe { get; set; }
public bool Data_Logging_Time_Of_Broken_Pipe { get; set; }
[CompareProperty]
public byte Data_Logging_Broken_Pipe_Flow { get; set; }
public bool Data_Logging_Broken_Pipe_Flow { get; set; }
[CompareProperty]
public byte Data_Logging_Current_Flow { get; set; }
public bool Data_Logging_Current_Flow { get; set; }
[CompareProperty]
public byte Data_Logging_Time_Of_Peak_Flow { get; set; }
public bool Data_Logging_Time_Of_Peak_Flow { get; set; }
[CompareProperty]
public byte Data_Logging_Peak_Flow { get; set; }
public bool Data_Logging_Peak_Flow { get; set; }
[CompareProperty]
public byte Data_Logging_Time_Of_Maximum_Flow { get; set; }
public bool Data_Logging_Time_Of_Maximum_Flow { get; set; }
[CompareProperty]
public byte Data_Logging_Maximum_Flow { get; set; }
public bool Data_Logging_Maximum_Flow { get; set; }
[CompareProperty]
public byte Data_Logging_Backward_Volume { get; set; }
public bool Data_Logging_Backward_Volume { get; set; }
[CompareProperty]
public byte Data_Logging_Counter { get; set; }
public bool Data_Logging_Counter { get; set; }
[CompareProperty]
public byte Data_Logging_Alarm_State { get; set; }
public bool Data_Logging_Alarm_State { get; set; }
[CompareProperty]
public byte FDR_Day_Of_Data_Reading { get; set; }
[CompareProperty]
public byte FDR_Forward_Counter { get; set; }
public bool FDR_Forward_Counter { get; set; }
[CompareProperty]
public byte FDR_Time_Of_Minimum_Flow { get; set; }
public bool FDR_Time_Of_Minimum_Flow { get; set; }
[CompareProperty]
public byte FDR_Minimum_Flow { get; set; }
public bool FDR_Minimum_Flow { get; set; }
[CompareProperty]
public byte FDR_Time_Of_Broken_Pipe { get; set; }
public bool FDR_Time_Of_Broken_Pipe { get; set; }
[CompareProperty]
public byte FDR_Broken_Pipe_Flow { get; set; }
public bool FDR_Broken_Pipe_Flow { get; set; }
[CompareProperty]
public byte FDR_Current_Flow { get; set; }
public bool FDR_Current_Flow { get; set; }
[CompareProperty]
public byte FDR_Time_Of_Peak_Flow { get; set; }
public bool FDR_Time_Of_Peak_Flow { get; set; }
[CompareProperty]
public byte FDR_Peak_Flow { get; set; }
public bool FDR_Peak_Flow { get; set; }
[CompareProperty]
public byte FDR_Time_Of_Maximum_Flow { get; set; }
public bool FDR_Time_Of_Maximum_Flow { get; set; }
[CompareProperty]
public byte FDR_Maximum_Flow { get; set; }
public bool FDR_Maximum_Flow { get; set; }
[CompareProperty]
public byte FDR_Backward_Volume { get; set; }
public bool FDR_Backward_Volume { get; set; }
[CompareProperty]
public byte FDR_Counter { get; set; }
public bool FDR_Counter { get; set; }
[CompareProperty]
public byte FDR_Alarm_State { get; set; }
public bool FDR_Alarm_State { get; set; }
public byte PowerLevelHex { get; set; }