No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

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