From b69d7735274b8d0f741da8a6bb8b8e1347477a5a Mon Sep 17 00:00:00 2001
From: wxr <464027401@qq.com>
Date: 星期四, 19 三月 2020 17:14:16 +0800
Subject: [PATCH] 20200319

---
 HDL_ON/Entity/Function/Light/Light.cs |  216 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 209 insertions(+), 7 deletions(-)

diff --git a/HDL_ON/Entity/Function/Light/Light.cs b/HDL_ON/Entity/Function/Light/Light.cs
index 5455156..7aeb593 100644
--- a/HDL_ON/Entity/Function/Light/Light.cs
+++ b/HDL_ON/Entity/Function/Light/Light.cs
@@ -6,9 +6,35 @@
 {
     public class Light : Function
     {
+        /*
+        鐏厜绫伙細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()
         {
-            bus_DeviceType = "Relay";
+        }
+        /// <summary>
+        /// 鑾峰彇鍔熻兘绫诲瀷
+        /// </summary>
+        /// <returns></returns>
+        protected override string GetFunctionType()
+        {
+            string type = "Relay";
+            if (PropertyArray.Contains("brightness"))
+            {
+                type = "Dimmer";
+                if (PropertyArray.Contains("color"))
+                {
+                    type = "RGB";
+                }
+            }
+            return type;
         }
 
         /// <summary>
@@ -16,9 +42,172 @@
         /// 0:鍏�
         /// 1:寮�
         /// </summary>
-        public int state = 0;
+        [Newtonsoft.Json.JsonIgnore]
+        public int on_off
+        {
+            get
+            {
+                try
+                {
+                    string o = "0";
+                    dicPropert.TryGetValue("on_off", out o);
+                    return Convert.ToInt32(o == "" ? "0" : o);
+                }
+                catch
+                {
+                    MainPage.Log("on_off 鏁版嵁鑾峰彇澶辫触.");
+                    dicPropert.TryAdd("on_off", "0");
+                    return 0;
+                }
+            }
+            set
+            {
+                try
+                {
+                    dicPropert["on_off"] = value.ToString();
+                }
+                catch
+                {
+                    MainPage.Log("on_off 鏁版嵁鍒锋柊澶辫触.");
+                    dicPropert.TryAdd("on_off", value.ToString());
+                }
+            }
+        }
+        /// <summary>
+        /// 浜害鍊�
+        /// </summary>
+        [Newtonsoft.Json.JsonIgnore]
+        public int brightness
+        {
+            get
+            {
+                string b = "0";
+                dicPropert.TryGetValue("brightness", out b);
+                return Convert.ToInt32(b == "" ? "0" : b);
+            }
+            set
+            {
+                try
+                {
+                    dicPropert["brightness"] = value.ToString();
+                }
+                catch
+                {
+                    MainPage.Log("brightness 鏁版嵁鍒锋柊澶辫触.");
+                }
+            }
+        }
 
-        public List<string> PropertyArray = new List<string>();
+        /// <summary>
+        /// RGB棰滆壊
+        /// 255255255
+        /// </summary>
+        [Newtonsoft.Json.JsonIgnore]
+        public string color
+        {
+            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 鏁版嵁鍒锋柊澶辫触.");
+                }
+            }
+        }
+        [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>
         /// 鎷兼帴銆佽幏鍙朅鍗忚鎿嶄綔鏁版嵁
@@ -29,14 +218,27 @@
             if (command == "write")
             {
 
-                sendJob = new JObject { { "Namespace", a_Protocol_Namespace }, { "Command", command }, { "Type", "device" } };
-                var data = new JObject { { "Switch", state }, { "sid", sid } };
+                sendJob = new JObject { { "vendor_code", a_Protocol_Namespace }, { "Command", command }, { "Type", "device" } };
+                JObject data = null;
+                switch (functionType)
+                {
+                    case "Relay"://缁х數鍣ㄦ帶鍒�
+                        data = new JObject { { "sid", sid }, { "switch", on_off } };
+                        break;
+                    case "Dimmer":
+                        data = new JObject { { "sid", sid }, { "brightness", brightness } };
+                        break;
+                    case "RGB":
+                        data = new JObject { { "sid", sid }, { "brightness", brightness }, { "color", color } };
+                        break;
+                }
                 sendJob.Add("objects", data);
-                return sendJob;
             }
             else if(command == "read")
             {
-
+                sendJob = new JObject { { "vendor_code", a_Protocol_Namespace }, { "Command", command }, { "Type", "device" } };
+                var data = new JObject {{ "sid", sid } };
+                sendJob.Add("objects", data);
             }
             return sendJob;
         }

--
Gitblit v1.8.0