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

604 lines
21 KiB

using HslCommunication.Profinet.Siemens;
using Leit.FrameWork;
using Leit.FrameWork.Log;
using Leit.FrameWork.Reflection;
using Leit.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Leit.StationFunction
{
public partial class ServiceData
{
static ServiceData()
{
// Leit.Model.
UpperCommunication.InitCommunication();
}
/// <summary>
/// 产品基础信息
/// </summary>
public static List<ProductInfo> ProductInfos { get; set; }
/// <summary>
/// ODS信息
/// </summary>
public static List<StepInfo> StepInfos { get; set; }
/// <summary>
/// 所有工位标签信息
/// </summary>
public static List<StationTagInfo> StationTagInfos { get; set; }
/// <summary>
/// 工位信息
/// </summary>
public static List<StationInfo> StationInfos { get; set; }
/// <summary>
/// 产线DB信息
/// </summary>
public static List<DeviceServerInfo> DeviceServerInfos { get; set; }
public static List<DbTag> DbTags { get; set; }
public static List<OpcTag> OpcTags { get; set; }
/// <summary>
/// PLC服务
/// </summary>
public static SiemensS7Net PlcServer;
/// <summary>
/// PLC服务是否停止
/// </summary>
public static bool PlcStop { get; set; }
static event Action LogicRunTimer;
static event Action PlcReadRunTimer;
/// <summary>
/// 初始化服务数据
/// </summary>
public static void ServiceDataInit()
{
try
{
ProductInfos = ProductInfo.QueryAll();
StepInfos = StepInfo.QueryAll();
StationTagInfos = StationTagInfo.QueryAll();
DeviceServerInfos = DeviceServerInfo.QueryAll();
StationInfos = StationInfo.QueryAll();
OpcTags = new List<OpcTag>();
DbTags = new List<DbTag>();
DbTag dbTag;
foreach (var dbItem in DeviceServerInfos)
{
dbTag = new DbTag();
dbTag.AreaId = dbItem.AreaId;
dbTag.StartByteIndex = dbItem.StartByteIndex;
dbTag.Length = Convert.ToUInt16(dbItem.ReadLength);
dbTag.OpcTags = new List<OpcTag>();
OpcTag opcTag;
//创建OPCTag
foreach (var item in StationTagInfos.Where(p => p.AreaId == dbItem.AreaId))
{
opcTag = new OpcTag();
opcTag.TagId = item.TagId;
opcTag.StartByteIndex = item.TagByteId;
opcTag.Length = Convert.ToUInt16(item.TagDataLong);
dbTag.OpcTags.Add(opcTag);
OpcTags.Add(opcTag);
}
DbTags.Add(dbTag);
}
}
catch (Exception ex)
{
LogHelper.WriteLog(LogHelper.GetMethodInfo(), ex);
}
}
/// <summary>
/// 初始化PLC服务
/// </summary>
public static void PlcServiceInit()
{
PlcReadRunTimer += CycleReadPlcData;
PlcStop = false;
Task.Run(()=> {
while(!PlcStop)
{
try
{
if (PlcServer == null)
{
PlcServer = new SiemensS7Net(SiemensPLCS.S1500, UserConfiguration.PlcComIpAddress);
PlcServer.ConnectTimeOut = 5000;
PlcServer.ConnectServer();
}
else
{
//每5S读取指定DB 如果读取失败则 重新连接
if (!PlcServer.ReadBool("M0.0").IsSuccess)
{
PlcServer.ConnectClose();
PlcServer.Dispose();
PlcServer = null;
}
else
{
Thread.Sleep(5000);
}
}
}
catch (Exception ex)
{
LogHelper.WriteLog(LogHelper.GetMethodInfo(), ex);
}
}
});
}
public static void WorkStationInit()
{
try
{
LogicRunTimer += Function_OP20.CycleRunTimer;
Function_OP20.StationInf = StationInfos.Where(p => p.StationCode == "OP20").First();
Function_OP20.StationTagInfos = StationTagInfos.Where(p => p.StationCode == "OP20").ToList();
Function_OP20.StationAlarmTagInfos = Function_OP20.StationTagInfos.Where(p => p.TagClass == 30).ToList();
Function_OP20.StationQualityTagInfos = Function_OP20.StationTagInfos.Where(p => p.TagClass == 30).ToList();
Function_OP20.StationCommunicatedTagInfos = Function_OP20.StationTagInfos.Where(p => p.TagClass == 10).ToList();
Function_OP20.WorkStepInfos = WorkStepInfo.Query().Where(p => p.StationCode == Function_OP20.StationInf.StationCode).ToList();
Function_OP20.ProductInfos = ProductInfo.QueryAll();
UpperCommunication.BarcodeDelegate += Function_OP20.BarCode;
LogicRunTimer += Function_OP40.CycleRunTimer;
Function_OP40.StationInf = StationInfos.Where(p => p.StationCode == "OP40").First();
Function_OP40.StationTagInfos = StationTagInfos.Where(p => p.StationCode == "OP40").ToList();
Function_OP40.StationAlarmTagInfos = Function_OP40.StationTagInfos.Where(p => p.TagClass == 30).ToList();
Function_OP40.StationQualityTagInfos = Function_OP40.StationTagInfos.Where(p => p.TagClass == 30).ToList();
Function_OP40.StationCommunicatedTagInfos = Function_OP40.StationTagInfos.Where(p => p.TagClass == 10).ToList();
LogicRunTimer += Function_OP50.CycleRunTimer;
Function_OP50.StationInf = StationInfos.Where(p => p.StationCode == "OP50").First();
Function_OP50.StationTagInfos = StationTagInfos.Where(p => p.StationCode == "OP50").ToList();
Function_OP50.StationAlarmTagInfos = Function_OP50.StationTagInfos.Where(p => p.TagClass == 30).ToList();
Function_OP50.StationQualityTagInfos = Function_OP50.StationTagInfos.Where(p => p.TagClass == 30).ToList();
Function_OP50.StationCommunicatedTagInfos = Function_OP50.StationTagInfos.Where(p => p.TagClass == 10).ToList();
Function_OP50.ProductInfos = ProductInfo.QueryAll();
UpperCommunication.BarcodeDelegate += Function_OP50.BarCode;
LogicRunTimer += Function_OP60.CycleRunTimer;
Function_OP60.StationInf = StationInfos.Where(p => p.StationCode == "OP60").First();
Function_OP60.StationTagInfos = StationTagInfos.Where(p => p.StationCode == "OP60").ToList();
Function_OP60.StationAlarmTagInfos = Function_OP60.StationTagInfos.Where(p => p.TagClass == 30).ToList();
Function_OP60.StationQualityTagInfos = Function_OP60.StationTagInfos.Where(p => p.TagClass == 30).ToList();
Function_OP60.StationCommunicatedTagInfos = Function_OP60.StationTagInfos.Where(p => p.TagClass == 10).ToList();
LogicRunTimer += Function_OP80.CycleRunTimer;
Function_OP80.StationInf = StationInfos.Where(p => p.StationCode == "OP80").First();
Function_OP80.StationTagInfos = StationTagInfos.Where(p => p.StationCode == "OP80").ToList();
Function_OP80.StationAlarmTagInfos = Function_OP80.StationTagInfos.Where(p => p.TagClass == 30).ToList();
Function_OP80.StationQualityTagInfos = Function_OP80.StationTagInfos.Where(p => p.TagClass == 30).ToList();
Function_OP80.StationCommunicatedTagInfos = Function_OP80.StationTagInfos.Where(p => p.TagClass == 10).ToList();
LogicRunTimer += Function_OP90.CycleRunTimer;
Function_OP90.StationInf = StationInfos.Where(p => p.StationCode == "OP90").First();
Function_OP90.StationTagInfos = StationTagInfos.Where(p => p.StationCode == "OP90").ToList();
Function_OP90.StationAlarmTagInfos = Function_OP90.StationTagInfos.Where(p => p.TagClass == 30).ToList();
Function_OP90.StationQualityTagInfos = Function_OP90.StationTagInfos.Where(p => p.TagClass == 30).ToList();
Function_OP90.StationCommunicatedTagInfos = Function_OP90.StationTagInfos.Where(p => p.TagClass == 10).ToList();
UpperCommunication.BarcodeDelegate += Function_OP90.BarcodeOk;
LogicRunTimer += Function_OP100.CycleRunTimer;
Function_OP100.StationInf = StationInfos.Where(p => p.StationCode == "OP100").First();
Function_OP100.StationTagInfos = StationTagInfos.Where(p => p.StationCode == "OP100").ToList();
Function_OP100.StationAlarmTagInfos = Function_OP100.StationTagInfos.Where(p => p.TagClass == 30).ToList();
Function_OP100.StationQualityTagInfos = Function_OP100.StationTagInfos.Where(p => p.TagClass == 30).ToList();
Function_OP100.StationCommunicatedTagInfos = Function_OP100.StationTagInfos.Where(p => p.TagClass == 10).ToList();
LogicRunTimer += HeartBeat.HeartBeatCycle;
}
catch (Exception ex)
{
LogHelper.WriteLog(LogHelper.GetMethodInfo(), ex);
}
}
public static void StationTimer()
{
Task.Run(() => {
while (true)
{
try
{
PlcReadRunTimer?.Invoke();
Thread.Sleep(500);
}
catch (Exception ex)
{
LogHelper.WriteLog(LogHelper.GetMethodInfo(), ex);
}
}
});
Thread.Sleep(2000);
Task.Run(() =>
{
while (true)
{
if (PlcInitOk)
{
try
{
LogicRunTimer?.Invoke();
Thread.Sleep(500);
}
catch (Exception ex)
{
LogHelper.WriteLog(LogHelper.GetMethodInfo(), ex);
}
}
}
});
}
public static void PrintOkLabel(int productTypeCode,int serialId)
{
DateTime dt = DateTime.Now;
//查询打印基础数据
var PrintDataInfo = ProductInfo.Query().Where(p => p.product_id == productTypeCode).First();
string admientPn = PrintDataInfo.yfas_rule;
string customerPn = PrintDataInfo.zy_rule + "/" + PrintDataInfo.dml_rule ;
string yearNum = dt.Year.ToString("0000");
string productionData = yearNum + dt.Month.ToString("00") + dt.Day.ToString("00");
string Shift = "N";
if (GetTimeSpan(dt))
{
Shift = "E";
}
string partVersion = PrintDataInfo.part_edition;
string q_Level = PrintDataInfo.quality_edition;
string sn = serialId.ToString("5000");
string yearCode = Print_YearCode.Query().Where(p => p.Year == yearNum).First().YearCode;
string qrCode = PrintDataInfo.zy_rule + "0000" + yearCode + GetMonthCode(dt) + dt.Day.ToString("00") + sn;
//创建打印实例数据
//创建 LabelHead
LabelHead labelHead = new LabelHead();
labelHead.PlantNr = 400;
labelHead.LabelId = "label." + DateTime.Now.Ticks.ToString();
labelHead.LabelTemplateId = UserConfiguration.LabelTemplateId;
labelHead.PrinterId = UserConfiguration.PrinterId;
labelHead.BusinessObjId = "";
labelHead.BusinessObjType = "";
labelHead.Status = 0;
labelHead.PrintQty = 1;
labelHead.LastUser = "ManuralPrint";
labelHead.CreateTime = DateTime.Now;
List<LabelDetail> labelDetails = new List<LabelDetail>();
LabelDetail labelDetail = new LabelDetail();
labelDetail.PlantNr = 400;
labelDetail.LabelId = labelHead.LabelId;
labelDetail.Pos = 1;
labelDetail.VariableName = "$Adient零件号$";
labelDetail.VariableValue = admientPn;
labelDetail.LastModify = DateTime.Now;
labelDetail.LastUser = "ManuralPrint";
labelDetail.CreateTime = DateTime.Now;
//LabelDetail.Insert(labelDetail, false);
labelDetails.Add(labelDetail);
labelDetail = new LabelDetail();
labelDetail.PlantNr = 400;
labelDetail.LabelId = labelHead.LabelId;
labelDetail.Pos = 2;
labelDetail.VariableName = "$客户零件号$";
labelDetail.VariableValue = customerPn;
labelDetail.LastModify = DateTime.Now;
labelDetail.LastUser = "ManuralPrint";
labelDetail.CreateTime = DateTime.Now;
labelDetails.Add(labelDetail);
labelDetail = new LabelDetail();
labelDetail.PlantNr = 400;
labelDetail.LabelId = labelHead.LabelId;
labelDetail.Pos = 3;
labelDetail.VariableName = "$零件名称$";
labelDetail.VariableValue = PrintDataInfo.product_english;
labelDetail.LastModify = DateTime.Now;
labelDetail.LastUser = "ManuralPrint";
labelDetail.CreateTime = DateTime.Now;
labelDetails.Add(labelDetail);
labelDetail = new LabelDetail();
labelDetail.PlantNr = 400;
labelDetail.LabelId = labelHead.LabelId;
labelDetail.Pos = 4;
labelDetail.VariableName = "$生产日期$";
labelDetail.VariableValue = productionData;
labelDetail.LastModify = DateTime.Now;
labelDetail.LastUser = "ManuralPrint";
labelDetail.CreateTime = DateTime.Now;
labelDetails.Add(labelDetail);
labelDetail = new LabelDetail();
labelDetail.PlantNr = 400;
labelDetail.LabelId = labelHead.LabelId;
labelDetail.Pos = 5;
labelDetail.VariableName = "$班次$";
labelDetail.VariableValue = Shift;
labelDetail.LastModify = DateTime.Now;
labelDetail.LastUser = "ManuralPrint";
labelDetail.CreateTime = DateTime.Now;
labelDetails.Add(labelDetail);
labelDetail = new LabelDetail();
labelDetail.PlantNr = 400;
labelDetail.LabelId = labelHead.LabelId;
labelDetail.Pos = 6;
labelDetail.VariableName = "$零件版本$";
labelDetail.VariableValue = partVersion;
labelDetail.LastModify = DateTime.Now;
labelDetail.LastUser = "ManuralPrint";
labelDetail.CreateTime = DateTime.Now;
labelDetails.Add(labelDetail);
labelDetail = new LabelDetail();
labelDetail.PlantNr = 400;
labelDetail.LabelId = labelHead.LabelId;
labelDetail.Pos = 7;
labelDetail.VariableName = "$质量版本$";
labelDetail.VariableValue = q_Level;
labelDetail.LastModify = DateTime.Now;
labelDetail.LastUser = "ManuralPrint";
labelDetail.CreateTime = DateTime.Now;
labelDetails.Add(labelDetail);
labelDetail = new LabelDetail();
labelDetail.PlantNr = 400;
labelDetail.LabelId = labelHead.LabelId;
labelDetail.Pos = 8;
labelDetail.VariableName = "$序列号$";
labelDetail.VariableValue = sn;
labelDetail.LastModify = DateTime.Now;
labelDetail.LastUser = "ManuralPrint";
labelDetail.CreateTime = DateTime.Now;
labelDetails.Add(labelDetail);
labelDetail = new LabelDetail();
labelDetail.PlantNr = 400;
labelDetail.LabelId = labelHead.LabelId;
labelDetail.Pos = 9;
labelDetail.VariableName = "$QR_Code$";
labelDetail.VariableValue = qrCode;
labelDetail.LastModify = DateTime.Now;
labelDetail.LastUser = "ManuralPrint";
labelDetail.CreateTime = DateTime.Now;
labelDetails.Add(labelDetail);
LabelDetail.Insert(labelDetails, false);
LabelHead.Insert(labelHead, false);
}
static string GetMonthCode(DateTime dt)
{
if (dt.Month < 9)
{
return dt.Month.ToString();
}
else if (dt.Month == 10)
{
return "A";
}
else if (dt.Month == 11)
{
return "B";
}
else if (dt.Month == 12)
{
return "C";
}
return "-1";
}
static bool GetTimeSpan(DateTime timeStr)
{
//判断当前时间是否在工作时间段内
string _strWorkingDayAM = "7:30";//工作时间上午08:30
string _strWorkingDayPM = "19:30";
TimeSpan dspWorkingDayAM = DateTime.Parse(_strWorkingDayAM).TimeOfDay;
TimeSpan dspWorkingDayPM = DateTime.Parse(_strWorkingDayPM).TimeOfDay;
//string time1 = "2017-2-17 8:10:00";
DateTime t1 = timeStr;
TimeSpan dspNow = t1.TimeOfDay;
if (dspNow > dspWorkingDayAM && dspNow < dspWorkingDayPM)
{
return true;
}
return false;
}
public static void PrintNGLabel(int productTypeCode, int serialId,string ngMsg)
{
//查询打印基础数据
var PrintDataInfo = ProductInfo.Query().Where(p => p.product_id == productTypeCode).First();
string settingRule = PrintDataInfo.zy_rule+ PrintDataInfo.sitting_rule;
string qrCode = settingRule + DateTime.Now.ToString("yyyyMMdd") + serialId.ToString("000");
//创建 LabelHead
LabelHead labelHead = new LabelHead();
labelHead.PlantNr = 400;
labelHead.LabelId = "label." + DateTime.Now.Ticks.ToString();
labelHead.LabelTemplateId = "X247_NG";
labelHead.PrinterId = UserConfiguration.PrinterId;
labelHead.BusinessObjId = "";
labelHead.BusinessObjType = "";
labelHead.Status = 0;
labelHead.PrintQty = 1;
labelHead.LastUser = "ManuralPrint";
labelHead.CreateTime = DateTime.Now;
List<LabelDetail> labelDetails = new List<LabelDetail>();
LabelDetail labelDetail = new LabelDetail();
labelDetail.PlantNr = 400;
labelDetail.LabelId = labelHead.LabelId;
labelDetail.Pos = 1;
labelDetail.VariableName = "$QR_Code$";
labelDetail.VariableValue = qrCode;
labelDetail.LastModify = DateTime.Now;
labelDetail.LastUser = "ManuralPrint";
labelDetail.CreateTime = DateTime.Now;
labelDetails.Add(labelDetail);
labelDetail = new LabelDetail();
labelDetail.PlantNr = 400;
labelDetail.LabelId = labelHead.LabelId;
labelDetail.Pos = 2;
labelDetail.VariableName = "$Show_Code$";
labelDetail.VariableValue = qrCode;
labelDetail.LastModify = DateTime.Now;
labelDetail.LastUser = "ManuralPrint";
labelDetail.CreateTime = DateTime.Now;
labelDetails.Add(labelDetail);
labelDetail = new LabelDetail();
labelDetail.PlantNr = 400;
labelDetail.LabelId = labelHead.LabelId;
labelDetail.Pos = 3;
labelDetail.VariableName = "$NG_Item$";
labelDetail.VariableValue = ngMsg;
labelDetail.LastModify = DateTime.Now;
labelDetail.LastUser = "ManuralPrint";
labelDetail.CreateTime = DateTime.Now;
labelDetails.Add(labelDetail);
LabelDetail.Insert(labelDetails, false);
LabelHead.Insert(labelHead, false);
}
}
}