天津投入产出系统后端
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.

786 lines
34 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMAPP.FJC.Entity.Basic;
using OPCPLC;
using QMAPP.FJC.DAL.Basic;
using QMAPP.FJC.Entity;
using System.Configuration;
using System.Threading;
using OPCAutomation;
using QMAPP.FJC.BLL.Process;
namespace EQUIPINTERFACETEST
{
public class SlushTest
{
//用来存储生产设备信息
public List<MachineInfo> machineList;
//用来存储设备加工参数信息
//key值样式为:设备号+模具号+读写状态+操作类别+数据库字段
public Dictionary<string, int> parameterValueDict;
private Dictionary<int, string> clientHandleDict = new Dictionary<int, string>();
private Dictionary<string, string> colunmConDict = new Dictionary<string, string>();
private int clientHandleValue = 0;
//加工参数列表静态变量
public List<ParameterConfig> paraConfigList;
//设备参数信息
public List<ParameterConfig> moldList;
//OPC零件信息列表静态变量
public Dictionary<string, OPCPLCAutomation> opcplcConnectionList;
public void InitParameter()
{
parameterValueDict = new Dictionary<string, int>();
moldList = new List<ParameterConfig>();
paraConfigList = new List<ParameterConfig>();
machineList = new List<MachineInfo>();
//获取数据库中所有的生产设备信息
opcplcConnectionList = new Dictionary<string, OPCPLCAutomation>();
try
{
#region 获取数据库中所有的生产设备信息
MachineInfoDAL machineDal = new MachineInfoDAL();
machineList = machineDal.GetList(new MachineInfo() { PROCESSTYPE = EnumGeter.ProcessType.tangsu.GetHashCode().ToString() });
#endregion
#region 获取所有的配置信息
ParameterConfigDAL paraConDal = new QMAPP.FJC.DAL.Basic.ParameterConfigDAL();
paraConfigList = paraConDal.GetList(new ParameterConfig() { MACHINECODDE = machineList[1].MACHINECODDE });
#endregion
#region 获取设备模块信息
//对加工参数配置信息按照设备及模具信息进行分组
var objlist = from pc in paraConfigList
group pc by new { pc.MACHINECODDE, pc.MOLDNUMBER } into g
select new ParameterConfig { MACHINECODDE = g.Key.MACHINECODDE, MOLDNUMBER = g.Key.MOLDNUMBER };
//添加到静态变量中
foreach (var ob in objlist)
{
moldList.Add(new ParameterConfig() { MACHINECODDE = ob.MACHINECODDE, MOLDNUMBER = ob.MOLDNUMBER });
}
//按照设备进行排序
moldList = moldList.OrderBy(o => o.MACHINECODDE).OrderBy(o => o.MOLDNUMBER).ToList<ParameterConfig>();
#endregion
#region 加载模块下的加工参数配置信息
//变量所有的模具信息
foreach (var m in moldList)
{
MachineInfo machineEntity = machineList.First(o => o.MACHINECODDE == m.MACHINECODDE);
#region 获取加工参数组
List<ParameterConfig> paraList = paraConfigList
.Where(o => o.MACHINECODDE == m.MACHINECODDE && o.MOLDNUMBER == m.MOLDNUMBER && o.COLUMNTYPE == "0")
.ToList<ParameterConfig>();
if (paraList.Count > 0)
{
OPCPLCAutomation gtxConnection = new OPCPLC.OPCPLCAutomation();
//opcServer服务器IP
string opcServerIP = ConfigurationManager.AppSettings.Get("OPCServerIP");
//opcServer服务名称
string opcServerName = ConfigurationManager.AppSettings.Get("OPCServerName");
gtxConnection.GetListOPCServers("127.0.0.1");
//连接Opc服务
gtxConnection.ConnectRemoteServer(opcServerName, "127.0.0.1");
gtxConnection.CreateGroup(m.MACHINECODDE + ":" + m.MOLDNUMBER + ":GetData");
//变该模块下的所有参数信息
foreach (var pc in paraList)
{
try
{
//初始化字典名称: 设备名称+模块编号+字段名称
string dicKeyStr = machineEntity.MACHINECODDE + ":" + m.MOLDNUMBER + ":" + pc.COLUMNCODE;
//初始化字段信息值为0
parameterValueDict.Add(dicKeyStr, 0);
//获取opc中tagName
string itemName = pc.CONNECTIONSTRING;
clientHandleValue++;
clientHandleDict.Add(clientHandleValue, dicKeyStr);
//将字典中的与PLC内存地址向绑定
parameterValueDict[dicKeyStr] = gtxConnection.AddKepItem(itemName, clientHandleValue);
colunmConDict.Add(dicKeyStr, m.MACHINECODDE + ":" + m.MOLDNUMBER + ":GetData");
}
catch (Exception ex)
{
throw;
}
}
//添加PLC连接信息
opcplcConnectionList.Add(m.MACHINECODDE + ":" + m.MOLDNUMBER + ":GetData", gtxConnection);
}
#endregion
#region 写入组
List<ParameterConfig> writeList = paraConfigList
.Where(o => o.MACHINECODDE == m.MACHINECODDE && o.MOLDNUMBER == m.MOLDNUMBER && o.COLUMNTYPE == "2")
.ToList<ParameterConfig>();
if (writeList.Count > 0)
{
OPCPLCAutomation gtxConnection = new OPCPLC.OPCPLCAutomation();
//opcServer服务器IP
string opcServerIP = ConfigurationManager.AppSettings.Get("OPCServerIP");
//opcServer服务名称
string opcServerName = ConfigurationManager.AppSettings.Get("OPCServerName");
gtxConnection.GetListOPCServers("127.0.0.1");
//连接Opc服务
gtxConnection.ConnectRemoteServer(opcServerName, "127.0.0.1");
gtxConnection.CreateGroup(m.MACHINECODDE + ":" + m.MOLDNUMBER + ":WriteData");
//变该模块下的所有参数信息
foreach (var pc in writeList)
{
//初始化字典名称: 设备名称+模块编号+字段名称
string dicKeyStr = machineEntity.MACHINECODDE + ":" + m.MOLDNUMBER + ":" + pc.COLUMNCODE;
//初始化字段信息值为0
parameterValueDict.Add(dicKeyStr, 0);
//获取opc中tagName
string itemName = pc.CONNECTIONSTRING;
clientHandleValue++;
clientHandleDict.Add(clientHandleValue, dicKeyStr);
//将字典中的与PLC内存地址向绑定
parameterValueDict[dicKeyStr] = gtxConnection.AddKepItem(itemName, clientHandleValue);
colunmConDict.Add(dicKeyStr, m.MACHINECODDE + ":" + m.MOLDNUMBER + ":WriteData");
}
//添加PLC连接信息
opcplcConnectionList.Add(m.MACHINECODDE + ":" + m.MOLDNUMBER + ":WriteData", gtxConnection);
}
#endregion
#region 监控组
//获取设备扫描条码完成标记和设备加工完成标记
List<ParameterConfig> monitorList = paraConfigList
.Where(o => o.MACHINECODDE == m.MACHINECODDE && o.MOLDNUMBER == m.MOLDNUMBER && (o.COLUMNTYPE == EQUIPINTERFACETESTCommon.COLUMNTYPE.EQUIPSCANFLAG.GetHashCode().ToString()
|| o.COLUMNTYPE == EQUIPINTERFACETESTCommon.COLUMNTYPE.COMPLETEFLAG.GetHashCode().ToString()))
.ToList<ParameterConfig>();
if (monitorList.Count > 0)
{
OPCPLCAutomation gtxConnection = new OPCPLC.OPCPLCAutomation();
//opcServer服务器IP
string opcServerIP = ConfigurationManager.AppSettings.Get("OPCServerIP");
//opcServer服务名称
string opcServerName = ConfigurationManager.AppSettings.Get("OPCServerName");
gtxConnection.GetListOPCServers("127.0.0.1");
//连接Opc服务
gtxConnection.ConnectRemoteServer(opcServerName, "127.0.0.1");
gtxConnection.CreateGroup(m.MACHINECODDE + ":" + m.MOLDNUMBER + ":Monitor");
//变该模块下的所有参数信息
foreach (var pc in monitorList)
{
//初始化字典名称: 设备名称+模块编号+字段名称
string dicKeyStr = machineEntity.MACHINECODDE + ":" + m.MOLDNUMBER + ":" + pc.COLUMNCODE;
//初始化字段信息值为0
parameterValueDict.Add(dicKeyStr, 0);
//获取opc中tagName
string itemName = pc.CONNECTIONSTRING;
clientHandleValue++;
clientHandleDict.Add(clientHandleValue, dicKeyStr);
//将字典中的与PLC内存地址向绑定
parameterValueDict[dicKeyStr] = gtxConnection.AddKepItem(itemName, clientHandleValue);
colunmConDict.Add(dicKeyStr, m.MACHINECODDE + ":" + m.MOLDNUMBER + ":Monitor");
}
gtxConnection.KepGroups.DefaultGroupIsActive = true;//激活组。
gtxConnection.KepGroups.DefaultGroupDeadband = 0;// 死区值,设为0时,服务器端该组内任何数据变化都通知组。
gtxConnection.KepGroups.DefaultGroupUpdateRate = 200;//默认组群的刷新频率为200ms
gtxConnection.KepGroup.UpdateRate = 200;//刷新频率为1秒。
gtxConnection.KepGroup.IsSubscribed = true;//使用订阅功能,即可以异步,默认false
gtxConnection.KepGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange);
//添加PLC连接信息
opcplcConnectionList.Add(m.MACHINECODDE + ":" + m.MOLDNUMBER + ":Monitor", gtxConnection);
}
#endregion
#region 公共参数组
List<ParameterConfig> commonPara = paraConfigList
.Where(o => o.MACHINECODDE == m.MACHINECODDE && o.MOLDNUMBER == m.MOLDNUMBER && o.COLUMNTYPE=="5" )
.ToList<ParameterConfig>();
if (commonPara.Count > 0)
{
OPCPLCAutomation gtxConnection = new OPCPLC.OPCPLCAutomation();
//opcServer服务器IP
string opcServerIP = ConfigurationManager.AppSettings.Get("OPCServerIP");
//opcServer服务名称
string opcServerName = ConfigurationManager.AppSettings.Get("OPCServerName");
gtxConnection.GetListOPCServers("127.0.0.1");
//连接Opc服务
gtxConnection.ConnectRemoteServer(opcServerName, "127.0.0.1");
gtxConnection.CreateGroup(m.MACHINECODDE + ":" + m.MOLDNUMBER + ":CommonPara");
//变该模块下的所有参数信息
foreach (var pc in commonPara)
{
try
{
//初始化字典名称: 设备名称+模块编号+字段名称
string dicKeyStr = machineEntity.MACHINECODDE + ":" + m.MOLDNUMBER + ":" + pc.COLUMNCODE;
//初始化字段信息值为0
parameterValueDict.Add(dicKeyStr, 0);
//获取opc中tagName
string itemName = pc.CONNECTIONSTRING;
clientHandleValue++;
clientHandleDict.Add(clientHandleValue, dicKeyStr);
//将字典中的与PLC内存地址向绑定
parameterValueDict[dicKeyStr] = gtxConnection.AddKepItem(itemName, clientHandleValue);
colunmConDict.Add(dicKeyStr, m.MACHINECODDE + ":" + m.MOLDNUMBER + ":CommonPara");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw;
}
}
//添加PLC连接信息
opcplcConnectionList.Add(m.MACHINECODDE + ":" + m.MOLDNUMBER + ":CommonPara", gtxConnection);
}
#endregion
}
#endregion
}
catch (Exception ex)
{
}
}
private void KepGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
{
try
{
//延迟一秒
Thread.Sleep(1000);
//为了测试,所以加了控制台的输出,来查看事物ID号
//Console.WriteLine("********"+TransactionID.ToString()+"*********");
for (int i = 1; i <= NumItems; i++)
{
//获取写入的值
var itemValue = ItemValues.GetValue(i);
//获取客户端句柄
int chvalue = Convert.ToInt32(ClientHandles.GetValue(i));
//获取服务端字典key值
string dicKeyStr = clientHandleDict[chvalue];
//获取设备信息
string machCode = dicKeyStr.Substring(0, dicKeyStr.IndexOf(':'));
string moldAndColumn = dicKeyStr.Substring(dicKeyStr.IndexOf(':') + 1);
//获取模块信息
string moldNumber = moldAndColumn.Substring(0, moldAndColumn.IndexOf(':'));
//获取字段名称
string columnCode = dicKeyStr.Substring(dicKeyStr.LastIndexOf(':') + 1);
ParameterConfig currentPC = paraConfigList.First(o => o.MACHINECODDE == machCode && o.MOLDNUMBER == moldNumber && o.COLUMNCODE == columnCode);
//完成标记
if (currentPC.COLUMNTYPE == OpcHostEnumGeter.COLUMNTYPE.COMPLETEFLAG.GetHashCode().ToString())
{
List<ParameterConfig> lastList = new List<ParameterConfig>();
if (itemValue == null)
{
Console.WriteLine(currentPC.COLUMNCODE.ToString() + "值为null");
WriteLog.Write(currentPC.COLUMNCODE.ToString() + "值为null");
continue;
}
//如果完成标记表示更新了加工参数
//获取所有的加工参数
if ((Boolean)itemValue == true)
{
string year = "";
string month = "";
string day = "";
string hour = "";
string minute="";
string second = "";
string machineno = "";
string producttype = "";
string hudorbasic = "";
string color = "";
string cavitytype = "";
string mouldcarrier="";
Console.WriteLine(System.DateTime.Now.ToLongTimeString() + " " + dicKeyStr + "完成标记修改为true");
List<ParameterConfig> paraList = paraConfigList
.Where(o =>
o.MACHINECODDE == machCode
&& o.MOLDNUMBER == moldNumber
&& (
o.COLUMNTYPE == OpcHostEnumGeter.COLUMNTYPE.PARAMETER.GetHashCode().ToString()
)
)
.ToList<ParameterConfig>();
Console.WriteLine(System.DateTime.Now.ToLongTimeString() + " " + "开始读取加工参数");
WriteLog.Write(System.DateTime.Now.ToLongTimeString() + " " + "开始读取加工参数");
foreach (var parameter in paraList)
{
try
{
string item = parameter.MACHINECODDE + ":" + parameter.MOLDNUMBER + ":" + parameter.COLUMNCODE;
string conListKey = colunmConDict[item];
OPCPLCAutomation con = opcplcConnectionList[conListKey];
var result = con.ReadtagValue(parameterValueDict[item]);
ParameterConfig paraConfig = new ParameterConfig();
#region 属性复制
paraConfig.COLUMNCODE = parameter.COLUMNCODE;
paraConfig.COLUMNTYPE = parameter.COLUMNTYPE;
paraConfig.DATATYPE = parameter.DATATYPE;
paraConfig.DBNUMBER = parameter.DBNUMBER;
paraConfig.DEALTYPE = parameter.DEALTYPE;
paraConfig.MACHINECODDE = parameter.MACHINECODDE;
paraConfig.MOLDNUMBER = parameter.MOLDNUMBER;
paraConfig.OPERATETYPE = parameter.OPERATETYPE;
paraConfig.PROCESSTYPE = parameter.PROCESSTYPE;
#endregion
paraConfig.PARAVALUE = result;
lastList.Add(paraConfig);
if (result != null)
{
if (item.IndexOf("YEAR") > 0
|| item.IndexOf("MONTH") > 0
|| item.IndexOf("DAY") > 0
|| item.IndexOf("HOUR") > 0
|| item.IndexOf("MINUTE") > 0
|| item.IndexOf("SECOND") > 0
|| item.IndexOf("SECOND") > 0)
{
Console.WriteLine(item.ToString() + "值为"
+ ConvertClass.DecToHex(result.ToString()));
WriteLog.Write(item.ToString() + "值为"
+ ConvertClass.DecToHex(result.ToString()));
#region 拼接表皮号
if (item.IndexOf("YEAR") > 0)
{
year = ConvertClass.DecToHex(result.ToString());
}
if (item.IndexOf("MONTH") > 0)
{
month = ConvertClass.DecToHex(result.ToString());
}
if (item.IndexOf("DAY") > 0)
{
day = ConvertClass.DecToHex(result.ToString());
}
if (item.IndexOf("HOUR") > 0)
{
hour = ConvertClass.DecToHex(result.ToString());
}
if (item.IndexOf("MINUTE") > 0)
{
minute = ConvertClass.DecToHex(result.ToString());
}
if (item.IndexOf("SECOND") > 0 && item.IndexOf("MILLISECOND") == 0)
{
second = ConvertClass.DecToHex(result.ToString());
}
#endregion
}
else
{
Console.WriteLine(item.ToString() + "值为" + result.ToString());
WriteLog.Write(item.ToString() + "值为" + result.ToString());
#region 拼接表皮号
if (item.IndexOf("MACHINENO") > 0)
{
machineno = result.ToString();
}
if (item.IndexOf("PRODUCT") > 0)
{
producttype = result.ToString();
}
if (item.IndexOf("HUDORBASIC") > 0)
{
hudorbasic = result.ToString();
}
if (item.IndexOf("SLUSHCOLOR") > 0)
{
color = result.ToString();
}
if (item.IndexOf("CAVITYTYPE") > 0)
{
cavitytype = result.ToString();
}
if (item.IndexOf("MOULDCARRIER") > 0)
{
mouldcarrier = result.ToString();
}
#endregion
}
}
else
{
Console.WriteLine(parameter.COLUMNCODE + ":");
WriteLog.Write(item.ToString() + ":");
}
}
catch (Exception)
{
Console.WriteLine("获取" + parameter.COLUMNCODE + "值异常");
WriteLog.Write("获取" + parameter.COLUMNCODE + "值异常");
continue;
}
}
StringBuilder slushCode1 = new StringBuilder();
StringBuilder slushCode2 = new StringBuilder();
slushCode1.Append(year.PadLeft(2, '0'));
slushCode2.Append(year.PadLeft(2, '0'));
slushCode1.Append(month.PadLeft(2, '0'));
slushCode2.Append(month.PadLeft(2, '0'));
slushCode1.Append(day.PadLeft(2, '0'));
slushCode2.Append(day.PadLeft(2, '0'));
slushCode1.Append(hour.PadLeft(2, '0'));
slushCode2.Append(hour.PadLeft(2, '0'));
slushCode1.Append(minute.PadLeft(2, '0'));
slushCode2.Append(minute.PadLeft(2, '0'));
slushCode1.Append(machineno);
slushCode2.Append(machineno);
slushCode1.Append(producttype);
slushCode2.Append(producttype);
List<string> hbList = hudorbasic.Split('/').ToList<string>();
slushCode1.Append(hbList[0]);
slushCode2.Append(hbList[1]);
slushCode1.Append(color);
slushCode2.Append(color);
List<string> ctList = cavitytype.Split('/').ToList<string>();
slushCode1.Append(ctList[0]);
slushCode2.Append(ctList[1]);
mouldcarrier = "2/2";
List<string> mcList = mouldcarrier.Split('/').ToList<string>();
slushCode1.Append(mcList[0].PadLeft(2, '0'));
slushCode2.Append(mcList[1].PadLeft(2, '0'));
Console.WriteLine("表皮条码1:" + slushCode1.ToString());
Console.WriteLine("表皮条码2:" + slushCode2.ToString());
WriteLog.Write("表皮条码1:" + slushCode1.ToString());
WriteLog.Write("表皮条码2:" + slushCode2.ToString());
#region slushparameter
List<ParameterConfig> commonParaList = paraConfigList
.Where(o => o.MACHINECODDE == machCode && o.COLUMNTYPE == "5")
.OrderBy(o => o.COLUMNCODE)
.ToList<ParameterConfig>();
if (commonParaList.Count > 0)
{
foreach (var parameter in commonParaList)
{
try
{
string item = parameter.MACHINECODDE + ":" + "0" + ":" + parameter.COLUMNCODE;
string conListKey = colunmConDict[item];
OPCPLCAutomation con = opcplcConnectionList[conListKey];
var result = con.ReadtagValue(parameterValueDict[item]);
if (result != null)
{
Console.WriteLine(item.ToString() + "值为" + result.ToString());
WriteLog.Write(item.ToString() + "值为" + result.ToString());
}
else
{
Console.WriteLine(parameter.COLUMNCODE+ ":");
WriteLog.Write(item.ToString() + ":");
}
ParameterConfig paraConfig = new ParameterConfig();
#region 属性复制
paraConfig.COLUMNCODE = parameter.COLUMNCODE;
paraConfig.COLUMNTYPE = parameter.COLUMNTYPE;
paraConfig.DATATYPE = parameter.DATATYPE;
paraConfig.DBNUMBER = parameter.DBNUMBER;
paraConfig.DEALTYPE = parameter.DEALTYPE;
paraConfig.MACHINECODDE = parameter.MACHINECODDE;
paraConfig.MOLDNUMBER = parameter.MOLDNUMBER;
paraConfig.OPERATETYPE = parameter.OPERATETYPE;
paraConfig.PROCESSTYPE = parameter.PROCESSTYPE;
#endregion
paraConfig.PARAVALUE = result;
lastList.Add(paraConfig);
}
catch (Exception ex)
{
Console.WriteLine("获取" + parameter.COLUMNCODE + "值异常");
WriteLog.Write("获取" + parameter.COLUMNCODE + "值异常");
continue;
}
}
}
#endregion
Console.WriteLine("---------读取加工参数完成---------");
WriteLog.Write("---------读取加工参数完成---------");
SlushProcess process = new SlushProcess();
List<string> barCodeList = process.Process(machineList[0], lastList);
//BarCodePrintClient client = new BarCodePrintClient();
//foreach (var barcode in barCodeList)
//{
// client.Print(barcode + ",*" + barcode + "*", "TS");
//}
}
else
{
Console.WriteLine(System.DateTime.Now.ToLongTimeString() + " " + dicKeyStr + "完成标记修改为false");
WriteLog.Write(System.DateTime.Now.ToLongTimeString() + " " + dicKeyStr + "完成标记修改为false");
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("系统出现异常:" + ex.Message);
}
}
public string change(string code)
{
string a = "";
switch (code)
{
case "TS1:1:T1":
a = "DB700.8.0 ";
break;
case "TS1:1:T2":
a = "DB700.11.0 ";
break;
case "TS1:1:T3":
a = "DB700.0.0";
break;
case "TS1:1:T4":
a = "DB700.12.0 ";
break;
case "TS1:1:T5":
a = "DB700.14.0 ";
break;
case "TS1:1:T6":
a = "DB700.18.0 ";
break;
}
return a;
}
public void test2()
{
string item = "TS1:0:PorductOK";
string conListKey = colunmConDict[item];
OPCPLCAutomation con = opcplcConnectionList[conListKey];
var result = con.ReadtagValue(parameterValueDict[item]);
if (result != null)
{
Console.WriteLine(item.ToString() + "值为" + result.ToString());
}
else
{
Console.WriteLine(item.ToString() + ":");
}
}
public void Test()
{
List<ParameterConfig> paraList = paraConfigList
.Where(o=>o.COLUMNCODE.Contains("MOULDCARRIER")).ToList<ParameterConfig>();
while (true)
{
Thread.Sleep(5000);
Console.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
foreach (var parameter in paraList)
{
string item = parameter.MACHINECODDE + ":" + parameter.MOLDNUMBER + ":" + parameter.COLUMNCODE;
string conListKey = colunmConDict[item];
OPCPLCAutomation con = opcplcConnectionList[conListKey];
int fff = parameterValueDict[item];
var result = con.ReadtagValue(fff);
if (result != null)
{
string a = result.ToString().Trim();
Console.WriteLine(item.ToString() + "值为" + result.ToString().Trim());
}
else
{
Console.WriteLine(item.ToString() + ":");
}
}
}
}
}
}