终端一体化运控平台
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

480 wiersze
20 KiB

  1. using BPA.Helper;
  2. using BPASmartClient.CustomResource.UserControls.MessageShow;
  3. using BPASmartClient.CustomResource.UserControls;
  4. using LiveCharts;
  5. using Microsoft.Windows.Input;
  6. using Newtonsoft.Json.Linq;
  7. using SqlSugar.Extensions;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Collections.Specialized;
  11. using System.Configuration;
  12. using System.Data;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using static Org.BouncyCastle.Asn1.Cmp.Challenge;
  17. using Org.BouncyCastle.Asn1.Crmf;
  18. namespace BPASmartClient.Academy.Model
  19. {
  20. public class Simens_PLC
  21. {
  22. private static Simens_PLC _instance;
  23. public static Simens_PLC GetInstance { get; set; } = _instance ??= new Simens_PLC();
  24. private Simens_PLC() { }
  25. public DataFeedback dataFeedback { get; set; } = new DataFeedback();
  26. public PipeLineMode PipeLine { get; set; } = new PipeLineMode();
  27. public string id { get; set; } = "";
  28. //public SiemensHelper siemens { get; set; } = new SiemensHelper();
  29. public ICommSiemens siemens = CommHelper.Siemens;
  30. public DeviceCtrl deviceCtrl { get; set; } = new DeviceCtrl();
  31. public Action<bool> connect { get; set; }
  32. public ConcurrentDictionary<string, string> AlarmInfo { get; set; } = new ConcurrentDictionary<string, string>();
  33. public bool IsConnected { get; set; } = false;
  34. bool heart = false;
  35. public void Connect()
  36. {
  37. Task.Factory.StartNew(() =>
  38. {
  39. CommHelper.CreateSiemens(CpuType.S7200Smart, Json<DevicePar>.Data.IpAddress).OnSuccess(o =>
  40. {
  41. siemens = o.Content;
  42. IsConnected = true;
  43. connect?.Invoke(true);
  44. ThreadManage.GetInstance.StartLong(() =>
  45. {
  46. ReadStatus();
  47. }, "状态读取");
  48. ThreadManage.GetInstance.StartLong(() =>
  49. {
  50. Heartbeat();
  51. connect?.Invoke(IsConnected);
  52. }, "心跳监听");
  53. ThreadManage.GetInstance.StartLong(() =>
  54. {
  55. ChartDataRead();
  56. }, "曲线数据");
  57. ThreadManage.GetInstance.StartLong(() =>
  58. {
  59. AlarmRead();
  60. }, "报警监控");
  61. }).OnFailure(o =>
  62. {
  63. IsConnected = false;
  64. });
  65. });
  66. }
  67. public void Init()
  68. {
  69. AlarmInfoGet();
  70. }
  71. public void AlarmInfoGet()
  72. {
  73. var x = (NameValueCollection)ConfigurationManager.GetSection("FlexBatchSystem/Alarm");
  74. if (x != null)
  75. {
  76. x.AllKeys.ToList().ForEach(o =>
  77. {
  78. if (!AlarmInfo.ContainsKey(o))
  79. {
  80. AlarmInfo.TryAdd(o, x.Get(o));
  81. }
  82. });
  83. }
  84. }
  85. public void AlarmRead()
  86. {
  87. if (IsConnected)
  88. {
  89. AlarmInfo.Keys.ToList().ForEach(o =>
  90. {
  91. siemens.Read<bool>(o).OnSuccess(p =>
  92. {
  93. //if (o == "DB1.DBX2004.4" && p.Content)
  94. //{
  95. // IsConnected = false;
  96. //}
  97. if (RTrig.GetInstance($"{o}").Start(p.Content))
  98. {
  99. App.Current.Dispatcher.Invoke(() =>
  100. {
  101. NoticeDemoViewModel.OpenMsg(EnumPromptType.Warn, App.MainWindow, "警告", Simens_PLC.GetInstance.AlarmInfo[o], 2, 3);
  102. MessageNotify.GetInstance.ShowAlarmLog(Simens_PLC.GetInstance.AlarmInfo[o]);
  103. });
  104. }
  105. });
  106. });
  107. }
  108. Thread.Sleep(2000);
  109. }
  110. public void ReadStatus()
  111. {
  112. if (IsConnected)
  113. {
  114. siemens.Read<bool>("DB1.DBX1003.7").OnSuccess(o =>
  115. {
  116. heart = o.Content;
  117. });
  118. /* siemens.Read<bool[]>("DB1.DBX2000.0", 20).OnSuccess(o =>
  119. {
  120. deviceCtrl.M101 = o.Content[0];
  121. deviceCtrl.M102 = o.Content[1];
  122. deviceCtrl.M103 = o.Content[2];
  123. deviceCtrl.VX100 = o.Content[3];
  124. deviceCtrl.VX101 = o.Content[4];
  125. deviceCtrl.VX102 = o.Content[5];
  126. deviceCtrl.VX103 = o.Content[6];
  127. deviceCtrl.VX104 = o.Content[7];
  128. deviceCtrl.VX105 = o.Content[8];
  129. deviceCtrl.VX106 = o.Content[9];
  130. deviceCtrl.VX107 = o.Content[10];
  131. deviceCtrl.VX108 = o.Content[11];
  132. deviceCtrl.VX109 = o.Content[12];
  133. deviceCtrl.VX110 = o.Content[13];
  134. deviceCtrl.VX111 = o.Content[14];
  135. deviceCtrl.VX112 = o.Content[15];
  136. deviceCtrl.VX113 = o.Content[16];
  137. deviceCtrl.VX114 = o.Content[17];
  138. deviceCtrl.VX115 = o.Content[18];
  139. });*/
  140. siemens.Read<bool>("DB1.DBX2000.0").OnSuccess(o =>
  141. {
  142. deviceCtrl.M101 = o.Content;
  143. });
  144. siemens.Read<bool>("DB1.DBX2000.1").OnSuccess(o =>
  145. {
  146. deviceCtrl.M102 = o.Content;
  147. });
  148. siemens.Read<bool>("DB1.DBX2000.2").OnSuccess(o =>
  149. {
  150. deviceCtrl.M103 = o.Content;
  151. });
  152. siemens.Read<bool>("DB1.DBX2000.3").OnSuccess(o =>
  153. {
  154. deviceCtrl.VX100 = o.Content;
  155. });
  156. siemens.Read<bool>("DB1.DBX2000.4").OnSuccess(o =>
  157. {
  158. deviceCtrl.VX101 = o.Content;
  159. });
  160. siemens.Read<bool>("DB1.DBX2000.5").OnSuccess(o =>
  161. {
  162. deviceCtrl.VX102 = o.Content;
  163. });
  164. siemens.Read<bool>("DB1.DBX2000.6").OnSuccess(o =>
  165. {
  166. deviceCtrl.VX103 = o.Content;
  167. });
  168. siemens.Read<bool>("DB1.DBX2000.7").OnSuccess(o =>
  169. {
  170. deviceCtrl.VX104 = o.Content;
  171. });
  172. siemens.Read<bool>("DB1.DBX2001.0").OnSuccess(o =>
  173. {
  174. deviceCtrl.VX105 = o.Content;
  175. });
  176. siemens.Read<bool>("DB1.DBX2001.1").OnSuccess(o =>
  177. {
  178. deviceCtrl.VX106 = o.Content;
  179. });
  180. siemens.Read<bool>("DB1.DBX2001.2").OnSuccess(o =>
  181. {
  182. deviceCtrl.VX107 = o.Content;
  183. });
  184. siemens.Read<bool>("DB1.DBX2001.3").OnSuccess(o =>
  185. {
  186. deviceCtrl.VX108 = o.Content;
  187. });
  188. siemens.Read<bool>("DB1.DBX2001.4").OnSuccess(o =>
  189. {
  190. deviceCtrl.VX109 = o.Content;
  191. });
  192. siemens.Read<bool>("DB1.DBX2001.5").OnSuccess(o =>
  193. {
  194. deviceCtrl.VX110 = o.Content;
  195. });
  196. siemens.Read<bool>("DB1.DBX2001.6").OnSuccess(o =>
  197. {
  198. deviceCtrl.VX111 = o.Content;
  199. });
  200. siemens.Read<bool>("DB1.DBX2001.7").OnSuccess(o =>
  201. {
  202. deviceCtrl.VX112 = o.Content;
  203. });
  204. siemens.Read<bool>("DB1.DBX2002.0").OnSuccess(o =>
  205. {
  206. deviceCtrl.VX113 = o.Content;
  207. });
  208. siemens.Read<bool>("DB1.DBX2002.1").OnSuccess(o =>
  209. {
  210. deviceCtrl.VX114 = o.Content;
  211. });
  212. siemens.Read<bool>("DB1.DBX2002.2").OnSuccess(o =>
  213. {
  214. deviceCtrl.VX115 = o.Content;
  215. });
  216. siemens.Read<bool>("DB1.DBX2002.3").OnSuccess(o =>
  217. {
  218. deviceCtrl.Compressor = o.Content;
  219. });
  220. siemens.Read<bool>("DB1.DBX2002.4").OnSuccess(o =>
  221. {
  222. deviceCtrl.ByPassMaterial = o.Content;
  223. });
  224. siemens.Read<bool>("DB1.DBX2002.5").OnSuccess(o =>
  225. {
  226. deviceCtrl.ByPassReact = o.Content;
  227. });
  228. siemens.Read<int>("DB1.DBD2022").OnSuccess(o =>
  229. {
  230. deviceCtrl.TempWok = o.Content;
  231. });
  232. siemens.Read<int>("DB1.DBD2026").OnSuccess(o =>
  233. {
  234. deviceCtrl.TempMaterial = o.Content;
  235. });
  236. siemens.Read<float>("DB1.DBD2018").OnSuccess(o =>
  237. {
  238. deviceCtrl.WeightWok = o.Content;
  239. });
  240. siemens.Read<int>("DB1.DBD2030").OnSuccess(o =>
  241. {
  242. deviceCtrl.TempVent = o.Content;
  243. });
  244. siemens.Read<float>("DB1.DBD2034").OnSuccess((o) =>
  245. {
  246. deviceCtrl.PressureWok = o.Content;
  247. });
  248. siemens.Read<float>("DB1.DBD2038").OnSuccess((o) =>
  249. {
  250. deviceCtrl.PressureWeek = o.Content;
  251. });
  252. siemens.Read<float>("DB1.DBD2014").OnSuccess((o) =>
  253. {
  254. deviceCtrl.OpenValue = o.Content;
  255. });
  256. PipeDetect();
  257. siemens.Read<bool[]>("DB1.DBX1000.0", 32).OnSuccess((s) =>
  258. {
  259. #region V1000
  260. deviceCtrl.配料罐搅拌电机 = s.Content[0];
  261. deviceCtrl.反应釜搅拌电机 = s.Content[1];
  262. deviceCtrl.真空泵 = s.Content[2];
  263. deviceCtrl.进料阀 = s.Content[3];
  264. deviceCtrl.蒸馏阀 = s.Content[4];
  265. deviceCtrl.回油阀 = s.Content[5];
  266. deviceCtrl.回水阀 = s.Content[6];
  267. deviceCtrl.汇馏阀 = s.Content[7];
  268. #endregion
  269. #region V1001
  270. deviceCtrl.排馏阀 = s.Content[8];
  271. deviceCtrl.冷凝器冷却阀 = s.Content[9];
  272. deviceCtrl.配料釜蒸汽阀 = s.Content[10];
  273. deviceCtrl.反应釜蒸汽阀 = s.Content[11];
  274. deviceCtrl.反应釜蒸汽疏水阀 = s.Content[12];
  275. deviceCtrl.反应釜冷却水阀 = s.Content[13];
  276. deviceCtrl.配料罐自动升温 = s.Content[14];
  277. deviceCtrl.反应釜自动升温 = s.Content[15];
  278. #endregion
  279. #region V1002
  280. deviceCtrl.系统启动 = s.Content[16];
  281. deviceCtrl.手自动 = s.Content[17];
  282. deviceCtrl.反应釜冷却水排水阀 = s.Content[18];
  283. deviceCtrl.分馏真空阀 = s.Content[19];
  284. deviceCtrl.蒸馏真空阀 = s.Content[20];
  285. deviceCtrl.空压机启动 = s.Content[21];
  286. //deviceCtrl.配料罐搅拌电机 = s.Content[22];
  287. deviceCtrl.水罐重量清零 = s.Content[23];
  288. #endregion
  289. #region V1003
  290. deviceCtrl.反应釜自动降温 = s.Content[24];
  291. deviceCtrl.反应釜出料阀 = s.Content[25];
  292. deviceCtrl.反应釜排空阀 = s.Content[26];
  293. deviceCtrl.配料釜蒸汽输水旁通阀 = s.Content[27];
  294. deviceCtrl.反应釜蒸汽输水旁通阀 = s.Content[28];
  295. //deviceCtrl.配料罐搅拌电机 = s.Content[29];
  296. //deviceCtrl.配料罐搅拌电机 = s.Content[30];
  297. deviceCtrl.心跳 = s.Content[31];
  298. #endregion
  299. });
  300. siemens.Read<bool[]>("DB1.DBX3000.0", 5).OnSuccess((s) =>
  301. {
  302. deviceCtrl.闪蒸 = s.Content[0];
  303. deviceCtrl.真空蒸馏 = s.Content[1];
  304. deviceCtrl.真空下料 = s.Content[2];
  305. deviceCtrl.二次闪蒸 = s.Content[3];
  306. deviceCtrl.排空阀 = s.Content[4];
  307. });
  308. Thread.Sleep(100);
  309. }
  310. }
  311. public void PipeDetect()
  312. {
  313. //阀门3-4连接
  314. if (deviceCtrl.VX102 || deviceCtrl.VX103)
  315. {
  316. PipeLine.PipeH_1.Fill = true;
  317. PipeLine.PipeH_1.Direction = 2;
  318. }
  319. else
  320. {
  321. PipeLine.PipeH_1.Fill = false;
  322. PipeLine.PipeH_1.Direction = 0;
  323. }
  324. //阀门10-11连接
  325. if (!deviceCtrl.VX110 && !deviceCtrl.VX109)
  326. {
  327. PipeLine.PipeH_2.Fill = false;
  328. PipeLine.PipeH_2.Direction = 0;
  329. }
  330. else
  331. {
  332. if (deviceCtrl.VX109)
  333. {
  334. PipeLine.PipeH_2.Fill = true;
  335. PipeLine.PipeH_2.Direction = 1;
  336. }
  337. if (deviceCtrl.VX110)
  338. {
  339. PipeLine.PipeH_2.Fill = true;
  340. PipeLine.PipeH_2.Direction = 2;
  341. }
  342. }
  343. //阀门9-12连接
  344. if (!deviceCtrl.VX108 && !deviceCtrl.VX111)
  345. {
  346. PipeLine.PipeH_3.Fill = false;
  347. PipeLine.PipeH_3.Direction = 0;
  348. }
  349. else
  350. {
  351. if (deviceCtrl.VX108)
  352. {
  353. PipeLine.PipeH_3.Fill = true;
  354. PipeLine.PipeH_3.Direction = 1;
  355. }
  356. if (deviceCtrl.VX111)
  357. {
  358. PipeLine.PipeH_3.Fill = true;
  359. PipeLine.PipeH_3.Direction = 2;
  360. }
  361. }
  362. //阀门13-14连接
  363. if (deviceCtrl.VX112 || deviceCtrl.VX113)
  364. {
  365. PipeLine.PipeH_4.Fill = true;
  366. PipeLine.PipeH_4.Direction = 1;
  367. }
  368. else
  369. {
  370. PipeLine.PipeH_4.Fill = false;
  371. PipeLine.PipeH_4.Direction = 0;
  372. }
  373. }
  374. public void ChartDataRead()
  375. {
  376. //DateTime date = DateTime.Now;
  377. //Random random = new Random();
  378. //SaveData saveData = new SaveData()
  379. //{
  380. // Id = id,
  381. // Name = dataFeedback.Name,
  382. // Date = DateTime.Now,
  383. // M101_Speed = random.Next(0, 2000),
  384. // M102_Speed = random.Next(0, 2000),
  385. // M103_Speed = random.Next(0, 2000),
  386. // OpenValve = random.Next(0, 5),
  387. // TempWok = random.Next(-10, 150),
  388. // TempMaterial = random.Next(-10, 150),
  389. // TempVent = random.Next(-10, 150),
  390. // PressureWok = random.NextDouble(),
  391. // WeightWok = random.Next(0, 50),
  392. //};
  393. //Sqlite.GetInstance.AddData(saveData);
  394. //DataView(dataFeedback.M101_Speed, new DataValue() { DateTime = date, Value = saveData.M101_Speed });
  395. //DataView(dataFeedback.M102_Speed, new DataValue() { DateTime = date, Value = saveData.M102_Speed });
  396. //DataView(dataFeedback.M103_Speed, new DataValue() { DateTime = date, Value = saveData.M103_Speed });
  397. //DataView(dataFeedback.TempWok, new DataValue() { DateTime = date, Value = saveData.TempWok });
  398. //DataView(dataFeedback.TempMaterial, new DataValue() { DateTime = date, Value = saveData.TempMaterial });
  399. //DataView(dataFeedback.TempVent, new DataValue() { DateTime = date, Value = saveData.TempVent });
  400. //DataView(dataFeedback.OpenValve, new DataValue() { DateTime = date, Value = saveData.OpenValve });
  401. //DataView(dataFeedback.PressureWok, new DataValue() { DateTime = date, Value = saveData.PressureWok });
  402. //DataView(dataFeedback.WeightWok, new DataValue() { DateTime = date, Value = saveData.WeightWok });
  403. if (siemens != null && id != "")
  404. {
  405. SaveData saveData = new SaveData()
  406. {
  407. ProductNumberId = Simens_PLC.GetInstance.id,
  408. Name = dataFeedback.Name,
  409. //ProductNumberId = dataFeedback.ProductNumberId,
  410. Date = DateTime.Now,
  411. M101_Speed = siemens.Read<int>("DB1.DBD2006").Content,
  412. M102_Speed = siemens.Read<int>("DB1.DBD2010").Content,
  413. TempWok = siemens.Read<int>("DB1.DBD2022").Content,
  414. TempMaterial = siemens.Read<int>("DB1.DBD2026").Content,
  415. TempVent = siemens.Read<int>("DB1.DBD2030").Content,
  416. WeightWok = siemens.Read<float>("DB1.DBD2018").Content,
  417. PressureWok = siemens.Read<float>("DB1.DBD2034").Content,
  418. PressureWok_Week = siemens.Read<float>("DB1.DBD2038").Content,
  419. OpenValve = siemens.Read<float>("DB1.DBD2014").Content,
  420. };
  421. Sqlite.GetInstance.AddData(saveData);
  422. DataView(dataFeedback.M101_Speed, new DataValue() { DateTime = saveData.Date, Value = saveData.M101_Speed });
  423. DataView(dataFeedback.M102_Speed, new DataValue() { DateTime = saveData.Date, Value = saveData.M102_Speed });
  424. DataView(dataFeedback.TempWok, new DataValue() { DateTime = saveData.Date, Value = saveData.TempWok });
  425. DataView(dataFeedback.TempMaterial, new DataValue() { DateTime = saveData.Date, Value = saveData.TempMaterial });
  426. DataView(dataFeedback.TempVent, new DataValue() { DateTime = saveData.Date, Value = saveData.TempVent });
  427. DataView(dataFeedback.WeightWok, new DataValue() { DateTime = saveData.Date, Value = saveData.WeightWok });
  428. DataView(dataFeedback.OpenValve, new DataValue() { DateTime = saveData.Date, Value = saveData.OpenValve });
  429. DataView(dataFeedback.PressureWok, new DataValue() { DateTime = saveData.Date, Value = saveData.PressureWok });
  430. DataView(dataFeedback.PressureWok_Week, new DataValue() { DateTime = saveData.Date, Value = saveData.PressureWok_Week });
  431. }
  432. Thread.Sleep(1000);
  433. }
  434. public void DataView<T>(ChartValues<T> values, T value)
  435. {
  436. if (values.Count >= 20)
  437. {
  438. values.RemoveAt(0);
  439. values.Add(value);
  440. }
  441. else
  442. {
  443. values.Add(value);
  444. }
  445. }
  446. public void Heartbeat()
  447. {
  448. if (!heart && siemens != null)
  449. {
  450. siemens.Write<bool>("DB1.DBX1003.7", true).OnFailure(o =>
  451. {
  452. IsConnected = false;
  453. GetInstance.Connect();
  454. }).OnSuccess(o =>
  455. {
  456. IsConnected = true;
  457. });
  458. }
  459. Thread.Sleep(3000);
  460. }
  461. }
  462. }