You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
4.8 KiB
C#

2 years ago
using AngleSharp.Dom;
using Khd.Core.Domain.Dto.wcs;
using Khd.Core.Domain.Models;
using Khd.Core.EntityFramework;
using Khd.Core.Wcs.Global;
using Masuit.Tools.Logging;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Khd.Core.Domain.Dto.webapi;
using Khd.Core.Plc.S7;
using Khd.Core.Wcs.Wcs;
using System.Data;
using Microsoft.EntityFrameworkCore;
using Z.EntityFramework.Plus;
using Masuit.Tools;
using Microsoft.IdentityModel.Tokens;
namespace Khd.Core.Wcs.Wcs
{
/// <summary>
/// 根据出入库记录创建出入库任务
/// </summary>
public class CreateTaskByRecord
{
private readonly IHost _host;
private readonly Plc.S7.Plc _plc;
List<BasePlcpoint> ScanPoint { get; set; }//点位信息
BaseSitenode? sitenode { get; set; }//站台信息
NodeSetting? NodeSettingCarNo { get; set; }
NodeSetting? NodeSettingCarState { get; set; }
NodeSetting? NodeSettingCarRun { get; set; }
NodeSetting? NodeSettingWcsState { get; set; }
NodeSetting? NodeSettingWcsSend { get; set; }
NodeSetting? NodeSettingWcsSendMaterial { get; set; }
NodeSetting? NodeSettingPLCSendSendMaterialstate { get; set; }
Thread FlowPointThread;
public CreateTaskByRecord(IHost host, Plc.S7.Plc plc, string siteNo)
{
this._host = host;
this._plc = plc;
this.ScanPoint = StaticData.BasePlcpointList.ToList();//加载当前站点所对应的点位
this.sitenode = StaticData.SiteNodeList.FirstOrDefault(t => t.siteNo == siteNo);//获取当前站台信息
//this.NodeSettingCarNo = this.ScanPoint.FirstOrDefault(t => t.plcpointNo.Contains("carno"));
//this.NodeSettingCarState = this.ScanPoint.FirstOrDefault(t => t.plcpointNo.Contains("carstate"));
//this.NodeSettingCarRun = this.ScanPoint.FirstOrDefault(t => t.plcpointNo.Contains("carrun"));
//this.NodeSettingWcsState = this.ScanPoint.FirstOrDefault(t => t.plcpointNo.Contains("plcsendK"));
//this.NodeSettingWcsSend = this.ScanPoint.FirstOrDefault(t => t.plcpointNo.Contains("wcsend"));
//this.NodeSettingWcsSendMaterial = this.ScanPoint.FirstOrDefault(t => t.plcpointNo.Contains("wcssendmessage"));
//this.NodeSettingPLCSendSendMaterialstate = this.ScanPoint.FirstOrDefault(t => t.plcpointNo.Contains("plcsendmessage"));
try
{
//默认启动,清理plc的上位机写入点位值
//this._plc.Write(NodeSettingCarRun.plcpointAddress, MainCentralControl.QingKongDianWei);
//this._plc.Write(NodeSettingWcsSend.plcpointAddress, MainCentralControl.QingKongDianWei);
//this._plc.Write(NodeSettingWcsSendMaterial.plcpointAddress, MainCentralControl.QingKongDianWei);
}
catch (Exception ex)
{
Console.WriteLine("站点" + siteNo + " 初始化数据异常" + ex.Message);
LogManager.Error(ex);
}
}
/// <summary>
/// 启动上件扫描监听
/// </summary>
public void StartPoint()
{
FlowPointThread = new Thread(MonitorInLocatorPoint);
FlowPointThread.Start();
}
public void MonitorInLocatorPoint()
{
while (true)
{
try
{
CreateTask();
}
catch (Exception ex)
{
LogManager.Error(ex);
}
finally
{
Thread.Sleep(2000);
}
}
}
private void CreateTask()
{
try
{
using var scope = _host.Services.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
var rawInstock = dbContext.WcsTask.Where(t => t.customerNo == "1" ).ToList();
if (rawInstock.Count>0)
{
foreach (var item in rawInstock)
{
WcsTask wcsTask = new WcsTask();
//wcsTask.customerNo = item.palletInfoCode;
dbContext.Add(wcsTask);
dbContext.SaveChanges();
}
}
}
catch (Exception ex)
{
//LogManager.Info($"错误日志输出 >>> OutWarePoint类 WriteMaterialMessage 方法报错 {ex}");
LogManager.Error(ex);
}
}
}
}