Worker.cs 14 KB

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