237 lines
7.0 KiB
C#
237 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 int countFailures;
|
|
|
|
public EregisterTask(int frequency)
|
|
{
|
|
this.Timeout = 15_000;
|
|
this.DelPamSemi = true;
|
|
this.Frequency = frequency;
|
|
this.RSSI = new RSSI(0, frequency);
|
|
this.Label = nameof(SIRTTaskState.Pending);
|
|
this.AnyTelegramReceived = true;
|
|
}
|
|
|
|
public event Action<EregisterTask> Changed;
|
|
public new 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;
|
|
|
|
this.Changed?.Invoke(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 FactoryState FactoryState { get; set; }
|
|
|
|
public int? RadioFWVersion { get; set; }
|
|
|
|
public string RadioFWHex { get; set; }
|
|
|
|
public ErrorMessage ErrorMessage { get; protected set; }
|
|
|
|
public override byte[] Start()
|
|
{
|
|
this.P12 = default(P12);
|
|
this.BUPMessage = default(BUP);
|
|
this.LATMessage = default(LAT);
|
|
this.DEBUGMessage = default(DEBUG);
|
|
this.SEMIMessage = default(SEMI);
|
|
this.AnyTelegramReceived = false;
|
|
this.debugReceived = false;
|
|
this.latReceived = false;
|
|
|
|
if (this.FactoryState == FactoryState.Shipment)
|
|
{
|
|
this.request.Payload = new EregisterAvailableParametersParser(this.request.Payload);
|
|
}
|
|
|
|
return base.Start();
|
|
}
|
|
|
|
public override void Receive(SIRTMessage response)
|
|
{
|
|
if (this.State == SIRTTaskState.Cancelled)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (response.Address == this.ResponseAddress)
|
|
{
|
|
var requestAddress = this.RequestAddress;
|
|
this.RequestAddress = this.ResponseAddress;
|
|
this.ResponseAddress = requestAddress;
|
|
this.Changed?.Invoke(this);
|
|
}
|
|
|
|
base.Receive(response);
|
|
this.MessageReceived?.Invoke(response);
|
|
}
|
|
|
|
public override string ToString() => BitConverter.ToString(this.request);
|
|
|
|
public static implicit operator byte[] (EregisterTask task)
|
|
=> task.request.Payload;
|
|
|
|
protected override void OnAKNReceived(SIRTMessage message)
|
|
{
|
|
this.P12 = new P12(message.P1);
|
|
this.Label = "AKN";
|
|
this.SetRunning();
|
|
}
|
|
|
|
protected override void OnBUPReceived(SIRTMessage message)
|
|
{
|
|
this.P11 = new P11(message.P1);
|
|
this.RSSI = new RSSI(message.P2, (ushort)this.Frequency);
|
|
this.BUPMessage = new BUP(message.Payload, this.P11);
|
|
this.AnyTelegramReceived = true;
|
|
this.Label = nameof(BUP);
|
|
this.SetRunning();
|
|
}
|
|
|
|
protected override void OnLATReceived(SIRTMessage message)
|
|
{
|
|
this.latReceived = true;
|
|
this.P11 = new P11(message.P1);
|
|
this.RSSI = new RSSI(message.P2, (ushort)this.Frequency);
|
|
this.LATMessage = new LAT(message.Payload, this.P11);
|
|
this.AnyTelegramReceived = true;
|
|
this.Label = nameof(LAT);
|
|
this.SetRunning();
|
|
}
|
|
|
|
protected override void OnDEBUGReceived(SIRTMessage message)
|
|
{
|
|
this.debugReceived = true;
|
|
this.P11 = new P11(message.P1);
|
|
this.RSSI = new RSSI(message.P2, (ushort)this.Frequency);
|
|
this.DEBUGMessage = new DEBUG(message.Payload, this.P11);
|
|
this.FactoryState = this.DEBUGMessage.FactoryState;
|
|
this.AnyTelegramReceived = true;
|
|
this.Label = nameof(DEBUG);
|
|
|
|
if (this.RequireLAT && !this.latReceived)
|
|
{
|
|
this.SetPending();
|
|
}
|
|
else
|
|
{
|
|
this.SetRunning();
|
|
}
|
|
}
|
|
|
|
protected override void OnSEMIReceived(SIRTMessage message)
|
|
{
|
|
this.P11 = new P11(message.P1);
|
|
this.RSSI = new RSSI(message.P2, (ushort)this.Frequency);
|
|
this.SEMIMessage = new SEMI(message.Payload, this.Frequency, this.P11);
|
|
this.AnyTelegramReceived = true;
|
|
|
|
if (this.RequireLAT && !this.latReceived)
|
|
{
|
|
this.SetPending();
|
|
this.Label = "!LAT";
|
|
}
|
|
else if (this.RequireDEBUG && !this.debugReceived)
|
|
{
|
|
this.SetPending();
|
|
this.Label = "!DEBUG";
|
|
}
|
|
else if (this.SEMIMessage.PAMStatus != PAMStatus.OK)
|
|
{
|
|
if (this.countFailures >= 2)
|
|
{
|
|
this.Label = "!OK";
|
|
this.SetCancelled();
|
|
}
|
|
else
|
|
{
|
|
this.Label = this.SEMIMessage.PAMStatus.ToString();
|
|
this.SetPending();
|
|
}
|
|
|
|
this.countFailures++;
|
|
}
|
|
else
|
|
{
|
|
this.Label = "OK";
|
|
this.SetCompleted();
|
|
}
|
|
}
|
|
|
|
protected override void OnUnknownReceived(SIRTMessage message)
|
|
{
|
|
this.Label = "UNKN";
|
|
this.SetPending();
|
|
}
|
|
|
|
protected override bool UseEncryption(SIRTMessage response)
|
|
{
|
|
if (response is null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var payload = response.Payload;
|
|
|
|
if (payload is null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var payloadLength = payload.Length;
|
|
|
|
if (payloadLength == 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
this.IsEncrypted = (payloadLength == 13 && payload.GetCrc() != 0) // is bup encrypted
|
|
|| response.Payload.Length == 54 // is debug encrypted
|
|
|| response.Payload.Length == 93; // is semi encrypted
|
|
|
|
return this.IsEncrypted;
|
|
}
|
|
}
|
|
} |