Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

311 linhas
13 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>.Init();
  126. //AlarmHelper<DataModel>.GetInstance($"{item.DeviceNum}号档口");
  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 MB7 = SiemensDicitonary[item.DeviceNum].Read<byte>("MB7");
  134. DataModels[item.DeviceNum].LeftWindowData.IsSwipe = Get8bitValue(MB7, 1);
  135. DataModels[item.DeviceNum].LeftWindowData.Complete = Get8bitValue(MB7, 2);
  136. DataModels[item.DeviceNum].RightWindowData.IsSwipe = Get8bitValue(MB7, 3);
  137. DataModels[item.DeviceNum].RightWindowData.Complete = Get8bitValue(MB7, 4);
  138. var MB10 = SiemensDicitonary[item.DeviceNum].Read<byte>("MB10");
  139. var MB11 = SiemensDicitonary[item.DeviceNum].Read<byte>("MB11");
  140. md.LeftWindowData.L2 = Get8bitValue(MB10, 2);
  141. md.LeftWindowData.L3 = Get8bitValue(MB10, 3);
  142. md.LeftWindowData.L4 = Get8bitValue(MB10, 4);
  143. md.LeftWindowData.L5 = Get8bitValue(MB10, 5);
  144. md.LeftWindowData.L6 = Get8bitValue(MB10, 6);
  145. md.LeftWindowData.L7 = Get8bitValue(MB10, 7);
  146. md.LeftWindowData.L8 = Get8bitValue(MB10, 8);
  147. md.LeftWindowData.L9 = Get8bitValue(MB11, 1);
  148. var MB12 = SiemensDicitonary[item.DeviceNum].Read<byte>("MB12");
  149. var MB13 = SiemensDicitonary[item.DeviceNum].Read<byte>("MB13");
  150. md.RightWindowData.R2 = Get8bitValue(MB12, 2);
  151. md.RightWindowData.R3 = Get8bitValue(MB12, 3);
  152. md.RightWindowData.R4 = Get8bitValue(MB12, 4);
  153. md.RightWindowData.R5 = Get8bitValue(MB12, 5);
  154. md.RightWindowData.R6 = Get8bitValue(MB12, 6);
  155. md.RightWindowData.R7 = Get8bitValue(MB12, 7);
  156. md.RightWindowData.R8 = Get8bitValue(MB12, 8);
  157. md.RightWindowData.R9 = Get8bitValue(MB13, 1);
  158. var MB14 = SiemensDicitonary[item.DeviceNum].Read<byte>("MB14");
  159. md.WaterTemperatureLow = Get8bitValue(MB14, 5);
  160. md.FrequencyConverterFault = Get8bitValue(MB14, 6);
  161. var MB15 = SiemensDicitonary[item.DeviceNum].Read<byte>("MB15");
  162. md.RobotCommFault = Get8bitValue(MB15, 6);
  163. bool TempFault = Get8bitValue(MB15, 5);
  164. if (TempFault)
  165. {
  166. md.RBStatus = RobotStatus.故障;
  167. }
  168. else
  169. {
  170. if (Get8bitValue(MB15, 1))
  171. md.RBStatus = RobotStatus.运行;
  172. else if (Get8bitValue(MB15, 2))
  173. md.RBStatus = RobotStatus.停止;
  174. else if (Get8bitValue(MB15, 3))
  175. md.RBStatus = RobotStatus.暂停;
  176. else if (Get8bitValue(MB15, 4))
  177. md.RBStatus = RobotStatus.准备;
  178. }
  179. }
  180. catch (Exception ex)
  181. {
  182. HKLog.HKLogImport.WriteInfo(ex.ToString());
  183. }
  184. Thread.Sleep(100);
  185. //}), $"号设备监听", true);
  186. }), $"{item.DeviceNum} 号设备监听", true);
  187. //ThreadManage.GetInstance().StartLong(new Action(() =>
  188. //{
  189. // for (int i = 0; i < 6; i++)
  190. // {
  191. // if (RTrig.GetInstance($"窗口{i + 1}").Start(GetIsComplete(i + 1)))
  192. // {
  193. // CompleteNotify[i]?.Invoke();
  194. // }
  195. // }
  196. // Thread.Sleep(100);
  197. //}), "完成通知");
  198. });
  199. SiemensDicitonary[item.DeviceNum].Connect(CpuType.S7200Smart, item.IpAddress);
  200. }), $"{item.DeviceNum} 号设备连接初始化");
  201. });
  202. StartRun();
  203. }
  204. private void StartRun()
  205. {
  206. ThreadManage.GetInstance().StartLong(new Action(() =>
  207. {
  208. while (CarQueue.Count > 0)
  209. {
  210. if (CarQueue.TryDequeue(out int carNum))
  211. {
  212. switch (carNum)
  213. {
  214. case 1:
  215. Control(1, "M6.1");
  216. break;
  217. case 2:
  218. Control(1, "M6.0");
  219. break;
  220. case 3:
  221. Control(2, "M6.1");
  222. break;
  223. case 4:
  224. Control(2, "M6.0");
  225. break;
  226. case 5:
  227. Control(3, "M6.1");
  228. break;
  229. case 6:
  230. Control(3, "M6.0");
  231. break;
  232. default:
  233. break;
  234. }
  235. }
  236. }
  237. Thread.Sleep(100);
  238. }), "开始运行设备");
  239. }
  240. private void Control(int num, string add)
  241. {
  242. HKLog.HKLogImport.WriteInfo($"地址:{add} {num} 号出餐口控制");
  243. SiemensDicitonary[num].Write(add, true);
  244. Thread.Sleep(500);
  245. SiemensDicitonary[num].Write(add, false);
  246. }
  247. private void DataInit()
  248. {
  249. Json<CommunicationPar>.Read();
  250. if (Json<CommunicationPar>.Data.CommunicationModels.Count < 3)
  251. {
  252. Json<CommunicationPar>.Data.CommunicationModels.Clear();
  253. Json<CommunicationPar>.Data.CommunicationModels.Add(new CommunicationModel()
  254. {
  255. IpAddress = "192.168.0.1",
  256. DeviceNum = 3
  257. });
  258. Json<CommunicationPar>.Data.CommunicationModels.Add(new CommunicationModel()
  259. {
  260. IpAddress = "192.168.0.2",
  261. DeviceNum = 2
  262. });
  263. Json<CommunicationPar>.Data.CommunicationModels.Add(new CommunicationModel()
  264. {
  265. IpAddress = "192.168.0.3",
  266. DeviceNum = 1
  267. });
  268. Json<CommunicationPar>.Save();
  269. }
  270. }
  271. /// <summary>
  272. /// 获取字节中指定位的值
  273. /// </summary>
  274. /// <param name="data">要获取的整数</param>
  275. /// <param name="offset">偏移量 范围(1-8)</param>
  276. /// <returns></returns>
  277. public bool Get8bitValue(byte data, byte offset)
  278. {
  279. if (offset > 8 || offset < 1) return false;
  280. return (data & 1 << offset - 1) == 0 ? false : true;
  281. }
  282. }
  283. }