Worker.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System.Text;
  2. using MessagePack;
  3. using Newtonsoft.Json;
  4. using ServiceCenter;
  5. using ServiceCenter.Extensions;
  6. using ServiceCenter.Logs;
  7. using ServiceCenter.Redis;
  8. using ServiceCenter.SqlSugars;
  9. using SqlSugar;
  10. using WCS.Core;
  11. using WCS.Service.PLCAccessors;
  12. using WCS.WorkEngineering;
  13. namespace WCS.Service;
  14. /// <summary>
  15. /// 工作服务
  16. /// </summary>
  17. public class Worker : BackgroundService
  18. {
  19. public static readonly string WcsDlog = "WCSDlog";
  20. public static readonly string Wcsdb = "WCSDB";
  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. /// <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") ??
  44. throw new Exception("请在Redis中配置RedisConnectionStrings");
  45. var configs = JsonConvert.DeserializeObject<List<DataBaseConnectionString>>(redisConnectionStrings);
  46. if (configs != null)
  47. if (configs.All(v => v.Key != "Monitor"))
  48. throw new Exception("请在RedisConnectionStrings中配置监控RedisDB库连接字符串");
  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. case "Virtual_PLC":
  75. PLCAccessorsCreater.RedisConnStr = redisConnection.ConnectionString;
  76. break;
  77. }
  78. }
  79. #endregion 初始化Redis连接
  80. #region 启用日志
  81. //var logConfigText = RedisHub.Default.Check("LogConfigText") ?? throw new Exception("请在Redis中配置log4net相关内容");
  82. //var logConfig = JsonConvert.DeserializeObject<LogConfig>(logConfigText);
  83. //LogHub.SetConfigInfo(logConfig!);
  84. #endregion 启用日志
  85. _logger.LogInformation("WCS开始启动");
  86. Configs.ProtocolProxyBaseType = typeof(ProtocolProxy);
  87. Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
  88. Configs.StringEncoding = Encoding.UTF8;
  89. //var warehouseName = RedisHub.Default.Check("WarehouseName") ?? throw new Exception("请在Redis中配置仓库名称");
  90. //if (string.IsNullOrEmpty(warehouseName)) throw new Exception("请在Redis中配置仓库名称");
  91. //ServiceHub.SetWarehouseName(warehouseName);
  92. #region 初始化数据库连接
  93. //InstanceFactory.CustomDbName = "TDengine";
  94. //InstanceFactory.CustomDllName = "SqlSugar.BzTDengineCore";
  95. //InstanceFactory.CustomNamespace = "SqlSugar.BzTDengineCore";
  96. var dbConnectionStrings =
  97. RedisHub.Default.Check("DbConnectionStrings") ?? throw new Exception("请在Redis中配置数据库连接相关内容");
  98. ServiceHub.DbConnectionStrings =
  99. JsonConvert.DeserializeObject<List<DataBaseConnectionString>>(dbConnectionStrings);
  100. if (ServiceHub.DbConnectionStrings != null)
  101. {
  102. //if (ServiceHub.DbConnectionStrings.All(v => v.Key != Wcsdb)) throw new Exception("请在DbConnectionStrings中配置WCS基础数据库连接字符串");
  103. //if (ServiceHub.DbConnectionStrings.All(v => v.Key == Wcsdb && !v.IsDefault)) throw new Exception("请在DbConnectionStrings中配置WCS基础数据库为默认数据库");
  104. // if (ServiceHub.DbConnectionStrings.All(v => v.Key != WcsDlog)) throw new Exception("请在DbConnectionStrings中配置WCS日志数据库连接字符串");
  105. }
  106. //设置连接信息
  107. var connectionConfigs = new List<ConnectionConfig>();
  108. foreach (var connectionString in ServiceHub.DbConnectionStrings!)
  109. {
  110. if (connectionString.Key == "PLC") Configs.QdbConnString = connectionString.ConnectionString;
  111. connectionConfigs.Add(new ConnectionConfig
  112. {
  113. ConfigId = connectionString.Key,
  114. ConnectionString = connectionString.ConnectionString, //连接符字串
  115. DbType = connectionString.DbType, //数据库类型
  116. IsAutoCloseConnection = true, //不设成true要手动close
  117. LanguageType = LanguageType.Chinese,
  118. MoreSettings = new ConnMoreSettings
  119. {
  120. IsNoReadXmlDescription = true
  121. }
  122. });
  123. }
  124. ;
  125. SqlSugarHelper.SetDb(new SqlSugarScope(connectionConfigs));
  126. //ServiceHub.DbConnectionStrings.InitDB();
  127. //Configs.QdbConnString = configuration.GetSection("Connections")["qdb"];
  128. #endregion 初始化数据库连接
  129. #region 初始化设备信息
  130. WorkStart.InitializeDeviceInfo();
  131. #endregion 初始化设备信息
  132. #region 初始化PLC访问器及PLC读取协议
  133. //创建PLC访问器
  134. Configs.PLCAccessorCreater = new PLCAccessorsCreater();
  135. try
  136. {
  137. #region 唤醒所有的世界
  138. World.StartAll();
  139. #endregion 唤醒所有的世界
  140. _logger.LogInformation("WCS启动成功");
  141. }
  142. catch (Exception ex)
  143. {
  144. _logger.LogError("WCS启动失败{0}", ex.Message);
  145. }
  146. #endregion 初始化PLC访问器及PLC读取协议
  147. LogHub.init();
  148. }
  149. }