终端一体化运控平台
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.
 
 
 

224 lines
9.6 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.S7200Smart, 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", 11).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. DeviceStatus.ReactPressure = s.Content[10];
  99. SamplingData sampling = new SamplingData()
  100. {
  101. Temperature = s.Content[0],
  102. SteamPressure = s.Content[1],
  103. SteamFlowRate = s.Content[2],
  104. CondensateWaterTemperature = s.Content[3],
  105. CondensateWaterHumidity = s.Content[4],
  106. NegativePressureFlowRate = s.Content[5],
  107. WeighingWaterTankWeight = s.Content[6],
  108. ReactEncoderValue = s.Content[7],
  109. ProportionalValveOpening = s.Content[8],
  110. BrineTankWeight = s.Content[9],
  111. ReactPressure = s.Content[10]
  112. };
  113. if (Delay.GetInstance("趋势数据").Start(_isReadSuccess, 2))
  114. {
  115. _isReadSuccess = false;
  116. }
  117. else if (!_isReadSuccess)
  118. {
  119. SqliteOperate.GetInstance.RealtimeChart(sampling);
  120. if (!string.IsNullOrWhiteSpace(id))
  121. {
  122. sampling.ProductNumberId = id;
  123. SqliteOperate.GetInstance.Add(sampling);
  124. }
  125. _isReadSuccess = true;
  126. }
  127. });
  128. comm.Read<bool>("DB1.DBX1003.7", 1).OnSuccess(s => DeviceStatus.HeartBeat = s.Content );
  129. comm.Read<bool[]>("DB1.DBX2004.0", 16).OnSuccess(s =>
  130. {
  131. AlarmHelper.GetInstance.Get<AlarmData>("50L反应釜").EStop = s.Content[0];
  132. AlarmHelper.GetInstance.Get<AlarmData>("50L反应釜").RotatingMachinesError = s.Content[1];
  133. AlarmHelper.GetInstance.Get<AlarmData>("50L反应釜").AbnormalNegativePressureFan = s.Content[2];
  134. AlarmHelper.GetInstance.Get<AlarmData>("50L反应釜").BrinePumpError = s.Content[3];
  135. AlarmHelper.GetInstance.Get<AlarmData>("50L反应釜").CompressorError = s.Content[4];
  136. AlarmHelper.GetInstance.Get<AlarmData>("50L反应釜").ReactorPressureLimit = s.Content[4];
  137. });
  138. Thread.Sleep(100);
  139. }, "数据获取", true);
  140. }
  141. private void WriteHeartBeat()
  142. {
  143. Task.Factory.StartNew( async() =>
  144. {
  145. while (true)
  146. {
  147. if (this.IsConnect)
  148. {
  149. //var tokenSource = new CancellationTokenSource();
  150. //tokenSource.CancelAfter(2000);
  151. //await WriteAsync(BoolAddEnum.心跳, true,tokenSource.Token);
  152. Write(BoolAddEnum.心跳, true);
  153. }
  154. //IsConnectChanged?.Invoke(this.IsConnect);
  155. await Task.Delay(2500);
  156. }
  157. },TaskCreationOptions.LongRunning);
  158. }
  159. public OperateResult Write(BoolAddEnum add, bool value)
  160. {
  161. if (boolAdds.ContainsKey(add))
  162. {
  163. return comm.Write(boolAdds[add], value);
  164. }
  165. else
  166. {
  167. return OperateResult.Failed($"未查找到该地址{add.ToString()}");
  168. }
  169. }
  170. public OperateResult Write(FloatAddEnum add, float value)
  171. {
  172. if (floatAdds.ContainsKey(add))
  173. {
  174. return comm.Write(floatAdds[add], value);
  175. }
  176. else
  177. {
  178. return OperateResult.Failed($"未查找到该地址{add.ToString()}");
  179. }
  180. }
  181. public async Task<OperateResult> WriteAsync(BoolAddEnum add, bool value,CancellationToken cancellationToken=default)
  182. {
  183. if (boolAdds.ContainsKey(add))
  184. {
  185. try
  186. {
  187. var result = await Task.Run(() =>
  188. {
  189. return comm.Write(boolAdds[add], value);
  190. }, cancellationToken);
  191. return result;
  192. }
  193. catch (OperationCanceledException)
  194. {
  195. return OperateResult.Failed($"操作被取消");
  196. }
  197. }
  198. else
  199. {
  200. return OperateResult.Failed($"未查找到该地址{add.ToString()}");
  201. }
  202. }
  203. }
  204. }