tbf/TBF/Rig/RegisterReaders/GenesisRegReader/implementations/ConfigStruct.cs
Michal Buzik 2522d1da9f Add Genesis radio/opto communication and configuration components
- Replace `OpthoHeadService` with `RadioService` to enhance Genesis communication handling via serial interface.
- Introduce `OptoReceivedEventArgs` to manage event-driven communication.
- Add foundational protocol implementations (`BaseProtocol`, `IProtocol`, etc.) for Genesis configuration and processing.
- Implement `SirtConfig`, `MeterConfig`, and `ProcessConfig` classes for flexible Genesis configuration management.
- Expand Genesis framework to support diagnostics, activity modes, and version retrieval.
2026-03-18 15:57:04 +01:00

207 lines
5.4 KiB
C#

///
/// Copyright (c) 2015-2020 Sensus Metering Systems
///
using System;
using System.IO;
using System.Text;
using TBF.Rig.TestMethods.iPerlCommunication.communication.C4.diagnosticLed;
using TBF.Rig.TestMethods.iPerlCommunication.communication.C4.protocolCommons;
namespace TBF.Rig.RegisterReaders.GenesisRegReader.implementations
{
public class ConfigStruct
{
//public const int Length = 32; // dynamic
//public Byte Version; // 0: 1 byte
public string PCBNumberString; // dynamic
public ProtocolStatuses StatusMode; // byte
public DiagnosticLedState OpthoStatusMode;// byte
public string Unit; //dynamic
public string Version; // dynamic
public ConfigStruct()
{
}
//Optho test status mode
public DiagnosticLedState TestModeConfig
{
get { return OpthoStatusMode; }
}
//Activity test status mode
public ProtocolStatuses MeterState
{
get
{
return StatusMode;
}
}
/// <summary>
/// Returns PCB number string (12 characters, 12 decimal digits)
/// </summary>
/// <returns>PCB number STRING</returns>
public string GetPcbNrString(){
return PCBNumberString;
}
public string GetStatusModeString()
{
return string.Format(
"Config: StatusMode={0}",
StatusMode
);
}
public string GetActiveModeString()
{
return string.Format(
"Config: StatusMode={0}, OpthoStatusMode={1}",
StatusMode,
OpthoStatusMode
);
}
public override string ToString()
{
return string.Format(
"Config: PCB#={0} StatusMode={1} Unit={2} V{3}",
GetPcbNrString(),
StatusMode,
Unit,
Version
);
}
public string ToString(int sel)
{
return string.Format(
"Config: PCB#={0} StatusMode={1} Unit={2} V{3}",
GetPcbNrString(),
StatusMode,
Unit,
Version
);
}
public virtual void WriteBinary(BinaryWriter writer)
{
if (writer == null)
throw new ArgumentNullException(nameof(writer));
// ---- Marker ----
writer.Write((byte)0x11);
// ---- Version ----
if (!string.IsNullOrEmpty(Version))
{
// Convert string to bytes (UTF8 is standard)
byte[] versionBytes = Encoding.UTF8.GetBytes(Version);
// 1) write length
writer.Write(versionBytes.Length);
// 2) write string bytes
writer.Write(versionBytes);
//writer.Write(Version);
}
else
{
writer.Write(0);//Length
}
// ---- StatusMode ----
writer.Write((byte)StatusMode);
// ---- PCB Number ----
if (!string.IsNullOrEmpty(PCBNumberString))
{
byte[] PCBNumberStringBytes = Encoding.UTF8.GetBytes(PCBNumberString);
// 1) write length
writer.Write(PCBNumberStringBytes.Length);
// 2) write string bytes
writer.Write(PCBNumberStringBytes);
}
else
{
writer.Write(0); //Length
}
// ---- Unit ----
if (!string.IsNullOrEmpty(Unit))
{
// Convert string to bytes (UTF8 is standard)
byte[] unitBytes = Encoding.UTF8.GetBytes(Unit);
// 1) write length
writer.Write(unitBytes.Length);
// 2) write string bytes
writer.Write(unitBytes);
//writer.Write(Version);
}
else
{
writer.Write(0); //Length
}
}
public virtual void ReadBinary(BinaryReader reader)
{
if (reader == null)
throw new ArgumentNullException(nameof(reader));
// ---- Marker ----
byte marker = reader.ReadByte();
if (marker != 0x11)
throw new InvalidDataException($"Invalid config marker: 0x{marker:X2}");
// ---- Version ----
int versionLength = reader.ReadInt32();
if (versionLength < 0)
throw new InvalidDataException("Invalid Version length.");
byte[] versionBytes = reader.ReadBytes(versionLength);
if (versionBytes.Length != versionLength)
throw new EndOfStreamException("Unexpected end of stream while reading Version.");
Version = versionLength > 0
? Encoding.UTF8.GetString(versionBytes)
: string.Empty;
// ---- StatusMode ----
StatusMode = (ProtocolStatuses)reader.ReadByte();
// ---- PCB Number ----
int pcbLength = reader.ReadInt32();
if (pcbLength < 0)
throw new InvalidDataException("Invalid PCB number length.");
byte[] pcbBytes = reader.ReadBytes(pcbLength);
if (pcbBytes.Length != pcbLength)
throw new EndOfStreamException("Unexpected end of stream while reading PCB number.");
PCBNumberString = pcbLength > 0
? Encoding.UTF8.GetString(pcbBytes)
: string.Empty;
// ---- Unit ----
int unitLength = reader.ReadInt32();
if (unitLength < 0)
throw new InvalidDataException("Invalid Unit length.");
byte[] unitBytes = reader.ReadBytes(unitLength);
if (unitBytes.Length != unitLength)
throw new EndOfStreamException("Unexpected end of stream while reading Unit.");
Unit = unitLength > 0
? Encoding.UTF8.GetString(unitBytes)
: string.Empty;
}
}
}