Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

196 righe
8.0 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. Debug.WriteLine($"{item.DeviceNum}:连接成功");
  70. ThreadManage.GetInstance().StartLong(new Action(() =>
  71. {
  72. var res = SiemensDicitonary[item.DeviceNum].Read<byte>("MB7");
  73. DataModels[item.DeviceNum].LeftWindowData.IsSwipe = Get8bitValue(res, 1);
  74. DataModels[item.DeviceNum].LeftWindowData.Complete = Get8bitValue(res, 2);
  75. DataModels[item.DeviceNum].RightWindowData.IsSwipe = Get8bitValue(res, 3);
  76. DataModels[item.DeviceNum].RightWindowData.Complete = Get8bitValue(res, 4);
  77. //DataModels[item.DeviceNum].LeftWindowData.IsSwipe = SiemensDicitonary[item.DeviceNum].Read<bool>("M7.0");
  78. //DataModels[item.DeviceNum].LeftWindowData.Complete = SiemensDicitonary[item.DeviceNum].Read<bool>("M7.1");
  79. //DataModels[item.DeviceNum].RightWindowData.IsSwipe = SiemensDicitonary[item.DeviceNum].Read<bool>("M7.2");
  80. //DataModels[item.DeviceNum].RightWindowData.Complete = SiemensDicitonary[item.DeviceNum].Read<bool>("M7.3");
  81. Thread.Sleep(100);
  82. }), $"{item.DeviceNum} 号设备监听");
  83. });
  84. SiemensDicitonary[item.DeviceNum].Connect(CpuType.S7200Smart, item.IpAddress);
  85. }), $"{item.DeviceNum} 号设备连接初始化");
  86. });
  87. StartRun();
  88. }
  89. private void StartRun()
  90. {
  91. ThreadManage.GetInstance().StartLong(new Action(() =>
  92. {
  93. while (CarQueue.Count > 0)
  94. {
  95. if (CarQueue.TryDequeue(out int carNum))
  96. {
  97. switch (carNum)
  98. {
  99. case 1:
  100. //SiemensDicitonary[1].Write("M6.0", true);
  101. Control(1, "M6.1");
  102. break;
  103. case 2:
  104. //SiemensDicitonary[1].Write("M6.1", true);
  105. Control(1, "M6.0");
  106. break;
  107. case 3:
  108. //SiemensDicitonary[2].Write("M6.0", true);
  109. Control(2, "M6.1");
  110. break;
  111. case 4:
  112. //SiemensDicitonary[2].Write("M6.1", true);
  113. Control(2, "M6.0");
  114. break;
  115. case 5:
  116. //SiemensDicitonary[3].Write("M6.0", true);
  117. Control(3, "M6.1");
  118. break;
  119. case 6:
  120. //SiemensDicitonary[3].Write("M6.1", true);
  121. Control(3, "M6.0");
  122. break;
  123. default:
  124. break;
  125. }
  126. }
  127. }
  128. Thread.Sleep(100);
  129. }), "开始运行设备");
  130. }
  131. private void Control(int num, string add)
  132. {
  133. Debug.WriteLine($"{num} 号出餐口控制");
  134. SiemensDicitonary[num].Write(add, true);
  135. Thread.Sleep(1000);
  136. SiemensDicitonary[num].Write(add, false);
  137. }
  138. private void DataInit()
  139. {
  140. Json<CommunicationPar>.Read();
  141. if (Json<CommunicationPar>.Data.CommunicationModels.Count < 3)
  142. {
  143. Json<CommunicationPar>.Data.CommunicationModels.Clear();
  144. Json<CommunicationPar>.Data.CommunicationModels.Add(new CommunicationModel()
  145. {
  146. IpAddress = "192.168.0.1",
  147. DeviceNum = 3
  148. });
  149. Json<CommunicationPar>.Data.CommunicationModels.Add(new CommunicationModel()
  150. {
  151. IpAddress = "192.168.0.2",
  152. DeviceNum = 2
  153. });
  154. Json<CommunicationPar>.Data.CommunicationModels.Add(new CommunicationModel()
  155. {
  156. IpAddress = "192.168.0.3",
  157. DeviceNum = 1
  158. });
  159. Json<CommunicationPar>.Save();
  160. }
  161. }
  162. /// <summary>
  163. /// 获取字节中指定位的值
  164. /// </summary>
  165. /// <param name="data">要获取的整数</param>
  166. /// <param name="offset">偏移量 范围(1-8)</param>
  167. /// <returns></returns>
  168. public bool Get8bitValue(byte data, byte offset)
  169. {
  170. if (offset > 8 || offset < 1) return false;
  171. return (data & 1 << offset - 1) == 0 ? false : true;
  172. }
  173. }
  174. }