123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- using MECF.Framework.Common.Communications;
- using System.Collections.Generic;
- using System.Text;
- using Aitex.Core.RT.Event;
- namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.EndPoints.CytEndPoints
- {
- public abstract class CytEndPointHandler : HandlerBase
- {
- public CytEndPoint Device { get; }
- public byte _command;
- protected CytEndPointHandler(CytEndPoint device, byte command)
- : base(BuildMessage(command))
- {
- Device = device;
- _command = command;
- }
- protected CytEndPointHandler(CytEndPoint device, byte command, byte[] buffer)
- : base(buffer)
- {
- Device = device;
- _command = command;
- }
- private static byte[] BuildMessage(byte cmd)
- {
- var header = new PacketHeader() { token = 0x0101, channel = 0, command = cmd, length = 0 };
- var data = new byte[PacketHeader.Size];
- System.Buffer.BlockCopy(ByteStructConverter.Struct2Bytes(header), 0, data, 0, PacketHeader.Size);
- return data;
- }
- public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
- {
- transactionComplete = true;
- CytEndPointMessage response = msg as CytEndPointMessage;
- if (!response.IsComplete)
- return false;
- if (response.IsEvent)
- return false;
- if (response.Header.command != _command)
- return false;
- if (response.IsError)
- {
- if (EPDDefine.ErrorMap.ContainsKey(response.Header.errorcode))
- {
- Device.NoteError(EPDDefine.ErrorMap[response.Header.errorcode]);
- }
- else
- {
- Device.NoteError($"EPD Error {response.Header.errorcode:X}");
- }
- }
- return true;
- }
- }
- public class CytEventHandler : CytEndPointHandler
- {
- public CytEventHandler(CytEndPoint device)
- : base(device, (byte)EPDCommand.Event)
- {
- }
- public override bool HandleMessage(MessageBase msg, out bool handled)
- {
- handled = true;
- if (msg is CytEndPointMessage cytMsg && msg.IsEvent && cytMsg.DataReader.ReadInt(out int type) && cytMsg.DataReader.ReadInt64(out long ticket))
- {
- switch (type)
- {
- case 0x03:
- Device.NoteStart(ticket);
- break;
- case 0x04:
- Device.NoteDelayEnd(ticket);
- break;
- case 0x05:
- Device.NoteNormalizeStart(ticket);
- break;
- case 0x06:
- Device.NoteSatisfied(ticket);
- break;
- case 0x07:
- Device.NoteTrigger(ticket);
- break;
- case 0x08:
- Device.NoteStop(ticket);
- break;
- case 0x10:
- var dummy = new byte[16];
- cytMsg.DataReader.ReadBytes(dummy, 16);
- var tmp = new byte[256];
- if (cytMsg.DataReader.ReadBytes(tmp, 256))
- {
- var data = Encoding.UTF8.GetString(tmp);
- data = data.Substring(0, data.IndexOf('\0'));
- Device.NoteTrendValue(data);
- }
- break;
- case 0x11:
- var dummy11 = new byte[16];
- cytMsg.DataReader.ReadBytes(dummy11, 16);
- var tmp11 = new byte[256];
- if (cytMsg.DataReader.ReadBytes(tmp11, 256))
- {
- var data = Encoding.UTF8.GetString(tmp11);
- data = data.Substring(0, data.IndexOf('\0'));
- Device.NoteTrendValue(data);
- }
- break;
- }
- }
- return true;
- }
- }
- public class CytConnectHandler : CytEndPointHandler
- {
- public CytConnectHandler(CytEndPoint device)
- : base(device, (byte)EPDCommand.Connect)
- {
- }
- public override bool HandleMessage(MessageBase msg, out bool handled)
- {
- if (base.HandleMessage(msg, out handled))
- {
- Device.NoteIsConnected(true);
- return true;
- }
- return false;
- }
- }
- public class CytDisconnectHandler : CytEndPointHandler
- {
- public CytDisconnectHandler(CytEndPoint device)
- : base(device, (byte)EPDCommand.Disconnect)
- {
- }
- public override bool HandleMessage(MessageBase msg, out bool handled)
- {
- if (base.HandleMessage(msg, out handled))
- {
- Device.NoteIsConnected(false);
- return true;
- }
- return false;
- }
- }
- public class CytQueryStateHandler : CytEndPointHandler
- {
- public CytQueryStateHandler(CytEndPoint device)
- : base(device, (byte)EPDCommand.QueryState)
- {
- }
- public override bool HandleMessage(MessageBase msg, out bool handled)
- {
- if (base.HandleMessage(msg, out handled))
- {
- if (msg is CytEndPointMessage cytMsg && cytMsg.DataReader.ReadUInt16(out ushort state))
- {
- var result = state.ToString();
- if (EPDDefine.StateMap.ContainsKey(state))
- result = EPDDefine.StateMap[state];
- Device.NoteState(result);
- }
- return true;
- }
- return false;
- }
- }
- public class CytQueryModeHandler : CytEndPointHandler
- {
- public CytQueryModeHandler(CytEndPoint device)
- : base(device, (byte)EPDCommand.QueryOperateMode)
- {
- }
- public override bool HandleMessage(MessageBase msg, out bool handled)
- {
- if (base.HandleMessage(msg, out handled))
- {
- if (msg is CytEndPointMessage cytMsg && cytMsg.DataReader.ReadUInt16(out ushort mode))
- {
- var result = mode.ToString();
- if (EPDDefine.ModeMap.ContainsKey(mode))
- result = EPDDefine.ModeMap[mode];
- Device.NoteMode(result);
- }
- return true;
- }
- return false;
- }
- }
- public class CytSetModeHandler : CytEndPointHandler
- {
- public CytSetModeHandler(CytEndPoint device, bool isRemote)
- : base(device, (byte)EPDCommand.SetOperateMode, BuildMessage(isRemote))
- {
- }
- private static byte[] BuildMessage(bool isRemote)
- {
- byte mode = isRemote ? (byte)2 : (byte)1;
- var header = new PacketHeader() { token = 0x0101, channel = 0, command = (byte)EPDCommand.SetOperateMode, length = 4 };
- var data = new byte[PacketHeader.Size + 4];
- System.Buffer.BlockCopy(ByteStructConverter.Struct2Bytes(header), 0, data, 0, PacketHeader.Size);
- System.Buffer.BlockCopy(new byte[] { mode, 0, 0, 0 }, 0, data, PacketHeader.Size, 4);
- return data;
- }
- }
- public class CytQueryConfigListHandler : CytEndPointHandler
- {
- public CytQueryConfigListHandler(CytEndPoint device)
- : base(device, (byte)EPDCommand.QueryCfgList)
- {
- }
- public override bool HandleMessage(MessageBase msg, out bool handled)
- {
- if (base.HandleMessage(msg, out handled))
- {
- if (msg is CytEndPointMessage cytMsg && cytMsg.DataReader.ReadInt(out int count))
- {
- var cfgs = new List<string>();
- var tmp = new byte[256];
- for (int i = 0; i < count; i++)
- {
- if (cytMsg.DataReader.ReadBytes(tmp, 256))
- {
- var name = Encoding.UTF8.GetString(tmp);
- name = name.Substring(0, name.IndexOf('\0'));
- cfgs.Add(name);
- }
- }
- Device.NoteConfigList(cfgs);
- }
- return true;
- }
- return false;
- }
- }
- public class CytRecipeStartHandler : CytEndPointHandler
- {
- public CytRecipeStartHandler(CytEndPoint device)
- : base(device, (byte)EPDCommand.RecipeStart)
- {
- }
- }
- public class CytRecipeStopHandler : CytEndPointHandler
- {
- public CytRecipeStopHandler(CytEndPoint device)
- : base(device, (byte)EPDCommand.RecipeStop)
- {
- }
- }
- public class CytStartHandler : CytEndPointHandler
- {
- public CytStartHandler(CytEndPoint device, string configName)
- : base(device, (byte)EPDCommand.Start, BuildMessage(configName))
- {
- }
- private static byte[] BuildMessage(string configName)
- {
- var header = new PacketHeader() { token = 0x0101, channel = 0, command = (byte)EPDCommand.Start, length = 256 };
- var data = new byte[PacketHeader.Size + 256];
- System.Buffer.BlockCopy(ByteStructConverter.Struct2Bytes(header), 0, data, 0, PacketHeader.Size);
- char[] chars = configName.ToCharArray();
- Encoding.UTF8.GetBytes(chars, 0, chars.Length, data, PacketHeader.Size);
- return data;
- }
- }
- public class CytStopHandler : CytEndPointHandler
- {
- public CytStopHandler(CytEndPoint device)
- : base(device, (byte)EPDCommand.Stop)
- {
- }
- }
- public class CytQueryCurrentConfigHandler : CytEndPointHandler
- {
- public CytQueryCurrentConfigHandler(CytEndPoint device)
- : base(device, (byte)EPDCommand.QueryCurrentConfig)
- {
- }
- //“Name=FallValue;IntegrationTime=100,DelayTime=100;Regions:R357=156.2,R516=260.8;Trends:EP1=3586.9,EP2=1645.2”
- public override bool HandleMessage(MessageBase msg, out bool handled)
- {
- if (base.HandleMessage(msg, out handled))
- {
- if (msg is CytEndPointMessage cytMsg)
- {
- var tmp = new byte[256];
- if (cytMsg.DataReader.ReadBytes(tmp, 256))
- {
- var cfg = Encoding.UTF8.GetString(tmp);
- cfg = cfg.Substring(0, cfg.IndexOf('\0'));
- string[] items = cfg.Split(';');
- List<string> names = new List<string>();
- foreach (var item in items)
- {
- if (item.StartsWith("Regions:"))
- {
- string[] data = item.Substring("Regions:".Length).Split(',');
- names.AddRange(data);
- }
- else if (item.StartsWith("Trends:"))
- {
- string[] data = item.Substring("Trends:".Length).Split(',');
- names.AddRange(data);
- }
- else
- {
- string[] data = item.Split('=');
- if (data[0] == "Name")
- {
- Device.NoteCurrentConfigName(data[1]);
- }
- if (data[0] == "IntegrationTime")
- {
- //Device.NoteCurrentConfigIntegrationTime(int.Parse(data[1]));
- }
- }
- }
- Device.NoteTrendName(names);
- }
- }
- return true;
- }
- return false;
- }
- }
- }
|