Worker.cs 7.3 KB

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