123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- using DBHelper_SqlSugar;
- using Logs;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.Hosting;
- using Microsoft.Extensions.Logging;
- using Newtonsoft.Json;
- using SqlSugar;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using Virtual_PLC;
- using WCS.Core;
- using WCS.Core.DataTrans;
- using WCS.Entity;
- using WCS.Entity.Protocol;
- using WCS.Entity.Protocol.BCR;
- using WCS.Entity.Protocol.Station;
- namespace WCS.Service
- {
- public class Worker : BackgroundService
- {
- private readonly ILogger<Worker> _logger;
- public Worker(ILogger<Worker> logger)
- {
- _logger = logger;
- }
- protected override async Task ExecuteAsync(CancellationToken stoppingToken)
- {
- if (stoppingToken.IsCancellationRequested)
- return;
- #region 启用日志
- var logConfig = JsonConvert.DeserializeObject<LogConfig>(await File.ReadAllTextAsync("config.json", Encoding.Default, stoppingToken));
- LogHelper.SetConfigInfo(logConfig!);
- #endregion 启用日志
- InfoLog.INFO_INIT("1111");
- _logger.LogInformation("WCS开始启动");
- //InfoLog.INFO_INIT("WCS开始启动");
- Configs.DebugRedisUrl = "127.0.0.1";
- Configs.ProtocolProxyBaseType = typeof(ProtocolProxy);
- Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
- Configs.StringEncoding = Encoding.UTF8;
- Db.CreateContext(new ConnectionConfig()
- {
- ConnectionString = AppSettings.Config.GetConnectionString("WCSDB"),
- DbType = DbType.SqlServer
- }, "WCSDB");
- Db.SetDefaultDbContextType("WCSDB");
- Db.Do(db =>
- {
- //TODO:DbMaintenance.CreateDatabase()并没起到作用,如果没有对应的数据库的话任然需要手动新建一个
- db.Default.DbMaintenance.CreateDatabase();
- db.Default.CodeFirst.InitTables(typeof(WCS_CMD));
- db.Default.CodeFirst.InitTables(typeof(WCS_PLC));
- db.Default.CodeFirst.InitTables(typeof(WCS_DATABLOCK));
- db.Default.CodeFirst.InitTables(typeof(WCS_DEVICE));
- db.Default.CodeFirst.InitTables(typeof(WCS_PATH));
- db.Default.CodeFirst.InitTables(typeof(WCS_PATHPOINT));
- db.Default.CodeFirst.InitTables(typeof(WCS_ROUTE));
- db.Default.CodeFirst.InitTables(typeof(WCS_TASK));
- db.Default.CodeFirst.InitTables(typeof(WCS_TASK_OLD));
- db.Default.CodeFirst.InitTables(typeof(WCS_EXCEPTION));
- db.Default.CodeFirst.InitTables(typeof(WCS_SystemConfig));
- db.Default.CodeFirst.InitTables(typeof(WCS_AGVTask));
- db.Default.CodeFirst.InitTables(typeof(WCS_DEVICEPROTOCOL));
- db.Default.CodeFirst.InitTables(typeof(WCS_GROUPMEMBER));
- db.Default.CodeFirst.InitTables(typeof(WCS_MAPPINGENTRY));
- db.Default.CodeFirst.InitTables(typeof(WCS_USERS));
- db.Default.CodeFirst.InitTables(typeof(WCS_StatusLog));
- db.Default.CodeFirst.InitTables(typeof(WCS_BCR80));
- db.Default.CodeFirst.InitTables(typeof(WCS_RGV520));
- db.Default.CodeFirst.InitTables(typeof(WCS_RGV521));
- db.Default.CodeFirst.InitTables(typeof(WCS_RGV523));
- db.Default.CodeFirst.InitTables(typeof(WCS_SRM520));
- db.Default.CodeFirst.InitTables(typeof(WCS_SRM521));
- db.Default.CodeFirst.InitTables(typeof(WCS_SRM537));
- db.Default.CodeFirst.InitTables(typeof(WCS_Station520));
- db.Default.CodeFirst.InitTables(typeof(WCS_Station521));
- db.Default.CodeFirst.InitTables(typeof(WCS_Station523));
- });
- //从现有结构解析出需要的结构
- List<PLCData> list = new List<PLCData>();
- Db.Do(db =>
- {
- var dataBlocks = db.Default.Queryable<WCS_DATABLOCK>().Includes(v => v.PLC).ToList();
- foreach (var dataBlock in dataBlocks)
- {
- list.Add(new PLCData()
- {
- IP = dataBlock.PLC.IP,
- DB = dataBlock.NO,
- Length = dataBlock.LENGTH,
- DataLength = dataBlock.DATALENGTH,
- });
- }
- });
- PlcData.Init("127.0.0.1,database=0").InitPlcData(list);
- //日志发布事件s
- Configs.PublishEvent += () =>
- {
- WMS.UploadDevInfo();
- ProtocolProxy.Do();
- };
- //异常上抛
- Configs.UploadException = (d, s) =>
- {
- if (s == "接口调用中") return;
- if (ProtocolProxy.AllDatas.ContainsKey(d))
- {
- ProtocolProxy.AllDatas[d].Info = s;
- ProtocolProxy.AllDatas[d].Frame = LogicHandler.Frame;
- }
- WMS.TaskException(d, s);
- };
- //LogicHandler.DbLog = Helpers.LogHelper.AddWCS_EXCEPTION;
- PlcAccessor.Creater = new PLCAccessors.PlcAccessorsCreater();
- try
- {
- Db.Do(db =>
- {
- var items = db.Default.Queryable<WCS_DEVICEPROTOCOL>()
- .Includes(d => d.DEVICE, r => r.ROUTES)
- .Includes(d => d.DEVICE, p => p.PATHS)
- .Includes(d => d.DB, p => p.PLC).ToArray();
- items.Select(v => v.Data()).ToArray();
- LogicHandler.AllObjects.AddRange(items);
- var devices = db.Default.Queryable<WCS_DEVICE>()
- .Includes(v => v.ROUTES)
- .Includes(v => v.PATHS)
- .Includes(v => v.DEVICEGROUP)
- .Includes(v => v.DEVICEPROTOCOLS, p => p.DB, c => c.PLC)
- .Includes(v => v.DEVICEPROTOCOLS, p => p.DEVICE)
- .ToList();
- LogicHandler.AllObjects.AddRange(devices);
- });
- #region 设备扩展数据配置
- //TODO:通过配置使用switch
- Device.AddFlag(DF.一楼RGV放货, "G1035", "G1044", "G1053", "G1062");
- Device.AddFlag(DF.SRM, "SRM1", "SRM2", "SRM3", "SRM4", "SRM5", "SRM6", "SRM7", "SRM8");
- Device.AddFlag(DF.月台, "G1469", "G1561", "G1538", "G1574", "G1509");
- Device.AddFlag(DF.SRM二级品取货, "1040", "1041", "1042", "1043", "1049", "1050", "1051", "1052");
- Device.AddFlag(DF.SRM二级品取货, "1058", "1059", "1060", "1061", "1067", "1068");
- Device.AddFlag(DF.SRMBOPP取货, "1195", "1194", "1193", "1192", "1204", "1203", "1202", "1201");
- Device.AddFlag(DF.SRMBOPP取货, "1213", "1212", "1210", "1211", "1220", "1219", "1230", "1228");
- Device.AddFlag(DF.SRM月台放货, "1473", "1476", "1474", "1475", "1491", "1492", "1493", "1494");
- Device.AddFlag(DF.SRM月台放货, "1520", "1521", "1522", "1523", "1545", "1546", "1451", "1453");
- Device.AddFlag(DF.SRM涂布取货, "1431", "1432", "1422", "1423", "1424", "1425", "1415", "1416");
- Device.AddFlag(DF.SRM涂布取货, "1605", "1606", "1406", "1407", "1408", "1409");
- Device.AddFlag(DF.SRM涂布放货, "1283", "1284", "1290", "1291", "1292", "1293", "1299", "1300");
- Device.AddFlag(DF.SRM涂布放货, "1301", "1302", "1308", "1309", "1310", "1311");
- Device.AddFlag(DF.涂布RGV, "RGV9", "RGV10", "RGV11", "RGV12", "RGV13", "RGV14");
- Device.AddFlag(DF.涂布RGV取货设备组, "G2", "G3", "G5", "G7", "G9", "G11");
- Device.AddFlag(DF.涂布RGV放货设备组, "G1", "G4", "G6", "G8", "G10");
- Device.AddFlag(DF.涂布出库RGV取货站台, "1285", "1286", "1294", "1295", "1303", "1304", "1312", "1313");
- Device.AddFlag(DF.涂布入库RGV取货站台, "1391", "1392", "1399", "1400");
- Device.AddFlag(DF.涂布RGV取货站台, "1285", "1286", "1294", "1295", "1303", "1304", "1312", "1313");
- Device.AddFlag(DF.涂布RGV取货站台, "1391", "1392", "1399", "1400");
- Device.AddFlag(DF.BOPPRGV, "RGV1", "RGV2", "RGV3", "RGV4", "RGV5", "RGV6", "RGV7");
- Device.AddFlag(DF.BOPPRGV取货设备组, "G19", "G23");
- Device.AddFlag(DF.BOPPRGV放货设备组, "G12", "G13", "G14", "G15", "G16");
- #endregion 设备扩展数据配置
- #region 启用所有的逻辑处理器
- var managerTypes = Assembly.GetExecutingAssembly().GetTypes().Where(v => v.IsSubclassOf(typeof(LogicHandler)) && !v.IsGenericType && !v.IsAbstract).ToArray();
- foreach (var type in managerTypes)
- {
- var m = Activator.CreateInstance(type);
- LogicHandler.AddManager(m as LogicHandler);
- }
- LogicHandler.StartAll();
- #endregion 启用所有的逻辑处理器
- _logger.LogInformation("WCS启动成功");
- //InfoLog.INFO_INIT("WCS启动成功");
- }
- catch (Exception ex)
- {
- _logger.LogError("WCS启动失败{0}", ex.Message);
- //InfoLog.INFO_INIT($"WCS启动失败{ex.Message}");
- }
- }
- public override Task StopAsync(CancellationToken cancellationToken)
- {
- _logger.LogError("WCS关闭");
- //InfoLog.INFO_INIT("WCS关闭");
- LogicHandler.StopAll();
- return base.StopAsync(cancellationToken);
- }
- }
- }
|