From af1cb3ecd0f4b0589e00b28f7f9edccf39e6e12b Mon Sep 17 00:00:00 2001
From: wxr <464027401@qq.com>
Date: 星期四, 10 九月 2020 16:15:11 +0800
Subject: [PATCH] 202009101

---
 HDL_ON/DriverLayer/Control.cs |  523 ++++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 468 insertions(+), 55 deletions(-)

diff --git a/HDL_ON/DriverLayer/Control.cs b/HDL_ON/DriverLayer/Control.cs
index c696878..6f71392 100644
--- a/HDL_ON/DriverLayer/Control.cs
+++ b/HDL_ON/DriverLayer/Control.cs
@@ -1,13 +1,42 @@
 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
 {
     public partial class Control
     {
+        /// <summary>
+        /// 鍙戦�佸懡浠�
+        /// </summary>
+        public static void Send(Function function)
+        {
+            switch(function.functionCategory)
+            {
+                case FunctionCategory.Scene:
+                    ControlScene(function as Scene);
+                    break;
+
+            }
+        }
+
+        /// <summary>
+        /// 鍦烘櫙鎺у埗鍏ュ彛
+        /// </summary>
+        /// <param name="scene"></param>
+        static void ControlScene(Scene scene)
+        {
+            if (DB_ResidenceData.residenceData.GatewayType == 0)
+            {
+                ControlBusScenes(scene);
+            }
+            else
+            {
+                ControlAProtocolScene(scene);
+            }
+        }
 
         /// <summary>
         /// 鍙戦�佸懡浠�
@@ -16,102 +45,486 @@
         /// </summary>
         /// <param name="commandString"></param>
         /// <param name="function"></param>
-        /// <param name="sendCount"></param>
         /// <returns></returns>
-        public static byte[] Send(string commandString, Function function, int sendCount = 3)
+        public static void SendWriteCommand(Function function, Dictionary<string, string> commandDictionary)
         {
-            if (function.bus_Data!=null)
+            function.usageCount++;
+            function.refreshTime = DateTime.Now;
+            if (function.bus_Data != null)
             {
                 try
                 {
-                    var ssd = function.GetSendJObject(commandString);
-                    var revBytes = DataConversion_Bus(ssd, function.bus_Data, function);
-                    return revBytes;
+                    WriteBusData(function, commandDictionary);
                 }
                 catch (Exception ex)
                 {
-                    MainPage.Log($"鍙戦�佹暟鎹紓甯革細 {ex.Message}");
+                    MainPage.Log($"鍙戦�佹暟鎹紓甯�: {ex.Message}");
                 }
             }
             else
             {
-
-                //var jObject = new JObject {
-                //{ "DeviceAddr", 11},
-                //{ "Epoint", 22},
-                //    { "Cluster_ID", 8 },
-                //    { "Command", 33},
-                //    { "SendMode", 2 }
-                //};
-                //var data = new JObject {
-                //    { "Level", 1 },
-                //    { "TransitionTime", 0 }
-                //};
-                //jObject.Add("Data", data);
-                //return revBytes;
+                //var commandObj = function.GetSendJObject(CommandType_A.write);
+                //var commandString = Newtonsoft.Json.JsonConvert.SerializeObject(commandObj);
+                //var commandBytes = CommonPage.MyEncodingUTF8.GetBytes(commandString);
+                //MqttCommon.MqttRemoteSend(commandBytes);
             }
-            return null;
         }
 
+        /// <summary>
+        /// 鍙戦�佽鍙栧懡浠�
+        /// 鑷姩鍒ゆ柇鏄惁涓篈鍗忚璁惧
+        /// </summary>
+        /// <param name="function"></param>
+        public static void SendReadCommand(Function function)
+        {
+            //function.usageCount++;
+            function.refreshTime = DateTime.Now;
+            if (function.bus_Data != null)
+            {
+                try
+                {
+                    ReadBusData(function);
+                }
+                catch (Exception ex)
+                {
+                    MainPage.Log($"鍙戦�佹暟鎹紓甯�: {ex.Message}");
+                }
+            }
+            else
+            {
+                //var commandObj = function.GetSendJObject(CommandType_A.read);
+                //var commandString = Newtonsoft.Json.JsonConvert.SerializeObject(commandObj);
+                //var commandBytes = CommonPage.MyEncodingUTF8.GetBytes(commandString);
+                //MqttCommon.MqttRemoteSend(commandBytes);
+            }
+        }
 
         /// <summary>
-        /// 灏咥鍗忚鎺у埗鏁版嵁杞崲绋媌us鍗忚鐨勬帶鍒舵暟鎹�
+        /// 鎺у埗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 = 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 = 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);
+                            }
+                            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[] { s.bus_Data.LoopID, s.trait_on_off.value.ToString() == "on" ? (byte)100 : (byte)0 });
+                        break;
+                }
+            }
+        }
+        /// <summary>
+        /// 鎺у埗a鍗忚鍦烘櫙
+        /// </summary>
+        /// <param name="scene"></param>
+        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";
+            //杞崲鎴恇us闇�瑕佺殑鏁版嵁鏍煎紡
+        }
+
+        /// <summary>
+        /// 鎺у埗bus璁惧
         /// </summary>
         /// <param name="commandString"></param>
         /// <returns></returns>
