using System.Collections.Generic; namespace EC.AutoWeightServer.IfManager.Light { public class LightManager : ILightManager { private LightRelay _lightRelay; private string _comName; private int _baudRate; public LightManager() { } public LightManager(string comName, int baudRate) { _comName = comName; _baudRate = baudRate; } #region Start & Stop Server /// /// 开启连接 /// /// public bool StartServer() { _lightRelay = new LightRelay(_comName, _baudRate); var ret = _lightRelay.StartServer(); return ret; } /// /// 关闭连接 /// /// public bool StopServer() { _lightRelay?.StopServer(); _lightRelay = null; return true; } public bool IsOpen() { return _lightRelay?.IsOpen() ?? false; } #endregion Start & Stop Server #region Control Lights Code /// /// 控制所有灯编码,1 为开启所有灯,2 为关闭所有灯 /// private readonly List _ctrlAllLightCodeList = new List() { "", "3301140000000048", "3301130000000047" }; /// /// 开启某一路灯编码,不互斥,index 互相对应 /// private readonly List _openLightCodeList = new List() { "", "3301120000000147", "3301120000000248", "3301120000000349", "330112000000044A" }; /// /// 关闭某一路灯编码,不互斥,index 互相对应 /// private readonly List _closeLightCodeList = new List() { "", "3301110000000146", "3301110000000247", "3301110000000348", "3301110000000449" }; /// /// 互锁编码,控制只有一路灯亮,index 互相对应 /// private readonly List _lockLightOnCodeList = new List() { "", "33011500000E045B", "33011500000D045A", "33011500000B0458", "3301150000070454" }; /// /// 互锁编码,控制只有一路灯灭,index 互相对应 /// private readonly List _lockLightOffCodeList = new List() { "", "330115000001044E", "330115000002044F", "3301150000040451", "3301150000080455" }; #endregion Control Lights Code #region Control Lights /// /// 开启所有灯 /// /// public bool OpenAllLight() { var code = _ctrlAllLightCodeList[1]; return _lightRelay.SendHex(code); } /// /// 关闭所有灯 /// /// public bool CloseAllLight() { var code = _ctrlAllLightCodeList[2]; return _lightRelay.SendHex(code); } /// /// 开启某一路灯 /// /// /// public bool OpenLight(int index) { var codeList = _openLightCodeList; if (!IsIndexValid(codeList, index)) return false; var code = codeList[index]; return _lightRelay.SendHex(code); } /// /// 关闭某一路灯 /// /// /// public bool CloseLight(int index) { var codeList = _closeLightCodeList; if (!IsIndexValid(codeList, index)) return false; var code = codeList[index]; return _lightRelay.SendHex(code); } /// /// 互斥开某一路灯 /// /// /// public bool LockLightOn(int index) { var codeList = _lockLightOnCodeList; if (!IsIndexValid(codeList, index)) return false; var code = codeList[index]; return _lightRelay.SendHex(code); } /// /// 互斥关某一路灯 /// /// /// public bool LockLightOff(int index) { var codeList = _lockLightOffCodeList; if (!IsIndexValid(codeList, index)) return false; var code = codeList[index]; return _lightRelay.SendHex(code); } /// /// /// /// public bool CombineLight() { return true; } /// /// 判断功能码是否可以使用 /// /// /// /// public bool IsIndexValid(List codeList, int index) { if (codeList == null) return false; if (index < 1 || index > codeList.Count - 1) return false; return true; } #endregion Control Lights } }