25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

289 satır
12 KiB

  1. using System.Collections.Concurrent;
  2. using System.Diagnostics;
  3. using BPA.Helper;
  4. using BPASmartClient.CustomResource.Pages.Model;
  5. using S7.Net;
  6. namespace HKControl
  7. {
  8. public class Main
  9. {
  10. private volatile static Main _Instance;
  11. public static Main GetInstance => _Instance ?? (_Instance = new Main());
  12. private Main() { }
  13. ConcurrentDictionary<int, Siemens> SiemensDicitonary = new ConcurrentDictionary<int, Siemens>();
  14. ConcurrentDictionary<int, DataModel> DataModels = new ConcurrentDictionary<int, DataModel>();
  15. ConcurrentQueue<int> CarQueue = new ConcurrentQueue<int>();
  16. public Action[] CompleteNotify { get; set; } = new Action[6];
  17. /// <summary>
  18. /// 判断指定出餐窗口是否允许刷卡(1-6)
  19. /// </summary>
  20. /// <param name="CarNum"></param>
  21. /// <returns></returns>
  22. public bool GetIsSwipe(int CarNum)
  23. {
  24. switch (CarNum)
  25. {
  26. case 1:
  27. if (!DataModels.ContainsKey(1)) return false;
  28. return DataModels[1].LeftWindowData.IsSwipe;
  29. case 2:
  30. if (!DataModels.ContainsKey(1)) return false;
  31. return DataModels[1].RightWindowData.IsSwipe;
  32. case 3:
  33. if (!DataModels.ContainsKey(2)) return false;
  34. return DataModels[2].LeftWindowData.IsSwipe;
  35. case 4:
  36. if (!DataModels.ContainsKey(2)) return false;
  37. return DataModels[2].RightWindowData.IsSwipe;
  38. case 5:
  39. if (!DataModels.ContainsKey(3)) return false;
  40. return DataModels[3].LeftWindowData.IsSwipe;
  41. case 6:
  42. if (!DataModels.ContainsKey(3)) return false;
  43. return DataModels[3].LeftWindowData.IsSwipe;
  44. default:
  45. break;
  46. }
  47. return false;
  48. }
  49. public bool GetIsConnect(int CarNum)
  50. {
  51. switch (CarNum)
  52. {
  53. case 1:
  54. if (!SiemensDicitonary.ContainsKey(1)) return false;
  55. return SiemensDicitonary[1].IsConnected;
  56. case 2:
  57. if (!SiemensDicitonary.ContainsKey(1)) return false;
  58. return SiemensDicitonary[1].IsConnected;
  59. case 3:
  60. if (!SiemensDicitonary.ContainsKey(2)) return false;
  61. return SiemensDicitonary[2].IsConnected;
  62. case 4:
  63. if (!SiemensDicitonary.ContainsKey(2)) return false;
  64. return SiemensDicitonary[2].IsConnected;
  65. case 5:
  66. if (!SiemensDicitonary.ContainsKey(3)) return false;
  67. return SiemensDicitonary[3].IsConnected;
  68. case 6:
  69. if (!SiemensDicitonary.ContainsKey(3)) return false;
  70. return SiemensDicitonary[3].IsConnected;
  71. default:
  72. break;
  73. }
  74. return false;
  75. }
  76. /// <summary>
  77. /// 获取指定档口是否配餐完成
  78. /// </summary>
  79. /// <param name="CarNum"></param>
  80. /// <returns></returns>
  81. public bool GetIsComplete(int CarNum)
  82. {
  83. if (!DataModels.ContainsKey(CarNum)) return false;
  84. switch (CarNum)
  85. {
  86. case 1:
  87. return DataModels[1].LeftWindowData.Complete;
  88. case 2:
  89. return DataModels[1].RightWindowData.Complete;
  90. case 3:
  91. return DataModels[2].LeftWindowData.Complete;
  92. case 4:
  93. return DataModels[2].RightWindowData.Complete;
  94. case 5:
  95. return DataModels[3].LeftWindowData.Complete;
  96. case 6:
  97. return DataModels[3].LeftWindowData.Complete;
  98. default:
  99. break;
  100. }
  101. return false;
  102. }
  103. /// <summary>
  104. /// 设置指定窗口开始出餐(1--6)
  105. /// </summary>
  106. /// <param name="CarNum"></param>
  107. public void Start(int CarNum)
  108. {
  109. CarQueue.Enqueue(CarNum);
  110. }
  111. /// <summary>
  112. /// 初始化
  113. /// </summary>
  114. public void Init()
  115. {
  116. DataInit();
  117. Json<CommunicationPar>.Data.CommunicationModels.ToList()?.ForEach(item =>
  118. {
  119. if (!DataModels.ContainsKey(item.DeviceNum)) { DataModels.TryAdd(item.DeviceNum, new DataModel()); };
  120. if (!SiemensDicitonary.ContainsKey(item.DeviceNum)) { SiemensDicitonary.TryAdd(item.DeviceNum, new Siemens()); }
  121. ThreadManage.GetInstance().Start(new Action(() =>
  122. {
  123. SiemensDicitonary[item.DeviceNum].ConnectOk = new Action(() =>
  124. {
  125. AlarmHelper<DataModel>.GetInstance($"{item.DeviceNum}号档口");
  126. AlarmHelper<DataModel>.Init();
  127. HKLog.HKLogImport.WriteInfo($"{item.DeviceNum}:连接成功");
  128. ThreadManage.GetInstance().StartLong(new Action(() =>
  129. {
  130. try
  131. {
  132. var md = AlarmHelper<DataModel>.GetInstance($"{item.DeviceNum}号档口");
  133. var res = SiemensDicitonary[item.DeviceNum].Read<byte>("MB7");
  134. var res1 = SiemensDicitonary[item.DeviceNum].Read<byte>("MB10");
  135. var res2 = SiemensDicitonary[item.DeviceNum].Read<byte>("MB11");
  136. md.LeftWindowData.L1 = Get8bitValue(res1, 2);
  137. md.LeftWindowData.L2 = Get8bitValue(res1, 3);
  138. md.LeftWindowData.L3 = Get8bitValue(res1, 4);
  139. md.LeftWindowData.L4 = Get8bitValue(res1, 5);
  140. md.LeftWindowData.L5 = Get8bitValue(res1, 6);
  141. md.LeftWindowData.L6 = Get8bitValue(res1, 7);
  142. md.LeftWindowData.L7 = Get8bitValue(res1, 8);
  143. md.LeftWindowData.L8 = Get8bitValue(res2, 1);
  144. md.LeftWindowData.L9 = Get8bitValue(res2, 2);
  145. var res3 = SiemensDicitonary[item.DeviceNum].Read<byte>("MB12");
  146. var res4 = SiemensDicitonary[item.DeviceNum].Read<byte>("MB13");
  147. md.RightWindowData.R1 = Get8bitValue(res3, 2);
  148. md.RightWindowData.R2 = Get8bitValue(res3, 3);
  149. md.RightWindowData.R3 = Get8bitValue(res3, 4);
  150. md.RightWindowData.R4 = Get8bitValue(res3, 5);
  151. md.RightWindowData.R5 = Get8bitValue(res3, 6);
  152. md.RightWindowData.R6 = Get8bitValue(res3, 7);
  153. md.RightWindowData.R7 = Get8bitValue(res3, 8);
  154. md.RightWindowData.R8 = Get8bitValue(res4, 1);
  155. md.RightWindowData.R9 = Get8bitValue(res4, 2);
  156. DataModels[item.DeviceNum].LeftWindowData.IsSwipe = Get8bitValue(res, 1);
  157. DataModels[item.DeviceNum].LeftWindowData.Complete = Get8bitValue(res, 2);
  158. DataModels[item.DeviceNum].RightWindowData.IsSwipe = Get8bitValue(res, 3);
  159. DataModels[item.DeviceNum].RightWindowData.Complete = Get8bitValue(res, 4);
  160. }
  161. catch (Exception ex)
  162. {
  163. HKLog.HKLogImport.WriteInfo(ex.ToString());
  164. }
  165. Thread.Sleep(100);
  166. }), $"{item.DeviceNum} 号设备监听", true);
  167. //ThreadManage.GetInstance().StartLong(new Action(() =>
  168. //{
  169. // for (int i = 0; i < 6; i++)
  170. // {
  171. // if (RTrig.GetInstance($"窗口{i + 1}").Start(GetIsComplete(i + 1)))
  172. // {
  173. // CompleteNotify[i]?.Invoke();
  174. // }
  175. // }
  176. // Thread.Sleep(100);
  177. //}), "完成通知");
  178. });
  179. SiemensDicitonary[item.DeviceNum].Connect(CpuType.S7200Smart, item.IpAddress);
  180. }), $"{item.DeviceNum} 号设备连接初始化");
  181. });
  182. StartRun();
  183. }
  184. private void StartRun()
  185. {
  186. ThreadManage.GetInstance().StartLong(new Action(() =>
  187. {
  188. while (CarQueue.Count > 0)
  189. {
  190. if (CarQueue.TryDequeue(out int carNum))
  191. {
  192. switch (carNum)
  193. {
  194. case 1:
  195. Control(1, "M6.1");
  196. break;
  197. case 2:
  198. Control(1, "M6.0");
  199. break;
  200. case 3:
  201. Control(2, "M6.1");
  202. break;
  203. case 4:
  204. Control(2, "M6.0");
  205. break;
  206. case 5:
  207. Control(3, "M6.1");
  208. break;
  209. case 6:
  210. Control(3, "M6.0");
  211. break;
  212. default:
  213. break;
  214. }
  215. }
  216. }
  217. Thread.Sleep(100);
  218. }), "开始运行设备");
  219. }
  220. private void Control(int num, string add)
  221. {
  222. HKLog.HKLogImport.WriteInfo($"地址:{add} {num} 号出餐口控制");
  223. SiemensDicitonary[num].Write(add, true);
  224. Thread.Sleep(500);
  225. SiemensDicitonary[num].Write(add, false);
  226. }
  227. private void DataInit()
  228. {
  229. Json<CommunicationPar>.Read();
  230. if (Json<CommunicationPar>.Data.CommunicationModels.Count < 3)
  231. {
  232. Json<CommunicationPar>.Data.CommunicationModels.Clear();
  233. Json<CommunicationPar>.Data.CommunicationModels.Add(new CommunicationModel()
  234. {
  235. IpAddress = "192.168.0.1",
  236. DeviceNum = 3
  237. });
  238. Json<CommunicationPar>.Data.CommunicationModels.Add(new CommunicationModel()
  239. {
  240. IpAddress = "192.168.0.2",
  241. DeviceNum = 2
  242. });
  243. Json<CommunicationPar>.Data.CommunicationModels.Add(new CommunicationModel()
  244. {
  245. IpAddress = "192.168.0.3",
  246. DeviceNum = 1
  247. });
  248. Json<CommunicationPar>.Save();
  249. }
  250. }
  251. /// <summary>
  252. /// 获取字节中指定位的值
  253. /// </summary>
  254. /// <param name="data">要获取的整数</param>
  255. /// <param name="offset">偏移量 范围(1-8)</param>
  256. /// <returns></returns>
  257. public bool Get8bitValue(byte data, byte offset)
  258. {
  259. if (offset > 8 || offset < 1) return false;
  260. return (data & 1 << offset - 1) == 0 ? false : true;
  261. }
  262. }
  263. }