BUP insted LAT message

This commit is contained in:
Stoyan Zlatev
2025-04-30 12:27:07 +02:00
parent afc19d9fd5
commit d0cc675252
11 changed files with 44 additions and 54 deletions
@@ -28,6 +28,11 @@
{
this.sirtStream = new SIRTStream(portName);
this.pampool = new ConcurrentDictionary<byte, uint>();
_ = this.pampool.GetOrAdd(0, 0);
_ = this.pampool.GetOrAdd(0, 0);
_ = this.pampool.GetOrAdd(0, 0);
_ = this.pampool.GetOrAdd(0, 0);
_ = this.pampool.GetOrAdd(0, 0);
this.tasks = new ConcurrentDictionary<uint, SIRTTask>();
}
@@ -89,9 +94,9 @@
{
this.Id = id;
this.Frequency = frequency;
this.pampoolStream = Task.Factory
.StartNew(this.PampoolAsync, this.cancellationToken)
.ContinueWith(this.LogContinuationError);
//this.pampoolStream = Task.Factory
// .StartNew(this.PampoolAsync, this.cancellationToken)
// .ContinueWith(this.LogContinuationError);
var connectedEvent = this.Connected;
@@ -118,21 +123,26 @@
if (task.Frequency == this.Frequency)
{
var address = task.RequestAddress;
var addresses = this.pampool.Values.AsEnumerable();
var addresses = this.pampool.Values.ToList();
if (this.pampool.Count < 5 || addresses.Any(x => x == 0 || x == address))
if (addresses.Any(x => x == 0 || x == address))
{
var newTask = this.tasks.AddOrUpdate(
key: address,
addValueFactory: addr => task,
updateValueFactory: (addr, oldTask) => task);
var bytes = newTask.Start();
var bytes = this.tasks
.AddOrUpdate(address, addr => task, (addr, oldTask) => task)
.Start();
this.sirtStream.Write(bytes);
SIRTLogger.LogMessage($"{this.PortName}|TX: {BitConverter.ToString(bytes)}");
return true;
}
else
{
SIRTLogger.LogMessage($"{this.PortName}|PP: {string.Join(", ", addresses)}");
}
}
else
{
SIRTLogger.LogMessage($"{this.PortName}|RF: {task.RequestAddress} -> {task.Frequency} != {this.Frequency}");
}
return false;
@@ -146,33 +156,9 @@
}
}
private void DeleteFromPamPool(SIRTTask task)
{
var waitEvent = new ManualResetEventSlim();
var deleteCmd = new DeleteFromPamPoolTask
{
Frequency = this.Frequency,
RequestAddress = task.RequestAddress,
}.Start();
this.sirtStream.Write(deleteCmd);
waitEvent.Wait(300);
waitEvent.Reset();
deleteCmd = new DeleteFromPamPoolTask
{
Frequency = this.Frequency,
RequestAddress = task.ResponseAddress,
}.Start();
this.sirtStream.Write(deleteCmd);
waitEvent.Wait(300);
waitEvent.Reset();
}
private void PampoolAsync()
{
var task = new ReadPampoolTask();
var task = new ReadPampoolTask(this.Frequency);
var addresses = task.Addresses;
var addressesCount = addresses.Length;
@@ -212,7 +198,7 @@
if (this.tasks.TryGetValue(message.Address, out var task))
{
task.Push(message);
task?.Push(message);
}
if (messageReceivedEvent != null)
@@ -47,7 +47,7 @@
// when no start byte found clear and break
if (startIndex < 0)
{
SIRTLogger.LogState("Message start not found");
SIRTLogger.LogState($"Message start not found startIndex: {startIndex} < 0");
buffer.Clear();
@@ -59,7 +59,7 @@
// when not enough bytes to the length index just break
if (lengthIndex >= bufferLength)
{
SIRTLogger.LogState("Message not complete");
SIRTLogger.LogState($"Message not complete lengthIndex: {lengthIndex} >= {bufferLength} :bufferLength");
break;
}
@@ -70,7 +70,7 @@
// when not enough bytes to the end of the message just break
if (endIndex >= bufferLength)
{
SIRTLogger.LogState("Message not complete");
SIRTLogger.LogState($"Message not complete endIndex: {endIndex} >= {bufferLength} :bufferLength");
break;
}
@@ -89,7 +89,7 @@
message = new byte[messageLength];
buffer.CopyTo(startIndex, message, 0, messageLength);
buffer.RemoveRange(0, endIndex + 1);
buffer.RemoveRange(0, endIndex);
yield return message;
@@ -4,9 +4,9 @@
public class ReadPampoolTask : ReadPamTask
{
public ReadPampoolTask()
public ReadPampoolTask(int frequency)
{
this.Frequency = this.Frequency;
this.Frequency = frequency;
this.RequestAddress = 0xFFFFFFFF;
this.Addresses = new uint[5];
this.Timeout = 3_000;
@@ -7,6 +7,7 @@
{
protected readonly ManualResetEventSlim awaiter = new ManualResetEventSlim();
protected readonly SIRTMessage request = new SIRTMessage();
protected readonly Guid taskId = Guid.NewGuid();
public event Action<SIRTTask> StateChanged;
@@ -92,11 +93,11 @@
public virtual byte[] Start()
{
this.awaiter.Reset();
this.State = SIRTTaskState.Running;
this.Timestamp = DateTimeOffset.UtcNow;
this.awaiter.Reset();
return this.request.GetBytes();
}
@@ -106,6 +107,9 @@
this.awaiter.Reset();
}
public override bool Equals(object other)
=> other is SIRTTask _task && _task.taskId == this.taskId;
public static implicit operator SIRTMessage(SIRTTask task) => task.request;
}
}
@@ -22,7 +22,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<NoWarn>IDE0003;IDE0011;IDE0049;</NoWarn>
<NoWarn>IDE0003;IDE0011;IDE0049;IDE1005;</NoWarn>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
@@ -2,6 +2,7 @@
{
using Common.Hardware.SIRT;
using Common.Hardware.SIRT.Tasks;
using Common.Hardware.WaterMeter.eRegister.Features;
using Common.Hardware.WaterMeter.eRegister.Models;
using System;
@@ -153,6 +154,8 @@
// break;
// }
this.UpdateTasksStateInTheQueue(task);
if (task.State == SIRTTaskState.Completed)
{
if (!this.runningTasks.TryDequeue(out _))
@@ -168,8 +171,6 @@
{
break;
}
this.UpdateTasksStateInTheQueue(task);
}
if (stopProgramming)
@@ -234,13 +235,11 @@
this.runningTasks.Enqueue(new EregisterCheckInitializationTask(this.parameters, frequency)
{
Timeout = 20_000,
SlotNr = features.SlotNr,
PoNr = features.PoNr,
SerialNr = features.SerialNr,
DelPamTx = true,
WakeUpCmd = true,
WakeUpModul = true,
RequestAddress = features.RequestAddress,
ResponseAddress = features.ResponseAddress,
EncryptionKey = features.EncryptionKey,
@@ -155,7 +155,7 @@
{
data = data.DecryptResponse(this.EncryptionKey);
this.BUPMessage = new BUP(data, this.P11);
this.IsEncrypted = this.LATMessage.Crc != 0;
this.IsEncrypted = this.BUPMessage.Crc != 0;
if (this.IsEncrypted)
{