Worker.cs 6.3 KB

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