using Edge.Core.Processor; using Edge.Core.IndustryStandardInterface.Pump; using Edge.Core.IndustryStandardInterface.ATG; using Edge.Core.UniversalApi; using Microsoft.AspNetCore.Mvc.ApiExplorer; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using ProGauge_StartItaliana_Probe.MessageEntity; using ProGauge_StartItaliana_Probe.MessageEntity.Incoming; using ProGauge_StartItaliana_Probe.MessageEntity.Outgoing; using System; using System.Collections.Generic; using System.Threading.Tasks; using Edge.Core.Processor.Dispatcher.Attributes; namespace ProGauge_StartItaliana_Probe { [MetaPartsDescriptor( "lang-zh-cn:Pro-gauge 探棒lang-en-us:Pro-gauge Probe", "lang-zh-cn:用于驱动基于 Pro-gauge RS485 协议的探棒lang-en-us:Used for driven Probes that use Pro-gauge RS485 probe protocol", new[] { "lang-zh-cn:探棒lang-en-us:Probe" })] public class Handler : IProbeHandler, IDeviceHandler { private ILogger logger = NullLogger.Instance; private int protocolVersion = 2; protected IContext context; protected int pollingInterval; public Probe Probe => this.probe; private Probe probe = null; [UniversalApi] public async Task GetProbeReadingAsync(ApiData input) { return await this.GetProbeReadingAsync(); } /// /// /// /// should range from 1-15 public Handler(int probeHardwareAddress) : this(probeHardwareAddress, 2) { } /// /// /// /// /// by 2020 May, only 1 or 2 is allowed public Handler(int probeHardwareAddress, int protocolVersion) { this.probe = new Probe() { HardwareIdentity = probeHardwareAddress.ToString(), DeviceId = probeHardwareAddress }; this.protocolVersion = protocolVersion; } public void Init(IContext context) { this.context = context; //var timeWindowWithActivePollingOutgoing = // this.context.Outgoing as TimeWindowWithActivePollingOutgoing; //if (timeWindowWithActivePollingOutgoing != null) //{ // this.pollingInterval = timeWindowWithActivePollingOutgoing.PollingInterval; // timeWindowWithActivePollingOutgoing.PollingMsgProducer = // () => // { // return new MeasureRequest(this.probe.DeviceId.ToString()); // }; //} } public async Task GetProbeReadingAsync() { var response = await this.context.Outgoing.WriteAsync(new MeasureRequest(this.probe.DeviceId.ToString()), (request, testResponse) => testResponse is MessageBase, 5000); if (response == null) throw new TimeoutException(); if (this.protocolVersion == 2) { var measureResponse = MeasureResponse_AnswerType2.Parse(response.RawContent.ToArray()); var probeReading = new ProbeReading() { Height = measureResponse.ProductLevel, Water = measureResponse.WaterLevel, Temperature = new double[] { measureResponse.Temperature }, }; return probeReading; } else if (this.protocolVersion == 1) { var measureResponse = MeasureResponse_AnswerType1.Parse(response.RawContent.ToArray()); var probeReading = new ProbeReading() { Height = measureResponse.ProductLevel, Water = measureResponse.WaterLevel, Temperature = new double[] { measureResponse.Temperature }, }; return probeReading; } throw new NotSupportedException("Unexpected ProtocolVersion: " + this.protocolVersion); } public Task Process(IContext context) { return Task.CompletedTask; //throw new NotImplementedException(); } } }