using Aitex.Core.RT.Event; using Aitex.Core.RT.SCCore; using Aitex.Core.Util; using MECF.Framework.Common.Communications; using MECF.Framework.Common.Device.Bases; using System.Collections.Generic; namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.MFCs.HoribaMFC { public abstract class HoribaHandler : HandlerBase { private const byte Header = 0x40; private const byte STX = 0x02; private const byte ETX = 0x03; protected HoribaConnection _connection; public string Address { get; set; } protected HoribaHandler(HoribaConnection connection, string msdLsdAddress, string data) : base(BuildMessage(msdLsdAddress, data)) { Address = msdLsdAddress; _connection = connection; } private static byte[] BuildMessage(string msdLsdAddress, string data) { List message = new List(); message.Add(Header); PacketMessage(msdLsdAddress, message); message.Add(STX); PacketMessage(data, message); message.Add(ETX); message.Add((byte)CalcBBC(data)); if (SC.ContainsItem("System.IsSimulatorMode") && SC.GetValue("System.IsSimulatorMode")) message.Add(0x0D); return message.ToArray(); } public override bool HandleMessage(MessageBase msg, out bool handled) { ResponseMessage = msg; handled = true; return true; } private static int CalcBBC(string value) { int sum = 0; foreach (var item in value) { sum += (int)item; } sum += ETX; return sum % 128; } private static List PacketMessage(string msg, List msgList) { foreach (var item in msg) { msgList.Add((byte)item); } return msgList; } private static byte[] String2Ascii(string s) { byte[] array = System.Text.Encoding.ASCII.GetBytes(s); return array; } } public class HoribaMFCSetFlow : HoribaHandler { public HoribaMFCSetFlow(HoribaConnection connection, string deviceAddress, float flow) : base(connection, deviceAddress, $"AFC{flow},B") { Name = "SetFlow"; } public override bool HandleMessage(MessageBase msg, out bool handled) { var result = msg as HoribaMessage; handled = false; if (!result.IsResponse) return true; if (result.IsError || result.IsFormatError || result.IsNak) { _connection.SetError(result.RawMessage); } else { if (result.Datas[0] != 0x4f && result.Datas[0] != 0x4b) { _connection.SetError(result.RawMessage); } } ResponseMessage = msg; handled = true; return true; } } public class HoribaMFCQueryFlow : HoribaHandler { public HoribaMFCQueryFlow(HoribaConnection connection, string deviceAddress) : base(connection, deviceAddress, $"RFV") { Name = "QueryFlow"; } public override bool HandleMessage(MessageBase msg, out bool handled) { var result = msg as HoribaMessage; handled = false; if (!result.IsResponse) return true; if (result.IsError || result.IsFormatError || result.IsNak) { _connection.SetError(result.RawMessage); } else { float.TryParse(System.Text.Encoding.ASCII.GetString(result.Datas), out float flow); //MFCDevice.FeedBack = flow; _connection.NoteFlowReadout(Address, flow); } ResponseMessage = msg; handled = true; return true; } } public class HoribaMFCQueryScale : HoribaHandler { public HoribaMFCQueryScale(HoribaConnection connection, string deviceAddress) : base(connection, deviceAddress, $"RFS") { Name = "QueryScale"; } public override bool HandleMessage(MessageBase msg, out bool handled) { var result = msg as HoribaMessage; handled = false; if (!result.IsResponse) return true; if (result.IsError || result.IsFormatError || result.IsNak) { _connection.SetError(result.RawMessage); } else { var dataArray = System.Text.Encoding.ASCII.GetString(result.Datas).Split(','); float.TryParse(dataArray[0], out float range); //temporary comment to fix compile issue if (dataArray.Length > 1) { //if (MFCDevice.Unit != dataArray[1].ToUpper()) // EV.PostInfoLog(MFCDevice.Module, $"{MFCDevice.Module} {MFCDevice.Name} change from {MFCDevice.Unit} to {dataArray[1].ToUpper()}"); //MFCDevice.Unit = dataArray[1].ToUpper(); _connection.NoteUnitReadout(Address, dataArray[1].ToUpper()); } } ResponseMessage = msg; handled = true; return true; } } public class HoribaMFCSetDigitalMode : HoribaHandler { public HoribaMFCSetDigitalMode(HoribaConnection connection, string deviceAddress) : base(connection, deviceAddress, $"'!+REVERSE") { Name = "SetDigitalMode"; } public override bool HandleMessage(MessageBase msg, out bool handled) { var result = msg as HoribaMessage; handled = false; if (!result.IsResponse) return true; if (result.IsError || result.IsFormatError || result.IsNak) { _connection.SetError(result.RawMessage); } else { if (result.Datas[0] != 0x4f && result.Datas[0] != 0x4b) { _connection.SetError(result.RawMessage); } } ResponseMessage = msg; handled = true; return true; } } public class HoribaMFCSetAnalogMode : HoribaHandler { public HoribaMFCSetAnalogMode(HoribaConnection connection, string deviceAddress) : base(connection, deviceAddress, $"'!+NORMAL") { Name = "SetAnalogMode"; } public override bool HandleMessage(MessageBase msg, out bool handled) { var result = msg as HoribaMessage; handled = false; if (!result.IsResponse) return true; if (result.IsError || result.IsFormatError || result.IsNak) { _connection.SetError(result.RawMessage); } else { if (result.Datas[0] != 0x4f && result.Datas[0] != 0x4b) { _connection.SetError(result.RawMessage); } } ResponseMessage = msg; handled = true; return true; } } }