Worker.cs 8.1 KB

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