BoxControl.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using DbCommon.BusinessCore.BaseCore;
  2. using DbCommon.Enties.DbModels;
  3. using ProjectManagementSystem.Device.CasunBox;
  4. using ProjectManagementSystem.Common.Config;
  5. using ProjectManagementSystem.Common.Log;
  6. using System;
  7. using System.Collections.Concurrent;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. namespace ProjectManagementSystem.Common.Core
  14. {
  15. public class BoxControl
  16. {
  17. private IBoxCommunication communication;
  18. private LocationPropertyManager m_locationManager = new LocationPropertyManager();
  19. private ConcurrentQueue<InfoBox> m_infoBoxesQueue = new ConcurrentQueue<InfoBox>();
  20. public IBoxCommunication Communication { get { return communication; } }
  21. public BoxControl(IBoxCommunication boxCommunication)
  22. {
  23. communication = boxCommunication;
  24. communication.OnLogMsg += Communication_OnLoging;
  25. communication.OnInfoBox += Communication_OnInfoBox;
  26. communication.Start();
  27. if(communication.IsStart)
  28. {
  29. Task.Factory.StartNew(UserThread);
  30. }
  31. }
  32. public void BoxLightOff(int route)
  33. {
  34. CommandButtonOff data = new CommandButtonOff();
  35. data.Route = route;
  36. communication.CommandEnqueue(data);
  37. }
  38. public void BoxQuery(int route)
  39. {
  40. CommandQuery data = new CommandQuery();
  41. data.Route = route;
  42. communication.CommandEnqueue(data);
  43. }
  44. public void BoxSetLight(int lightNumber, int lightType)
  45. {
  46. CommandSetLight data = new CommandSetLight();
  47. data.LightType = (byte)lightType;
  48. data.Route = lightNumber;
  49. communication.CommandEnqueue(data);
  50. }
  51. private void Communication_OnInfoBox(InfoBox infoBox)
  52. {
  53. m_infoBoxesQueue.Enqueue(infoBox);
  54. }
  55. private void Communication_OnLoging(string obj, Exception ex)
  56. {
  57. if (ex == null)
  58. {
  59. CLog.Instance.GetLog("System").WriteDebug(obj);
  60. }
  61. else
  62. {
  63. CLog.Instance.GetLog("System").WriteException("Exception", obj, ex);
  64. }
  65. }
  66. public void RouteProc()
  67. {
  68. while (m_infoBoxesQueue.TryDequeue(out InfoBox infoBox))
  69. {
  70. if(infoBox.EBoxDevice != EnumBoxDevice.CallBox)
  71. {
  72. continue;
  73. }
  74. if(!infoBox.Called
  75. && !infoBox.CancelCall)
  76. {
  77. continue;
  78. }
  79. string infoCall = infoBox.Called ? "叫料" : "叫料取消";
  80. CLog.Instance.GetLog("System").WriteInfo($"线路{infoBox.Route} {infoCall}");
  81. int route = infoBox.Route;
  82. var routeConfig = ExcelConfig.Instance.GetRouteConfig(route);
  83. if (routeConfig != null
  84. && !string.IsNullOrEmpty(routeConfig.LocationCode))
  85. {
  86. string location = routeConfig.LocationCode;
  87. LocationStatus status = infoBox.Called ?
  88. (LocationStatus)routeConfig.CallLineStauts : (LocationStatus)routeConfig.CancelLineStauts;
  89. bool result = true;
  90. if (location.Contains(","))
  91. {
  92. string[] loacations = location.Split(new char[] { ',' });
  93. for (int i = 0; i < loacations.Length; i++)
  94. {
  95. result &= m_locationManager.UpdateStatusNotLocked(loacations[i], status);
  96. }
  97. }
  98. else
  99. {
  100. result = m_locationManager.UpdateStatusNotLocked(location, status);
  101. }
  102. if(result)
  103. {
  104. CLog.Instance.GetLog("System").WriteInfo($"更新库位状态成功:{location}:{status}");
  105. }
  106. }
  107. }
  108. }
  109. private void UserThread()
  110. {
  111. byte conter = 0;
  112. while (true)
  113. {
  114. try
  115. {
  116. //if(conter == 0)
  117. //{
  118. // BoxQuery(0);
  119. //}
  120. RouteProc();
  121. }
  122. catch (Exception ex)
  123. {
  124. CLog.Instance.GetLog("System").WriteException("Exception", ex);
  125. Thread.Sleep(5000);
  126. }
  127. conter++;
  128. Thread.Sleep(100);
  129. }
  130. }
  131. }
  132. }