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.

204 líneas
8.3 KiB

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