您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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