From 611786df5108dca0bdcff03834cc285cba4b8e61 Mon Sep 17 00:00:00 2001
From: 陈嘉乐 <cjl@hdlchina.com.cn>
Date: 星期二, 13 十月 2020 16:48:43 +0800
Subject: [PATCH] 2020-10-13-1

---
 HDL_ON/DriverLayer/Control.cs |  306 +++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 250 insertions(+), 56 deletions(-)

diff --git a/HDL_ON/DriverLayer/Control.cs b/HDL_ON/DriverLayer/Control.cs
index 812afed..5f6ad52 100644
--- a/HDL_ON/DriverLayer/Control.cs
+++ b/HDL_ON/DriverLayer/Control.cs
@@ -1,5 +1,7 @@
 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;
@@ -8,6 +10,10 @@
 {
     public partial class Control
     {
+        public static void Send(Function function)
+        {
+            ControlBusScenes(function as Scene);
+        }
 
         /// <summary>
         /// 鍙戦�佸懡浠�
@@ -16,17 +22,16 @@
         /// </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 Send(CommandType_A commandType, Function function)
         {
+            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;
+                    ControlBusData(function, commandType);
                 }
                 catch (Exception ex)
                 {
@@ -35,89 +40,278 @@
             }
             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);
+                var commandString = Newtonsoft.Json.JsonConvert.SerializeObject(commandObj);
+                var commandBytes = CommonPage.MyEncodingUTF8.GetBytes(commandString);
+                MqttCommon.MqttRemoteSend(commandBytes);
             }
-            return null;
         }
 
+        /// <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(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;
+                        }
+                    }
+                }
+            }
+        }
 
         /// <summary>
         /// 灏咥鍗忚鎺у埗鏁版嵁杞崲绋媌us鍗忚鐨勬帶鍒舵暟鎹�
         /// </summary>
         /// <param name="commandString"></param>
         /// <returns></returns>
-        static byte[] DataConversion_Bus(JObject commandString,BusData bus_Data, Function function)
+        static void ControlBusData(Function function, CommandType_A command)
         {
-            //鍙嶉:
-            //{
-            //"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 (command)
                 {
-                    case "write":
-                        switch ((FunctionType)Enum.ToObject(typeof(FunctionType), Convert.ToInt32(functionType)))
+                    case CommandType_A.write:
+                        switch (function.functionCategory)
                         {
-                            case FunctionType.Light:
-                                if (jobj.ContainsKey("brightness"))
+                            case FunctionCategory.Light:
+                                var light = function as Light;
+                                switch (light.functionType)
                                 {
-                                    var state = Convert.ToByte(jobj.GetValue("brightness"));
-                                    var bytes = new byte[] { Convert.ToByte(loopId), state, 0, 0 };
-                                    return ControlBytesSendHasReturn(Command.SetSingleLight, subnetId, deviceId, bytes);
+                                    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;
                                 }
-                                else if (jobj.ContainsKey("switch"))//缁х數鍣�
+                                break;
+                            case FunctionCategory.Curtain:
+                                var curtain = function as Curtain;
+                                switch (curtain.functionType)
                                 {
-                                    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);
+                                    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 });
+                                        }
+                                        else if (curtain.on_off == "on")
+                                        {
+                                            ControlBytesSend(Command.SetCurtainModelStutas, subnetId, deviceId, new byte[] { 17, 100 });
+                                        }
+                                        else if (curtain.on_off == "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.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 "read":
-                        switch ((FunctionType)Enum.ToObject(typeof(FunctionType), Convert.ToInt32(functionType)))
+                    case CommandType_A.read:
+                        switch (function.functionCategory)
                         {
-                            case FunctionType.Light:
-                                if (jobj.ContainsKey("switch"))//缁х數鍣�
+                            case FunctionCategory.Light:
+                                switch (function.functionType)
                                 {
-                                    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);
+                                    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;
+                            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;
                         }
                         break;
                 }
-
             }
-            catch (Exception ex) {
+            catch (Exception ex)
+            {
                 MainPage.Log($"DataConversion_Bus Erorr : {ex.Message}");
             }
-
-            return null;
         }
-
     }
 }
\ No newline at end of file

--
Gitblit v1.8.0