RouteCalled.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using DbCommon.BusinessCore.BaseCore;
  2. using DbCommon.Enties.DbModels;
  3. using ProjectManagementSystem.Common.Config;
  4. using ProjectManagementSystem.Common.Logger;
  5. using ProjectManagementSystem.Device.CommandCallback;
  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 RouteCalled
  16. {
  17. private static RouteCalled m_instance;
  18. private ConcurrentQueue<InfoBox> m_calledRoute = new ConcurrentQueue<InfoBox>();
  19. private LocationPropertyManager m_locationManager = new LocationPropertyManager();
  20. public static RouteCalled Instance
  21. {
  22. get
  23. {
  24. if (m_instance == null)
  25. {
  26. m_instance = new RouteCalled();
  27. }
  28. return m_instance;
  29. }
  30. }
  31. private RouteCalled()
  32. {
  33. Task.Factory.StartNew(UserThread, TaskCreationOptions.LongRunning);
  34. }
  35. public void Call(InfoBox infoBox)
  36. {
  37. m_calledRoute.Enqueue(infoBox);
  38. }
  39. public void RouteProc()
  40. {
  41. while (m_calledRoute.TryDequeue(out InfoBox infoBox))
  42. {
  43. CLog.Instance.SystemLog.WriteInfo($"线路{infoBox.Route} 叫料{infoBox.BoxCalled} 叫料取消{infoBox.BoxCancelCall}");
  44. int route = infoBox.Route;
  45. var routeConfig = ExcelConfig.Instance.RouteConfigList.FirstOrDefault(d => d.Route > 0 && d.Route == route);
  46. if (routeConfig != null
  47. && !string.IsNullOrEmpty(routeConfig.LocationCode))
  48. {
  49. string location = string.IsNullOrEmpty(routeConfig.Remark1) ? routeConfig.LocationCode : routeConfig.Remark1;
  50. LocationStatus status = infoBox.BoxCalled ? (LocationStatus)routeConfig.CallLineStauts : (LocationStatus)routeConfig.CancelLineStauts;
  51. bool result = true;
  52. if (location.Contains(","))
  53. {
  54. string[] loacations = location.Split(new char[] { ',' });
  55. for (int i = 0; i < loacations.Length; i++)
  56. {
  57. var lData = m_locationManager.GetById(loacations[i]);
  58. lData.Status = status;
  59. result &= m_locationManager.UpdateWithTaskId(lData);
  60. }
  61. }
  62. else
  63. {
  64. var lData = m_locationManager.GetById(location);
  65. if (lData != null
  66. && (lData.Status != status))
  67. {
  68. lData.Status = status;
  69. result = m_locationManager.UpdateWithTaskId(lData);
  70. if (result)
  71. {
  72. CLog.Instance.SystemLog.WriteInfo($"更新库位状态成功:{location}:{status}");
  73. }
  74. }
  75. }
  76. }
  77. }
  78. }
  79. private void UserThread()
  80. {
  81. CLog.Instance.SystemLog.WriteDebug($"RouteCalled已启动");
  82. byte conter = 0;
  83. while (true)
  84. {
  85. try
  86. {
  87. RouteProc();
  88. }
  89. catch (Exception ex)
  90. {
  91. CLog.Instance.SystemLog.WriteException("RouteCalled", ex);
  92. Thread.Sleep(5000);
  93. }
  94. conter++;
  95. Thread.Sleep(100);
  96. }
  97. }
  98. }
  99. }