wxr
2020-11-04 e6a26ee148587327478d9a82624a820c907b6e16
HDL_ON/DriverLayer/Control.cs
@@ -1,39 +1,141 @@
using System;
using System.Collections.Generic;
using HDL_ON.DAL;
using HDL_ON.DAL.Net;
using HDL_ON.Entity;
using HDL_ON.UI;
namespace HDL_ON
namespace HDL_ON.DriverLayer
{
    public partial class Control
    /// <summary>
    /// 通讯方式
    /// </summary>
    public enum CommunicationMode
    {
        /// <summary>
        /// 发送命令
        /// 本地udp
        /// </summary>
        public static void Send(Function function)
        {
            switch (function.functionCategory)
            {
                case FunctionCategory.Scene:
                    ControlScene(function as Scene);
                    break;
        local_BusUdp,
        /// <summary>
        /// 本地tcp服务端
        /// </summary>
        tcp_local_server,
        /// <summary>
        /// 本地tcp客户端
        /// </summary>
        tcp_local_client,
    }
    public class Control
    {
        static Control _control;
        public static Control ins
        {
            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>
        /// <param name="scene"></param>
        static void ControlScene(Scene scene)
        public void ControlScene(Scene scene)
        {
            if (DB_ResidenceData.residenceData.GatewayType == 0)
            if(myUdp!=null)
            {
                ControlBusScenes(scene);
                myUdp.ControlBusScenes(scene);
            }
            else
            if(myTcpClient!= null)
            {
                ControlAProtocolScene(scene);
            }
@@ -47,7 +149,7 @@
        /// <param name="commandString"></param>
        /// <param name="function"></param>
        /// <returns></returns>
        public static void SendWriteCommand(Function function, Dictionary<string, string> commandDictionary)
        public void SendWriteCommand(Function function, Dictionary<string, string> commandDictionary)
        {
            function.usageCount++;
            function.refreshTime = DateTime.Now;
@@ -55,7 +157,10 @@
            {
                try
                {
                    WriteBusData(function, commandDictionary);
                    if(myUdp != null)
                    {
                        myUdp.WriteBusData(function, commandDictionary);
                    }
                }
                catch (Exception ex)
                {
@@ -64,10 +169,13 @@
            }
            else
            {
                //var commandObj = function.GetSendJObject(CommandType_A.write);
                //var commandString = Newtonsoft.Json.JsonConvert.SerializeObject(commandObj);
                //var commandBytes = CommonPage.MyEncodingUTF8.GetBytes(commandString);
                //MqttCommon.MqttRemoteSend(commandBytes);
                if (myTcpClient != null)
                {
                    var sendDataObj = function.GetControlSendData(commandDictionary);
                    var sendJson = Newtonsoft.Json.JsonConvert.SerializeObject(sendDataObj);
                    var sendBytes = System.Text.Encoding.UTF8.GetBytes(sendJson);
                    myTcpClient.SendMessage(sendBytes);
                }
            }
        }
@@ -75,16 +183,14 @@
        /// 发送读取命令
        /// 自动判断是否为A协议设备
        /// </summary>
        /// <param name="function"></param>
        public static void SendReadCommand(Function function)
        public void SendReadCommand(Function function)
        {
            //function.usageCount++;
            function.refreshTime = DateTime.Now;
            if (function.bus_Data != null)
            {
                try
                {
                    ReadBusData(function);
                    //ReadBusData(function);
                }
                catch (Exception ex)
                {
@@ -95,104 +201,11 @@
            {
                //var commandObj = function.GetSendJObject(CommandType_A.read);
                //var commandString = Newtonsoft.Json.JsonConvert.SerializeObject(commandObj);
                //var commandBytes = CommonPage.MyEncodingUTF8.GetBytes(commandString);
                //var commandBytes = System.Text.Encoding.UTF8.GetBytes(commandString);
                //MqttCommon.MqttRemoteSend(commandBytes);
            }
        }
        /// <summary>
        /// 控制bus场景
        /// </summary>
        static void ControlBusScenes(Scene scene)
        {
            foreach (var function in scene.sceneFunctionList)
            {
                switch (function.functionType)
                {
                    case FunctionType.Curtain:
                        byte curtainState = 0;
                        if (function.trait_on_off.value.ToString() == "on")
                        {
                            curtainState = 1;
                        }
                        else if (function.trait_on_off.value.ToString() == "off")
                        {
                            curtainState = 2;
                        }
                        ControlBytesSend(Command.SetCurtainModelStutas, function.bus_Data.SubnetID, function.bus_Data.DeviceID,
                            new byte[] {
                                function.bus_Data.LoopID,
                                curtainState,
                            });
                        break;
                    case FunctionType.AC:
                        var aC = Newtonsoft.Json.JsonConvert.DeserializeObject<AC>(Newtonsoft.Json.JsonConvert.SerializeObject(function));//as AC;
                        ControlBytesSend(Command.InstructionPanelKey, aC.bus_Data.SubnetID, aC.bus_Data.DeviceID, new byte[] { 3, aC.trait_on_off.value.ToString() == "on" ? (byte)1 : (byte)0, aC.bus_Data.LoopID });
                        ControlBytesSend(Command.InstructionPanelKey, aC.bus_Data.SubnetID, aC.bus_Data.DeviceID, new byte[] { 6, aC.curModeIndex, aC.bus_Data.LoopID });
                        ControlBytesSend(Command.InstructionPanelKey, aC.bus_Data.SubnetID, aC.bus_Data.DeviceID, new byte[] { 5, aC.curFanIndex, aC.bus_Data.LoopID });
                        byte modeKey = 4;
                        switch (aC.curModeIndex)
                        {
                            case 3:
                                modeKey = 8;
                                break;
                            case 0:
                                modeKey = 4;
                                break;
                            case 1:
                                modeKey = 7;
                                break;
                            case 4:
                                modeKey = 19;
                                break;
                            case 2:
                                modeKey = 2;
                                break;
                        }
                        ControlBytesSend(Command.InstructionPanelKey, aC.bus_Data.SubnetID, aC.bus_Data.DeviceID, new byte[] { modeKey, Convert.ToByte(aC.trait_temp.value), aC.bus_Data.LoopID });
                        break;
                    case FunctionType.Relay:
                    case FunctionType.Dimmer:
                        byte brightness = 0;
                        if (function.trait_on_off.value.ToString() == "on")
                        {
                            var brightnessTrait = function.function.Find((obj) => obj.name == "brightness");
                            if (brightnessTrait != null)
                            {
                                brightness = Convert.ToByte(brightnessTrait.value);
                            }
                        }
                        ControlBytesSend(Command.SetSingleLight, function.bus_Data.SubnetID, function.bus_Data.DeviceID,
                            new byte[] {
                                function.bus_Data.LoopID,brightness
                            });
                        break;
                    case FunctionType.RGB:
                        //ControlBytesSend(Command.SetLogicLoopColor, function.bus_Data.SubnetID, function.bus_Data.DeviceID,
                        //    new byte[] {
                        //    });
                        break;
                    case FunctionType.FloorHeating:
                        var fh = Newtonsoft.Json.JsonConvert.DeserializeObject<FloorHeating>(Newtonsoft.Json.JsonConvert.SerializeObject(function));//as AC;
                        var fhModeTempTrait = function.function.Find((obj) => obj.name == "mode");
                        if (fhModeTempTrait.value_key.Count == 4)
                        {
                            byte b1 = function.trait_on_off.value.ToString() == "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["normal"], 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[] { function.bus_Data.LoopID, function.trait_on_off.value.ToString() == "on" ? (byte)100 : (byte)0 });
                        break;
                }
            }
        }
        /// <summary>
        /// 控制a协议场景
        /// </summary>
@@ -200,323 +213,10 @@
        static void ControlAProtocolScene(Scene scene)
        {
            var sendSidObj = new AProtocolSendingObject();
            //sendSidObj.from_oid = "";
            //sendSidObj.to_oid = "";
            sendSidObj.time_stamp = "";
            sendSidObj.type = "device_sid";
            sendSidObj.command = "get_list_response";
            //转换成bus需要的数据格式
        }
        /// <summary>
        /// 控制bus设备
        /// </summary>
        /// <param name="commandString"></param>
        /// <returns></returns>
        static void WriteBusData(Function function, Dictionary<string, string> commandDictionary)
        {
            try
            {
                var subnetId = function.bus_Data.SubnetID;
                var deviceId = function.bus_Data.DeviceID;
                var loopId = function.bus_Data.LoopID;
                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;//开关操作依据on_off字段,实际开关值依据brightness,当on_off为打开,brightness不能为0
                                if (light.trait_on_off.value.ToString() == "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,Convert.ToByte(light.fadeTime) ,3,
                                            (byte)light.redColor,(byte)light.greenColor,
                                            (byte)light.blueColor,
                                            0,0};
                                ControlBytesSend(Command.SetLogicLoopColor, subnetId, deviceId, bytes, 1);
                                break;
                            case FunctionType.Dimmer:
                                byte b1 = 100;
                                if (light.trait_on_off.value.ToString() == "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, Convert.ToByte(light.fadeTime) });
                                DimmerPage.UpdataStates(light);
                                break;
                            case FunctionType.Relay:
                                ControlBytesSend(Command.SetSingleLight, subnetId, deviceId, new byte[] { function.bus_Data.LoopID, light.trait_on_off.value.ToString() == "on" ? (byte)100 : (byte)0, 0, 0 });
                                RelayPage.UpdataState(light);
                                break;
                        }
                        break;
                    case FunctionCategory.Curtain:
                        var curtain = function as Curtain;
                        switch (curtain.functionType)
                        {
                            case FunctionType.Curtain:
                                byte b1 = 0;
                                if (curtain.trait_on_off.value.ToString() == "stop")
                                {
                                    b1 = 0;
                                }
                                else if (curtain.trait_on_off.value.ToString() == "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 (commandDictionary.Count > 0)
                                //{
                                if (curtain.trait_on_off.value.ToString() == "stop")
                                {
                                    ControlBytesSend(Command.SetCurtainModelStutas, subnetId, deviceId, new byte[] { curtain.bus_Data.LoopID, 0 });
                                }
                                else
                                {
                                    ControlBytesSend(Command.SetCurtainModelStutas, subnetId, deviceId, new byte[] { 17, Convert.ToByte(curtain.percent) });
                                }
                                //}
                                //else
                                //{
                                //    if (curtain.trait_on_off.value.ToString() == "stop")
                                //    {
                                //        ControlBytesSend(Command.SetCurtainModelStutas, subnetId, deviceId, new byte[] { curtain.bus_Data.LoopID, 0 });
                                //    }
                                //    else if (curtain.trait_on_off.value.ToString() == "on")
                                //    {
                                //        ControlBytesSend(Command.SetCurtainModelStutas, subnetId, deviceId, new byte[] { 17, 100 });
                                //    }
                                //    else if (curtain.trait_on_off.value.ToString() == "off")
                                //    {
                                //        ControlBytesSend(Command.SetCurtainModelStutas, subnetId, deviceId, new byte[] { 17, 0 });
                                //    }
                                //}
                                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.trait_on_off.value.ToString() == "on" ? (byte)1 : (byte)0, aC.curModeIndex, aC.curFanIndex, Convert.ToByte(aC.trait_temp.value), 0 });
                                //ControlBytesSend(Command.InstructionPanelKey, aC.bus_Data.SubnetID, aC.bus_Data.DeviceID, new byte[] { 3, aC.on_off == "on" ? (byte)1 : (byte)0, aC.bus_Data.LoopID });
                                foreach (var dic in commandDictionary)
                                {
                                    switch (dic.Key)
                                    {
                                        case "on_off":
                                            ControlBytesSend(Command.InstructionPanelKey, aC.bus_Data.SubnetID, aC.bus_Data.DeviceID, new byte[] { 3, aC.trait_on_off.value.ToString() == "on" ? (byte)1 : (byte)0, aC.bus_Data.LoopID });
                                            break;
                                        case "mode":
                                            ControlBytesSend(Command.InstructionPanelKey, aC.bus_Data.SubnetID, aC.bus_Data.DeviceID, new byte[] { 6, aC.curModeIndex, aC.bus_Data.LoopID });
                                            break;
                                        case "fan":
                                            ControlBytesSend(Command.InstructionPanelKey, aC.bus_Data.SubnetID, aC.bus_Data.DeviceID, new byte[] { 5, aC.curFanIndex, aC.bus_Data.LoopID });
                                            break;
                                        case "temp":
                                            byte modeKey = 4;
                                            switch (aC.curModeIndex)
                                            {
                                                case 3:
                                                    modeKey = 8;
                                                    break;
                                                case 0:
                                                    modeKey = 4;
                                                    break;
                                                case 1:
                                                    modeKey = 7;
                                                    break;
                                                case 4:
                                                    modeKey = 19;
                                                    break;
                                                case 2:
                                                    modeKey = 2;
                                                    break;
                                            }
                                            ControlBytesSend(Command.InstructionPanelKey, aC.bus_Data.SubnetID, aC.bus_Data.DeviceID, new byte[] { modeKey, Convert.ToByte(aC.trait_temp.value), aC.bus_Data.LoopID });
                                            break;
                                        default:
                                            MainPage.Log($"功能未支持 : {dic.Key}");
                                            break;
                                    }
                                }
                                break;
                            case FunctionType.FloorHeating:
                                var fh = function as FloorHeating;
                                if (fh.modeTemp.Count == 4)
                                {
                                    byte b1 = fh.trait_on_off.value.ToString() == "on" ? (byte)1 : (byte)0;
                                    if (fh.workMode > 0)
                                    {
                                        b1 += (byte)(16 + fh.workMode);
                                    }
                                    if (commandDictionary.ContainsKey("temp"))
                                    {
                                        var dicTempString = "";
                                        commandDictionary.TryGetValue("temp", out dicTempString);
                                        var dicTemp = Convert.ToByte(dicTempString);
                                        switch (fh.trait_mode.value)
                                        {
                                            case "day":
                                                fh.modeTemp["day"] = dicTemp;
                                                break;
                                            case "night":
                                                fh.modeTemp["night"] = dicTemp;
                                                break;
                                            case "away":
                                                fh.modeTemp["away"] = dicTemp;
                                                break;
                                            case "normal":
                                                fh.modeTemp["normal"] = dicTemp;
                                                break;
                                        }
                                    }
                                    ControlBytesSend(Command.SetFloorHeat, subnetId, deviceId, new byte[] { fh.bus_Data.LoopID, b1, (byte)fh.curTempType, fh.curModeIndex, fh.modeTemp["normal"], fh.modeTemp["day"], fh.modeTemp["night"], fh.modeTemp["away"], 0, 0 });
                                }
                                break;
                        }
                        break;
                    case FunctionCategory.Electrical:
                        switch (function.functionType)
                        {
                            case FunctionType.Fan:
                                var fan = function as Fan;
                                if (fan.trait_on_off.value.ToString() == "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.trait_on_off.value.ToString() == "on" ? (byte)100 : (byte)0 });
                                break;
                        }
                        break;
                }
                #region 发送命令立即更新UI
                HomePage.UpdataFunctionStates(function);
                RoomPage.UpdataStates(function);
                FunctionPage.UpdataStates(function);
                ClassificationPage.UpdataInfo(function);
                #endregion
            }
            catch (Exception ex)
            {
                MainPage.Log($"DataConversion_Bus Erorr : {ex.Message}");
            }
        }
        /// <summary>
        /// 读取bus设备数据
        /// </summary>
        static void ReadBusData(Function function)
        {
            try
            {
                var subnetId = function.bus_Data.SubnetID;
                var deviceId = function.bus_Data.DeviceID;
                var loopId = function.bus_Data.LoopID;
                switch (function.functionCategory)
                {
                    case FunctionCategory.Light:
                        switch (function.functionType)
                        {
                            case FunctionType.Relay:
                            case FunctionType.Dimmer:
                                ControlBytesSend(Command.ReadLightAllLoopBrightness, subnetId, deviceId, new byte[] { });
                                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.SetSingleLight, 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.SetSingleLight, subnetId, deviceId, new byte[] { s.bus_Data.LoopID });
                                break;
                        }
                        break;
                    case FunctionCategory.Sensor:
                        var sensor = function as Sensor;
                        ControlBytesSend(Command.ReadDeviceLoopInfo, subnetId, deviceId, new byte[] { (byte)5,
                                            (byte)((int)sensor.functionType % 256), sensor.bus_Data.LoopID });
                        break;
                }
            }
            catch (Exception ex)
            {
                MainPage.Log($"Read DataConversion_Bus Erorr : {ex.Message}");
            }
        }
    }
}