Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

227 строки
8.3 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. if (!DataModels.ContainsKey(CarNum)) return false;
  24. switch (CarNum)
  25. {
  26. case 1:
  27. return DataModels[1].LeftWindowData.IsSwipe;
  28. case 2:
  29. return DataModels[1].RightWindowData.IsSwipe;
  30. case 3:
  31. return DataModels[2].LeftWindowData.IsSwipe;
  32. case 4:
  33. return DataModels[2].RightWindowData.IsSwipe;
  34. case 5:
  35. return DataModels[3].LeftWindowData.IsSwipe;
  36. case 6:
  37. return DataModels[3].LeftWindowData.IsSwipe;
  38. default:
  39. break;
  40. }
  41. return false;
  42. }
  43. /// <summary>
  44. /// 获取指定档口是否配餐完成
  45. /// </summary>
  46. /// <param name="CarNum"></param>
  47. /// <returns></returns>
  48. public bool GetIsComplete(int CarNum)
  49. {
  50. if (!DataModels.ContainsKey(CarNum)) return false;
  51. switch (CarNum)
  52. {
  53. case 1:
  54. return DataModels[1].LeftWindowData.Complete;
  55. case 2:
  56. return DataModels[1].RightWindowData.Complete;
  57. case 3:
  58. return DataModels[2].LeftWindowData.Complete;
  59. case 4:
  60. return DataModels[2].RightWindowData.Complete;
  61. case 5:
  62. return DataModels[3].LeftWindowData.Complete;
  63. case 6:
  64. return DataModels[3].LeftWindowData.Complete;
  65. default:
  66. break;
  67. }
  68. return false;
  69. }
  70. /// <summary>
  71. /// 设置指定窗口开始出餐(1--6)
  72. /// </summary>
  73. /// <param name="CarNum"></param>
  74. public void Start(int CarNum)
  75. {
  76. CarQueue.Enqueue(CarNum);
  77. }
  78. /// <summary>
  79. /// 初始化
  80. /// </summary>
  81. public void Init()
  82. {
  83. DataInit();
  84. Json<CommunicationPar>.Data.CommunicationModels.ToList()?.ForEach(item =>
  85. {
  86. if (!DataModels.ContainsKey(item.DeviceNum)) { DataModels.TryAdd(item.DeviceNum, new DataModel()); };
  87. if (!SiemensDicitonary.ContainsKey(item.DeviceNum)) { SiemensDicitonary.TryAdd(item.DeviceNum, new Siemens()); }
  88. ThreadManage.GetInstance().Start(new Action(() =>
  89. {
  90. SiemensDicitonary[item.DeviceNum].ConnectOk = new Action(() =>
  91. {
  92. HKLog.HKLogImport.WriteInfo($"{item.DeviceNum}:连接成功");
  93. ThreadManage.GetInstance().StartLong(new Action(() =>
  94. {
  95. try
  96. {
  97. var res = SiemensDicitonary[item.DeviceNum].Read<byte>("MB7");
  98. DataModels[item.DeviceNum].LeftWindowData.IsSwipe = Get8bitValue(res, 1);
  99. DataModels[item.DeviceNum].LeftWindowData.Complete = Get8bitValue(res, 2);
  100. DataModels[item.DeviceNum].RightWindowData.IsSwipe = Get8bitValue(res, 3);
  101. DataModels[item.DeviceNum].RightWindowData.Complete = Get8bitValue(res, 4);
  102. }
  103. catch (Exception ex)
  104. {
  105. HKLog.HKLogImport.WriteInfo(ex.ToString());
  106. }
  107. Thread.Sleep(100);
  108. }), $"{item.DeviceNum} 号设备监听", true);
  109. ThreadManage.GetInstance().StartLong(new Action(() =>
  110. {
  111. for (int i = 0; i < 6; i++)
  112. {
  113. if (RTrig.GetInstance($"窗口{i + 1}").Start(GetIsComplete(i + 1)))
  114. {
  115. CompleteNotify[i]?.Invoke();
  116. }
  117. }
  118. Thread.Sleep(100);
  119. }), "完成通知");
  120. });
  121. SiemensDicitonary[item.DeviceNum].Connect(CpuType.S7200Smart, item.IpAddress);
  122. }), $"{item.DeviceNum} 号设备连接初始化");
  123. });
  124. StartRun();
  125. }
  126. private void StartRun()
  127. {
  128. ThreadManage.GetInstance().StartLong(new Action(() =>
  129. {
  130. while (CarQueue.Count > 0)
  131. {
  132. if (CarQueue.TryDequeue(out int carNum))
  133. {
  134. switch (carNum)
  135. {
  136. case 1:
  137. Control(1, "M6.1");
  138. break;
  139. case 2:
  140. Control(1, "M6.0");
  141. break;
  142. case 3:
  143. Control(2, "M6.1");
  144. break;
  145. case 4:
  146. Control(2, "M6.0");
  147. break;
  148. case 5:
  149. Control(3, "M6.1");
  150. break;
  151. case 6:
  152. Control(3, "M6.0");
  153. break;
  154. default:
  155. break;
  156. }
  157. }
  158. }
  159. Thread.Sleep(100);
  160. }), "开始运行设备");
  161. }
  162. private void Control(int num, string add)
  163. {
  164. HKLog.HKLogImport.WriteInfo($"{num} 号出餐口控制");
  165. SiemensDicitonary[num].Write(add, true);
  166. Thread.Sleep(1000);
  167. SiemensDicitonary[num].Write(add, false);
  168. }
  169. private void DataInit()
  170. {
  171. Json<CommunicationPar>.Read();
  172. if (Json<CommunicationPar>.Data.CommunicationModels.Count < 3)
  173. {
  174. Json<CommunicationPar>.Data.CommunicationModels.Clear();
  175. Json<CommunicationPar>.Data.CommunicationModels.Add(new CommunicationModel()
  176. {
  177. IpAddress = "192.168.0.1",
  178. DeviceNum = 3
  179. });
  180. Json<CommunicationPar>.Data.CommunicationModels.Add(new CommunicationModel()
  181. {
  182. IpAddress = "192.168.0.2",
  183. DeviceNum = 2
  184. });
  185. Json<CommunicationPar>.Data.CommunicationModels.Add(new CommunicationModel()
  186. {
  187. IpAddress = "192.168.0.3",
  188. DeviceNum = 1
  189. });
  190. Json<CommunicationPar>.Save();
  191. }
  192. }
  193. /// <summary>
  194. /// 获取字节中指定位的值
  195. /// </summary>
  196. /// <param name="data">要获取的整数</param>
  197. /// <param name="offset">偏移量 范围(1-8)</param>
  198. /// <returns></returns>
  199. public bool Get8bitValue(byte data, byte offset)
  200. {
  201. if (offset > 8 || offset < 1) return false;
  202. return (data & 1 << offset - 1) == 0 ? false : true;
  203. }
  204. }
  205. }