Worker.cs 8.5 KB

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