Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

149 рядки
5.3 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. DataModels[item.DeviceNum].LeftWindowData.IsSwipe = SiemensDicitonary[item.DeviceNum].Read<bool>("M0.0");
  66. DataModels[item.DeviceNum].LeftWindowData.Complete = SiemensDicitonary[item.DeviceNum].Read<bool>("M0.0");
  67. DataModels[item.DeviceNum].RightWindowData.IsSwipe = SiemensDicitonary[item.DeviceNum].Read<bool>("M0.0");
  68. DataModels[item.DeviceNum].RightWindowData.Complete = SiemensDicitonary[item.DeviceNum].Read<bool>("M0.0");
  69. Thread.Sleep(100);
  70. }), $"{item.DeviceNum} 号设备监听");
  71. });
  72. }), $"{item.DeviceNum} 号设备连接初始化");
  73. });
  74. StartRun();
  75. }
  76. private void StartRun()
  77. {
  78. ThreadManage.GetInstance().StartLong(new Action(() =>
  79. {
  80. while (CarQueue.Count > 0)
  81. {
  82. if (CarQueue.TryDequeue(out int carNum))
  83. {
  84. switch (carNum)
  85. {
  86. case 1:
  87. case 2:
  88. SiemensDicitonary[1].Write("M0.0", true);
  89. break;
  90. case 3:
  91. case 4:
  92. SiemensDicitonary[2].Write("M0.0", true);
  93. break;
  94. case 5:
  95. case 6:
  96. SiemensDicitonary[3].Write("M0.0", true);
  97. break;
  98. default:
  99. break;
  100. }
  101. }
  102. }
  103. Thread.Sleep(100);
  104. }), "开始运行设备");
  105. }
  106. private void DataInit()
  107. {
  108. Json<CommunicationPar>.Read();
  109. if (Json<CommunicationPar>.Data.CommunicationModels.Count < 3)
  110. {
  111. Json<CommunicationPar>.Data.CommunicationModels.Clear();
  112. Json<CommunicationPar>.Data.CommunicationModels.Add(new CommunicationModel()
  113. {
  114. IpAddress = "192.168.0.1",
  115. DeviceNum = 1
  116. });
  117. Json<CommunicationPar>.Data.CommunicationModels.Add(new CommunicationModel()
  118. {
  119. IpAddress = "192.168.0.2",
  120. DeviceNum = 2
  121. });
  122. Json<CommunicationPar>.Data.CommunicationModels.Add(new CommunicationModel()
  123. {
  124. IpAddress = "192.168.0.3",
  125. DeviceNum = 3
  126. });
  127. Json<CommunicationPar>.Save();
  128. }
  129. }
  130. }
  131. }