using Khd.Core.Domain.Dto.wcs; using Khd.Core.Domain.Dto.webapi; using Khd.Core.Domain.Models; using Khd.Core.EntityFramework; using Khd.Core.Plc.S7; using Khd.Core.Wcs.Global; using Khd.Core.Wcs.Wcs; using Masuit.Tools; using Masuit.Tools.Logging; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Khd.Core.Wcs { public class MainCentralControl { public static object QingKongDianWei; public static object WcsChuLiWanCheng; public static object WcsMoRenQuXiang; private readonly IHost _host; public MainCentralControl(IHost host) { this._host = host; } public void Start() { try { using (var scope = _host.Services.CreateScope()) { using (var dbContext = scope.ServiceProvider.GetRequiredService()) { //设置程序启动时清空点位的数据=>0为 Int16位 QingKongDianWei = getValue("2", "0"); //设置默认处理完成=>1为 Int16位 WcsChuLiWanCheng = getValue("2", "1"); //设置默认去向=>1为 Int16位 WcsMoRenQuXiang = getValue("2", "1"); //加载物料信息 StaticData.MateriaList = dbContext.BaseMaterialinfo.Where(t => t.isDelete == 0).OrderBy(t => t.materialNo).ToList(); //加载库区信息 StaticData.BaseAreaList = dbContext.BaseArea.Where(t => t.isDelete == 0).OrderBy(t => t.areaOrder).ToList(); //加载站台 StaticData.SiteNodeList = dbContext.BaseSitenode.Where(t => t.isDelete == 0).OrderBy(t => t.siteNo).ToList(); //加载小车 StaticData.CarList = dbContext.BaseCar.Where(t => t.isDelete == 0).OrderBy(t => t.carNo).ToList(); //加载点位 StaticData.NodeSettingList = GetNodeSettingList(dbContext); //加载配置项 //连接PLC判断 var plc = new Khd.Core.Plc.S7.Plc((CpuType)PlcConfig.CpuType, PlcConfig.IP, PlcConfig.Port, PlcConfig.Rack, PlcConfig.Slot); try { plc.Open(); if (plc.IsConnected) if (true) { //启动处理订单线程 BackUpData orderbak = new BackUpData(this._host); orderbak.StartPoint(); Console.WriteLine("处理订单线程启动完毕!"); //启动上件点线程1 UpLine up1 = new UpLine(this._host, plc, "K46"); up1.StartPoint(); Console.WriteLine("2号上件点线程启动完毕!"); //启动上件点线程2 UpLine up2 = new UpLine(this._host, plc, "K48"); up2.StartPoint(); Console.WriteLine("1号上件点线程启动完毕!"); //启动流转点线程1 FlowPoint flow1 = new FlowPoint(this._host, plc, "K18"); flow1.StartPoint(); Console.WriteLine("K18流转点线程启动完毕!"); //启动流转点线程2 FlowPoint flow2 = new FlowPoint(this._host, plc, "K22"); flow2.StartPoint(); Console.WriteLine("K22流转点线程启动完毕!"); //启动流转点线程3 FlowPoint flow3 = new FlowPoint(this._host, plc, "K48"); flow3.StartPoint(); Console.WriteLine("K48流转点线程启动完毕!"); //启动下件点线程1 OutWarePoint out1 = new OutWarePoint(this._host, plc, "K07"); out1.StartPoint(); Console.WriteLine("K07下件点线程启动完毕!"); //启动下件点线程2 OutWarePoint out2 = new OutWarePoint(this._host, plc, "K02"); out2.StartPoint(); Console.WriteLine("K02下件点线程启动完毕!"); //启动入库点线程 //启动出库点线程 //启动出库线程 //报警监控线程 } } catch (Exception ex) { //Console.WriteLine("PLC连接失败,重新连接"); LogManager.Info($"程序启动失败 >>> {ex.Message}"); return; } } } } catch (Exception ex) { LogManager.Error(ex); } } /// /// 加载点位 /// /// /// public List GetNodeSettingList(DefaultDbContext defaultDbContext) { try { var listSiteNode = defaultDbContext.BaseSitenode.Where(t => t.isDelete == 0).ToList(); var listPlcPoint = defaultDbContext.BasePlcpoint.Where(t => t.isDelete == 0).ToList(); var resultList = (from siteNode in listSiteNode join plcPoint in listPlcPoint on siteNode.id equals plcPoint.sitenodeId select new NodeSetting { // 根据需要设置NodeSetting的属性值 id = (Guid)siteNode.id, siteNo = siteNode.siteNo, siteName = siteNode.siteName, siteTasktype = siteNode.siteTasktype, siteIpaddress = siteNode.siteIpaddress, siteServerport = siteNode.siteServerport, thriftPort = siteNode.thriftPort, isDelete = siteNode.isDelete, plcpointNo = plcPoint.plcpointNo, plcpointName = plcPoint.plcpointName, plcpointLength = plcPoint.plcpointLength, plcpointAddress = plcPoint.plcpointAddress, plcpointEquipmentId = plcPoint.plcpointEquipmentId, plcpointEquipmentNo = plcPoint.plcpointEquipmentNo, plcpointEquipmentName = plcPoint.plcpointEquipmentName, plcpointType = plcPoint.plcpointType }).ToList(); if (resultList == null) { return null; } return resultList; } catch (Exception ex) { LogManager.Error(ex); return null; } } /// /// 电气写入点位高低位转换 /// /// 点位地址位长度 /// 写入数值 /// public static object getValue(string len, string value) { if (len == "2") { return Convert.ToInt16(value); } if (len == "4") { return Convert.ToInt32(value); } return 0; } } }