// ******************************************************************************** // 文件名字: SECSContext // 文件描述: SECSContext // 开发人员: Michael // 创建时间: 2019/11/8 0:08 // // 更新历史: // + 创建 SECSContext.cs 文件. by Michael @2019/11/8 0:08 // ******************************************************************************** using ARI.EAP.HOST; using ARI.EAP.HOST.Common; using Glorysoft.SECS.EQP.Utilities; using Glorysoft.SECSwell; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Threading.Tasks; namespace Glorysoft.SECS.EQP.Common { public class SECSContext { public string Name { get; set; } public short DeviceID { get; set; } = 0; public eHSMS_CONNECT_MODE ConnectionMode { get; set; } = eHSMS_CONNECT_MODE.ACTIVE; public string RemoteIPAddress { get; set; } = "127.0.0.1"; public int RemoteIPPort { get; set; } = 6000; public string LocalIPAddress { get; set; } = "127.0.0.1"; public int LocalIPPort { get; set; } = 20002; public bool AsciiValueAutoFillSpace { get; set; } = true; public int T1 { get; set; } = 1; public int T2 { get; set; } = 2; public int T3 { get; set; } = 45; public int T4 { get; set; } = 4; public int T5 { get; set; } = 10; public int T6 { get; set; } = 6; public int T7 { get; set; } = 10; public int T8 { get; set; } = 5; public eLOG_LEVEL LogLevel { get; set; } = eLOG_LEVEL.DEBUG; public string LogPath { get; set; } = @"logs\"; public string SECSLibraryFile { get; set; } = @"Configuration\SECSLibrary.xml"; public bool IsConnected => Port != null && Port.Connected; private readonly SECSwell.SECSPort Port; private static Dictionary Handlers; public SECSContext(string name, short deviceId, string remoteIPAddress, int remoteIPPort) { LogPath = string.Format(@"{0}{1}\", LogPath, name); Name = name; DeviceID = deviceId; RemoteIPAddress = remoteIPAddress; RemoteIPPort = remoteIPPort; SECSwell.SECSPort.SetAsciiValueAutoFillSpace = AsciiValueAutoFillSpace; Port = new SECSwell.SECSPort(Name) { Name = Name, DeviceID = DeviceID, LogPath = LogPath, LogLevel = LogLevel }; HSMSParameters parameters = new HSMSParameters { DeviceID = DeviceID, ConnectionMode = ConnectionMode, RemoteIP = RemoteIPAddress, RemotePort = RemoteIPPort, LocalIP = LocalIPAddress, LocalPort = LocalIPPort, T3 = T3, T5 = T5, T6 = T6, T7 = T7, T8 = T8 }; Port.Library.Load(SECSLibraryFile); Port.HsmsParameters = parameters; Port.IsS9FxCheck = false; Port.OnSECSEvent += OnRecieved; Handlers = new Dictionary(); foreach (Type item in MethodBase.GetCurrentMethod().DeclaringType.Assembly.GetTypes()) { if (item.IsClass && item.GetInterface(nameof(ISECSMessageHandler)) != null) { Handlers.Add(item.Name, Activator.CreateInstance(item) as ISECSMessageHandler); } } } private void OnRecieved(SECSEventType type, SECSTransaction trans, SECSErrors err, string errmsg) { var msgName = string.Empty; switch (type) { case SECSEventType.HSMSConnected: msgName = $"{nameof(SECSEventType.HSMSConnected)}Handler"; break; case SECSEventType.HSMSDisconnected: msgName = $"{nameof(SECSEventType.HSMSDisconnected)}Handler"; break; case SECSEventType.PrimaryRcvd: msgName = $"S{trans.Primary.Stream}F{trans.Primary.Function}Handler"; break; case SECSEventType.SecondaryRcvd: msgName = $"S{trans.Secondary.Stream}F{trans.Secondary.Function}Handler"; break; default: msgName = $"{nameof(SECSEventType.Error)}Handler"; break; } if (Handlers.ContainsKey(msgName)) { EquipmentStatus.S1F1HeartBit.timer1.Stop(); EquipmentStatus.S1F1HeartBit.timer1.Start(); Task.Run(() => Handlers[msgName].Execute(this, trans, err, errmsg)); } } public virtual bool Close() { try { if (IsConnected) { Port.ClosePort(); EquipmentStatus.EqConnectState = ConnectState.disconnected; return true; } return false; } catch (Exception e) { LoggerService.SECSLogger.Error(e); return false; } } public virtual bool Open() { try { if (Port != null) { Port.OpenPort(); return Port.Connected; } return false; } catch (Exception e) { LoggerService.SECSLogger.Error(e); return false; } } public virtual void ReplyMessage(SECSTransaction trans) { try { if (Port != null && Port.PortIsOpen) { Port.Reply(trans); } } catch (Exception e) { LoggerService.SECSLogger.Error(e); } } public virtual void SendMessage(SECSTransaction trans) { try { if (Port != null && Port.PortIsOpen) { Port.Send(trans); } } catch (Exception e) { LoggerService.SECSLogger.Error(e); } } public virtual void SendMessage(string name) { try { if (Port != null && Port.PortIsOpen) { SECSTransaction transaction = new SECSTransaction { Primary = Port.Library.FindMessage(name) }; transaction.Secondary = Port.Library.FindMessage(transaction.Primary.Stream, transaction.Primary.Function + 1).FirstOrDefault(); Port.Send(transaction); } } catch (Exception e) { LoggerService.SECSLogger.Error(e); } } public SECSTransaction GetTransaction(int stream, int function, string msgName = null) { try { string messageName = string.IsNullOrWhiteSpace(msgName) ? $"S{stream}F{function}" : msgName.ToUpper(); if (Port == null) { LoggerService.SECSLogger.Error($"Method: GetTransaction, Port is null!"); return null; } if (Port.Library == null) { LoggerService.SECSLogger.Error($"Method: GetTransaction, Port.Library is null!"); return null; } return Port.Library.FindTransaction(messageName); } catch (Exception e) { LoggerService.SECSLogger.Error(e); return null; } } } }