204 lines
7.0 KiB
C#
204 lines
7.0 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.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; }
|
|
|
|
public string Label { get; protected set; }
|
|
|
|
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 BUP { get; private set; }
|
|
|
|
public LAT LAT { get; private set; }
|
|
|
|
public DEBUG DEBUG { get; private set; }
|
|
|
|
public SEMI SEMI { get; protected 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 override byte[] Run()
|
|
{
|
|
var payloadLength = this.request.Payload.Length;
|
|
this.payload = new byte[payloadLength];
|
|
|
|
Array.Copy(this.request.Payload, 0, this.payload, 0, payloadLength);
|
|
|
|
if (this.FactoryState == FactoryState.Shipment)
|
|
{
|
|
this.payload = new EregisterAvailableParametersParser(payload);
|
|
}
|
|
|
|
if (this.IsEncrypted)
|
|
{
|
|
this.payload = payload.EncryptRequest(this.EncryptionKey, this.RequestAddress);
|
|
}
|
|
|
|
base.Run();
|
|
|
|
return new SIRTMessage
|
|
{
|
|
Address = this.request.Address,
|
|
Command = this.request.Command,
|
|
Payload = this.payload,
|
|
Frequency = this.Frequency,
|
|
P1 = this.request.P1,
|
|
P2 = this.request.P2,
|
|
};
|
|
}
|
|
|
|
public override void Push(SIRTMessage response)
|
|
{
|
|
if (this.State == SIRTTaskState.Cancelled)
|
|
{
|
|
this.Label = nameof(SIRTTaskState.Cancelled);
|
|
}
|
|
else
|
|
{
|
|
if (response.Address != this.RequestAddress)
|
|
{
|
|
var requestAddress = this.RequestAddress;
|
|
this.RequestAddress = this.ResponseAddress;
|
|
this.ResponseAddress = requestAddress;
|
|
}
|
|
|
|
this.State = SIRTTaskState.Running;
|
|
|
|
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 (this.IsEncrypted)
|
|
{
|
|
data = data.DecryptResponse(this.EncryptionKey);
|
|
}
|
|
|
|
if (length > 0)
|
|
{
|
|
var type = data[0];
|
|
|
|
if (type == SIRTConstants.BUP)
|
|
{
|
|
this.Label = nameof(SIRTConstants.BUP);
|
|
this.BUP = new BUP(data, this.P11);
|
|
this.IsEncrypted = this.BUP.Crc != 0;
|
|
}
|
|
else if (type == SIRTConstants.LAT)
|
|
{
|
|
this.Label = nameof(SIRTConstants.LAT);
|
|
this.LAT = new LAT(data, this.P11);
|
|
this.IsEncrypted = this.LAT.Crc != 0;
|
|
}
|
|
else if (type == SIRTConstants.DEBUG)
|
|
{
|
|
this.Label = nameof(SIRTConstants.DEBUG);
|
|
this.DEBUG = new DEBUG(data, this.P11);
|
|
this.IsEncrypted = this.DEBUG.Crc != 0;
|
|
this.FactoryState = this.DEBUG.FactoryState;
|
|
}
|
|
else if (type == SIRTConstants.SEMI)
|
|
{
|
|
this.SEMI = new SEMI(data, this.Frequency, this.P11);
|
|
this.IsEncrypted = this.SEMI.Crc != 0;
|
|
this.Label = default(string);
|
|
|
|
if (this.SEMI.PAMStatus == PAMStatus.OK || this.RadioFWVersion < 4352)
|
|
{
|
|
if (this.request.Length > 0 && this.request.Payload[0] == Pam.GetDebug && this.DEBUG == null)
|
|
{
|
|
this.Label = "!DEBUG";
|
|
}
|
|
else
|
|
{
|
|
this.State = SIRTTaskState.Completed;
|
|
this.Label = this.SEMI.PAMStatus.ToString();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
this.Label = this.SEMI.PAMStatus.ToString();
|
|
|
|
if (this.SEMI.PAMStatus == PAMStatus.EXPIREDAUTH)
|
|
{
|
|
this.State = SIRTTaskState.Cancelled;
|
|
}
|
|
else if (++this.countFailures > 3)
|
|
{
|
|
this.State = SIRTTaskState.Cancelled;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
this.Label = "NA";
|
|
}
|
|
}
|
|
}
|
|
else if (response.Command == SIRTConstants.ACKN_ANSW)
|
|
{
|
|
this.P12 = new P12(response.P1);
|
|
this.Label = "ACKN";
|
|
}
|
|
else
|
|
{
|
|
this.Label = "NA";
|
|
}
|
|
|
|
this.NotifyChanged();
|
|
this.MessageReceived?.Invoke(response);
|
|
}
|
|
}
|
|
|
|
protected void NotifyChanged() => this.Changed?.Invoke(this);
|
|
|
|
public static implicit operator byte[] (EregisterTask task)
|
|
=> task.payload ?? Array.Empty<byte>();
|
|
}
|
|
} |