-        static byte[] DataConversion_Bus(JObject commandString,BusData bus_Data, Function function)
+        static void WriteBusData(Function function, Dictionary<string, string> commandDictionary)
         {
-            //鍙嶉:
-            //{
-            //"Namespace": "HDL", "Command": "writeRespone",
-            //鈥淭ype鈥�:鈥漝evice鈥�, "objects": [{
-            //"sid": "0001002019101209000025670100010001鈥�, "statue": "success鈥�
-            //}] }
             try
             {
-                var subnetId = bus_Data.SubnetID;
-                var deviceId = bus_Data.DeviceID;
-                var loopId = bus_Data.LoopID;
+                var subnetId = function.bus_Data.SubnetID;
+                var deviceId = function.bus_Data.DeviceID;
+                var loopId = function.bus_Data.LoopID;
 
-                var jobj = JObject.FromObject(commandString.GetValue("objects"));
-                var functionType = jobj.GetValue("sid").ToString().Substring(22, 2);//鎴彇24锛�25瀛楃鑾峰彇鎺у埗鐨勮澶囩殑绫诲瀷
-                switch (commandString.GetValue("Command").ToString())
+
+                switch (function.functionCategory)
                 {
-                    case "write":
-                        switch ((FunctionType)Enum.ToObject(typeof(FunctionType), Convert.ToInt32(functionType)))
+                    case FunctionCategory.Light:
+                        var light = function as Light;
+                        switch (light.functionType)
                         {
-                            case FunctionType.Light:
-                                if (jobj.ContainsKey("switch"))//缁х數鍣�
+                            case FunctionType.RGB:
+                                MainPage.Log($"rgb level : {light.brightness}");
+                                byte b = 100;//寮�鍏虫搷浣滀緷鎹畂n_off瀛楁锛屽疄闄呭紑鍏冲�间緷鎹産rightness锛屽綋on_off涓烘墦寮�锛宐rightness涓嶈兘涓�0
+                                if (light.trait_on_off.value.ToString() == "off")
                                 {
-                                    var state = jobj.GetValue("switch").ToString() == "0" ? (byte)0 : (byte)100;
-                                    var bytes = new byte[] { Convert.ToByte(loopId), state, 0, 0 };
-                                    return ControlBytesSendHasReturn(Command.SetSingleLight, subnetId, deviceId, bytes);
+                                    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};
+                                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, 0 });
+                                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 });
+                                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 (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 "read":
-                        switch ((FunctionType)Enum.ToObject(typeof(FunctionType), Convert.ToInt32(functionType)))
+                    case FunctionCategory.Thermostat:
+                        switch (function.functionType)
                         {
-                            case FunctionType.Light:
-                                if (jobj.ContainsKey("switch"))//缁х數鍣�
+                            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, (byte)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)
                                 {
-                                    var state = jobj.GetValue("switch").ToString() == "0" ? (byte)0 : (byte)100;
-                                    var bytes = new byte[] { Convert.ToByte(loopId), state, 0, 0 };
-                                    return ControlBytesSendHasReturn(Command.ReadLightSingleLoopBrightness, subnetId, deviceId, bytes);
+                                    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);
+                                    }
+                                    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 });
+                                }
+                                //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.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;
                 }
 
+
+                foreach (var dic in commandDictionary)
+                {
+                    switch (dic.Key)
+                    {
+                        case "on_off":
+                            break;
+                        case "brightness":
+                            break;
+                        case "color":
+                            break;
+                        default:
+                            MainPage.Log($"鍔熻兘鏈敮鎸� : {dic.Key}");
+                            break;
+                            //case "cct":
+                            //case "delay":
+                            //case "fade_time":
+                            //case "open_level":
+                            //case "lock":
+                            //case "ico":
+                            //case "mode":
+                            //case "fan":
+                            //case "temp":
+                            //case "swing":
+                            //case "set_ point":
+                            //case "pm25":
+                            //case "volume":
+                            //case "vol_step":
+                            //case "source":
+                            //case "treble":
+                            //case "bass":
+                            //case "playlist":
+                            //case "song_name":
+                            //case "current_status":
+                            //case "enable":
+                            //case "lux":
+                            //case "adjust_value":
+                            //case "range":
+                            //case "humidity":
+                            //case "type":
+                            //case "state":
+                            //case "sensitivity":
+                            //case "pm25value":
+                    }
+                }
+
+
+
             }
-            catch (Exception ex) {
+            catch (Exception ex)
+            {
                 MainPage.Log($"DataConversion_Bus Erorr : {ex.Message}");
             }
-
-            return null;
         }
 
+        /// <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.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;
+                    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}");
+            }
+        }
     }
 }
\ No newline at end of file

--
Gitblit v1.8.0