using Newtonsoft.Json; using ServiceCenter; using ServiceCenter.Redis; using ServiceCenter.SqlSugars; using ServiceCenter.Virtual_PLC; using SqlSugar; using System.Text; using WCS.Core; using WCS.Entity; using WCS.Entity.Protocol.BCR; using WCS.Entity.Protocol.Station; using WCS.Service.Systems; namespace WCS.Service { /// /// 工作服务 /// public class Worker : BackgroundService { /// /// 记录器 /// private readonly ILogger _logger; /// /// 构造函数 /// /// 记录器 public Worker(ILogger logger) { _logger = logger; } public static readonly string WcsDlog = "WCSDlog"; public static readonly string Wcsdb = "WCSDB"; /// /// 执行 /// /// 停止令牌 /// 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(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>(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 connectionConfigs = new List(); 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_PlcSet)); _db.CodeFirst.InitTables(typeof(WCS_PlcDataBlock)); _db.CodeFirst.InitTables(typeof(WCS_PlcData)); _db.CodeFirst.InitTables(typeof(WCS_DeviceInfo)); _db.CodeFirst.InitTables(typeof(WCS_DeviceGrp)); _db.CodeFirst.InitTables(typeof(WCS_DeviceProt)); _db.CodeFirst.InitTables(typeof(WCS_PathInfo)); _db.CodeFirst.InitTables(typeof(WCS_PathGrp)); _db.CodeFirst.InitTables(typeof(WCS_Route)); _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.SetDlog(connectionString.Key); _db.DbMaintenance.CreateDatabase(); _db.CodeFirst.InitTables(typeof(WCS_BCR80)); //db.Context(WcsDlog).CodeFirst.InitTables(typeof(WCS_RGV520)); //db.Context(WcsDlog).CodeFirst.InitTables(typeof(WCS_RGV521)); //db.Context(WcsDlog).CodeFirst.InitTables(typeof(WCS_RGV523)); //_db.CodeFirst.InitTables(typeof(WCS_SRM520)); //_db.CodeFirst.InitTables(typeof(WCS_SRM521)); //db.Context(WcsDlog).CodeFirst.InitTables(typeof(WCS_SRM537)); _db.CodeFirst.InitTables(typeof(WCS_Station520)); _db.CodeFirst.InitTables(typeof(WCS_Station521)); _db.CodeFirst.InitTables(typeof(WCS_Station523)); _db.CodeFirst.InitTables(typeof(WCS_Station91)); break; default: //其他库 break; }; }; }); #endregion 初始化数据库连接 #region 初始化设备信息 SqlSugarHelper.Do(db => { ServiceHub.deviceInfos = db.Default.Queryable() .Includes(v => v.NextRoutes) .Includes(v => v.FormerRoutes) .Includes(v => v.Paths) .Includes(v => v.DeviceGroup) .Includes(v => v.DeviceProtocol) .ToList(); }); #endregion 初始化设备信息 #region 创建虚拟PLC var isOpenVirtualPlc = RedisHub.Default.Check("isOpenVirtualPLC") ?? throw new Exception("请在Redsi中配置是否启用虚拟PLC"); if (isOpenVirtualPlc == "1") { var plcDataConnectionString = RedisHub.Default.Check("plcDataConnectionString") ?? throw new Exception("请在Redsi中配置虚拟PLC使用的Redis连接字符串"); //从现有结构解析出需要的结构 var list = new List(); SqlSugarHelper.Do(db => { var _db = db.Connect; var dataBlocks = _db.Queryable().Includes(v => v.PLC).ToList(); list.AddRange(dataBlocks.Select(dataBlock => new PLCData() { IP = dataBlock.PLC.IP, DB = dataBlock.NO, Length = dataBlock.Length, DataLength = dataBlock.DataLength, })); }); PlcData.Init(plcDataConnectionString).InitPlcData(list); ServiceHub.AddSystemMode(SystemMode.虚拟plc); } #endregion 创建虚拟PLC var a = typeof(IStation520); #region 初始化PLC访问器及PLC读取协议 //创建PLC访问器 Configs.PLCAccessorCreater = new PLCAccessors.PLCAccessorsCreater(); try { SqlSugarHelper.Do(db => { var _db = db.Connect; //获取所有DB块读写协议 var dbProtocols = _db.Queryable().Includes(v => v.DB, p => p.PLC).ToList(); foreach (var dbProtocol in dbProtocols) { Add(Type.GetType(dbProtocol.DB.Protocol), dbProtocol.DeviceCode, dbProtocol.Position, dbProtocol.DB, dbProtocol.DB.PLC); } }); #region 唤醒所有的世界 World.StartAll(); #endregion 唤醒所有的世界 _logger.LogInformation("WCS启动成功"); #region 启用数据采集器 while (true) { World.GetSystemInstance().Invoke(true); } #endregion 启用数据采集器 } catch (Exception ex) { _logger.LogError("WCS启动失败{0}", ex.Message); } #endregion 初始化PLC访问器及PLC读取协议 } /// /// 添加协议 /// /// 协议类型 /// 设备号 /// 地址 /// db /// PLC public static void Add(Type type, string code, int position, WCS_PlcDataBlock db, WCS_PlcSet plc) { var info = new ProtocolInfo { Position = position, DBInfo = new DBInfo { No = (ushort)db.NO, PLCInfo = new PLCInfo { IP = plc.IP, Port = plc.Port, Rack = plc.Rack, Slot = plc.Slot, Type = Core.PLCType.Siemens } } }; try { Protocols.Add(type, code, info); } catch (Exception ex) { var a = ex; } } } }