Worker.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using Newtonsoft.Json;
  2. using PlcSiemens.Core.Extension;
  3. using ServiceCenter;
  4. using ServiceCenter.Logs;
  5. using ServiceCenter.Redis;
  6. using ServiceCenter.SqlSugars;
  7. using SqlSugar;
  8. using System.Text;
  9. using WCS.Core;
  10. using WCS.Entity;
  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 启用日志
  43. //var logConfigText = RedisHub.Default.Check("LogConfigText") ?? throw new Exception("请在Redis中配置log4net相关内容");
  44. //var logConfig = JsonConvert.DeserializeObject<LogConfig>(logConfigText);
  45. //LogHub.SetConfigInfo(logConfig!);
  46. #endregion 启用日志
  47. _logger.LogInformation("WCS开始启动");
  48. Configs.ProtocolProxyBaseType = typeof(ProtocolProxy);
  49. Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
  50. Configs.StringEncoding = Encoding.UTF8;
  51. var warehouseName = RedisHub.Default.Check("WarehouseName") ?? throw new Exception("请在Redis中配置仓库名称");
  52. if (string.IsNullOrEmpty(warehouseName)) throw new Exception("请在Redis中配置仓库名称");
  53. ServiceHub.SetWarehouseName(warehouseName);
  54. #region 初始化数据库连接
  55. var dbConnectionStrings = RedisHub.Default.Check("DbConnectionStrings") ?? throw new Exception("请在Redis中配置数据库连接相关内容");
  56. ServiceHub.DbConnectionStrings = JsonConvert.DeserializeObject<List<DataBaseConnectionString>>(dbConnectionStrings);
  57. if (ServiceHub.DbConnectionStrings != null)
  58. {
  59. if (ServiceHub.DbConnectionStrings.All(v => v.Key != Wcsdb)) throw new Exception("请在DbConnectionStrings中配置WCS基础数据库连接字符串");
  60. if (ServiceHub.DbConnectionStrings.All(v => v.Key == Wcsdb && !v.IsDefault)) throw new Exception("请在DbConnectionStrings中配置WCS基础数据库为默认数据库");
  61. // if (ServiceHub.DbConnectionStrings.All(v => v.Key != WcsDlog)) throw new Exception("请在DbConnectionStrings中配置WCS日志数据库连接字符串");
  62. }
  63. //设置连接信息
  64. List<ConnectionConfig> connectionConfigs = new List<ConnectionConfig>();
  65. foreach (var connectionString in ServiceHub.DbConnectionStrings!)
  66. {
  67. connectionConfigs.Add(new ConnectionConfig()
  68. {
  69. ConfigId = connectionString.Key,
  70. ConnectionString = connectionString.ConnectionString,//连接符字串
  71. DbType = connectionString.DbType,//数据库类型
  72. IsAutoCloseConnection = true,//不设成true要手动close
  73. });
  74. };
  75. SqlSugarHelper.SetDb(new SqlSugarScope(connectionConfigs));
  76. //初始化数据库
  77. SqlSugarHelper.Do(db =>
  78. {
  79. foreach (var connectionString in ServiceHub.DbConnectionStrings!)
  80. {
  81. var _db = db.Connect.GetConnectionScope(connectionString.Key);
  82. switch (connectionString.Key)
  83. {
  84. case "WCSDB"://WCS基本数据库
  85. SqlSugarHelper.SetDefault(connectionString.Key);
  86. _db.CodeFirst.InitTables(typeof(WCS_PlcData));
  87. _db.CodeFirst.InitTables(typeof(WCS_TaskInfo));
  88. _db.CodeFirst.InitTables(typeof(WCS_TaskDtl));
  89. _db.CodeFirst.InitTables(typeof(WCS_TaskOld));
  90. _db.CodeFirst.InitTables(typeof(WCS_AgvTaskInfo));
  91. break;
  92. case "WCSDlog"://WCS日志数据库
  93. //SqlSugarHelper.SetDefault(connectionString.Key);
  94. //_db.CodeFirst.InitTables(typeof(w));
  95. //_db.CodeFirst.InitTables(typeof(WCS_TaskInfo));
  96. //_db.CodeFirst.InitTables(typeof(WCS_TaskDtl));
  97. //_db.CodeFirst.InitTables(typeof(WCS_TaskOld));
  98. //_db.CodeFirst.InitTables(typeof(WCS_AgvTaskInfo));
  99. break;
  100. default: //其他库
  101. break;
  102. };
  103. };
  104. });
  105. #endregion 初始化数据库连接
  106. #region 初始化设备信息
  107. WorkStart.InitializeDeviceInfo();
  108. #endregion 初始化设备信息
  109. #region 初始化PLC访问器及PLC读取协议
  110. //创建PLC访问器
  111. Configs.PLCAccessorCreater = new PLCAccessors.PLCAccessorsCreater();
  112. try
  113. {
  114. #region 唤醒所有的世界
  115. World.StartAll();
  116. #endregion 唤醒所有的世界
  117. _logger.LogInformation("WCS启动成功");
  118. }
  119. catch (Exception ex)
  120. {
  121. _logger.LogError("WCS启动失败{0}", ex.Message);
  122. }
  123. #endregion 初始化PLC访问器及PLC读取协议
  124. LogHub.init();
  125. }
  126. }
  127. }