using Edge.Core.Processor; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Threading.Tasks; using PressureGage_3051.MessageEntity; using PressureGage_3051.MessageEntity.Outgoing; using PressureGage_3051.MessageEntity.Incoming; using System.Text.Json; namespace PressureGage_3051 { public class SensorHandler { public byte DeviceAddress { get; set; } // It is also DeviceId public EventHandler OnJsonDataRecieved; private IContext context; private readonly Queue requestQueue = new Queue(); private ILogger logger = null; private DeviceState deviceState = DeviceState.Disconnected; public SensorHandler(byte deviceAddress, ILogger logger) { DeviceAddress = deviceAddress; this.logger = logger; logger.LogInformation($"Create sensor handler with deviceAddress: {deviceAddress}"); } public void Init(IContext context) { this.context = context; this.context.Incoming.OnLongTimeNoSeeMessage += (_, __) => { this.deviceState = DeviceState.Disconnected; FireStateChange(this.deviceState); }; this.context.Incoming.LongTimeNoSeeMessageTimeout = 90000; } public void ReadRegister(MessageBase request) { if (requestQueue.Count > 8) { requestQueue.Dequeue(); } requestQueue.Enqueue(request); } public MessageBase GetRequestMessage() { switch (this.deviceState) { case DeviceState.Connected: { if (requestQueue.Count > 0) { return requestQueue.Dequeue(); } else return new ReadPressure_Request(this.DeviceAddress); } case DeviceState.Disconnected: { return new ReadPressure_Request(this.DeviceAddress); // PresetSingleRegister_Request(this.DeviceAddress, 0, "Pa"); } default: { return null; } }; } public Task Process(IContext context) { if (this.deviceState != DeviceState.Connected) { this.deviceState = DeviceState.Connected; FireStateChange(this.deviceState); } FireDataRecieved(context.Incoming.Message); return Task.CompletedTask; } private void FireDataRecieved(MessageBase message) { dynamic response = null; switch (message.FunctionCode) { case FunctionCode.READ_HOLDING_REGISTERS: if (message.RawDataField[0] == 0x0A) { response = message as ReadUnit_Response; logger.LogDebug($"Pressure Gage with addr {this.DeviceAddress} read PressureUnit {response.PressureUnit}, StaticPressureUnit {response.StaticPressureUnit},\n" + $" DeviceType {response.DeviceType}, SensorType {response.SensorType}, MeasuringRange {response.MeasuringRange}"); } else if (message.RawDataField[0] == 0x12) { response = message as ReadView_Response; logger.LogDebug($"Pressure Gage with addr {this.DeviceAddress} read ViewType {response.ViewType}, MinMeasuringRange {response.MinMeasuringRange},\n" + $" MaxMeasuringRange {response.MaxMeasuringRange}, DampPeriod {response.DampPeriod}, SmallSignalExcision {response.SmallSignalExcision}"); } break; case FunctionCode.READ_INPUT_REGISTERS: if (message.RawDataField[0] == 0x10) { response = message as ReadPressure_Response; logger.LogDebug($"Pressure Gage with addr {this.DeviceAddress} read Pressure {response.Pressure.ToString("F1")}, PressurePercentage {response.PressurePercentage.ToString("F1")}"); OnJsonDataRecieved?.Invoke(this, JsonSerializer.Serialize(new { DeviceAddress = this.DeviceAddress, Pressure = response.Pressure })); } else if (message.RawDataField[0] == 0x08) { response = message as ReadAlarm_Response; logger.LogDebug($"Pressure Gage with addr {this.DeviceAddress} read LowerAlert {response.LowerAlert}, UpperAlert {response.UpperAlert}"); } break; case FunctionCode.PRESET_SINGLE_REGISTER: response = message as PresetRegisters_Response; logger.LogDebug($"Pressure Gage with addr {this.DeviceAddress} read WriteValue {response.WriteValue}"); break; default: break; }; } private void FireStateChange(DeviceState deviceState) { logger.LogInformation($"Device: {this.DeviceAddress}, DeviceState is {deviceState}"); OnJsonDataRecieved?.Invoke(this, JsonSerializer.Serialize(new { DeviceAddress = this.DeviceAddress, DeviceState = deviceState.ToString() })); } } }