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.

Main.cs 13 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. }), $"{item.DeviceNum} 号设备监听", true);
  186. //ThreadManage.GetInstance().StartLong(new Action(() =>
  187. //{
  188. // for (int i = 0; i < 6; i++)
  189. // {
  190. // if (RTrig.GetInstance($"窗口{i + 1}").Start(GetIsComplete(i + 1)))
  191. // {
  192. // CompleteNotify[i]?.Invoke();
  193. // }
  194. // }
  195. // Thread.Sleep(100);
  196. //}), "完成通知");
  197. });
  198. SiemensDicitonary[item.DeviceNum].Connect(CpuType.S7200Smart, item.IpAddress);
  199. }), $"{item.DeviceNum} 号设备连接初始化");
  200. });
  201. StartRun();
  202. }
  203. private void StartRun()
  204. {
  205. ThreadManage.GetInstance().StartLong(new Action(() =>
  206. {
  207. while (CarQueue.Count > 0)
  208. {
  209. if (CarQueue.TryDequeue(out int carNum))
  210. {
  211. switch (carNum)
  212. {
  213. case 1:
  214. Control(1, "M6.1");
  215. break;
  216. case 2:
  217. Control(1, "M6.0");
  218. break;
  219. case 3:
  220. Control(2, "M6.1");
  221. break;
  222. case 4:
  223. Control(2, "M6.0");
  224. break;
  225. case 5:
  226. Control(3, "M6.1");
  227. break;
  228. case 6:
  229. Control(3, "M6.0");
  230. break;
  231. default:
  232. break;
  233. }
  234. }
  235. }
  236. Thread.Sleep(100);
  237. }), "开始运行设备");
  238. }
  239. private void Control(int num, string add)
  240. {
  241. HKLog.HKLogImport.WriteInfo($"地址:{add} {num} 号出餐口控制");
  242. SiemensDicitonary[num].Write(add, true);
  243. Thread.Sleep(500);
  244. SiemensDicitonary[num].Write(add, false);
  245. }
  246. private void DataInit()
  247. {
  248. Json<CommunicationPar>.Read();
  249. if (Json<CommunicationPar>.Data.CommunicationModels.Count < 3)
  250. {
  251. Json<CommunicationPar>.Data.CommunicationModels.Clear();
  252. Json<CommunicationPar>.Data.CommunicationModels.Add(new CommunicationModel()
  253. {
  254. IpAddress = "192.168.0.1",
  255. DeviceNum = 3
  256. });
  257. Json<CommunicationPar>.Data.CommunicationModels.Add(new CommunicationModel()
  258. {
  259. IpAddress = "192.168.0.2",
  260. DeviceNum = 2
  261. });
  262. Json<CommunicationPar>.Data.CommunicationModels.Add(new CommunicationModel()
  263. {
  264. IpAddress = "192.168.0.3",
  265. DeviceNum = 1
  266. });
  267. Json<CommunicationPar>.Save();
  268. }
  269. }
  270. /// <summary>
  271. /// 获取字节中指定位的值
  272. /// </summary>
  273. /// <param name="data">要获取的整数</param>
  274. /// <param name="offset">偏移量 范围(1-8)</param>
  275. /// <returns></returns>
  276. public bool Get8bitValue(byte data, byte offset)
  277. {
  278. if (offset > 8 || offset < 1) return false;
  279. return (data & 1 << offset - 1) == 0 ? false : true;
  280. }
  281. }
  282. }