Worker.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. using MessagePack.ImmutableCollection;
  13. using MessagePack.Resolvers;
  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 初始化Redis连接
  45. var redisConnectionStrings = RedisHub.Default.Check("RedisConnectionStrings") ?? throw new Exception("请在Redis中配置RedisConnectionStrings");
  46. var configs = JsonConvert.DeserializeObject<List<DataBaseConnectionString>>(redisConnectionStrings);
  47. if (configs != null)
  48. {
  49. if (configs.All(v => v.Key != "Monitor")) throw new Exception("请在RedisConnectionStrings中配置监控RedisDB库连接字符串");
  50. }
  51. foreach (var redisConnection in configs!)
  52. {
  53. RedisHub.CreateContext(redisConnection.ConnectionString, redisConnection.Key);
  54. switch (redisConnection.Key)
  55. {
  56. case "Monitor":
  57. RedisHub.SetMonitorContextType(redisConnection.Key);
  58. RedisHub.Monitor.Serialize = obj =>
  59. {
  60. var bytes = MessagePackSerializer.Serialize(obj);
  61. return bytes;
  62. };
  63. RedisHub.Monitor.DeserializeRaw = (bytes, type) =>
  64. {
  65. var obj = MessagePackSerializer.Deserialize(type, bytes);
  66. return obj;
  67. };
  68. break;
  69. case "DebugRedisUrl":
  70. RedisHub.SetDebugContextType(redisConnection.Key);
  71. Configs.DebugRedisUrl = redisConnection.ConnectionString;
  72. break;
  73. case "WMS":
  74. RedisHub.SetWMSContextType(redisConnection.Key);
  75. break;
  76. }
  77. }
  78. #endregion 初始化Redis连接
  79. #region 启用日志
  80. //var logConfigText = RedisHub.Default.Check("LogConfigText") ?? throw new Exception("请在Redis中配置log4net相关内容");
  81. //var logConfig = JsonConvert.DeserializeObject<LogConfig>(logConfigText);
  82. //LogHub.SetConfigInfo(logConfig!);
  83. #endregion 启用日志
  84. _logger.LogInformation("WCS开始启动");
  85. Configs.ProtocolProxyBaseType = typeof(ProtocolProxy);
  86. Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
  87. Configs.StringEncoding = Encoding.UTF8;
  88. //var warehouseName = RedisHub.Default.Check("WarehouseName") ?? throw new Exception("请在Redis中配置仓库名称");
  89. //if (string.IsNullOrEmpty(warehouseName)) throw new Exception("请在Redis中配置仓库名称");
  90. //ServiceHub.SetWarehouseName(warehouseName);
  91. #region 初始化数据库连接
  92. //InstanceFactory.CustomDbName = "TDengine";
  93. //InstanceFactory.CustomDllName = "SqlSugar.BzTDengineCore";
  94. //InstanceFactory.CustomNamespace = "SqlSugar.BzTDengineCore";
  95. var dbConnectionStrings = RedisHub.Default.Check("DbConnectionStrings") ?? throw new Exception("请在Redis中配置数据库连接相关内容");
  96. ServiceHub.DbConnectionStrings = JsonConvert.DeserializeObject<List<DataBaseConnectionString>>(dbConnectionStrings);
  97. if (ServiceHub.DbConnectionStrings != null)
  98. {
  99. //if (ServiceHub.DbConnectionStrings.All(v => v.Key != Wcsdb)) throw new Exception("请在DbConnectionStrings中配置WCS基础数据库连接字符串");
  100. //if (ServiceHub.DbConnectionStrings.All(v => v.Key == Wcsdb && !v.IsDefault)) throw new Exception("请在DbConnectionStrings中配置WCS基础数据库为默认数据库");
  101. // if (ServiceHub.DbConnectionStrings.All(v => v.Key != WcsDlog)) throw new Exception("请在DbConnectionStrings中配置WCS日志数据库连接字符串");
  102. }
  103. //设置连接信息
  104. List<ConnectionConfig> connectionConfigs = new List<ConnectionConfig>();
  105. foreach (var connectionString in ServiceHub.DbConnectionStrings!)
  106. {
  107. connectionConfigs.Add(new ConnectionConfig()
  108. {
  109. ConfigId = connectionString.Key,
  110. ConnectionString = connectionString.ConnectionString,//连接符字串
  111. DbType = connectionString.DbType,//数据库类型
  112. IsAutoCloseConnection = true,//不设成true要手动close
  113. LanguageType = LanguageType.Chinese,
  114. MoreSettings = new ConnMoreSettings()
  115. {
  116. IsNoReadXmlDescription = true
  117. }
  118. });
  119. };
  120. SqlSugarHelper.SetDb(new SqlSugarScope(connectionConfigs));
  121. ServiceHub.DbConnectionStrings.InitDB();
  122. #endregion 初始化数据库连接
  123. #region 初始化设备信息
  124. WorkStart.InitializeDeviceInfo();
  125. #endregion 初始化设备信息
  126. #region 初始化PLC访问器及PLC读取协议
  127. //创建PLC访问器
  128. Configs.PLCAccessorCreater = new PLCAccessors.PLCAccessorsCreater();
  129. try
  130. {
  131. #region 唤醒所有的世界
  132. World.StartAll();
  133. #endregion 唤醒所有的世界
  134. _logger.LogInformation("WCS启动成功");
  135. }
  136. catch (Exception ex)
  137. {
  138. _logger.LogError("WCS启动失败{0}", ex.Message);
  139. }
  140. #endregion 初始化PLC访问器及PLC读取协议
  141. LogHub.init();
  142. }
  143. }
  144. }