终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

221 lines
9.4 KiB

  1. using BPA.Communication.Interfaces;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Collections.Concurrent;
  9. using System.Diagnostics;
  10. using BPA.Helper;
  11. namespace BPASmartClient.Academy._50L
  12. {
  13. public class PlcControl : AddData
  14. {
  15. private volatile static PlcControl _Instance;
  16. public static PlcControl GetInstance => _Instance ?? (_Instance = new PlcControl());
  17. private PlcControl() { }
  18. public bool IsConnect => comm != null && comm.IsConnected();
  19. public DeviceStatus DeviceStatus { get; set; } = new DeviceStatus();
  20. private ICommSiemens comm = CommHelper.Siemens;
  21. public string id { get; set; } = "";
  22. public void Init()
  23. {
  24. Task.Factory.StartNew(() =>
  25. {
  26. CommHelper.CreateSiemens(CpuType.S71500, Json<DevicePar>.Data.IpAddress).OnSuccess(s =>
  27. {
  28. comm = s.Content;
  29. WriteHeartBeat();
  30. Read();
  31. });
  32. });
  33. }
  34. private volatile bool _isReadSuccess;
  35. private void Read()
  36. {
  37. TaskManage.GetInstance.StartLong(() =>
  38. {
  39. comm.Read<bool[]>("DB1.DBX2000.0", 31).OnSuccess(s =>
  40. {
  41. //Data[FeedbackData.旋转电机启停反馈] = s.Content[0];
  42. //Data[FeedbackData.负压风机启动反馈] = s.Content[1];
  43. //Data[FeedbackData.压缩空气进气阀反馈] = s.Content[2];
  44. //Data[FeedbackData.排热空气阀反馈] = s.Content[3];
  45. //Data[FeedbackData.泄压阀反馈] = s.Content[4];
  46. //Data[FeedbackData.进冷却水阀反馈] = s.Content[5];
  47. //Data[FeedbackData.出冷却水阀反馈] = s.Content[6];
  48. //Data[FeedbackData.蒸汽梳水阀反馈] = s.Content[7];
  49. //Data[FeedbackData.冷凝水罐进水阀反馈] = s.Content[8];
  50. //Data[FeedbackData.称重水罐负压阀反馈] = s.Content[9];
  51. //Data[FeedbackData.反应釜原点位置反馈] = s.Content[10];
  52. #region V2000
  53. DeviceStatus.ReactTurnMotor = s.Content[0];
  54. DeviceStatus.Fans = s.Content[1];
  55. DeviceStatus.BrinePumpState = s.Content[2];
  56. DeviceStatus.ReactInSteamValue = s.Content[3];
  57. DeviceStatus.ReactInColdValve = s.Content[4];
  58. DeviceStatus.ReactOutColdValve = s.Content[5];
  59. DeviceStatus.ReactDrainValve = s.Content[6];
  60. DeviceStatus.ReactInCleanWaterValve = s.Content[7];
  61. #endregion
  62. #region V2001
  63. DeviceStatus.ReactPressureAirInValve = s.Content[8];
  64. DeviceStatus.ReactInBrineWaterValve = s.Content[9];
  65. DeviceStatus.ReactOutHotGasValve = s.Content[10];
  66. DeviceStatus.HotGasEmptyValve = s.Content[11];
  67. DeviceStatus.ColdWaterTankInAirValve = s.Content[12];
  68. DeviceStatus.ColdWaterTankInWaterValve = s.Content[13];
  69. DeviceStatus.ReactHomeBit = s.Content[14];
  70. DeviceStatus.ReactPressureReliefValve = s.Content[15];
  71. #endregion
  72. #region V2002
  73. DeviceStatus.ColdWaterTankVacuumValve = s.Content[16];
  74. DeviceStatus.CompressorRunning = s.Content[17];
  75. #endregion
  76. });
  77. comm.Read<float[]>("DB1.DBD2006", 10).OnSuccess(s =>
  78. {
  79. //Data[FeedbackData.反应釜温度] = s.Content[0];
  80. //Data[FeedbackData.反应釜蒸汽压力] = s.Content[1];
  81. //Data[FeedbackData.反应釜蒸汽流量] = s.Content[2];
  82. //Data[FeedbackData.冷凝水罐温度] = s.Content[3];
  83. //Data[FeedbackData.冷凝水罐湿度] = s.Content[4];
  84. //Data[FeedbackData.负压流量] = s.Content[5];
  85. //Data[FeedbackData.称重水罐重量] = s.Content[6];
  86. //Data[FeedbackData.反应釜编码器值] = s.Content[7];
  87. //Data[FeedbackData.蒸汽比例阀实际开度] = s.Content[8];
  88. DeviceStatus.ReactTemp = s.Content[0];
  89. DeviceStatus.ReactSteamPressure = s.Content[1];
  90. DeviceStatus.ReactSteamFlow = s.Content[2];
  91. DeviceStatus.ColdWaterTankTemp = s.Content[3];
  92. DeviceStatus.ColdWaterTankHumidity = s.Content[4];
  93. DeviceStatus.VacuumFlow = s.Content[5];
  94. DeviceStatus.WaterTankWeight = s.Content[6];
  95. DeviceStatus.ReactEncoderValue = s.Content[7];
  96. DeviceStatus.SteamProportValveRealOpening = s.Content[8];
  97. DeviceStatus.BrineTankWeight = s.Content[9];
  98. SamplingData sampling = new SamplingData()
  99. {
  100. Temperature = s.Content[0],
  101. SteamPressure = s.Content[1],
  102. SteamFlowRate = s.Content[2],
  103. CondensateWaterTemperature = s.Content[3],
  104. CondensateWaterHumidity = s.Content[4],
  105. NegativePressureFlowRate = s.Content[5],
  106. WeighingWaterTankWeight = s.Content[6],
  107. ReactEncoderValue = s.Content[7],
  108. ProportionalValveOpening = s.Content[8],
  109. BrineTankWeight = s.Content[9]
  110. };
  111. if (Delay.GetInstance("趋势数据").Start(_isReadSuccess, 2))
  112. {
  113. _isReadSuccess = false;
  114. }
  115. else if (!_isReadSuccess)
  116. {
  117. SqliteOperate.GetInstance.RealtimeChart(sampling);
  118. if (!string.IsNullOrWhiteSpace(id))
  119. {
  120. sampling.ProductNumberId = id;
  121. SqliteOperate.GetInstance.Add(sampling);
  122. }
  123. _isReadSuccess = true;
  124. }
  125. });
  126. comm.Read<bool>("DB1.DBX1003.7", 1).OnSuccess(s => DeviceStatus.HeartBeat = s.Content );
  127. comm.Read<bool[]>("DB1.DBX2004.0", 16).OnSuccess(s =>
  128. {
  129. AlarmHelper.GetInstance.Get<AlarmData>("50L反应釜").EStop = s.Content[0];
  130. AlarmHelper.GetInstance.Get<AlarmData>("50L反应釜").RotatingMachinesError = s.Content[1];
  131. AlarmHelper.GetInstance.Get<AlarmData>("50L反应釜").AbnormalNegativePressureFan = s.Content[2];
  132. AlarmHelper.GetInstance.Get<AlarmData>("50L反应釜").BrinePumpError = s.Content[3];
  133. AlarmHelper.GetInstance.Get<AlarmData>("50L反应釜").CompressorError = s.Content[4];
  134. });
  135. Thread.Sleep(100);
  136. }, "数据获取", true);
  137. }
  138. private void WriteHeartBeat()
  139. {
  140. Task.Factory.StartNew( async() =>
  141. {
  142. while (true)
  143. {
  144. if (this.IsConnect)
  145. {
  146. //var tokenSource = new CancellationTokenSource();
  147. //tokenSource.CancelAfter(2000);
  148. //await WriteAsync(BoolAddEnum.心跳, true,tokenSource.Token);
  149. Write(BoolAddEnum.心跳, true);
  150. }
  151. //IsConnectChanged?.Invoke(this.IsConnect);
  152. await Task.Delay(2500);
  153. }
  154. },TaskCreationOptions.LongRunning);
  155. }
  156. public OperateResult Write(BoolAddEnum add, bool value)
  157. {
  158. if (boolAdds.ContainsKey(add))
  159. {
  160. return comm.Write(boolAdds[add], value);
  161. }
  162. else
  163. {
  164. return OperateResult.Failed($"未查找到该地址{add.ToString()}");
  165. }
  166. }
  167. public OperateResult Write(FloatAddEnum add, float value)
  168. {
  169. if (floatAdds.ContainsKey(add))
  170. {
  171. return comm.Write(floatAdds[add], value);
  172. }
  173. else
  174. {
  175. return OperateResult.Failed($"未查找到该地址{add.ToString()}");
  176. }
  177. }
  178. public async Task<OperateResult> WriteAsync(BoolAddEnum add, bool value,CancellationToken cancellationToken=default)
  179. {
  180. if (boolAdds.ContainsKey(add))
  181. {
  182. try
  183. {
  184. var result = await Task.Run(() =>
  185. {
  186. return comm.Write(boolAdds[add], value);
  187. }, cancellationToken);
  188. return result;
  189. }
  190. catch (OperationCanceledException)
  191. {
  192. return OperateResult.Failed($"操作被取消");
  193. }
  194. }
  195. else
  196. {
  197. return OperateResult.Failed($"未查找到该地址{add.ToString()}");
  198. }
  199. }
  200. }
  201. }