From 4cc04639fbadde3a7f3b9875353df0d90fc09043 Mon Sep 17 00:00:00 2001
From: wxr <464027401@qq.com>
Date: 星期六, 18 一月 2020 14:33:40 +0800
Subject: [PATCH] 节前备份

---
 HDL_ON/DriverLayer/Control.cs |  113 +++++++++++++++++++++++++++++++-------------------------
 1 files changed, 63 insertions(+), 50 deletions(-)

diff --git a/HDL_ON/DriverLayer/Control.cs b/HDL_ON/DriverLayer/Control.cs
index b2e310e..ddc4a95 100644
--- a/HDL_ON/DriverLayer/Control.cs
+++ b/HDL_ON/DriverLayer/Control.cs
@@ -1,13 +1,8 @@
 using System;
 using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using HDL_ON.DAL;
-using HDL_ON.DAL.Net;
 using HDL_ON.Entity;
-using HDL_ON.Entity.Enumerative;
-using HDL_ON.UI;
 using Newtonsoft.Json.Linq;
+using Shared;
 
 namespace HDL_ON
 {
@@ -15,18 +10,15 @@
     {
 
 
-        public static byte[] Send(JObject commandString, Function function, int sendCount)
+        public static byte[] Send(string commandString, Function function, int sendCount = 3)
         {
-            if (string.IsNullOrEmpty(function.A_Protocol))
+            if (!string.IsNullOrEmpty(function.bus_Data))
             {
                 try
                 {
-                    var subnetID = Convert.ToByte(JObject.FromObject(function.Bus_Protocol).GetValue("SubnetID"));
-                    var deviceID = Convert.ToByte(JObject.FromObject(function.Bus_Protocol).GetValue("DeviceID"));
-                    byte[] bytes = DataConversion_Bus(commandString);
-
-                    var sss = ControlBytesSendHasReturn(Command.SetSingleLight, subnetID, deviceID, bytes, sendCount);
-                    return sss;
+                    var ssd = function.GetSendJObject(commandString);
+                    var revBytes = DataConversion_Bus(ssd, function.bus_Data, function);
+                    return revBytes;
                 }
                 catch (Exception ex)
                 {
@@ -35,20 +27,20 @@
             }
             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 null;
+                //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;
             }
             return null;
         }
@@ -59,7 +51,7 @@
         /// </summary>
         /// <param name="commandString"></param>
         /// <returns></returns>
-        static byte[] DataConversion_Bus(JObject commandString)
+        static byte[] DataConversion_Bus(JObject commandString,string bus_Data, Function function)
         {
             //鍙嶉:
             //{
@@ -67,32 +59,53 @@
             //鈥淭ype鈥�:鈥漝evice鈥�, "objects": [{
             //"sid": "0001002019101209000025670100010001鈥�, "statue": "success鈥�
             //}] }
-            byte[] sendBytes; 
-
-            switch (commandString.GetValue("Command").ToString())
+            try
             {
-                case "write":
-                    var jobj = JObject.FromObject(commandString.GetValue("objects"));
-                    var functionType = jobj.GetValue("sid").ToString().Substring(24, 2);
-                    switch ((FunctionType)Enum.ToObject(typeof(FunctionType), Convert.ToInt32(functionType)))
-                    {
-                        case FunctionType.Light:
-                            if (jobj.ContainsKey("Switch"))//缁х數鍣�
-                            {
-                                if (jobj.ContainsKey("Dimmer"))
-                                {//璋冨厜鍣�
-
-                                }
-                                else//缁х數鍣�
+                var sss = JObject.Parse(bus_Data);
+                var subnetId = Convert.ToByte(sss.GetValue("SubnetID").ToString());
+                var deviceId = Convert.ToByte(sss.GetValue("DeviceID").ToString());
+                var loopId = sss.GetValue("LoopID").ToString();
+                switch (commandString.GetValue("Command").ToString())
+                {
+                    case "write":
+                        var jobj = JObject.FromObject(commandString.GetValue("objects"));
+                        var functionType = jobj.GetValue("sid").ToString().Substring(24, 2);//鎴彇24锛�25瀛楃鑾峰彇鎺у埗鐨勮澶囩殑绫诲瀷
+                        switch ((FunctionType)Enum.ToObject(typeof(FunctionType), Convert.ToInt32(functionType)))
+                        {
+                            case FunctionType.Light:
+                                if (jobj.ContainsKey("Switch"))//缁х數鍣�
                                 {
-
+                                    var state = jobj.GetValue("Switch").ToString() == "0" ? (byte)0 : (byte)100;
+                                    if (jobj.ContainsKey("Dimmer"))
+                                    {   //璋冨厜鍣�
+                                        var brightness = Convert.ToByte(jobj.GetValue("Dimmer").ToString());
+                                        if (brightness > 0)
+                                        {
+                                            (function as Dimmer).brightness = brightness;
+                                        }
+                                        (function as Dimmer).state = state;
+                                        var bytes = new byte[] { Convert.ToByte(loopId), brightness, 0, 0 };
+                                        (function as Dimmer).lastState = Language.StringByID(StringId.Brightness) + " : " + brightness.ToString() + "%";
+                                        return ControlBytesSendHasReturn(Command.SetSingleLight, subnetId, deviceId, bytes);
+                                    }
+                                    else//缁х數鍣�
+                                    {
+                                        var bytes = new byte[] { Convert.ToByte(loopId), state, 0, 0 };
+                                        return ControlBytesSendHasReturn(Command.SetSingleLight, subnetId, deviceId, bytes);
+                                    }
                                 }
-                            }
-                            break;
-                    }
-                    break;
-            }
+                                break;
+                        }
+                        break;
+                    case "read":
 
+                        break;
+                }
+
+            }
+            catch (Exception ex) {
+                MainPage.Log($"DataConversion_Bus Erorr : {ex.Message}");
+            }
 
             return null;
         }

--
Gitblit v1.8.0