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.

261 rivejä
9.8 KiB

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