RouteCalled.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using DbCommon.BusinessCore.BaseCore;
  2. using DbCommon.Enties.DbModels;
  3. using ProjectManagementSystem.Common.Config;
  4. using ProjectManagementSystem.Common.Extenions;
  5. using ProjectManagementSystem.Common.Function;
  6. using ProjectManagementSystem.Common.Logger;
  7. using ProjectManagementSystem.Common.WebApi;
  8. using System;
  9. using System.Collections.Concurrent;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. namespace ProjectManagementSystem.Common.Core
  16. {
  17. public class RouteCalled
  18. {
  19. private static RouteCalled m_instance;
  20. private ConcurrentQueue<RouteCalledInfo> m_calledRoute = new ConcurrentQueue<RouteCalledInfo>();
  21. private LocationPropertyManager m_locationManager = new LocationPropertyManager();
  22. private CallTaskManager callTaskManager = new CallTaskManager();
  23. private ConcurrentDictionary<int, DateTime> routeCallTimeDict = new ConcurrentDictionary<int, DateTime>();
  24. private int coldTime = 60;
  25. private int[] lesRoutes = AppSetting.TryGetValue<string>("LesCallBoxRoutes").ToValueArray<int>();
  26. public static RouteCalled Instance
  27. {
  28. get
  29. {
  30. if (m_instance == null)
  31. {
  32. m_instance = new RouteCalled();
  33. }
  34. return m_instance;
  35. }
  36. }
  37. private RouteCalled()
  38. {
  39. Task.Factory.StartNew(UserThread);
  40. }
  41. public void Call(int route, bool called, bool cancelCall, string taskType = "")
  42. {
  43. m_calledRoute.Enqueue(new RouteCalledInfo() { Route = route, Called = called, CancelCalled = cancelCall, TaskType = taskType });
  44. }
  45. public void RouteProc()
  46. {
  47. while (m_calledRoute.TryDequeue(out RouteCalledInfo infoBox))
  48. {
  49. CLog.Instance.SystemLog.WriteInfo($"线路{infoBox.Route} 叫料:{infoBox.Called} 叫料取消:{infoBox.CancelCalled}");
  50. int route = infoBox.Route;
  51. if (!lesRoutes.Contains(route))
  52. {
  53. continue;
  54. }
  55. routeCallTimeDict.TryGetValue(route, out var clickTime);
  56. if (clickTime != null)
  57. {
  58. var totalSec = (DateTime.Now - clickTime).TotalSeconds;
  59. if (totalSec <= coldTime)
  60. {
  61. CLog.Instance.SystemLog.WriteInfo($"线路{infoBox.Route} 未到冷却时间{coldTime}s忽略处理 距离上次上报LES成功{totalSec:F1}s");
  62. continue;
  63. }
  64. }
  65. var routeConfig = ExcelConfig.Instance.RouteConfigList.Find(d => d.LesRoutes.Contains(route));
  66. if (routeConfig != null
  67. && !string.IsNullOrEmpty(routeConfig.LocationCode))
  68. {
  69. if (infoBox.Called)
  70. {
  71. int idx = routeConfig.LesRoutes.ToList().IndexOf(route);
  72. string podTyp = routeConfig.LesPodTypes.Length > idx ? routeConfig.LesPodTypes[idx] : null;
  73. bool result = false;
  74. if (string.IsNullOrEmpty(podTyp))
  75. {
  76. result = CustomerApi.AgvAndonRequestApi(new Models.RequestAgvAndonRequestApi() { buttonId = route.ToString() }, out string content);
  77. }
  78. else
  79. {
  80. var data = new Models.RequestAgvEmptyBoxReqApi()
  81. {
  82. location = routeConfig.LocationCode,
  83. podTyp = podTyp
  84. };
  85. result = CustomerApi.AgvEmptyBoxReqApi(data, out string content);
  86. }
  87. //上报成功灭灯
  88. if (result)
  89. {
  90. for (int j = 0; j < 5; j++)
  91. {
  92. DeviceControl.Instance.CallBoxLightOff(route);
  93. Thread.Sleep(200);
  94. }
  95. routeCallTimeDict.AddOrUpdate(route, DateTime.Now, (k, v) => DateTime.Now);
  96. }
  97. else
  98. {
  99. CLog.Instance.SystemLog.WriteInfo($"线路{infoBox.Route} 上报LES不成功");
  100. }
  101. ////上传不成功,报警灯提示
  102. //int lightId = 10;
  103. //int lightType = result? 1 : 0;
  104. //DeviceControl.Instance.SetAlarmLight(lightId, lightType);
  105. }
  106. }
  107. else
  108. {
  109. //没有配置路线
  110. if (infoBox.Called)
  111. {
  112. var result = CustomerApi.AgvAndonRequestApi(new Models.RequestAgvAndonRequestApi() { buttonId = route.ToString() }, out string content);
  113. if (result)
  114. {
  115. for (int j = 0; j < 5; j++)
  116. {
  117. DeviceControl.Instance.CallBoxLightOff(route);
  118. Thread.Sleep(200);
  119. }
  120. routeCallTimeDict.AddOrUpdate(route, DateTime.Now, (k, v) => DateTime.Now);
  121. }
  122. else
  123. {
  124. CLog.Instance.SystemLog.WriteInfo($"线路{infoBox.Route} 按灯上报LES不成功");
  125. }
  126. ////上传不成功,报警灯提示
  127. //int lightId = 10;
  128. //int lightType = result ? 1 : 0;
  129. //DeviceControl.Instance.SetAlarmLight(lightId, lightType);
  130. }
  131. }
  132. }
  133. }
  134. private void UserThread()
  135. {
  136. CLog.Instance.SystemLog.WriteDebug($"RouteCalled已启动");
  137. byte conter = 0;
  138. while (true)
  139. {
  140. try
  141. {
  142. RouteProc();
  143. }
  144. catch (Exception ex)
  145. {
  146. CLog.Instance.SystemLog.WriteException("RouteCalled", ex);
  147. Thread.Sleep(5000);
  148. }
  149. conter++;
  150. Thread.Sleep(100);
  151. }
  152. }
  153. }
  154. public class RouteCalledInfo
  155. {
  156. public int Route { get; set; }
  157. public bool Called { get; set; }
  158. public bool CancelCalled { get; set; }
  159. public string TaskType { get; set; }
  160. }
  161. }