From 2075f6cac8ea1b7a37ca51b05334e8973829e41d Mon Sep 17 00:00:00 2001 From: fajiao <1519100073@qq.com> Date: Fri, 10 Jun 2022 10:08:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20LightManager?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AutoWeightServer/Center/DeviceCenter.cs | 4 +- .../IfManager/Light/ILightManager.cs | 32 ++++ .../IfManager/Light/Light2Manager.cs | 178 ++++++++++++++++++ .../IfManager/Light/LightManager.cs | 4 +- 4 files changed, 213 insertions(+), 5 deletions(-) create mode 100644 AutoWeightServer/IfManager/Light/ILightManager.cs create mode 100644 AutoWeightServer/IfManager/Light/Light2Manager.cs diff --git a/AutoWeightServer/Center/DeviceCenter.cs b/AutoWeightServer/Center/DeviceCenter.cs index 0f52200..34fc2d5 100644 --- a/AutoWeightServer/Center/DeviceCenter.cs +++ b/AutoWeightServer/Center/DeviceCenter.cs @@ -31,7 +31,7 @@ namespace EC.AutoWeightServer.Center /// /// 灯光控制 /// - internal LightManager LightManager { get; set; } + internal ILightManager LightManager { get; set; } /// /// 指纹识别 @@ -218,7 +218,7 @@ namespace EC.AutoWeightServer.Center { var com = ServerConfig.LightRelayCom; var rate = ServerConfig.LightRelayBaudRate; - LightManager = new LightManager(com, rate); + LightManager = new Light2Manager(com, rate); LightManager.StartServer(); } diff --git a/AutoWeightServer/IfManager/Light/ILightManager.cs b/AutoWeightServer/IfManager/Light/ILightManager.cs new file mode 100644 index 0000000..b035e96 --- /dev/null +++ b/AutoWeightServer/IfManager/Light/ILightManager.cs @@ -0,0 +1,32 @@ +namespace EC.AutoWeightServer.IfManager.Light +{ + public interface ILightManager + { + #region Start & Stop Server + + public bool StartServer(); + + public bool StopServer(); + + public bool IsOpen(); + + #endregion Start & Stop Server + + #region Control Lights + + public bool OpenAllLight(); + + public bool CloseAllLight(); + + public bool OpenLight(int index); + + public bool CloseLight(int index); + + public bool LockLightOn(int index); + + public bool LockLightOff(int index); + + #endregion Control Lights + + } +} \ No newline at end of file diff --git a/AutoWeightServer/IfManager/Light/Light2Manager.cs b/AutoWeightServer/IfManager/Light/Light2Manager.cs new file mode 100644 index 0000000..3bab7ba --- /dev/null +++ b/AutoWeightServer/IfManager/Light/Light2Manager.cs @@ -0,0 +1,178 @@ +using System.Collections.Generic; + +namespace EC.AutoWeightServer.IfManager.Light +{ + public class Light2Manager : ILightManager + { + private LightRelay _lightRelay; + private string _comName; + private int _baudRate; + + public Light2Manager() + { + } + + public Light2Manager(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 + + //private int num = 0xFF; + + /// + /// 控制所有灯编码,1 为开启所有灯,2 为关闭所有灯 + /// + private readonly List _ctrlAllLightCodeList = new List() { "", "01108050000204020F0804A0ED", "011080500002040200080490EE" }; + + /// + /// 开启某一路灯编码,不互斥,index 互相对应 + /// + private readonly List _openLightCodeList = new List() { "", "01050000FF008C3A", "01050001FF00DDFA", "01050002FF002DFA", "01050003FF007C3A" }; + + /// + /// 关闭某一路灯编码,不互斥,index 互相对应 + /// + private readonly List _closeLightCodeList = new List() { "", "010500000000CDCA", "0105000100009C0A", "0105000200006C0A", "0105000300003DCA" }; + + /// + /// 互锁编码,控制只有一路灯亮,index 互相对应 + /// + private readonly List _lockLightOnCodeList = new List() { "", "0110805000020402010804C12E", "0110805000020402020804312E", "0110805000020402040804D12F", "0110805000020402080804112C" }; + + /// + /// 互锁编码,控制只有一路灯灭,index 互相对应 + /// + private readonly List _lockLightOffCodeList = new List() { "", "01108050000204020E0804F12D", "01108050000204020D0804012D", "01108050000204020B0804E12C", "0110805000020402070804212F" }; + + #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); + } + + /// + /// 判断功能码是否可以使用 + /// + /// + /// + /// + private 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 + } +} \ No newline at end of file diff --git a/AutoWeightServer/IfManager/Light/LightManager.cs b/AutoWeightServer/IfManager/Light/LightManager.cs index 8c227e9..f025c1c 100644 --- a/AutoWeightServer/IfManager/Light/LightManager.cs +++ b/AutoWeightServer/IfManager/Light/LightManager.cs @@ -2,7 +2,7 @@ namespace EC.AutoWeightServer.IfManager.Light { - public class LightManager + public class LightManager : ILightManager { private LightRelay _lightRelay; private string _comName; @@ -51,8 +51,6 @@ namespace EC.AutoWeightServer.IfManager.Light #region Control Lights Code - //private int num = 0xFF; - /// /// 控制所有灯编码,1 为开启所有灯,2 为关闭所有灯 ///