Worker.cs 6.9 KB

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