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

313 lines
14 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. using System.Collections.Specialized;
  12. using System.Configuration;
  13. using BPASmartClient.CustomResource.UserControls.MessageShow;
  14. namespace BPASmartClient.Academy._50L
  15. {
  16. public class PlcControl : AddData
  17. {
  18. private volatile static PlcControl _Instance;
  19. public static PlcControl GetInstance => _Instance ?? (_Instance = new PlcControl());
  20. private PlcControl() { }
  21. public bool IsConnect => comm != null && comm.IsConnected();
  22. public DeviceStatus DeviceStatus { get; set; } = new DeviceStatus();
  23. public ConcurrentDictionary<string, string> AlarmInfo { get; private set; } = new ConcurrentDictionary<string, string>();
  24. private ICommSiemens comm = CommHelper.Siemens;
  25. public string id { get; set; } = "";
  26. public void Init()
  27. {
  28. AlarmInfoGet();
  29. Task.Factory.StartNew(() =>
  30. {
  31. CommHelper.CreateSiemens(CpuType.S7200Smart, Json<DevicePar>.Data.IpAddress).OnSuccess(s =>
  32. {
  33. comm = s.Content;
  34. WriteHeartBeat();
  35. Read();
  36. });
  37. });
  38. }
  39. private volatile bool _isReadSuccess;
  40. public void AlarmInfoGet()
  41. {
  42. var x = (NameValueCollection)ConfigurationManager.GetSection("Reactor_50L_Alarm/Alarm");
  43. if (x != null)
  44. {
  45. x.AllKeys.ToList().ForEach(o =>
  46. {
  47. if (!AlarmInfo.ContainsKey(o))
  48. {
  49. AlarmInfo.TryAdd(o, x.Get(o));
  50. }
  51. });
  52. }
  53. }
  54. public void AlarmRead()
  55. {
  56. if (this.IsConnect)
  57. {
  58. AlarmInfo.Keys.ToList().ForEach(o =>
  59. {
  60. comm.Read<bool>(o).OnSuccess(p =>
  61. {
  62. if (RTrig.GetInstance($"{o}").Start(p.Content))
  63. {
  64. App.Current.Dispatcher.Invoke(() =>
  65. {
  66. NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, App.MainWindow, "报警", this.AlarmInfo[o]);
  67. MessageNotify.GetInstance.ShowAlarmLog(this.AlarmInfo[o]);
  68. });
  69. }
  70. });
  71. });
  72. }
  73. Thread.Sleep(2000);
  74. }
  75. private void Read()
  76. {
  77. TaskManage.GetInstance.StartLong(() =>
  78. {
  79. comm.Read<bool[]>("DB1.DBX1000.0", 32).OnSuccess(s =>
  80. {
  81. #region V1000
  82. DeviceStatus.IsSetReactTurnMotor = s.Content[0];
  83. DeviceStatus.IsSetFans = s.Content[1];
  84. DeviceStatus.IsSetBrinePumpState = s.Content[2];
  85. DeviceStatus.IsSetReactHomeBit = s.Content[3];
  86. DeviceStatus.IsSetReactOut = s.Content[4];
  87. DeviceStatus.IsSetReactTurnMotorJogging = s.Content[5];
  88. DeviceStatus.IsSetReactHeat = s.Content[6];
  89. DeviceStatus.IsSetReactCool = s.Content[7];
  90. #endregion
  91. #region V1001
  92. DeviceStatus.IsSetCWTCool = s.Content[8];
  93. DeviceStatus.IsSetReactPressureReliefValve = s.Content[9];
  94. DeviceStatus.IsSetReactOutHotGasValve = s.Content[10];
  95. DeviceStatus.IsSetHotGasEmptyValve = s.Content[11];
  96. DeviceStatus.IsSetColdWaterTankInAirValve = s.Content[12];
  97. DeviceStatus.IsSetReactDrainValve = s.Content[13];
  98. DeviceStatus.IsSetReactInCleanWaterValve = s.Content[14];
  99. DeviceStatus.IsSetReactPressureAirInValve = s.Content[15];
  100. #endregion
  101. #region V1002
  102. DeviceStatus.IsSetReactInBrineWaterValve = s.Content[16];
  103. DeviceStatus.IsSetColdWaterTankVacuumValve = s.Content[17];
  104. DeviceStatus.IsSetCompressorRunning = s.Content[18];
  105. DeviceStatus.IsSetReactEnter = s.Content[19];
  106. DeviceStatus.IsSetReactReverse = s.Content[20];
  107. DeviceStatus.IsSetStartEnterBrine = s.Content[21];
  108. DeviceStatus.IsSetStartEmptyWater = s.Content[22];
  109. DeviceStatus.IsSetReactOutColdValve = s.Content[23];
  110. #endregion
  111. #region V1003
  112. DeviceStatus.IsSetColdWaterTankInWaterValve = s.Content[24];
  113. DeviceStatus.IsSetStartClean = s.Content[25];
  114. DeviceStatus.IsSetVacuumDistill = s.Content[26];
  115. DeviceStatus.IsSetGoAddPosition = s.Content[27];
  116. DeviceStatus.IsSetColdWaterTankZero = s.Content[28];
  117. DeviceStatus.IsSetBrineTankZero = s.Content[29];
  118. DeviceStatus.IsSetSystemStart = s.Content[30];
  119. DeviceStatus.IsSetHeartBeat = s.Content[31];
  120. #endregion
  121. });
  122. comm.Read<bool[]>("DB1.DBX2000.0", 31).OnSuccess(s =>
  123. {
  124. //Data[FeedbackData.旋转电机启停反馈] = s.Content[0];
  125. //Data[FeedbackData.负压风机启动反馈] = s.Content[1];
  126. //Data[FeedbackData.压缩空气进气阀反馈] = s.Content[2];
  127. //Data[FeedbackData.排热空气阀反馈] = s.Content[3];
  128. //Data[FeedbackData.泄压阀反馈] = s.Content[4];
  129. //Data[FeedbackData.进冷却水阀反馈] = s.Content[5];
  130. //Data[FeedbackData.出冷却水阀反馈] = s.Content[6];
  131. //Data[FeedbackData.蒸汽梳水阀反馈] = s.Content[7];
  132. //Data[FeedbackData.冷凝水罐进水阀反馈] = s.Content[8];
  133. //Data[FeedbackData.称重水罐负压阀反馈] = s.Content[9];
  134. //Data[FeedbackData.反应釜原点位置反馈] = s.Content[10];
  135. #region V2000
  136. DeviceStatus.ReactTurnMotor = s.Content[0];
  137. DeviceStatus.Fans = s.Content[1];
  138. DeviceStatus.BrinePumpState = s.Content[2];
  139. DeviceStatus.ReactInSteamValue = s.Content[3];
  140. DeviceStatus.ReactInColdValve = s.Content[4];
  141. DeviceStatus.ReactOutColdValve = s.Content[5];
  142. DeviceStatus.ReactDrainValve = s.Content[6];
  143. DeviceStatus.ReactInCleanWaterValve = s.Content[7];
  144. #endregion
  145. #region V2001
  146. DeviceStatus.ReactPressureAirInValve = s.Content[8];
  147. DeviceStatus.ReactInBrineWaterValve = s.Content[9];
  148. DeviceStatus.ReactOutHotGasValve = s.Content[10];
  149. DeviceStatus.HotGasEmptyValve = s.Content[11];
  150. DeviceStatus.ColdWaterTankInAirValve = s.Content[12];
  151. DeviceStatus.ColdWaterTankInWaterValve = s.Content[13];
  152. DeviceStatus.ReactHomeBit = s.Content[14];
  153. DeviceStatus.ReactPressureReliefValve = s.Content[15];
  154. #endregion
  155. #region V2002
  156. DeviceStatus.ColdWaterTankVacuumValve = s.Content[16];
  157. DeviceStatus.CompressorRunning = s.Content[17];
  158. #endregion
  159. });
  160. comm.Read<float[]>("DB1.DBD2006", 11).OnSuccess(s =>
  161. {
  162. //Data[FeedbackData.反应釜温度] = s.Content[0];
  163. //Data[FeedbackData.反应釜蒸汽压力] = s.Content[1];
  164. //Data[FeedbackData.反应釜蒸汽流量] = s.Content[2];
  165. //Data[FeedbackData.冷凝水罐温度] = s.Content[3];
  166. //Data[FeedbackData.冷凝水罐湿度] = s.Content[4];
  167. //Data[FeedbackData.负压流量] = s.Content[5];
  168. //Data[FeedbackData.称重水罐重量] = s.Content[6];
  169. //Data[FeedbackData.反应釜编码器值] = s.Content[7];
  170. //Data[FeedbackData.蒸汽比例阀实际开度] = s.Content[8];
  171. DeviceStatus.ReactTemp = s.Content[0];
  172. DeviceStatus.ReactSteamPressure = s.Content[1];
  173. DeviceStatus.ReactSteamFlow = s.Content[2];
  174. DeviceStatus.ColdWaterTankTemp = s.Content[3];
  175. DeviceStatus.ColdWaterTankHumidity = s.Content[4];
  176. DeviceStatus.VacuumFlow = s.Content[5];
  177. DeviceStatus.WaterTankWeight = s.Content[6];
  178. DeviceStatus.ReactEncoderValue = s.Content[7];
  179. DeviceStatus.SteamProportValveRealOpening = s.Content[8];
  180. DeviceStatus.BrineTankWeight = s.Content[9];
  181. DeviceStatus.ReactPressure = s.Content[10];
  182. SamplingData sampling = new SamplingData()
  183. {
  184. Temperature = s.Content[0],
  185. SteamPressure = s.Content[1],
  186. SteamFlowRate = s.Content[2],
  187. CondensateWaterTemperature = s.Content[3],
  188. CondensateWaterHumidity = s.Content[4],
  189. NegativePressureFlowRate = s.Content[5],
  190. WeighingWaterTankWeight = s.Content[6],
  191. ReactEncoderValue = s.Content[7],
  192. ProportionalValveOpening = s.Content[8],
  193. BrineTankWeight = s.Content[9],
  194. ReactPressure = s.Content[10]
  195. };
  196. if (Delay.GetInstance("趋势数据").Start(_isReadSuccess, 2))
  197. {
  198. _isReadSuccess = false;
  199. }
  200. else if (!_isReadSuccess)
  201. {
  202. SqliteOperate.GetInstance.RealtimeChart(sampling);
  203. if (!string.IsNullOrWhiteSpace(id))
  204. {
  205. sampling.ProductNumberId = id;
  206. SqliteOperate.GetInstance.Add(sampling);
  207. }
  208. _isReadSuccess = true;
  209. }
  210. });
  211. comm.Read<bool>("DB1.DBX1003.7", 1).OnSuccess(s => DeviceStatus.HeartBeat = s.Content );
  212. //comm.Read<bool[]>("DB1.DBX2004.0", 16).OnSuccess(s =>
  213. //{
  214. // AlarmHelper.GetInstance.Get<AlarmData>("50L反应釜").EStop = s.Content[0];
  215. // AlarmHelper.GetInstance.Get<AlarmData>("50L反应釜").RotatingMachinesError = s.Content[1];
  216. // AlarmHelper.GetInstance.Get<AlarmData>("50L反应釜").AbnormalNegativePressureFan = s.Content[2];
  217. // AlarmHelper.GetInstance.Get<AlarmData>("50L反应釜").BrinePumpError = s.Content[3];
  218. // AlarmHelper.GetInstance.Get<AlarmData>("50L反应釜").CompressorError = s.Content[4];
  219. // AlarmHelper.GetInstance.Get<AlarmData>("50L反应釜").ReactorPressureLimit = s.Content[5];
  220. //});
  221. AlarmRead();
  222. Thread.Sleep(100);
  223. }, "数据获取", true);
  224. }
  225. private void WriteHeartBeat()
  226. {
  227. Task.Factory.StartNew( async() =>
  228. {
  229. while (true)
  230. {
  231. if (this.IsConnect)
  232. {
  233. //var tokenSource = new CancellationTokenSource();
  234. //tokenSource.CancelAfter(2000);
  235. //await WriteAsync(BoolAddEnum.心跳, true,tokenSource.Token);
  236. Write(BoolAddEnum.心跳, true);
  237. }
  238. //IsConnectChanged?.Invoke(this.IsConnect);
  239. await Task.Delay(2500);
  240. }
  241. },TaskCreationOptions.LongRunning);
  242. }
  243. public OperateResult Write(BoolAddEnum add, bool value)
  244. {
  245. if (boolAdds.ContainsKey(add))
  246. {
  247. return comm.Write(boolAdds[add], value);
  248. }
  249. else
  250. {
  251. return OperateResult.Failed($"未查找到该地址{add.ToString()}");
  252. }
  253. }
  254. public OperateResult Write(FloatAddEnum add, float value)
  255. {
  256. if (floatAdds.ContainsKey(add))
  257. {
  258. return comm.Write(floatAdds[add], value);
  259. }
  260. else
  261. {
  262. return OperateResult.Failed($"未查找到该地址{add.ToString()}");
  263. }
  264. }
  265. public async Task<OperateResult> WriteAsync(BoolAddEnum add, bool value,CancellationToken cancellationToken=default)
  266. {
  267. if (boolAdds.ContainsKey(add))
  268. {
  269. try
  270. {
  271. var result = await Task.Run(() =>
  272. {
  273. return comm.Write(boolAdds[add], value);
  274. }, cancellationToken);
  275. return result;
  276. }
  277. catch (OperationCanceledException)
  278. {
  279. return OperateResult.Failed($"操作被取消");
  280. }
  281. }
  282. else
  283. {
  284. return OperateResult.Failed($"未查找到该地址{add.ToString()}");
  285. }
  286. }
  287. }
  288. }