wei
2020-12-22 d07d62be7111d78b2aaa44c9cdee23a7d22b4dda
HDL_ON/Entity/Function/Light.cs
@@ -19,6 +19,9 @@
        public Light()
        {
        }
        [Newtonsoft.Json.JsonIgnore]
        FunctionAttributes trait_brightness;
        /// <summary>
        /// 亮度值
        /// </summary>
@@ -27,15 +30,60 @@
        {
            get
            {
                string b = "0";
                dicPropert.TryGetValue("brightness", out b);
                return Convert.ToInt32(b == "" ? "0" : b);
                if (trait_brightness == null)
                {
                    trait_brightness = attributes.Find((obj) => obj.key == "brightness");
                    //找不到属性需要声明一个,防止报错闪退
                    if (trait_brightness == null)
                    {
                        trait_brightness = new FunctionAttributes()
                        {
                            key = "brightness",
                            value = new List<string> { "up", "down" },
                            max = 100,
                            min = 0,
                            curValue = 0,
                        };
                    }
                    if (trait_brightness.curValue.ToString() == "{}")
                        trait_brightness.curValue = 0;
                }
                try
                {
                    string textValue = trait_brightness.curValue.ToString();
                    if (textValue == string.Empty)
                    {
                        trait_brightness.curValue = 0;
                    }
                    return Convert.ToInt32(trait_brightness.curValue);
                }
                catch
                {
                    trait_brightness.curValue = 0;
                    return 0;
                }
            }
            set
            {
                try
                {
                    dicPropert["brightness"] = value.ToString();
                    if (trait_brightness == null)
                    {
                        trait_brightness = attributes.Find((obj) => obj.key == "brightness");
                        //找不到属性需要声明一个,防止报错闪退
                        if (trait_brightness == null)
                        {
                            trait_brightness = new FunctionAttributes()
                            {
                                key = "brightness",
                                value = new List<string> { "up", "down" },
                                max = 100,
                                min = 0,
                                curValue = 0
                            };
                        }
                    }
                    trait_brightness.curValue = value;
                }
                catch
                {
@@ -43,150 +91,176 @@
                }
            }
        }
        /// <summary>
        /// RGB颜色
        /// 255255255
        /// 上一次打开的亮度
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public string color
        public int lastBrightness = 100;
        [Newtonsoft.Json.JsonIgnore]
        FunctionAttributes trait_fadeTime;
        /// <summary>
        /// 亮度值
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public int fadeTime
        {
            get
            {
                string c = "255255255";
                dicPropert.TryGetValue("color", out c);
                if (c.Length != 9)
                if (trait_fadeTime == null)
                {
                    dicPropert["color"] = "255255255";
                    trait_fadeTime = attributes.Find((obj) => obj.key == FunctionAttributeKey.FadeTime);
                    //找不到属性需要声明一个,防止报错闪退
                    if (trait_fadeTime == null)
                    {
                        trait_fadeTime = new FunctionAttributes()
                        {
                            key = FunctionAttributeKey.FadeTime,
                            value = new List<string> { "up", "down" },
                            max = 10,
                            min = 0,
                            curValue = 0
                        };
                    }
                }
                return c.Length == 9 ? c : "255255255";
                int result = 0;
                int.TryParse(trait_fadeTime.curValue.ToString(), out result);
                return result;
            }
            set
            {
                try
                {
                    dicPropert["color"] = value.ToString();
                    if (trait_fadeTime == null)
                    {
                        trait_fadeTime = attributes.Find((obj) => obj.key == FunctionAttributeKey.FadeTime);
                        //找不到属性需要声明一个,防止报错闪退
                        if (trait_fadeTime == null)
                        {
                            trait_fadeTime = new FunctionAttributes()
                            {
                                key = FunctionAttributeKey.FadeTime,
                                value = new List<string> { "up", "down" },
                                max = 100,
                                min = 0,
                                curValue = 0
                            };
                        }
                    }
                    trait_fadeTime.curValue = value;
                }
                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}");
                }
            }
        }
        #region RGB
        /// <summary>
        /// 获取rgb颜色
        /// </summary>
        /// <returns></returns>
        public int GetRGBcolor()
        {
            if (trait_color == null)
            {
                trait_color = attributes.Find((obj) => obj.key == FunctionAttributeKey.RGB);
                //找不到属性需要声明一个,防止报错闪退
                if (trait_color == null)
                {
                    trait_color = new FunctionAttributes()
                    {
                        key = "color",
                        value = new List<string> { "255,255,255" },
                        curValue = "255,255,255"
                    };
                }
                if (trait_color.curValue.ToString() == "{}")
                    trait_color.curValue = "255,255,255";
                int.TryParse(trait_color.curValue.ToString().Split(",")[0], out redColor);
                int.TryParse(trait_color.curValue.ToString().Split(",")[1], out greenColor);
                int.TryParse(trait_color.curValue.ToString().Split(",")[2], out blueColor);
            }
            int recolor = redColor * 256 * 256 + greenColor * 256 + blueColor;
            return recolor;
        }
        /// <summary>
        /// 获取rgb 控制字符串
        /// </summary>
        /// <returns></returns>
        public string GetRGBcolorString()
        {
            return redColor + "," + greenColor + "," + blueColor;
        }
        /// <summary>
        /// 拼接、获取A协议操作数据
        /// 设置rgb颜色
        /// </summary>
        public override JObject GetSendJObject(string command )
        public void SetRGBcolor(byte[] color)
        {
            var sendJob = new JObject();
            if (command == "write")
            {
                sendJob = new JObject { { "vendor_code", vendor_code }, { "Command", command }, { "Type", "device" } };
                JObject data = null;
                switch (functionType)
                {
                    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;
                }
                sendJob.Add("objects", data);
            }
            else if (command == "read")
            {
                sendJob = new JObject { { "vendor_code", vendor_code }, { "Command", command }, { "Type", "device" } };
                var data = new JObject {{ "sid", sid } };
                sendJob.Add("objects", data);
            }
            return sendJob;
            redColor = color[0];
            greenColor = color[1];
            blueColor = color[2];
        }
        [Newtonsoft.Json.JsonIgnore]
        public FunctionAttributes trait_color;
        int redColor = 255;
        int greenColor = 255;
        int blueColor = 255;
        public int RedColor {
            get
            {
                return redColor;
            }
        }
        public int GreenColor {
            get
            {
                return greenColor;
            }
        }
        public int BlueColor
        {
            get
            {
                return blueColor;
            }
        }
        #endregion
        #region CCT
        FunctionAttributes attr_CCT;
        /// <summary>
        /// 色温
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public FunctionAttributes Attr_CCT
        {
            get
            {
                if(attr_CCT == null)
                {
                    attr_CCT = attributes.Find((obj) => obj.key == FunctionAttributeKey.CCT);
                    if(attr_CCT== null)
                    {
                        attr_CCT = new FunctionAttributes()
                        {
                            key = FunctionAttributeKey.CCT,
                            value = new List<string> (),
                            max = 6500,
                            min = 2700,
                            curValue = 2700
                        };
                    }
                }
                var result = 2700;
                int.TryParse(attr_CCT.curValue.ToString(), out result);
                return attr_CCT;
            }
        }
        #endregion
    }
}