Worker.cs 6.7 KB

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