Worker.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using DBHelper_SqlSugar;
  2. using Logs;
  3. using Microsoft.Extensions.Hosting;
  4. using Microsoft.Extensions.Logging;
  5. using Newtonsoft.Json;
  6. using System;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using WCS.Core;
  14. using WCS.Entity;
  15. namespace WCS.Service
  16. {
  17. public class Worker : BackgroundService
  18. {
  19. private readonly ILogger<Worker> _logger;
  20. public Worker(ILogger<Worker> logger)
  21. {
  22. _logger = logger;
  23. }
  24. protected override async Task ExecuteAsync(CancellationToken stoppingToken)
  25. {
  26. if (stoppingToken.IsCancellationRequested)
  27. return;
  28. #region 启用日志
  29. var logConfig = JsonConvert.DeserializeObject<LogConfig>(await File.ReadAllTextAsync("config.json", Encoding.Default, stoppingToken));
  30. Logs.LogHelper.SetConfigInfo(logConfig);
  31. #endregion 启用日志
  32. _logger.LogInformation("WCS开始启动");
  33. //InfoLog.INFO_INIT("WCS开始启动");
  34. Configs.DebugRedisUrl = "127.0.0.1";
  35. Configs.ProtocolProxyBaseType = typeof(ProtocolProxy);
  36. Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
  37. Configs.StringEncoding = Encoding.UTF8;
  38. Db.SetDefaultDbContextType("WCSDB");
  39. //日志发布事件
  40. Configs.PublishEvent += () =>
  41. {
  42. WMS.UploadDevInfo();
  43. ProtocolProxy.Do();
  44. };
  45. //异常上抛
  46. Configs.UploadException = (d, s) =>
  47. {
  48. if (s == "接口调用中")
  49. return;
  50. if (ProtocolProxy.AllDatas.ContainsKey(d))
  51. {
  52. ProtocolProxy.AllDatas[d].Info = s;
  53. ProtocolProxy.AllDatas[d].Frame = LogicHandler.Frame;
  54. }
  55. WMS.TaskException(d, s);
  56. };
  57. //LogicHandler.DbLog = Helpers.LogHelper.AddWCS_EXCEPTION;
  58. PLCAccessor.Creater = new PLCAccessors.PLCAccessorsCreater();
  59. try
  60. {
  61. Db.Do(db =>
  62. {
  63. var items = db.Default.Queryable<WCS_DEVICEPROTOCOL>()
  64. .Includes(v => v.DEVICE.ROUTES)
  65. .Includes(v => v.DEVICE.PATHS)
  66. .Includes(v => v.DB.PLC).ToArray();
  67. var objects = items.Select(v => v.Data()).ToArray();
  68. LogicHandler.AllObjects.AddRange(items);
  69. var devices = db.Default.Queryable<WCS_DEVICE>().Includes(v => v.ROUTES).Includes(v => v.PATHS).Includes(v => v.DEVICEGROUP).ToArray();
  70. LogicHandler.AllObjects.AddRange(devices);
  71. });
  72. #region 设备扩展数据配置
  73. Device.AddFlag(DF.一楼RGV放货, "G1035", "G1044", "G1053", "G1062");
  74. Device.AddFlag(DF.SRM, "SRM1", "SRM2", "SRM3", "SRM4", "SRM5", "SRM6", "SRM7", "SRM8");
  75. Device.AddFlag(DF.月台, "G1469", "G1561", "G1538", "G1574", "G1509");
  76. Device.AddFlag(DF.SRM二级品取货, "1040", "1041", "1042", "1043", "1049", "1050", "1051", "1052");
  77. Device.AddFlag(DF.SRM二级品取货, "1058", "1059", "1060", "1061", "1067", "1068");
  78. Device.AddFlag(DF.SRMBOPP取货, "1195", "1194", "1193", "1192", "1204", "1203", "1202", "1201");
  79. Device.AddFlag(DF.SRMBOPP取货, "1213", "1212", "1210", "1211", "1220", "1219", "1230", "1228");
  80. Device.AddFlag(DF.SRM月台放货, "1473", "1476", "1474", "1475", "1491", "1492", "1493", "1494");
  81. Device.AddFlag(DF.SRM月台放货, "1520", "1521", "1522", "1523", "1545", "1546", "1451", "1453");
  82. Device.AddFlag(DF.SRM涂布取货, "1431", "1432", "1422", "1423", "1424", "1425", "1415", "1416");
  83. Device.AddFlag(DF.SRM涂布取货, "1605", "1606", "1406", "1407", "1408", "1409");
  84. Device.AddFlag(DF.SRM涂布放货, "1283", "1284", "1290", "1291", "1292", "1293", "1299", "1300");
  85. Device.AddFlag(DF.SRM涂布放货, "1301", "1302", "1308", "1309", "1310", "1311");
  86. Device.AddFlag(DF.涂布RGV, "RGV9", "RGV10", "RGV11", "RGV12", "RGV13", "RGV14");
  87. Device.AddFlag(DF.涂布RGV取货设备组, "G2", "G3", "G5", "G7", "G9", "G11");
  88. Device.AddFlag(DF.涂布RGV放货设备组, "G1", "G4", "G6", "G8", "G10");
  89. Device.AddFlag(DF.涂布出库RGV取货站台, "1285", "1286", "1294", "1295", "1303", "1304", "1312", "1313");
  90. Device.AddFlag(DF.涂布入库RGV取货站台, "1391", "1392", "1399", "1400");
  91. Device.AddFlag(DF.涂布RGV取货站台, "1285", "1286", "1294", "1295", "1303", "1304", "1312", "1313");
  92. Device.AddFlag(DF.涂布RGV取货站台, "1391", "1392", "1399", "1400");
  93. Device.AddFlag(DF.BOPPRGV, "RGV1", "RGV2", "RGV3", "RGV4", "RGV5", "RGV6", "RGV7");
  94. Device.AddFlag(DF.BOPPRGV取货设备组, "G19", "G23");
  95. Device.AddFlag(DF.BOPPRGV放货设备组, "G12", "G13", "G14", "G15", "G16");
  96. #endregion 设备扩展数据配置
  97. #region 启用所有的逻辑处理器
  98. var managerTypes = Assembly.GetExecutingAssembly().GetTypes().Where(v => v.IsSubclassOf(typeof(LogicHandler)) && !v.IsGenericType && !v.IsAbstract).ToArray();
  99. foreach (var type in managerTypes)
  100. {
  101. var m = Activator.CreateInstance(type);
  102. LogicHandler.AddManager(m as LogicHandler);
  103. }
  104. LogicHandler.StartAll();
  105. #endregion 启用所有的逻辑处理器
  106. _logger.LogInformation("WCS启动成功");
  107. //InfoLog.INFO_INIT("WCS启动成功");
  108. }
  109. catch (Exception ex)
  110. {
  111. _logger.LogError("WCS启动失败{0}", ex.Message);
  112. //InfoLog.INFO_INIT($"WCS启动失败{ex.Message}");
  113. }
  114. }
  115. public override Task StopAsync(CancellationToken cancellationToken)
  116. {
  117. _logger.LogError("WCS关闭");
  118. //InfoLog.INFO_INIT("WCS关闭");
  119. LogicHandler.StopAll();
  120. return base.StopAsync(cancellationToken);
  121. }
  122. }
  123. }