Worker.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using Newtonsoft.Json;
  2. using ServiceCenter;
  3. using ServiceCenter.Redis;
  4. using ServiceCenter.SqlSugars;
  5. using ServiceCenter.Virtual_PLC;
  6. using SqlSugar;
  7. using System.Text;
  8. using WCS.Core;
  9. using WCS.Entity;
  10. using WCS.Entity.Protocol.BCR;
  11. using WCS.Entity.Protocol.Station;
  12. using WCS.Service.Systems;
  13. using WCS.WorkEngineering;
  14. namespace WCS.Service
  15. {
  16. /// <summary>
  17. /// 工作服务
  18. /// </summary>
  19. public class Worker : BackgroundService
  20. {
  21. /// <summary>
  22. /// 记录器
  23. /// </summary>
  24. private readonly ILogger<Worker> _logger;
  25. /// <summary>
  26. /// 构造函数
  27. /// </summary>
  28. /// <param name="logger">记录器</param>
  29. public Worker(ILogger<Worker> logger)
  30. {
  31. _logger = logger;
  32. }
  33. public static readonly string WcsDlog = "WCSDlog";
  34. public static readonly string Wcsdb = "WCSDB";
  35. /// <summary>
  36. /// 执行
  37. /// </summary>
  38. /// <param name="stoppingToken">停止令牌</param>
  39. /// <returns></returns>
  40. protected override async Task ExecuteAsync(CancellationToken stoppingToken)
  41. {
  42. if (stoppingToken.IsCancellationRequested)
  43. return;
  44. #region 启用日志
  45. //var logConfigText = RedisHub.Default.Check("LogConfigText") ?? throw new Exception("请在Redis中配置log4net相关内容");
  46. //var logConfig = JsonConvert.DeserializeObject<LogConfig>(logConfigText);
  47. //LogHub.SetConfigInfo(logConfig!);
  48. #endregion 启用日志
  49. _logger.LogInformation("WCS开始启动");
  50. Configs.ProtocolProxyBaseType = typeof(ProtocolProxy);
  51. Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
  52. Configs.StringEncoding = Encoding.UTF8;
  53. var warehouseName = RedisHub.Default.Check("WarehouseName") ?? throw new Exception("请在Redis中配置仓库名称");
  54. if (string.IsNullOrEmpty(warehouseName)) throw new Exception("请在Redis中配置仓库名称");
  55. ServiceHub.SetWarehouseName(warehouseName);
  56. #region 初始化数据库连接
  57. var dbConnectionStrings = RedisHub.Default.Check("DbConnectionStrings") ?? throw new Exception("请在Redis中配置数据库连接相关内容");
  58. ServiceHub.DbConnectionStrings = JsonConvert.DeserializeObject<List<DataBaseConnectionString>>(dbConnectionStrings);
  59. if (ServiceHub.DbConnectionStrings != null)
  60. {
  61. if (ServiceHub.DbConnectionStrings.All(v => v.Key != Wcsdb)) throw new Exception("请在DbConnectionStrings中配置WCS基础数据库连接字符串");
  62. if (ServiceHub.DbConnectionStrings.All(v => v.Key == Wcsdb && !v.IsDefault)) throw new Exception("请在DbConnectionStrings中配置WCS基础数据库为默认数据库");
  63. if (ServiceHub.DbConnectionStrings.All(v => v.Key != WcsDlog)) throw new Exception("请在DbConnectionStrings中配置WCS日志数据库连接字符串");
  64. }
  65. //设置连接信息
  66. List<ConnectionConfig> connectionConfigs = new List<ConnectionConfig>();
  67. foreach (var connectionString in ServiceHub.DbConnectionStrings!)
  68. {
  69. connectionConfigs.Add(new ConnectionConfig()
  70. {
  71. ConfigId = connectionString.Key,
  72. ConnectionString = connectionString.ConnectionString,//连接符字串
  73. DbType = connectionString.DbType,//数据库类型
  74. IsAutoCloseConnection = true,//不设成true要手动close
  75. });
  76. };
  77. SqlSugarHelper.SetDb(new SqlSugarScope(connectionConfigs));
  78. //初始化数据库
  79. SqlSugarHelper.Do(db =>
  80. {
  81. foreach (var connectionString in ServiceHub.DbConnectionStrings!)
  82. {
  83. var _db = db.Connect.GetConnectionScope(connectionString.Key);
  84. switch (connectionString.Key)
  85. {
  86. case "WCSDB"://WCS基本数据库
  87. SqlSugarHelper.SetDefault(connectionString.Key);
  88. _db.CodeFirst.InitTables(typeof(WCS_PlcSet));
  89. _db.CodeFirst.InitTables(typeof(WCS_PlcDataBlock));
  90. _db.CodeFirst.InitTables(typeof(WCS_PlcData));
  91. _db.CodeFirst.InitTables(typeof(WCS_DeviceInfo));
  92. _db.CodeFirst.InitTables(typeof(WCS_DeviceGrp));
  93. _db.CodeFirst.InitTables(typeof(WCS_DeviceProt));
  94. _db.CodeFirst.InitTables(typeof(WCS_PathInfo));
  95. _db.CodeFirst.InitTables(typeof(WCS_PathGrp));
  96. _db.CodeFirst.InitTables(typeof(WCS_Route));
  97. _db.CodeFirst.InitTables(typeof(WCS_TaskInfo));
  98. _db.CodeFirst.InitTables(typeof(WCS_TaskDtl));
  99. _db.CodeFirst.InitTables(typeof(WCS_TaskOld));
  100. _db.CodeFirst.InitTables(typeof(WCS_AgvTaskInfo));
  101. break;
  102. case "WCSDlog"://WCS日志数据库
  103. //SqlSugarHelper.SetDlog(connectionString.Key);
  104. //_db.DbMaintenance.CreateDatabase();
  105. //_db.CodeFirst.InitTables(typeof(WCS_BCR80));
  106. ////db.Context(WcsDlog).CodeFirst.InitTables(typeof(WCS_RGV520));
  107. ////db.Context(WcsDlog).CodeFirst.InitTables(typeof(WCS_RGV521));
  108. ////db.Context(WcsDlog).CodeFirst.InitTables(typeof(WCS_RGV523));
  109. ////_db.CodeFirst.InitTables(typeof(WCS_SRM520));
  110. ////_db.CodeFirst.InitTables(typeof(WCS_SRM521));
  111. ////db.Context(WcsDlog).CodeFirst.InitTables(typeof(WCS_SRM537));
  112. //_db.CodeFirst.InitTables(typeof(WCS_Station520));
  113. //_db.CodeFirst.InitTables(typeof(WCS_Station521));
  114. //_db.CodeFirst.InitTables(typeof(WCS_Station523));
  115. //_db.CodeFirst.InitTables(typeof(WCS_Station91));
  116. break;
  117. default: //其他库
  118. break;
  119. };
  120. };
  121. });
  122. #endregion 初始化数据库连接
  123. #region 初始化设备信息
  124. WorkStart.InitializeDeviceInfo();
  125. #endregion 初始化设备信息
  126. #region 创建虚拟PLC
  127. var isOpenVirtualPlc = RedisHub.Default.Check("isOpenVirtualPLC") ?? throw new Exception("请在Redsi中配置是否启用虚拟PLC");
  128. if (isOpenVirtualPlc == "1")
  129. {
  130. var plcDataConnectionString = RedisHub.Default.Check("plcDataConnectionString") ?? throw new Exception("请在Redsi中配置虚拟PLC使用的Redis连接字符串");
  131. //从现有结构解析出需要的结构
  132. var list = new List<PLCData>();
  133. SqlSugarHelper.Do(db =>
  134. {
  135. var _db = db.Connect;
  136. var dataBlocks = _db.Queryable<WCS_PlcDataBlock>().Includes(v => v.PLC).ToList();
  137. list.AddRange(dataBlocks.Select(dataBlock => new PLCData()
  138. {
  139. IP = dataBlock.PLC.IP,
  140. DB = dataBlock.NO,
  141. Length = dataBlock.Length,
  142. DataLength = dataBlock.DataLength,
  143. }));
  144. });
  145. PlcData.Init(plcDataConnectionString).InitPlcData(list);
  146. ServiceHub.AddSystemMode(SystemMode.虚拟plc);
  147. }
  148. #endregion 创建虚拟PLC
  149. var a = typeof(IStation520);
  150. #region 初始化PLC访问器及PLC读取协议
  151. //创建PLC访问器
  152. Configs.PLCAccessorCreater = new PLCAccessors.PLCAccessorsCreater();
  153. try
  154. {
  155. #region 唤醒所有的世界
  156. World.StartAll();
  157. #endregion 唤醒所有的世界
  158. _logger.LogInformation("WCS启动成功");
  159. #region 启用数据采集器
  160. while (true)
  161. {
  162. World.GetSystemInstance<DataCollectionSysyem>().Invoke(true);
  163. }
  164. #endregion 启用数据采集器
  165. }
  166. catch (Exception ex)
  167. {
  168. _logger.LogError("WCS启动失败{0}", ex.Message);
  169. }
  170. #endregion 初始化PLC访问器及PLC读取协议
  171. }
  172. }
  173. }