From a715181089be0d31cd737a5367ffd02690b9d77f Mon Sep 17 00:00:00 2001 From: wxr <464027401@qq.com> Date: 星期四, 12 十一月 2020 13:36:01 +0800 Subject: [PATCH] 20201112 --- HDL_ON/DriverLayer/Control.cs | 433 +++++++++++++++++++++-------------------------------- 1 files changed, 175 insertions(+), 258 deletions(-) diff --git a/HDL_ON/DriverLayer/Control.cs b/HDL_ON/DriverLayer/Control.cs index b248598..ea4b55e 100644 --- a/HDL_ON/DriverLayer/Control.cs +++ b/HDL_ON/DriverLayer/Control.cs @@ -1,18 +1,144 @@ using System; using System.Collections.Generic; -using HDL_ON.DAL; -using HDL_ON.DAL.Net; using HDL_ON.Entity; -using Newtonsoft.Json.Linq; -using Shared; -namespace HDL_ON +namespace HDL_ON.DriverLayer { - public partial class Control + + /// <summary> + /// 閫氳鏂瑰紡 + /// </summary> + public enum CommunicationMode { - public static void Send(Function function) + /// <summary> + /// 鏈湴udp + /// </summary> + local_BusUdp, + /// <summary> + /// 鏈湴tcp鏈嶅姟绔� + /// </summary> + tcp_local_server, + /// <summary> + /// 鏈湴tcp瀹㈡埛绔� + /// </summary> + tcp_local_client, + } + + + + public class Control + { + static Control _control; + public static Control ins { - ControlBusScenes(function as Scene); + get + { + if (_control == null) + { + _control = new Control(); + } + return _control; + } + } + /// <summary> + /// 閫氳鍦板潃IP + /// </summary> + public string reportIp = "255.255.255.255"; + /// <summary> + /// tcp鏈嶅姟绔� + /// </summary> + public Control_TcpServer myTcpServer = null; + /// <summary> + /// tcp瀹㈡湇绔� + /// </summary> + public Control_TcpClient myTcpClient = null; + /// <summary> + /// 鏈湴udp + /// </summary> + public Control_Udp_Bus myUdp = null; + + /// <summary> + /// 閫氳鏂瑰紡 + /// </summary> + public CommunicationMode communicationMode; + + /// <summary> + /// 鏀瑰彉閫氳鏂瑰紡 + /// </summary> + public void ChangeCommunicationMode(CommunicationMode communicationMode) + { + this.communicationMode = communicationMode; + + + switch (this.communicationMode) + { + case CommunicationMode.local_BusUdp: + if (myUdp == null) + { + myUdp = new Control_Udp_Bus(); + } + UdpSocket._BusSocket.Start(); + if (myTcpServer != null) + { + myTcpClient.Close(); + myTcpClient = null; + } + if (myTcpClient != null) + { + myTcpClient.Close(); + myTcpClient = null; + } + break; + case CommunicationMode.tcp_local_server: + if (myTcpServer == null) + { + myTcpServer = new Control_TcpServer(); + myTcpServer.OpenServer(); + } + if (myTcpClient != null) + { + myTcpClient.Close(); + myTcpClient = null; + } + if(myUdp != null) + { + UdpSocket._BusSocket.Stop(); + myUdp = null; + } + break; + case CommunicationMode.tcp_local_client: + if (myTcpClient == null) + { + myTcpClient = new Control_TcpClient(reportIp); + myTcpClient.Connect(); + } + if (myTcpServer != null) + { + myTcpClient.Close(); + myTcpClient = null; + } + if (myUdp != null) + { + UdpSocket._BusSocket.Stop(); + myUdp = null; + } + break; + } + } + + /// <summary> + /// 鍦烘櫙鎺у埗鍏ュ彛 + /// </summary> + public void ControlScene(Scene scene) + { + if(myUdp!=null) + { + myUdp.ControlBusScenes(scene); + } + if(myTcpClient!= null) + { + ControlAProtocolScene(scene); + } } /// <summary> @@ -23,283 +149,74 @@ /// <param name="commandString"></param> /// <param name="function"></param> /// <returns></returns> - public static void Send(CommandType_A commandType, Function function) + public void SendWriteCommand(Function function, Dictionary<string, string> commandDictionary) { function.usageCount++; function.refreshTime = DateTime.Now; - if (function.bus_Data!=null) + if (DB_ResidenceData.residenceData.GatewayType == 0) { try { - ControlBusData(function, commandType); + if(myUdp != null) + { + myUdp.WriteBusData(function, commandDictionary); + } } catch (Exception ex) { - MainPage.Log($"鍙戦�佹暟鎹紓甯革細 {ex.Message}"); + MainPage.Log($"鍙戦�佹暟鎹紓甯�: {ex.Message}"); } } else { - var commandObj = function.GetSendJObject(commandType); - var commandString = Newtonsoft.Json.JsonConvert.SerializeObject(commandObj); - var commandBytes = CommonPage.MyEncodingUTF8.GetBytes(commandString); - MqttCommon.MqttRemoteSend(commandBytes); - } - } - - /// <summary> - /// 鎺у埗bus鍦烘櫙 - /// </summary> - static void ControlBusScenes(Scene scene) - { - foreach(var busSceneData in scene.bus_SceneData) - { - foreach(var function in DB_ResidenceData.residenceData.functionList.GetAllDeviceFunctionList()) + if (myTcpClient != null) { - if(busSceneData.Key == function.sid) - { - switch (function.functionType) - { - case FunctionType.Curtain: - ControlBytesSend(Command.SetCurtainModelStutas, function.bus_Data.SubnetID, function.bus_Data.DeviceID, busSceneData.Value); - break; - case FunctionType.AC: - ControlBytesSend(Command.SetACMode, function.bus_Data.SubnetID, function.bus_Data.DeviceID, busSceneData.Value); - break; - case FunctionType.Relay: - case FunctionType.Dimmer: - ControlBytesSend(Command.SetSingleLight, function.bus_Data.SubnetID, function.bus_Data.DeviceID, busSceneData.Value); - break; - case FunctionType.RGB: - ControlBytesSend(Command.SetLogicLoopColor, function.bus_Data.SubnetID, function.bus_Data.DeviceID, busSceneData.Value); - break; - case FunctionType.FloorHeating: - var fh = function as FloorHeating; - if (fh.modeTemp.Count == 4) - { - byte b1 = fh.on_off == "on" ? (byte)1 : (byte)0; - if (fh.workMode > 0) - { - b1 += (byte)(16 + fh.workMode); - } - ControlBytesSend(Command.SetFloorHeat, function.bus_Data.SubnetID, function.bus_Data.DeviceID, new byte[] { fh.bus_Data.LoopID, b1, (byte)fh.curTempType, fh.curModeIndex, fh.modeTemp["ordinary"], fh.modeTemp["day"], fh.modeTemp["night"], fh.modeTemp["away"], 0, 0 }); - } - break; - case FunctionType.Socket: - var s = function as SwitchSocket; - ControlBytesSend(Command.SetSingleLight, function.bus_Data.SubnetID, function.bus_Data.DeviceID, new byte[] { s.bus_Data.LoopID, s.on_off == "on" ? (byte)100 : (byte)0 }); - break; - } - } + var sendDataObj = function.GetControlSendData(commandDictionary); + var sendJson = Newtonsoft.Json.JsonConvert.SerializeObject(sendDataObj); + var sendBytes = System.Text.Encoding.UTF8.GetBytes(sendJson); + myTcpClient.SendMessage(sendBytes); } } } /// <summary> - /// 灏咥鍗忚鎺у埗鏁版嵁杞崲绋媌us鍗忚鐨勬帶鍒舵暟鎹� + /// 鍙戦�佽鍙栧懡浠� + /// 鑷姩鍒ゆ柇鏄惁涓篈鍗忚璁惧 /// </summary> - /// <param name="commandString"></param> - /// <returns></returns> - static void ControlBusData(Function function, CommandType_A command) + public void SendReadCommand(Function function) { - try + function.refreshTime = DateTime.Now; + if (function.bus_Data != null) { - var subnetId = function.bus_Data.SubnetID; - var deviceId = function.bus_Data.DeviceID; - var loopId = function.bus_Data.LoopID; - - switch (command) + try { - case CommandType_A.write: - switch (function.functionCategory) - { - case FunctionCategory.Light: - var light = function as Light; - switch (light.functionType) - { - case FunctionType.RGB: - MainPage.Log($"rgb level : {light.brightness}"); - byte b = 100;//寮�鍏虫搷浣滀緷鎹畂n_off瀛楁锛屽疄闄呭紑鍏冲�间緷鎹産rightness锛屽綋on_off涓烘墦寮�锛宐rightness涓嶈兘涓�0 - if (light.on_off == "off") - { - b = 0; - } - else - { - b = (byte)light.brightness == 0 ? (byte)100 : (byte)light.brightness; - } - var bytes = new byte[] { function.bus_Data.LoopID, - b, - 254, 0,0,3, - (byte)light.redColor,(byte)light.greenColor, - (byte)light.blueColor, - 0,0}; - MainPage.Log($"rgb level1 : {light.brightness}"); - ControlBytesSend(Command.SetLogicLoopColor, subnetId, deviceId, bytes,1); - MainPage.Log($"rgb level2 : {light.brightness}"); - break; - case FunctionType.Dimmer: - byte b1 = 100; - if (light.on_off == "off") - { - b1 = 0; - } - else - { - b1 = (byte)light.brightness == 0 ? (byte)100 : (byte)light.brightness; - } - ControlBytesSend(Command.SetSingleLight, subnetId, deviceId, new byte[] { - function.bus_Data.LoopID, - b1, - 0, 0 }); - break; - case FunctionType.Relay: - ControlBytesSend(Command.SetSingleLight, subnetId, deviceId, new byte[] { function.bus_Data.LoopID, light.on_off == "on" ? (byte)100 : (byte)0, 0, 0 }); - break; - } - break; - case FunctionCategory.Curtain: - var curtain = function as Curtain; - switch (curtain.functionType) - { - case FunctionType.Curtain: - byte b1 = 0; - if (curtain.on_off == "stop") - { - b1 = 0; - }else if(curtain.on_off == "on") - { - b1 = 1; - } - else - { - b1 = 2; - } - ControlBytesSend(Command.SetCurtainModelStutas, subnetId, deviceId, new byte[] { curtain.bus_Data.LoopID, b1 }); - break; - case FunctionType.MotorCurtain: - case FunctionType.RollingShutter: - if (curtain.on_off == "stop") - { - ControlBytesSend(Command.SetCurtainModelStutas, subnetId, deviceId, new byte[] { curtain.bus_Data.LoopID, 0 }); - } - ControlBytesSend(Command.SetCurtainModelStutas, subnetId, deviceId, new byte[] { 17, (byte)curtain.openLevel }); - break; - } - break; - case FunctionCategory.Thermostat: - switch (function.functionType) - { - case FunctionType.AC: - var aC = function as AC; - ControlBytesSend(Command.SetACMode, subnetId, deviceId, new byte[] { aC.bus_Data.LoopID, (byte)aC.curTempType, 32, 32, 32, 32, 32, 0, aC.on_off == "on" ? (byte)1 : (byte)0, aC.curModeIndex, aC.curFanIndex, (byte)aC.curTemp, 0 }); - break; - case FunctionType.FloorHeating: - var fh = function as FloorHeating; - if (fh.modeTemp.Count == 4) - { - byte b1 = fh.on_off == "on" ? (byte)1 : (byte)0; - if (fh.workMode > 0) - { - b1 += (byte)(16 + fh.workMode); - } - ControlBytesSend(Command.SetFloorHeat, subnetId, deviceId, new byte[] { fh.bus_Data.LoopID, b1,(byte)fh.curTempType,fh.curModeIndex, fh.modeTemp["ordinary"], fh.modeTemp["day"], fh.modeTemp["night"], fh.modeTemp["away"], 0, 0 }); - } - //else - //{ - // ControlBytesSend(Command.ReadFloorHeat, subnetId,deviceId,new byte[] { fh.bus_Data.LoopID}); - //} - break; - } - break; - case FunctionCategory.Electrical: - switch (function.functionType) - { - case FunctionType.Fan: - var fan = function as Fan; - if(fan.on_off == "on") - { - ControlBytesSend(Command.SetSingleLight, subnetId, deviceId, new byte[] { fan.bus_Data.LoopID, (byte)fan.openLevel }); - } - { - ControlBytesSend(Command.SetSingleLight, subnetId, deviceId, new byte[] { fan.bus_Data.LoopID, 0 }); - } - break; - } - break; - case FunctionCategory.SwitchDevice: - switch(function.functionType) - { - case FunctionType.Socket: - var s = function as SwitchSocket; - ControlBytesSend(Command.SetSingleLight, subnetId, deviceId, new byte[] { s.bus_Data.LoopID, s.on_off == "on" ? (byte)100 : (byte)0 }); - break; - } - break; - } - break; - case CommandType_A.read: - switch (function.functionCategory) - { - case FunctionCategory.Light: - switch (function.functionType) - { - case FunctionType.Relay: - case FunctionType.Dimmer: - ControlBytesSend(Command.ReadLightSingleLoopBrightness, subnetId, deviceId, new byte[] { function.bus_Data.LoopID }); - break; - case FunctionType.RGB: - ControlBytesSend(Command.ReadLogicLoopColor, subnetId, deviceId, new byte[] { function.bus_Data.LoopID }); - break; - } - break; - case FunctionCategory.Curtain: - switch (function.functionType) - { - case FunctionType.Curtain: - case FunctionType.MotorCurtain: - case FunctionType.RollingShutter: - ControlBytesSend(Command.ReadCurtainStatus, subnetId, deviceId, new byte[] { function.bus_Data.LoopID }); - break; - } - break; - case FunctionCategory.Thermostat: - switch (function.functionType) - { - case FunctionType.AC: - ControlBytesSend(Command.ReadACMode, subnetId, deviceId, new byte[] { function.bus_Data.LoopID }); - break; - case FunctionType.FloorHeating: - ControlBytesSend(Command.ReadFloorHeat, subnetId, deviceId, new byte[] { function.bus_Data.LoopID }); - break; - } - break; - case FunctionCategory.Electrical: - switch (function.functionType) - { - case FunctionType.Fan: - var fan = function as Fan; - ControlBytesSend(Command.SetSingleLightACK, subnetId, deviceId, new byte[] { fan.bus_Data.LoopID }); - break; - } - break; - case FunctionCategory.SwitchDevice: - switch (function.functionType) - { - case FunctionType.Socket: - var s = function as SwitchSocket; - ControlBytesSend(Command.SetSingleLightACK, subnetId, deviceId, new byte[] { s.bus_Data.LoopID }); - break; - } - break; - } - break; + //ReadBusData(function); + } + catch (Exception ex) + { + MainPage.Log($"鍙戦�佹暟鎹紓甯�: {ex.Message}"); } } - catch (Exception ex) + else { - MainPage.Log($"DataConversion_Bus Erorr : {ex.Message}"); + //var commandObj = function.GetSendJObject(CommandType_A.read); + //var commandString = Newtonsoft.Json.JsonConvert.SerializeObject(commandObj); + //var commandBytes = System.Text.Encoding.UTF8.GetBytes(commandString); + //MqttCommon.MqttRemoteSend(commandBytes); } } + + /// <summary> + /// 鎺у埗a鍗忚鍦烘櫙 + /// </summary> + /// <param name="scene"></param> + static void ControlAProtocolScene(Scene scene) + { + var sendSidObj = new AProtocolSendingObject(); + sendSidObj.time_stamp = ""; + sendSidObj.type = "device_sid"; + sendSidObj.command = "get_list_response"; + } + } } \ No newline at end of file -- Gitblit v1.8.0