Sirt message payloads translated

This commit is contained in:
Stoyan Zlatev
2024-06-10 14:50:42 +02:00
parent e97a600c3a
commit 73621f687e
10 changed files with 414 additions and 40 deletions
@@ -56,6 +56,7 @@
<Compile Include="Messages\Requests\WriteRegRequest.cs" />
<Compile Include="Messages\Responses\AckOrAnswerResponse.cs" />
<Compile Include="Messages\Responses\FromAirResponse.cs" />
<Compile Include="Messages\Responses\MessageType.cs" />
<Compile Include="Messages\Responses\SirtResponse.cs" />
<Compile Include="Messages\SirtMessage.cs" />
<Compile Include="Messages\SirtMessageStructure.cs" />
@@ -4,7 +4,7 @@
using System;
public class FromAirResponse : SirtMessage
public class FromAirResponse : SirtResponse
{
private readonly P11 parameterP11;
@@ -53,6 +53,8 @@
/// </summary>
public Boolean ModeSensus => this.parameterP11.ModeSensus;
public MessageTypes MessageType => (MessageTypes)this.Payload[0];
public Int32 MHz
{
get => this.mhz;
@@ -0,0 +1,14 @@
namespace Common.Hardware.Interfaces.Ports.SIRT.Messages.Responses
{
public enum MessageTypes
{
Bup = 0x00,
Lat = 0x01,
Semi = 0x08,
Logger = 0x09,
Test = 0x0A,
Debug = 0x0B,
EncFlexNet = 0x10,
Pam = 0x20,
}
}
@@ -44,6 +44,8 @@
<Compile Include="Payloads\Ack.cs" />
<Compile Include="Payloads\Answer.cs" />
<Compile Include="Payloads\Bup.cs" />
<Compile Include="Payloads\Debug.cs" />
<Compile Include="Payloads\Lat.cs" />
<Compile Include="Payloads\Semi.cs" />
<Compile Include="SirtAddresses.cs" />
<Compile Include="SirtTask.cs" />
@@ -2,11 +2,44 @@
{
using Common.Hardware.Interfaces.Ports.SIRT.Messages.Responses;
public class Bup
{
private readonly FromAirResponse response;
using System;
public Bup(FromAirResponse response)
=> this.response = response;
public class Bup : FromAirResponse
{
public Bup(FromAirResponse response) : base(response)
{
var payload = response.Payload;
var index = 0;
this.AppCode = payload[index++];
this.Counter = payload[index++];
this.Serial = BitConverter.ToUInt32(payload, index += 4);
this.Reading = BitConverter.ToUInt32(payload, index += 4);
this.Alarm = payload[index++];
}
public Byte AppCode { get; }
public Byte Counter { get; }
public UInt32 Serial { get; }
public UInt32 Reading { get; }
public Byte Alarm { get; }
}
}
/*
Select Case bLevel
Case 1
ProvideAuthLevelHexCommand = "4301143068D0"
Case 2
ProvideAuthLevelHexCommand = "4302140E8040"
Case 3
ProvideAuthLevelHexCommand = "430363937322"
End Select
*/
@@ -0,0 +1,131 @@
namespace Common.Hardware.Interfaces.Protocols.SIRT.Payloads
{
using Common.Hardware.Interfaces.Ports.SIRT.Messages.Parameters;
using Common.Hardware.Interfaces.Ports.SIRT.Messages.Responses;
using System;
public class Debug : FromAirResponse
{
public Debug(FromAirResponse response) : base(response)
{
var payload = this.Payload;
var index = 0;
this.AppCode = payload[index++];
this.FlexNetCounter = payload[index++];
this.ModuleAddress = BitConverter.ToUInt32(payload, index += 4);
this.OxOO = payload[index++];
this.MeterType = payload[index++];
this.RadioFirmwareVersion = this.VersionString(payload[index++], payload[index++]);
this.MetrologyFirmwareVersion = this.VersionString(payload[index++], payload[index++]);
this.HrdwareVersion = payload[index++];
this.FactoryState = payload[index++];
this.PCBID = BitConverter.ToUInt32(payload, index += 4);
this.BoardTemperature = payload[index++];
this.BoardVoltage = BitConverter.ToUInt16(payload, index += 2);
this.WakeupInterval = payload[index++];
this.SensusRFSetting = payload[index++];
this.FlexNetRFSetting = payload[index++];
this.FrequencyOffset = BitConverter.ToUInt32(payload, index += 4);
this.ResetCount = payload[index++];
this.WatchdogReset = payload[index++];
this.BatterySeconds = BitConverter.ToUInt32(payload, index += 4);
this.MeterSampleRate = payload[index++];
this.HistoryScale = payload[index++];
this.RegisterMode = payload[index++];
this.HistorySize = payload[index++];
this.FactoryCalibration = payload[index++];
this.MeterSize = payload[index++];
this.VolumePerLedPulse = BitConverter.ToUInt32(payload, index += 4);
this.VolumePerPulse = BitConverter.ToUInt32(payload, index += 4);
this.OffsetFactor = BitConverter.ToUInt32(payload, index += 4);
this.OffsetTime = payload[index++];
this.ActivationByFlowThreshold = payload[index++];
}
public Byte AppCode { get; }
public Byte FlexNetCounter { get; }
public UInt32 ModuleAddress { get; }
public Byte OxOO { get; }
/// <summary>
/// 0x0A for Residential and 0x0C for C & I
/// </summary>
public Byte MeterType { get; }
public String RadioFirmwareVersion { get; }
public String MetrologyFirmwareVersion { get; }
public Byte HrdwareVersion { get; }
public Byte FactoryState { get; }
public UInt32 PCBID { get; }
public Byte BoardTemperature { get; }
public UInt16 BoardVoltage { get; }
public Byte WakeupInterval { get; }
public Byte SensusRFSetting { get; }
public Byte FlexNetRFSetting { get; }
public UInt32 FrequencyOffset { get; }
public Byte ResetCount { get; }
public Byte WatchdogReset { get; }
public UInt32 BatterySeconds { get; }
public Byte MeterSampleRate { get; }
public Byte HistoryScale { get; }
public Byte RegisterMode { get; }
public Byte HistorySize { get; }
public Byte FactoryCalibration { get; }
public Byte MeterSize { get; }
public UInt32 VolumePerLedPulse { get; }
public UInt32 VolumePerPulse { get; }
public UInt32 OffsetFactor { get; }
public Byte OffsetTime { get; }
public Byte ActivationByFlowThreshold { get; }
/// <summary>
/// Bit 15 – Beta or Release
/// Bits 12-14 – Major Version
/// Bits 8-11 – Minor Version
/// Bits 0-7 – Patch Version
/// </summary>
private String VersionString(Byte param, Byte patch)
{
var bits = param.GetBits();
var _major = new Byte[8];
var _minor = new Byte[8];
Array.Copy(bits, 0, _major, 0, 4);
Array.Copy(bits, 4, _minor, 0, 3);
var major = _major.GetByte();
var minor = _minor.GetByte();
return $"{bits[7]}.{major}.{minor}.{patch}";
}
}
}
@@ -0,0 +1,11 @@
namespace Common.Hardware.Interfaces.Protocols.SIRT.Payloads
{
using Common.Hardware.Interfaces.Ports.SIRT.Messages.Responses;
public class Lat : Bup
{
public Lat(FromAirResponse response) : base(response)
{
}
}
}
@@ -2,11 +2,147 @@
{
using Common.Hardware.Interfaces.Ports.SIRT.Messages.Responses;
public class Semi
{
private readonly FromAirResponse response;
using System;
using System.Text;
public Semi(FromAirResponse response)
=> this.response = response;
public class Semi : FromAirResponse
{
public Semi(FromAirResponse response) : base(response)
{
var payload = this.Payload;
var index = 0;
this.ModuleAddress = BitConverter.ToUInt32(payload, index += 4);
this.DisplayVolume = BitConverter.ToUInt32(payload, index += 4);
this.Alarms = payload[index++];
this.PamStatus = payload[index++];
this.PamRssi = payload[index++];
this.MeterType = payload[index++];
this.TransmissionInterval = BitConverter.ToUInt16(payload, index += 2);
this.LATInterval = BitConverter.ToUInt16(payload, index += 2);
this.CurrentFlow = BitConverter.ToUInt16(payload, index += 2);
this.BackwardVolume = BitConverter.ToUInt32(payload, index += 4);
this.NumberOfDials = payload[index++];
this.Units = payload[index++];
this.MeterID = BitConverter.ToUInt32(payload, index += 4);
this.MBusStatus = payload[index++];
this.MBusTransmissionInterval = BitConverter.ToUInt16(payload, index += 2);
this.AlarmActiveInformation = payload[index++];
this.LeakageDetectionParameters = payload[index++];
this.BrokenPipeDetectionParameters = payload[index++];
this.BatteryRemaining = BitConverter.ToUInt16(payload, index += 2);
this.BatteryEndDetected = BitConverter.ToUInt16(payload, index += 2);
this.MaximumFlow = BitConverter.ToUInt16(payload, index += 2);
this.TimeOfMaximumFlow = BitConverter.ToUInt16(payload, index += 2);
this.LowBatteryDetected = BitConverter.ToUInt16(payload, index += 2);
this.LeakStart = BitConverter.ToUInt16(payload, index += 2);
this.LeakEnd = BitConverter.ToUInt16(payload, index += 2);
this.MagneticTamperStart = BitConverter.ToUInt16(payload, index += 2);
this.MagneticTamperEnd = BitConverter.ToUInt16(payload, index += 2);
this.ModuleRemoveStart = BitConverter.ToUInt16(payload, index += 2);
this.ModuleRemoveEnd = BitConverter.ToUInt16(payload, index += 2);
this.BackflowStart = BitConverter.ToUInt16(payload, index += 2);
this.BackflowEnd = BitConverter.ToUInt16(payload, index += 2);
this.BrokenPipeStart = BitConverter.ToUInt16(payload, index += 2);
this.BrokenPipeEnd = BitConverter.ToUInt16(payload, index += 2);
this.LogContent = BitConverter.ToUInt16(payload, index += 2);
this.LoggingInterval = BitConverter.ToUInt16(payload, index += 2);
this.FixedDateReadingContent = BitConverter.ToUInt16(payload, index += 2);
this.DayOfMonth = payload[index++];
this.ActualDateTime = BitConverter.ToUInt32(payload, index += 4);
this.HistoricalErrorLimit = payload[index++];
this.CustomerSpecificText = Encoding.ASCII.GetString(payload, index += 9, 9);
this.TimeOfFDRReset = BitConverter.ToUInt16(payload, index += 2);
this.TimeOfLogReset = BitConverter.ToUInt16(payload, index += 2);
this.MeterSize = payload[index++];
}
public UInt32 ModuleAddress { get; }
public UInt32 DisplayVolume { get; }
public Byte Alarms { get; }
public Byte PamStatus { get; }
public Byte PamRssi { get; }
public Byte MeterType { get; }
public UInt16 TransmissionInterval { get; }
public UInt16 LATInterval { get; }
public UInt16 CurrentFlow { get; }
public UInt32 BackwardVolume { get; }
public Byte NumberOfDials { get; }
public Byte Units { get; }
public UInt32 MeterID { get; }
public Byte MBusStatus { get; }
public UInt16 MBusTransmissionInterval { get; }
public Byte AlarmActiveInformation { get; }
public Byte LeakageDetectionParameters { get; }
public Byte BrokenPipeDetectionParameters { get; }
public UInt16 BatteryRemaining { get; }
public UInt16 BatteryEndDetected { get; }
public UInt16 MaximumFlow { get; }
public UInt16 TimeOfMaximumFlow { get; }
public UInt16 LowBatteryDetected { get; }
public UInt16 LeakStart { get; }
public UInt16 LeakEnd { get; }
public UInt16 MagneticTamperStart { get; }
public UInt16 MagneticTamperEnd { get; }
public UInt16 ModuleRemoveStart { get; }
public UInt16 ModuleRemoveEnd { get; }
public UInt16 BackflowStart { get; }
public UInt16 BackflowEnd { get; }
public UInt16 BrokenPipeStart { get; }
public UInt16 BrokenPipeEnd { get; }
public UInt16 LogContent { get; }
public UInt16 LoggingInterval { get; }
public UInt16 FixedDateReadingContent { get; }
public Byte DayOfMonth { get; }
public UInt32 ActualDateTime { get; }
public Byte HistoricalErrorLimit { get; }
public String CustomerSpecificText { get; }
public UInt16 TimeOfFDRReset { get; }
public UInt16 TimeOfLogReset { get; }
public Byte MeterSize { get; }
public override String ToString() => BitConverter.ToString(this.Payload);
}
}
@@ -18,8 +18,6 @@
public SirtBroadcaster(string comport)
{
this.sirtPort = new SirtPort(comport);
this.sirtPort.Logging += Console.WriteLine;
this.sirtPort.FromAirReceived += this.FromAirReceived;
this.sirtPort.AckOrAnswerReceived += this.AckOrAnswerReceived;
this.availableAddresses = new ConcurrentDictionary<UInt32, Bup>();
this.sirtTasks = new ConcurrentDictionary<UInt32, ConcurrentQueue<SirtTask>>();
@@ -27,7 +25,13 @@
public void Connect()
{
this.sirtPort.Logging += Console.WriteLine;
this.sirtPort.FromAirReceived += this.FromAirReceived;
this.availableAddresses.Clear();
this.sirtTasks.Clear();
this.sirtPort.Open();
this.sirtPort.Write(new WriteRegRequest
{
Address = SirtAddresses.SirtOperating,
@@ -59,6 +63,11 @@
public void Disconnect()
{
this.sirtPort.Logging -= Console.WriteLine;
this.sirtPort.FromAirReceived -= this.FromAirReceived;
this.availableAddresses.Clear();
this.sirtTasks.Clear();
this.sirtPort.Close();
}
@@ -112,46 +121,72 @@
private void FromAirReceived(FromAirResponse response)
{
var address = response.Address;
var payloadLength = response.Payload.Length;
var task = default(SirtTask);
_ = this.sirtTasks.TryGetValue(address, out var queue)
&& queue.TryPeek(out task);
if (payloadLength == 13)
if (this.sirtTasks.TryGetValue(address, out var queue) && queue.TryPeek(out var task))
{
var bup = new Bup(response);
task?.Add(bup);
this.availableAddresses.AddOrUpdate(address, bup, (_1, _2) => bup);
if (response.MessageType == MessageTypes.Bup)
{
task.Add(new Bup(response));
}
else if (response.MessageType == MessageTypes.Lat)
{
task.Add(new Lat(response));
}
else if (response.MessageType == MessageTypes.Semi)
{
task.Add(new Semi(response));
}
else if (response.MessageType == MessageTypes.Debug)
{
task.Add(new Debug(response));
}
else if (response.MessageType == MessageTypes.Logger)
{
// TODO: TBD
// TODO: logging
}
else if (response.MessageType == MessageTypes.Test)
{
// TODO: TBD
// TODO: logging
}
else if (response.MessageType == MessageTypes.EncFlexNet)
{
// TODO: TBD
// TODO: logging
}
else if (response.MessageType == MessageTypes.Pam)
{
// TODO: TBD
// TODO: logging
}
}
else if (payloadLength == 87)
else
{
var semi = new Semi(response);
task?.Add(semi);
// TODO: logging
}
}
private void AckOrAnswerReceived(AckOrAnswerResponse response)
{
var address = response.Address;
var payloadLength = response.Payload.Length;
var task = default(SirtTask);
_ = this.sirtTasks.TryGetValue(address, out var queue)
&& queue.TryPeek(out task);
if (payloadLength == 0)
if (this.sirtTasks.TryGetValue(address, out var queue) && queue.TryPeek(out var task))
{
var ack = new Ack(response);
var payloadLength = response.Payload.Length;
task?.Add(ack);
if (payloadLength == 0)
{
task.Add(new Ack(response));
}
else
{
task.Add(new Answer(response));
}
}
else
{
var answer = new Answer(response);
task?.Add(answer);
// TODO: logging
}
}
}
@@ -30,9 +30,9 @@
&& task.Address == this.Address;
public override Int32 GetHashCode()
=> (int)this.Address;
=> (Int32)this.Address;
public override string ToString()
public override String ToString()
=> this.Address.ToString();
internal void Add(Bup bup)
@@ -40,6 +40,11 @@
}
internal void Add(Lat lat)
{
}
internal void Add(Semi semi)
{
this.IsRunning = false;
@@ -47,6 +52,10 @@
this.Done?.Invoke(this);
}
internal void Add(Debug debug)
{
}
internal void Add(Ack ack)
{
@@ -66,7 +75,7 @@
return task.request;
}
public static implicit operator uint(SirtTask task)
public static implicit operator UInt32(SirtTask task)
=> task.Address;
}
}