From 9ab5bd54fadd8fc9c542b48c99a117a4e182669b Mon Sep 17 00:00:00 2001
From: wei <kaede@kaededeMacBook-Air.local>
Date: 星期四, 14 十月 2021 16:37:56 +0800
Subject: [PATCH] 智能空开能源界面数据更新

---
 HDL_ON/Entity/Function/Light.cs |  242 ++++++++++++++++--------------------------------
 1 files changed, 80 insertions(+), 162 deletions(-)

diff --git a/HDL_ON/Entity/Function/Light.cs b/HDL_ON/Entity/Function/Light.cs
index e155396..021d510 100644
--- a/HDL_ON/Entity/Function/Light.cs
+++ b/HDL_ON/Entity/Function/Light.cs
@@ -4,190 +4,108 @@
 
 namespace HDL_ON.Entity
 {
-    public class Light : Function
+    public class Light 
     {
-        /*
-        鐏厜绫伙細trait: [switch,brightness,color,cct,delay,fadeTime]
-        灞炴��	鎻忚堪
-        switch	on/off;
-        brightness	0-100;
-        color	int (red (0-255) green (0-255) blue (0-255))
-        cct	int (warm light(0-255) cold light (0-255) )
-        delay	0-3600s
-        fadetime	0-3600s
-        */
-        public Light()
+        #region RGB
+        /// <summary>
+        /// 鑾峰彇rgb棰滆壊
+        /// </summary>
+        /// <returns></returns>
+        public int GetRGBcolor(string rgbString)
         {
+            //var color = function.GetAttrState(FunctionAttributeKey.RGB).Split(",");
+            //if(!string.IsNullOrEmpty(rgbString))
+            //{
+               var color = rgbString.Split(",");
+            //}
+            if(color.Length!= 3)
+            {
+                color = new string[] {"255", "255", "255" };
+            }
+            int redColor = 0;
+            int greenColor = 0;
+            int blueColor = 0;
+
+            int.TryParse(color[0], out redColor);
+            int.TryParse(color[1], out greenColor);
+            int.TryParse(color[2], out blueColor);
+
+            int recolor = redColor * 256 * 256 + greenColor * 256 + blueColor;
+
+            return recolor;
         }
         /// <summary>
-        /// 浜害鍊�
+        /// 鑾峰彇rgb棰滆壊鏁扮粍
         /// </summary>
-        [Newtonsoft.Json.JsonIgnore]
-        public int brightness
+        /// <param name="function"></param>
+        /// <returns></returns>
+        public byte[] GetRGBbytes(Function function)
         {
-            get
+            var color = function.GetAttrState(FunctionAttributeKey.RGB).Split(",");
+
+            if (color.Length != 3)
             {
-                string b = "0";
-                dicPropert.TryGetValue("brightness", out b);
-                return Convert.ToInt32(b == "" ? "0" : b);
+                color = new string[] { "100", "100", "100" };
             }
-            set
-            {
-                try
-                {
-                    dicPropert["brightness"] = value.ToString();
-                    MainPage.Log($"brightness 鏁版嵁鍒锋柊{value}.");
-                }
-                catch
-                {
-                    MainPage.Log("brightness 鏁版嵁鍒锋柊澶辫触.");
-                }
-            }
+            byte redColor = 0;
+            byte greenColor = 0;
+            byte blueColor = 0;
+
+            byte.TryParse(color[0], out redColor);
+            byte.TryParse(color[1], out greenColor);
+            byte.TryParse(color[2], out blueColor);
+
+            return new byte[] { redColor, greenColor, blueColor };
         }
 
         /// <summary>
-        /// RGB棰滆壊
-        /// 255255255
+        /// 鑾峰彇rgb 鎺у埗瀛楃涓�
         /// </summary>
-        [Newtonsoft.Json.JsonIgnore]
-        public string color
+        /// <returns></returns>
+        public string GetRGBcolorString(Function function)
         {
-            get
-            {
-                string c = "255255255";
-                dicPropert.TryGetValue("color", out c);
-                if (c.Length != 9)
-                {
-                    dicPropert["color"] = "255255255";
-                }
-                return c.Length == 9 ? c : "255255255";
-            }
-            set
-            {
-                try
-                {
-                    dicPropert["color"] = value.ToString();
-                }
-                catch
-                {
-                    MainPage.Log("color 鏁版嵁鍒锋柊澶辫触.");
-                }
-            }
+            var color = function.GetAttrState(FunctionAttributeKey.RGB).Split(",");
+            return color[0] + "," + color[1] + "," + color[2];
         }
-        [Newtonsoft.Json.JsonIgnore]
-        public byte redColor
-        {
-            get {
-                try
-                {
-                    return Convert.ToByte(color.Substring(0, 3));
-                }
-                catch (Exception ex)
-                {
-                    MainPage.Log($"Get red color error : {ex.Message}");
-                    return 0;
-                }
-            }
-            set
-            {
-                try
-                {
-                    dicPropert["color"] = dicPropert["color"].ToString().Remove(0, 3).Insert(0, value.ToString().PadLeft(3, '0'));
-                }
-                catch (Exception ex)
-                {
-                    MainPage.Log($"set red color error : {ex.Message}");
-                }
-            }
-        }
-        [Newtonsoft.Json.JsonIgnore]
-        public byte greenColor
-        {
-            get
-            {
-                try
-                {
-                    return Convert.ToByte(color.Substring(3, 3));
-                }
-                catch (Exception ex)
-                {
-                    MainPage.Log($"Get green color error : {ex.Message}");
-                    return 0;
-                }
-            }
-            set
-            {
-                try
-                {
-                    dicPropert["color"] = dicPropert["color"].ToString().Remove(3, 3).Insert(3, value.ToString().PadLeft(3, '0'));
-                }
-                catch (Exception ex)
-                {
-                    MainPage.Log($"set green color error : {ex.Message}");
-                }
-            }
-        }
-        [Newtonsoft.Json.JsonIgnore]
-        public byte blueColor
-        {
-            get
-            {
-                try
-                {
-                    return Convert.ToByte(color.Substring(6, 3));
-                }
-                catch (Exception ex)
-                {
-                    MainPage.Log($"Get blue color error : {ex.Message}");
-                    return 0;
-                }
-            }
-            set
-            {
-                try
-                {
-                    dicPropert["color"] = dicPropert["color"].ToString().Remove(6, 3).Insert(6, value.ToString().PadLeft(3, '0'));
-                }
-                catch (Exception ex)
-                {
-                    MainPage.Log($"set blue color error : {ex.Message}");
-                }
-            }
-        }
-
 
         /// <summary>
-        /// 鎷兼帴銆佽幏鍙朅鍗忚鎿嶄綔鏁版嵁
+        /// 鑾峰彇rgb棰滆壊
         /// </summary>
-        public override JObject GetSendJObject(CommandType_A command )
+        /// <param name="index">0:red 1:green 2:blue</param>
+        /// <returns></returns>
+        public int GetColor( int index,Function function)
         {
-            var sendJob = new JObject();
-            if (command == CommandType_A.write)
+            int color = 0;
+            if (index <= 2)
             {
-
-                sendJob = new JObject { { "vendor_code", vendor_code }, { "Command", command.ToString() }, { "Type", "device" } };
-                JObject data = null;
-                switch (functionType)
+                try
                 {
-                    case FunctionType.Relay:
-                        data = new JObject { { "sid", sid }, { "switch", on_off } };
-                        break;
-                    case FunctionType.Dimmer:
-                        data = new JObject { { "sid", sid }, { "brightness", brightness } };
-                        break;
-                    case FunctionType.RGB:
-                        data = new JObject { { "sid", sid }, { "brightness", brightness }, { "color", color } };
-                        break;
+                    int.TryParse(function.GetAttrState(FunctionAttributeKey.RGB).Split(",")[index], out color);
                 }
-                sendJob.Add("objects", data);
+                catch { }
             }
-            else if (command == CommandType_A.read)
-            {
-                sendJob = new JObject { { "vendor_code", vendor_code }, { "Command", command.ToString() }, { "Type", "device" } };
-                var data = new JObject {{ "sid", sid } };
-                sendJob.Add("objects", data);
-            }
-            return sendJob;
+            return color;
         }
+
+        /// <summary>
+        /// 璁剧疆rgb棰滆壊
+        /// </summary>
+        public void SetRGBcolor(byte[] color,Function function)
+        {
+            function.SetAttrState(FunctionAttributeKey.RGB, color[0] + "," + color[1] + "," + color[2]);
+        }
+
+        /// <summary>
+        /// 璁剧疆鑹叉俯
+        /// </summary>
+        /// <param name="cct"></param>
+        /// <param name="function"></param>
+        public void SetCCT(byte[] cct,Function function)
+        {
+            function.SetAttrState(FunctionAttributeKey.CCT, cct[0] * 256 + cct[1]);
+        }
+
+        #endregion
+
     }
 }

--
Gitblit v1.8.0