Handler.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using Edge.Core.Processor;
  2. using Edge.Core.IndustryStandardInterface.Pump;
  3. using Edge.Core.IndustryStandardInterface.ATG;
  4. using Edge.Core.UniversalApi;
  5. using Microsoft.AspNetCore.Mvc.ApiExplorer;
  6. using Microsoft.Extensions.Logging;
  7. using Microsoft.Extensions.Logging.Abstractions;
  8. using ProGauge_StartItaliana_Probe.MessageEntity;
  9. using ProGauge_StartItaliana_Probe.MessageEntity.Incoming;
  10. using ProGauge_StartItaliana_Probe.MessageEntity.Outgoing;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Threading.Tasks;
  14. using Edge.Core.Processor.Dispatcher.Attributes;
  15. namespace ProGauge_StartItaliana_Probe
  16. {
  17. [MetaPartsDescriptor(
  18. "lang-zh-cn:Pro-gauge 探棒lang-en-us:Pro-gauge Probe",
  19. "lang-zh-cn:用于驱动基于 Pro-gauge RS485 协议的探棒lang-en-us:Used for driven Probes that use Pro-gauge RS485 probe protocol",
  20. new[] { "lang-zh-cn:探棒lang-en-us:Probe" })]
  21. public class Handler : IProbeHandler, IDeviceHandler<byte[], ProGauge_StartItaliana_Probe.MessageEntity.MessageBase>
  22. {
  23. private ILogger logger = NullLogger.Instance;
  24. private int protocolVersion = 2;
  25. protected IContext<byte[], MessageBase> context;
  26. protected int pollingInterval;
  27. public Probe Probe => this.probe;
  28. private Probe probe = null;
  29. [UniversalApi]
  30. public async Task<ProbeReading> GetProbeReadingAsync(ApiData input)
  31. {
  32. return await this.GetProbeReadingAsync();
  33. }
  34. /// <summary>
  35. ///
  36. /// </summary>
  37. /// <param name="probeHardwareAddress">should range from 1-15</param>
  38. public Handler(int probeHardwareAddress) : this(probeHardwareAddress, 2)
  39. {
  40. }
  41. /// <summary>
  42. ///
  43. /// </summary>
  44. /// <param name="probeHardwareAddress"></param>
  45. /// <param name="protocolVersion">by 2020 May, only 1 or 2 is allowed</param>
  46. public Handler(int probeHardwareAddress, int protocolVersion)
  47. {
  48. this.probe = new Probe()
  49. {
  50. HardwareIdentity = probeHardwareAddress.ToString(),
  51. DeviceId = probeHardwareAddress
  52. };
  53. this.protocolVersion = protocolVersion;
  54. }
  55. public void Init(IContext<byte[], MessageBase> context)
  56. {
  57. this.context = context;
  58. //var timeWindowWithActivePollingOutgoing =
  59. // this.context.Outgoing as TimeWindowWithActivePollingOutgoing<byte[], MessageBase>;
  60. //if (timeWindowWithActivePollingOutgoing != null)
  61. //{
  62. // this.pollingInterval = timeWindowWithActivePollingOutgoing.PollingInterval;
  63. // timeWindowWithActivePollingOutgoing.PollingMsgProducer =
  64. // () =>
  65. // {
  66. // return new MeasureRequest(this.probe.DeviceId.ToString());
  67. // };
  68. //}
  69. }
  70. public async Task<ProbeReading> GetProbeReadingAsync()
  71. {
  72. var response = await this.context.Outgoing.WriteAsync(new MeasureRequest(this.probe.DeviceId.ToString()),
  73. (request, testResponse) => testResponse is MessageBase,
  74. 5000);
  75. if (response == null) throw new TimeoutException();
  76. if (this.protocolVersion == 2)
  77. {
  78. var measureResponse = MeasureResponse_AnswerType2.Parse(response.RawContent.ToArray());
  79. var probeReading = new ProbeReading()
  80. {
  81. Height = measureResponse.ProductLevel,
  82. Water = measureResponse.WaterLevel,
  83. Temperature = new double[] { measureResponse.Temperature },
  84. };
  85. return probeReading;
  86. }
  87. else if (this.protocolVersion == 1)
  88. {
  89. var measureResponse = MeasureResponse_AnswerType1.Parse(response.RawContent.ToArray());
  90. var probeReading = new ProbeReading()
  91. {
  92. Height = measureResponse.ProductLevel,
  93. Water = measureResponse.WaterLevel,
  94. Temperature = new double[] { measureResponse.Temperature },
  95. };
  96. return probeReading;
  97. }
  98. throw new NotSupportedException("Unexpected ProtocolVersion: " + this.protocolVersion);
  99. }
  100. public Task Process(IContext<byte[], MessageBase> context)
  101. {
  102. return Task.CompletedTask;
  103. //throw new NotImplementedException();
  104. }
  105. }
  106. }