Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

174 строки
6.6 KiB

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