85 lines
3.2 KiB
C#
85 lines
3.2 KiB
C#
using System;
|
|
using Xylem.Common.Hardware.Interfaces.Ports.PortCore.EventArguments;
|
|
using Xylem.Common.Hardware.Interfaces.Protocols.ProtocolCore;
|
|
using Xylem.Common.Hardware.Interfaces.Protocols.ProtocolCore.EventArguments;
|
|
using Xylem.Common.Hardware.WaterMeter.MagFlux.DataPackages.EventArguments;
|
|
|
|
namespace Xylem.Common.Hardware.WaterMeter.MagFlux.Protocols.StreamingProtocol
|
|
{
|
|
/// <inheritdoc />
|
|
/// <summary>
|
|
/// Layer between <see cref="StreamingDecoder" /> CRC16CCITT handler and Events
|
|
/// block trashy telegrams
|
|
/// </summary>
|
|
public class StreamingProtocol : BaseProtocol
|
|
{
|
|
|
|
/// <summary>
|
|
/// Decode data and hold results
|
|
/// </summary>
|
|
private StreamingDecoder _streamingDecode;
|
|
private readonly Boolean _ignoreCorruptedData;
|
|
private String _sensorIdMask;
|
|
private String _uniqueIdMask;
|
|
|
|
/// <inheritdoc />
|
|
public override event EventHandler<BaseDataEventArgs> OnRecordIsDecoded;
|
|
|
|
/// <summary>
|
|
/// Set decoding mask for streaming protocol, wipes out all not matching records:
|
|
/// - PcbIdMask equals the sensor serial no
|
|
/// - UniqueIdMask
|
|
/// </summary>
|
|
/// <param name="sensorIdMask"></param>
|
|
/// <param name="uniqueIdMask"></param>
|
|
/// <returns>true if decoding was successful and data has been validated</returns>
|
|
/// <remarks date="2023-Mai-30" author="T.Wiedebusch">
|
|
/// - Initial.
|
|
/// </remarks>
|
|
/// <remarks date="2024-Aug-19" author="T.Wiedebusch">
|
|
/// - Changed from PcbId to SensorId.
|
|
/// </remarks>
|
|
public void SetDecodingMask(String sensorIdMask, String uniqueIdMask)
|
|
{
|
|
_sensorIdMask = sensorIdMask;
|
|
_uniqueIdMask = uniqueIdMask;
|
|
}
|
|
/// <inheritdoc />
|
|
protected override void DecodeRecord(IPortDataEventArgs dataArgs)
|
|
{
|
|
var data = (String)dataArgs.GetData();
|
|
|
|
//Data decoding
|
|
_streamingDecode = new StreamingDecoder(_ignoreCorruptedData);
|
|
_streamingDecode.SetDecodingMask(_sensorIdMask, _uniqueIdMask);
|
|
_streamingDecode.DecodeMsg(data);
|
|
|
|
//Event for new data
|
|
if (_streamingDecode.DataMagFlux != null)
|
|
{
|
|
_streamingDecode.DataMagFlux.SyncMarkRecord = dataArgs.GetSyncMarkRecord();
|
|
_streamingDecode.DataMagFlux.ReceivedTime = dataArgs.GetReceivedTime();
|
|
|
|
if (_streamingDecode.DataMagFlux.IsValid)
|
|
{
|
|
OnRecordIsDecoded?.Invoke(this,
|
|
new MagFluxFlowDataEventArgs { NewData = _streamingDecode.DataMagFlux, RawData = data });
|
|
}
|
|
else if (!_ignoreCorruptedData)
|
|
{
|
|
OnRecordIsDecoded?.Invoke(this,
|
|
new MagFluxFlowDataEventArgs { NewData = _streamingDecode.DataMagFlux, RawData = data });
|
|
}
|
|
|
|
}
|
|
|
|
//toDo: handle trashy telegrams
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public StreamingProtocol(String ident, Boolean ignoreCorruptedData = true) : base(ident)
|
|
{
|
|
_ignoreCorruptedData = ignoreCorruptedData;
|
|
}
|
|
}
|
|
} |