SensorHandler.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using Edge.Core.Processor;
  2. using Microsoft.Extensions.Logging;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Threading.Tasks;
  6. using PressureGage_3051.MessageEntity;
  7. using PressureGage_3051.MessageEntity.Outgoing;
  8. using PressureGage_3051.MessageEntity.Incoming;
  9. using System.Text.Json;
  10. namespace PressureGage_3051
  11. {
  12. public class SensorHandler
  13. {
  14. public byte DeviceAddress { get; set; } // It is also DeviceId
  15. public EventHandler<string> OnJsonDataRecieved;
  16. private IContext<byte[], MessageBase> context;
  17. private readonly Queue<MessageBase> requestQueue = new Queue<MessageBase>();
  18. private ILogger logger = null;
  19. private DeviceState deviceState = DeviceState.Disconnected;
  20. public SensorHandler(byte deviceAddress, ILogger logger)
  21. {
  22. DeviceAddress = deviceAddress;
  23. this.logger = logger;
  24. logger.LogInformation($"Create sensor handler with deviceAddress: {deviceAddress}");
  25. }
  26. public void Init(IContext<byte[], MessageBase> context)
  27. {
  28. this.context = context;
  29. this.context.Incoming.OnLongTimeNoSeeMessage += (_, __) =>
  30. {
  31. this.deviceState = DeviceState.Disconnected;
  32. FireStateChange(this.deviceState);
  33. };
  34. this.context.Incoming.LongTimeNoSeeMessageTimeout = 90000;
  35. }
  36. public void ReadRegister(MessageBase request)
  37. {
  38. if (requestQueue.Count > 8)
  39. {
  40. requestQueue.Dequeue();
  41. }
  42. requestQueue.Enqueue(request);
  43. }
  44. public MessageBase GetRequestMessage()
  45. {
  46. switch (this.deviceState)
  47. {
  48. case DeviceState.Connected:
  49. {
  50. if (requestQueue.Count > 0)
  51. {
  52. return requestQueue.Dequeue();
  53. }
  54. else
  55. return new ReadPressure_Request(this.DeviceAddress);
  56. }
  57. case DeviceState.Disconnected:
  58. {
  59. return new ReadPressure_Request(this.DeviceAddress); // PresetSingleRegister_Request(this.DeviceAddress, 0, "Pa");
  60. }
  61. default: { return null; }
  62. };
  63. }
  64. public Task Process(IContext<byte[], MessageBase> context)
  65. {
  66. if (this.deviceState != DeviceState.Connected)
  67. {
  68. this.deviceState = DeviceState.Connected;
  69. FireStateChange(this.deviceState);
  70. }
  71. FireDataRecieved(context.Incoming.Message);
  72. return Task.CompletedTask;
  73. }
  74. private void FireDataRecieved(MessageBase message)
  75. {
  76. dynamic response = null;
  77. switch (message.FunctionCode)
  78. {
  79. case FunctionCode.READ_HOLDING_REGISTERS:
  80. if (message.RawDataField[0] == 0x0A)
  81. {
  82. response = message as ReadUnit_Response;
  83. logger.LogDebug($"Pressure Gage with addr {this.DeviceAddress} read PressureUnit {response.PressureUnit}, StaticPressureUnit {response.StaticPressureUnit},\n" +
  84. $" DeviceType {response.DeviceType}, SensorType {response.SensorType}, MeasuringRange {response.MeasuringRange}");
  85. }
  86. else if (message.RawDataField[0] == 0x12)
  87. {
  88. response = message as ReadView_Response;
  89. logger.LogDebug($"Pressure Gage with addr {this.DeviceAddress} read ViewType {response.ViewType}, MinMeasuringRange {response.MinMeasuringRange},\n" +
  90. $" MaxMeasuringRange {response.MaxMeasuringRange}, DampPeriod {response.DampPeriod}, SmallSignalExcision {response.SmallSignalExcision}");
  91. }
  92. break;
  93. case FunctionCode.READ_INPUT_REGISTERS:
  94. if (message.RawDataField[0] == 0x10)
  95. {
  96. response = message as ReadPressure_Response;
  97. logger.LogDebug($"Pressure Gage with addr {this.DeviceAddress} read Pressure {response.Pressure.ToString("F1")}, PressurePercentage {response.PressurePercentage.ToString("F1")}");
  98. OnJsonDataRecieved?.Invoke(this, JsonSerializer.Serialize(new { DeviceAddress = this.DeviceAddress, Pressure = response.Pressure }));
  99. }
  100. else if (message.RawDataField[0] == 0x08)
  101. {
  102. response = message as ReadAlarm_Response;
  103. logger.LogDebug($"Pressure Gage with addr {this.DeviceAddress} read LowerAlert {response.LowerAlert}, UpperAlert {response.UpperAlert}");
  104. }
  105. break;
  106. case FunctionCode.PRESET_SINGLE_REGISTER:
  107. response = message as PresetRegisters_Response;
  108. logger.LogDebug($"Pressure Gage with addr {this.DeviceAddress} read WriteValue {response.WriteValue}");
  109. break;
  110. default:
  111. break;
  112. };
  113. }
  114. private void FireStateChange(DeviceState deviceState)
  115. {
  116. logger.LogInformation($"Device: {this.DeviceAddress}, DeviceState is {deviceState}");
  117. OnJsonDataRecieved?.Invoke(this, JsonSerializer.Serialize(new { DeviceAddress = this.DeviceAddress, DeviceState = deviceState.ToString() }));
  118. }
  119. }
  120. }