260 lines
8.8 KiB
C#
260 lines
8.8 KiB
C#
namespace Common.Hardware.WaterMeter.eRegister
|
|
{
|
|
using Common.Hardware.SIRT;
|
|
using Common.Hardware.SIRT.Parameters;
|
|
using Common.Hardware.SIRT.Tasks;
|
|
using Common.Hardware.WaterMeter.eRegister.Features;
|
|
using Common.Hardware.WaterMeter.eRegister.Models;
|
|
using Common.Hardware.WaterMeter.eRegister.Telegrams;
|
|
|
|
using System;
|
|
|
|
public class EregisterTask : WritePamTask
|
|
{
|
|
private byte[] payload;
|
|
private int countFailures;
|
|
|
|
public EregisterTask(int frequency)
|
|
{
|
|
this.Timeout = 10_000;
|
|
this.Frequency = frequency;
|
|
this.RSSI = new RSSI(0, frequency);
|
|
this.Label = nameof(SIRTTaskState.Pending);
|
|
}
|
|
|
|
public event Action<EregisterTask> Changed;
|
|
public event Action<byte[]> MessageReceived;
|
|
|
|
public int SlotNr { get; internal set; }
|
|
|
|
public int PoNr { get; internal set; }
|
|
|
|
public int SerialNr { get; internal set; }
|
|
|
|
public string Name { get; internal set; }
|
|
|
|
private string label;
|
|
public string Label
|
|
{
|
|
get => this.label;
|
|
set
|
|
{
|
|
this.label = value;
|
|
|
|
var changeEvent = this.Changed;
|
|
|
|
if (changeEvent != null)
|
|
{
|
|
changeEvent(this);
|
|
}
|
|
}
|
|
}
|
|
|
|
public string Info { get; private set; }
|
|
|
|
public string RSSI { get; private set; }
|
|
|
|
public P11 P11 { get; private set; }
|
|
|
|
public P12 P12 { get; private set; }
|
|
|
|
public BUP BUPMessage { get; private set; }
|
|
|
|
public LAT LATMessage { get; private set; }
|
|
|
|
public DEBUG DEBUGMessage { get; private set; }
|
|
|
|
public SEMI SEMIMessage { get; protected set; }
|
|
|
|
public bool AnyTelegramReceived { get; private set; }
|
|
|
|
public bool IsEncrypted { get; set; }
|
|
|
|
public byte[] EncryptionKey { get; set; }
|
|
|
|
public FactoryState FactoryState { get; set; }
|
|
|
|
public int? RadioFWVersion { get; set; }
|
|
|
|
public string RadioFWHex { get; set; }
|
|
|
|
public ErrorMessage ErrorMessage { get; protected set; }
|
|
|
|
public override byte[] Start()
|
|
{
|
|
var payloadL = this.request.Payload.Length;
|
|
this.payload = new byte[payloadL];
|
|
|
|
Array.Copy(this.request.Payload, 0, this.payload, 0, payloadL);
|
|
|
|
if (this.FactoryState == FactoryState.Shipment)
|
|
{
|
|
this.payload = new EregisterAvailableParametersParser(this.payload);
|
|
}
|
|
|
|
if (this.IsEncrypted)
|
|
{
|
|
this.payload = this.payload.EncryptRequest(this.EncryptionKey, this.RequestAddress);
|
|
}
|
|
|
|
this.P12 = default(P12);
|
|
this.BUPMessage = default(BUP);
|
|
this.LATMessage = default(LAT);
|
|
this.DEBUGMessage = default(DEBUG);
|
|
this.SEMIMessage = default(SEMI);
|
|
|
|
this.SetRunning();
|
|
|
|
return new SIRTMessage
|
|
{
|
|
Direction = this.request.Direction,
|
|
Command = this.request.Command,
|
|
P1 = this.request.P1,
|
|
P2 = this.request.P2,
|
|
Address = this.request.Address,
|
|
Payload = this.payload,
|
|
Frequency = this.Frequency,
|
|
|
|
}.GetBytes();
|
|
}
|
|
|
|
public override void Push(SIRTMessage response)
|
|
{
|
|
this.MessageReceived?.Invoke(response);
|
|
this.AnyTelegramReceived = true;
|
|
|
|
if (this.State == SIRTTaskState.Cancelled)
|
|
{
|
|
this.Label = nameof(SIRTTaskState.Cancelled);
|
|
}
|
|
else if (this.State != SIRTTaskState.Completed)
|
|
{
|
|
if (response.Address == this.ResponseAddress)
|
|
{
|
|
var requestAddress = this.RequestAddress;
|
|
this.RequestAddress = this.ResponseAddress;
|
|
this.ResponseAddress = requestAddress;
|
|
}
|
|
|
|
if (response.Command == SIRTConstants.FROM_AIR)
|
|
{
|
|
this.P11 = new P11(response.P1);
|
|
this.RSSI = new RSSI(response.P2, (ushort)this.Frequency);
|
|
|
|
var data = response.Payload;
|
|
var length = data.Length;
|
|
|
|
if (length > 0)
|
|
{
|
|
var type = data[0];
|
|
|
|
if (type == SIRTConstants.BUP) // always: 13 - enc: CRC != 0
|
|
{
|
|
this.Label = nameof(SIRTConstants.BUP);
|
|
var bupMessage = new BUP(data, this.P11);
|
|
this.IsEncrypted = bupMessage.Crc != 0;
|
|
|
|
if (this.IsEncrypted)
|
|
{
|
|
data = data.DecryptResponse(this.EncryptionKey);
|
|
bupMessage = new LAT(data, this.P11);
|
|
}
|
|
|
|
this.BUPMessage = bupMessage;
|
|
}
|
|
else if (type == SIRTConstants.LAT) // always: 13 - enc: CRC != 0
|
|
{
|
|
this.Label = nameof(SIRTConstants.LAT);
|
|
var latMessage = new LAT(data, this.P11);
|
|
this.IsEncrypted = latMessage.Crc != 0;
|
|
|
|
if (this.IsEncrypted)
|
|
{
|
|
data = data.DecryptResponse(this.EncryptionKey);
|
|
latMessage = new LAT(data, this.P11);
|
|
}
|
|
|
|
this.LATMessage = latMessage;
|
|
}
|
|
else if (type == SIRTConstants.DEBUG) // plain: 52 - enc: 54
|
|
{
|
|
this.IsEncrypted = data.Length > DEBUG.MIN_LENGTH;
|
|
|
|
if (this.IsEncrypted)
|
|
{
|
|
data = data.DecryptResponse(this.EncryptionKey);
|
|
}
|
|
|
|
this.Label = nameof(SIRTConstants.DEBUG);
|
|
this.DEBUGMessage = new DEBUG(data, this.P11);
|
|
this.FactoryState = this.DEBUGMessage.FactoryState;
|
|
}
|
|
else if (type == SIRTConstants.SEMI) // plain: 91 - enc: 93
|
|
{
|
|
this.IsEncrypted = data.Length > SEMI.MIN_LENGTH;
|
|
|
|
if (this.IsEncrypted)
|
|
{
|
|
data = data.DecryptResponse(this.EncryptionKey);
|
|
}
|
|
|
|
this.SEMIMessage = new SEMI(data, this.Frequency, this.P11);
|
|
var pamStatus = this.SEMIMessage.PAMStatus;
|
|
this.Label = pamStatus.ToString();
|
|
|
|
if (pamStatus == PAMStatus.OK)
|
|
{
|
|
if (this.request.Length > 0 && this.request.Payload[0] == Pam.GetDebug && this.DEBUGMessage == null)
|
|
{
|
|
this.Label = "!DEBUG";
|
|
}
|
|
else if (this.LATMessage is null)
|
|
{
|
|
this.Label = "!LAT";
|
|
}
|
|
else
|
|
{
|
|
this.SetCompleted();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (pamStatus.EXPIRED_AUTH && ++this.countFailures > 3)
|
|
{
|
|
this.countFailures = 0;
|
|
|
|
this.SetCancelled();
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
this.Label = "NA";
|
|
}
|
|
|
|
response.Payload = data;
|
|
}
|
|
}
|
|
else if (response.Command == SIRTConstants.AKN_ANS)
|
|
{
|
|
this.P12 = new P12(response.P1);
|
|
this.Label = "AKN";
|
|
|
|
if (this.Label != "AKN")
|
|
{
|
|
this.SetPending();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
this.Label = "NA";
|
|
}
|
|
}
|
|
|
|
this.MessageReceived?.Invoke(response);
|
|
}
|
|
|
|
public static implicit operator byte[] (EregisterTask task)
|
|
=> task?.payload ?? Array.Empty<byte>();
|
|
}
|
|
} |