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

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