Worker.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using Newtonsoft.Json;
  2. using SqlSugar;
  3. using System.Text;
  4. using WCS.Core;
  5. using WCS.Core.DataTrans;
  6. using WCS.Core.DbHelper;
  7. using WCS.Core.Helpers;
  8. using WCS.Core.Log;
  9. using WCS.Core.Virtual_PLC;
  10. using WCS.Entity;
  11. using WCS.Entity.Protocol;
  12. using WCS.Entity.Protocol.BCR;
  13. using WCS.Entity.Protocol.Station;
  14. using WCS.WebApi.WMS;
  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. public static readonly string WcsDlog = "WCSDlog";
  25. protected override async System.Threading.Tasks.Task ExecuteAsync(CancellationToken stoppingToken)
  26. {
  27. if (stoppingToken.IsCancellationRequested)
  28. return;
  29. #region 启用日志
  30. var logConfig = JsonConvert.DeserializeObject<LogConfig>(await File.ReadAllTextAsync("config.json", Encoding.Default, stoppingToken));
  31. LogHelper.SetConfigInfo(logConfig!);
  32. #endregion 启用日志
  33. InfoLog.INFO_INIT("1111");
  34. _logger.LogInformation("WCS开始启动");
  35. //InfoLog.INFO_INIT("WCS开始启动");
  36. //Configs.DebugRedisUrl = "127.0.0.1";
  37. Configs.ProtocolProxyBaseType = typeof(ProtocolProxy);
  38. Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
  39. Configs.StringEncoding = Encoding.UTF8;
  40. #region 需要改成循环遍历,以实现根据配置项完成多个数据库链接创建
  41. Db.CreateContext(new ConnectionConfig()
  42. {
  43. ConnectionString = AppSettings.Config.GetConnectionString("WCSDB"),
  44. DbType = DbType.SqlServer
  45. }, "WCSDB");
  46. Db.SetDefaultDbContextType("WCSDB");
  47. Db.Do(db =>
  48. {
  49. //TODO:DbMaintenance.CreateDatabase()并没起到作用,如果没有对应的数据库的话任然需要手动新建一个
  50. db.Default.DbMaintenance.CreateDatabase();
  51. db.Default.CodeFirst.InitTables(typeof(WCS_CMD));
  52. db.Default.CodeFirst.InitTables(typeof(WCS_PLC));
  53. db.Default.CodeFirst.InitTables(typeof(WCS_DATABLOCK));
  54. db.Default.CodeFirst.InitTables(typeof(WCS_DEVICE));
  55. db.Default.CodeFirst.InitTables(typeof(WCS_PATH));
  56. db.Default.CodeFirst.InitTables(typeof(WCS_PATHPOINT));
  57. db.Default.CodeFirst.InitTables(typeof(WCS_ROUTE));
  58. db.Default.CodeFirst.InitTables(typeof(WCS_TASK));
  59. db.Default.CodeFirst.InitTables(typeof(WCS_TASK_OLD));
  60. db.Default.CodeFirst.InitTables(typeof(WCS_EXCEPTION));
  61. db.Default.CodeFirst.InitTables(typeof(WCS_SystemConfig));
  62. db.Default.CodeFirst.InitTables(typeof(WCS_AGVTask));
  63. db.Default.CodeFirst.InitTables(typeof(WCS_DEVICEPROTOCOL));
  64. db.Default.CodeFirst.InitTables(typeof(WCS_GROUPMEMBER));
  65. db.Default.CodeFirst.InitTables(typeof(WCS_MAPPINGENTRY));
  66. db.Default.CodeFirst.InitTables(typeof(WCS_USERS));
  67. db.Default.CodeFirst.InitTables(typeof(WCS_StatusLog));
  68. db.Default.CodeFirst.InitTables(typeof(WCS.Entity.PlcRawData));
  69. //db.Default.CodeFirst.InitTables(typeof(WCS_BCR80));
  70. //db.Default.CodeFirst.InitTables(typeof(WCS_RGV520));
  71. //db.Default.CodeFirst.InitTables(typeof(WCS_RGV521));
  72. //db.Default.CodeFirst.InitTables(typeof(WCS_RGV523));
  73. //db.Default.CodeFirst.InitTables(typeof(WCS_SRM520));
  74. //db.Default.CodeFirst.InitTables(typeof(WCS_SRM521));
  75. //db.Default.CodeFirst.InitTables(typeof(WCS_SRM537));
  76. //db.Default.CodeFirst.InitTables(typeof(WCS_Station520));
  77. //db.Default.CodeFirst.InitTables(typeof(WCS_Station521));
  78. //db.Default.CodeFirst.InitTables(typeof(WCS_Station523));
  79. });
  80. Db.CreateContext(new ConnectionConfig()
  81. {
  82. ConnectionString = AppSettings.Config.GetConnectionString(WcsDlog),
  83. DbType = DbType.PostgreSQL
  84. }, WcsDlog);
  85. Db.Do(db =>
  86. {
  87. //TODO:DbMaintenance.CreateDatabase()并没起到作用,如果没有对应的数据库的话任然需要手动新建一个
  88. db.Context(WcsDlog).DbMaintenance.CreateDatabase();
  89. db.Context(WcsDlog).CodeFirst.InitTables(typeof(WCS_BCR80));
  90. db.Context(WcsDlog).CodeFirst.InitTables(typeof(WCS_RGV520));
  91. db.Context(WcsDlog).CodeFirst.InitTables(typeof(WCS_RGV521));
  92. db.Context(WcsDlog).CodeFirst.InitTables(typeof(WCS_RGV523));
  93. db.Context(WcsDlog).CodeFirst.InitTables(typeof(WCS_SRM520));
  94. db.Context(WcsDlog).CodeFirst.InitTables(typeof(WCS_SRM521));
  95. db.Context(WcsDlog).CodeFirst.InitTables(typeof(WCS_SRM537));
  96. db.Context(WcsDlog).CodeFirst.InitTables(typeof(WCS_Station520));
  97. db.Context(WcsDlog).CodeFirst.InitTables(typeof(WCS_Station521));
  98. db.Context(WcsDlog).CodeFirst.InitTables(typeof(WCS_Station523));
  99. db.Context(WcsDlog).CodeFirst.InitTables(typeof(WCS.Entity.PlcRawData));
  100. });
  101. #endregion 需要改成循环遍历,以实现根据配置项完成多个数据库链接创建
  102. //从现有结构解析出需要的结构
  103. List<PLCData> list = new List<PLCData>();
  104. Db.Do(db =>
  105. {
  106. var dataBlocks = db.Default.Queryable<WCS_DATABLOCK>().Includes(v => v.PLC).ToList();
  107. foreach (var dataBlock in dataBlocks)
  108. {
  109. list.Add(new PLCData()
  110. {
  111. IP = dataBlock.PLC.IP,
  112. DB = dataBlock.NO,
  113. Length = dataBlock.LENGTH,
  114. DataLength = dataBlock.DATALENGTH,
  115. });
  116. }
  117. });
  118. PlcData.Init("127.0.0.1,database=0").InitPlcData(list);
  119. //日志发布事件s
  120. Configs.PublishEvent += () =>
  121. {
  122. WMS.UploadDevInfo();
  123. ProtocolProxy.Do();
  124. };
  125. //异常上抛
  126. Configs.UploadException = (d, s) =>
  127. {
  128. if (s == "接口调用中") return;
  129. if (ProtocolProxy.AllDatas.ContainsKey(d))
  130. {
  131. ProtocolProxy.AllDatas[d].Info = s;
  132. ProtocolProxy.AllDatas[d].Frame = LogicHandler.Frame;
  133. }
  134. WMS.TaskException(d, s);
  135. };
  136. //LogicHandler.DbLog = Helpers.LogHelper.AddWCS_EXCEPTION;
  137. PlcAccessor.Creater = new PLCAccessors.PlcAccessorsCreater();
  138. try
  139. {
  140. Db.Do(db =>
  141. {
  142. var items = db.Default.Queryable<WCS_DEVICEPROTOCOL>()
  143. .Includes(d => d.DEVICE, r => r.ROUTES)
  144. .Includes(d => d.DEVICE, p => p.PATHS)
  145. .Includes(d => d.DB, p => p.PLC).ToArray();
  146. items.Select(v => v.Data()).ToArray();
  147. LogicHandler.AllObjects.AddRange(items);
  148. var devices = db.Default.Queryable<WCS_DEVICE>()
  149. .Includes(v => v.ROUTES)
  150. .Includes(v => v.PATHS)
  151. .Includes(v => v.DEVICEGROUP, g => g.GROUP)
  152. .AsNavQueryable()
  153. .Includes(v => v.DEVICEGROUP, g => g.MEMBER, d => d.DEVICEPROTOCOLS, p => p.DB, c => c.PLC)
  154. .Includes(v => v.DEVICEGROUP, g => g.MEMBER, d => d.DEVICEPROTOCOLS, p => p.DEVICE)
  155. .Includes(v => v.DEVICEPROTOCOLS, p => p.DB, c => c.PLC)
  156. .Includes(v => v.DEVICEPROTOCOLS, p => p.DEVICE)
  157. .ToList();
  158. LogicHandler.AllObjects.AddRange(devices);
  159. });
  160. #region 设备扩展数据配置
  161. //WCS_DEVICEExtension.AddFlag(DF.一楼RGV放货, "G1035", "G1044", "G1053", "G1062");
  162. //WCS_DEVICEExtension.AddFlag(DF.SRM, "SRM1", "SRM2", "SRM3", "SRM4", "SRM5", "SRM6", "SRM7", "SRM8");
  163. //WCS_DEVICEExtension.AddFlag(DF.月台, "G1469", "G1561", "G1538", "G1574", "G1509");
  164. //WCS_DEVICEExtension.AddFlag(DF.SRM二级品取货, "1040", "1041", "1042", "1043", "1049", "1050", "1051", "1052");
  165. //WCS_DEVICEExtension.AddFlag(DF.SRM二级品取货, "1058", "1059", "1060", "1061", "1067", "1068");
  166. //WCS_DEVICEExtension.AddFlag(DF.SRMBOPP取货, "1195", "1194", "1193", "1192", "1204", "1203", "1202", "1201");
  167. //WCS_DEVICEExtension.AddFlag(DF.SRMBOPP取货, "1213", "1212", "1210", "1211", "1220", "1219", "1230", "1228");
  168. //WCS_DEVICEExtension.AddFlag(DF.SRM月台放货, "1473", "1476", "1474", "1475", "1491", "1492", "1493", "1494");
  169. //WCS_DEVICEExtension.AddFlag(DF.SRM月台放货, "1520", "1521", "1522", "1523", "1545", "1546", "1451", "1453");
  170. //WCS_DEVICEExtension.AddFlag(DF.SRM涂布取货, "1431", "1432", "1422", "1423", "1424", "1425", "1415", "1416");
  171. //WCS_DEVICEExtension.AddFlag(DF.SRM涂布取货, "1605", "1606", "1406", "1407", "1408", "1409");
  172. //WCS_DEVICEExtension.AddFlag(DF.SRM涂布放货, "1283", "1284", "1290", "1291", "1292", "1293", "1299", "1300");
  173. //WCS_DEVICEExtension.AddFlag(DF.SRM涂布放货, "1301", "1302", "1308", "1309", "1310", "1311");
  174. //WCS_DEVICEExtension.AddFlag(DF.涂布RGV, "RGV9", "RGV10", "RGV11", "RGV12", "RGV13", "RGV14");
  175. //WCS_DEVICEExtension.AddFlag(DF.涂布RGV取货设备组, "G2", "G3", "G5", "G7", "G9", "G11");
  176. //WCS_DEVICEExtension.AddFlag(DF.涂布RGV放货设备组, "G1", "G4", "G6", "G8", "G10");
  177. //WCS_DEVICEExtension.AddFlag(DF.涂布出库RGV取货站台, "1285", "1286", "1294", "1295", "1303", "1304", "1312", "1313");
  178. //WCS_DEVICEExtension.AddFlag(DF.涂布入库RGV取货站台, "1391", "1392", "1399", "1400");
  179. //WCS_DEVICEExtension.AddFlag(DF.涂布RGV取货站台, "1285", "1286", "1294", "1295", "1303", "1304", "1312", "1313");
  180. //WCS_DEVICEExtension.AddFlag(DF.涂布RGV取货站台, "1391", "1392", "1399", "1400");
  181. //WCS_DEVICEExtension.AddFlag(DF.BOPPRGV, "RGV1", "RGV2", "RGV3", "RGV4", "RGV5", "RGV6", "RGV7");
  182. //WCS_DEVICEExtension.AddFlag(DF.BOPPRGV取货设备组, "G19", "G23");
  183. //WCS_DEVICEExtension.AddFlag(DF.BOPPRGV放货设备组, "G12", "G13", "G14", "G15", "G16");
  184. #endregion 设备扩展数据配置
  185. #region 启用所有的逻辑处理器
  186. var managerTypes = ReflectionHelper.GetTypesByImplementInterface<LogicHandler>().ToArray();
  187. foreach (var type in managerTypes)
  188. {
  189. var m = Activator.CreateInstance(type);
  190. LogicHandler.AddManager(m as LogicHandler);
  191. }
  192. LogicHandler.StartAll();
  193. #endregion 启用所有的逻辑处理器
  194. _logger.LogInformation("WCS启动成功");
  195. //InfoLog.INFO_INIT("WCS启动成功");
  196. }
  197. catch (Exception ex)
  198. {
  199. _logger.LogError("WCS启动失败{0}", ex.Message);
  200. //InfoLog.INFO_INIT($"WCS启动失败{ex.Message}");
  201. }
  202. }
  203. public override System.Threading.Tasks.Task StopAsync(CancellationToken cancellationToken)
  204. {
  205. _logger.LogError("WCS关闭");
  206. //InfoLog.INFO_INIT("WCS关闭");
  207. LogicHandler.StopAll();
  208. return base.StopAsync(cancellationToken);
  209. }
  210. }
  211. }