1、客户端结构
客户端主界面Load方法从配置文件中获取产线编号,使用LineBuilder类静态方法Build创建单例引擎,初始化引擎,
引擎中创建客户端到服务端连接,包含mesService 和opcService,其中opcService是双向通讯。
LineBuilder类接着便初始化产线站点,站点属性,站点脚本。
LineBuilder类
public static class LineBuilder
{
//1)创建产线
//2)创建各个站点
//3)创建站点属性
//4)创建站点脚本
//5)创建站点脚本规则
//6)装配好产线、站点、属性、脚本
public static Engine engine;
//private static List<Property> properties = new List<Property>();
//private static List<Station> allStations = new List<Station>();
public static Line Build(int line_id)
{
DateTime now = DateTime.Now;
engine = Engine.CreateInstance();
engine.Init();
mes_ent_info ent = engine.conmetMesService.GetEntInfoByIdName(line_id, null, null);
Line line2 = new Line(Guid.NewGuid().ToString(), ent.ent_name, ent.ent_description);
line2.LineId = ent.ent_id;
line2.Continer = engine;
engine.Line = line2;
buildAllStation(line2);
//var op20Cup = buildStation("192.168.1.20", Guid.NewGuid().ToString(), "AssyL2OP20CUP", "OP20CUP", line2);
//buildProperty("stp_partNum1", "AssyL2OP20CUP.AssyL2OP20CUP.stp_partNum1", op20Cup, TagType.Trigger, DataType.String);
//var op20Cup_stp_partSerial1Prop = buildProperty("stp_partSerial1", "AssyL2OP20CUP.AssyL2OP20CUP.stp_partSerial1", op20Cup, TagType.Trigger, DataType.String);
//var op20Cup_PartStatus1Prop = buildProperty("stp_partStatus1", "AssyL2OP20CUP.AssyL2OP20CUP.stp_partStatus1", op20Cup, TagType.Trigger, DataType.Int);
//buildProperty("stp_prod1Arrive", "AssyL2OP20CUP.AssyL2OP20CUP.stp_prod1Arrive", op20Cup, TagType.Trigger, DataType.Bool);
//buildProperty("stp_prod1Start", "AssyL2OP20CUP.AssyL2OP20CUP.stp_prod1Start", op20Cup, TagType.Trigger, DataType.Bool);
//buildProperty("stp_prod1Finish", "AssyL2OP20CUP.AssyL2OP20CUP.stp_prod1Finish", op20Cup, TagType.Trigger, DataType.Bool);
//buildProperty("stp_prod1Leave", "AssyL2OP20CUP.AssyL2OP20CUP.stp_prod1Start", op20Cup, TagType.Trigger, DataType.Bool);
//buildProperty("stp_workOrder1", "AssyL2OP20CUP.AssyL2OP20CUP.stp_workOrder1", op20Cup, TagType.Trigger, DataType.String);
//buildProperty("stp_upFinalPre", "AssyL2OP20CUP.AssyL2OP20CUP.stp_upFinalPre", op20Cup, TagType.Normal, DataType.Float);
//buildProperty("rec_downPreHeadDwelltime", "AssyL2OP20CUP.AssyL2OP20CUP.rec_downPreHeadDwelltime", op20Cup, TagType.Normal, DataType.Float);
//buildProperty("rec_downPreHeadPre", "AssyL2OP20CUP.AssyL2OP20CUP.rec_downPreHeadPre", op20Cup, TagType.Normal, DataType.Float);
//buildProperty("rec_downPreHeadPreHiLimit", "AssyL2OP20CUP.AssyL2OP20CUP.rec_downPreHeadPreHiLimit", op20Cup, TagType.Normal, DataType.Float);
//buildProperty("rec_downPreHeadPreLoLimit", "AssyL2OP20CUP.AssyL2OP20CUP.rec_downPreHeadPreLoLimit", op20Cup, TagType.Normal, DataType.Float);
//buildProperty("rec_upPreHeadPre", "AssyL2OP20CUP.AssyL2OP20CUP.rec_upPreHeadPre", op20Cup, TagType.Normal, DataType.Float);
//buildProperty("rec_upPreHeadPreHiLimit", "AssyL2OP20CUP.AssyL2OP20CUP.rec_upPreHeadPreHiLimit", op20Cup, TagType.Normal, DataType.Float);
//buildProperty("rec_upPreHeadPreLoLimit", "AssyL2OP20CUP.AssyL2OP20CUP.rec_upPreHeadPreLoLimit", op20Cup, TagType.Normal, DataType.Float);
//buildProperty("stp_downFinalPre", "AssyL2OP20CUP.AssyL2OP20CUP.stp_downFinalPre", op20Cup, TagType.Normal, DataType.Float);
//Script_partSerialChange op20partSerialChange = new Script_partSerialChange(op20Cup);
//Script_stp_partStatus1Change op20partStatus1Change = new Script_stp_partStatus1Change(op20Cup);
//Script_onTrueTest op20OnTrueTest = new Script_onTrueTest(op20Cup);
//Script_WhileTrueTest op20WhileTrueTest = new Script_WhileTrueTest(op20Cup);
//op20Cup.Scripts.AddRange(new List<Script>() { op20partSerialChange, op20partStatus1Change, op20OnTrueTest, op20WhileTrueTest });
//line2.Stations.AddRange(new List<Station>() { op20Cup });
return null;
}
private static Station buildStation(string ip, string sid, string name, string des, Line line)
{
var station = new Station(sid, name, des);
station.Adress = ip;
station.Continer = line;
station.CurrState = State.Idle;
station.Enable = true;
return station;
}
private static void buildAllStation(Line line)
{
List<mes_ent_info> entList = engine.conmetMesService.GetEntInfoByParentId(line.LineId);
foreach (var ent in entList)
{
var station = buildStation("", ent.ent_id.ToString(), ent.ent_name, ent.ent_description, line);
//allStations.Add(station);
line.Stations.Add(station);
buildAllPropertys(station);
}
if (System.IO.File.Exists("Conmet.MES.Scripts.dll") == false)
{
engine.messageQueue.Enqueue($"Error:Class:LineBuilder,Method:buildAllStation-buildAllScript,Param:[{line.Name}],Msg:Not Find 'Conmet.MES.Scripts.dll'");
return;
}
Assembly asm = Assembly.Load("Conmet.MES.Scripts");//.GetExecutingAssembly();
foreach (var station in line.Stations)
{
buildAllScript(asm, station);
}
}
public static void buildAllScript(Assembly asm, Station station)
{
var ent_scripts = engine.conmetMesService.GetEntScriptByEntId(int.Parse(station.SID), null, 1, null, 1);
foreach(var script in ent_scripts)
{
//engine.messageQueue.Enqueue($"Info:Class:LineBuilder,Method:buildAllScript,NeedAdd:{ script.class_path}");
object[] parameters = new object[1];
parameters[0] = station;
Object obj = asm.CreateInstance(script.class_path, true, BindingFlags.Default, null, parameters, null, null);
if(obj is Script)
{
Script sc = obj as Script;
sc.Name = script.name;
station.Scripts.Add(sc);
engine.messageQueue.Enqueue($"Info:Class:LineBuilder,Method:buildAllScript,Add:{ sc.Name}");
}
//script.class_path;
}
}
public static List<Property> buildAllPropertys(Station station)
{
var ent_propertys = engine.conmetMesService.GetPropertys(int.Parse(station.SID), null, null, 1);
var ent_tags = engine.opcservice.GetOpcTagByEntAndNo(int.Parse(station.SID), null);
List<Property> properties = new List<Property>();
foreach(var property in ent_propertys)
{
string adress = "";
TagType tagType = 0;
if(property.is_tag > 0)
{
mes_opc_tag tag = ent_tags.Where(s => s.property_id == property.id).FirstOrDefault();
if(tag != null)
{
adress = tag.tag_adress;
tagType = (tag.attribute == "1" ? TagType.Trigger : TagType.Normal);
}
else
{
engine.messageQueue.Enqueue($"Error:Class:LineBuilder,Method:buildAllPropertys,Param:{property.property_name},Msg:Not Find Match OpcTag");
}
}
//DataType dataType;
var pro = buildProperty(property.property_name, adress, station, tagType, (DataType)property.data_type);
properties.Add(pro);
}
return properties;
}
private static Property buildProperty(string propertyName, string adress, Station station, TagType tagType, DataType dataType)
{
Property prop = new Property();
prop.CurrDataType = dataType;
prop.Container = station;
prop.Name = propertyName;
prop.OldValue = "";
prop.Value = "";
OpcTag tag = new OpcTag(Guid.NewGuid().ToString(), propertyName, "", adress, prop, true, tagType);
prop.Tag = tag;
station.Properties.Add(prop);
return prop;
}
}
标签:AssyL2OP20CUP,之二,station,stp,数采,OPC,ent,op20Cup,TagType
From: https://blog.51cto.com/kenji/7684517