123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- using Newtonsoft.Json;
- using PlcSiemens.Core.Extension;
- using ServiceCenter;
- using ServiceCenter.Logs;
- using ServiceCenter.Redis;
- using ServiceCenter.SqlSugars;
- using SqlSugar;
- using System.Text;
- using WCS.Core;
- using WCS.Entity;
- using WCS.WorkEngineering;
- namespace WCS.Service
- {
- /// <summary>
- /// 工作服务
- /// </summary>
- public class Worker : BackgroundService
- {
- /// <summary>
- /// 记录器
- /// </summary>
- private readonly ILogger<Worker> _logger;
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="logger">记录器</param>
- public Worker(ILogger<Worker> logger)
- {
- _logger = logger;
- }
- public static readonly string WcsDlog = "WCSDlog";
- public static readonly string Wcsdb = "WCSDB";
- /// <summary>
- /// 执行
- /// </summary>
- /// <param name="stoppingToken">停止令牌</param>
- /// <returns></returns>
- protected override async Task ExecuteAsync(CancellationToken stoppingToken)
- {
- if (stoppingToken.IsCancellationRequested)
- return;
- #region 启用日志
- //var logConfigText = RedisHub.Default.Check("LogConfigText") ?? throw new Exception("请在Redis中配置log4net相关内容");
- //var logConfig = JsonConvert.DeserializeObject<LogConfig>(logConfigText);
- //LogHub.SetConfigInfo(logConfig!);
- #endregion 启用日志
- _logger.LogInformation("WCS开始启动");
- Configs.ProtocolProxyBaseType = typeof(ProtocolProxy);
- Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
- Configs.StringEncoding = Encoding.UTF8;
- var warehouseName = RedisHub.Default.Check("WarehouseName") ?? throw new Exception("请在Redis中配置仓库名称");
- if (string.IsNullOrEmpty(warehouseName)) throw new Exception("请在Redis中配置仓库名称");
- ServiceHub.SetWarehouseName(warehouseName);
- #region 初始化数据库连接
- var dbConnectionStrings = RedisHub.Default.Check("DbConnectionStrings") ?? throw new Exception("请在Redis中配置数据库连接相关内容");
- ServiceHub.DbConnectionStrings = JsonConvert.DeserializeObject<List<DataBaseConnectionString>>(dbConnectionStrings);
- if (ServiceHub.DbConnectionStrings != null)
- {
- if (ServiceHub.DbConnectionStrings.All(v => v.Key != Wcsdb)) throw new Exception("请在DbConnectionStrings中配置WCS基础数据库连接字符串");
- if (ServiceHub.DbConnectionStrings.All(v => v.Key == Wcsdb && !v.IsDefault)) throw new Exception("请在DbConnectionStrings中配置WCS基础数据库为默认数据库");
- // if (ServiceHub.DbConnectionStrings.All(v => v.Key != WcsDlog)) throw new Exception("请在DbConnectionStrings中配置WCS日志数据库连接字符串");
- }
- //设置连接信息
- List<ConnectionConfig> connectionConfigs = new List<ConnectionConfig>();
- foreach (var connectionString in ServiceHub.DbConnectionStrings!)
- {
- connectionConfigs.Add(new ConnectionConfig()
- {
- ConfigId = connectionString.Key,
- ConnectionString = connectionString.ConnectionString,//连接符字串
- DbType = connectionString.DbType,//数据库类型
- IsAutoCloseConnection = true,//不设成true要手动close
- });
- };
- SqlSugarHelper.SetDb(new SqlSugarScope(connectionConfigs));
- //初始化数据库
- SqlSugarHelper.Do(db =>
- {
- foreach (var connectionString in ServiceHub.DbConnectionStrings!)
- {
- var _db = db.Connect.GetConnectionScope(connectionString.Key);
- switch (connectionString.Key)
- {
- case "WCSDB"://WCS基本数据库
- SqlSugarHelper.SetDefault(connectionString.Key);
- _db.CodeFirst.InitTables(typeof(WCS_PlcData));
- _db.CodeFirst.InitTables(typeof(WCS_TaskInfo));
- _db.CodeFirst.InitTables(typeof(WCS_TaskDtl));
- _db.CodeFirst.InitTables(typeof(WCS_TaskOld));
- _db.CodeFirst.InitTables(typeof(WCS_AgvTaskInfo));
- break;
- case "WCSDlog"://WCS日志数据库
- //SqlSugarHelper.SetDefault(connectionString.Key);
- //_db.CodeFirst.InitTables(typeof(w));
- //_db.CodeFirst.InitTables(typeof(WCS_TaskInfo));
- //_db.CodeFirst.InitTables(typeof(WCS_TaskDtl));
- //_db.CodeFirst.InitTables(typeof(WCS_TaskOld));
- //_db.CodeFirst.InitTables(typeof(WCS_AgvTaskInfo));
- break;
- default: //其他库
- break;
- };
- };
- });
- #endregion 初始化数据库连接
-
- #region 初始化设备信息
- WorkStart.InitializeDeviceInfo();
- #endregion 初始化设备信息
- #region 初始化PLC访问器及PLC读取协议
- //创建PLC访问器
- Configs.PLCAccessorCreater = new PLCAccessors.PLCAccessorsCreater();
- try
- {
- #region 唤醒所有的世界
- World.StartAll();
- #endregion 唤醒所有的世界
- _logger.LogInformation("WCS启动成功");
- }
- catch (Exception ex)
- {
- _logger.LogError("WCS启动失败{0}", ex.Message);
- }
- #endregion 初始化PLC访问器及PLC读取协议
- LogHub.init();
- }
- }
- }
|