/// <summary> /// 设置电源状态 /// </summary> /// <param name="power">电源状态</param> /// <param name="effect">渐变效果</param> /// <param name="duration">渐变时长</param> /// <returns></returns> private async Task <bool> Device_set_power_Async(YeelightPower power, YeelightTransformEffect effect, int duration) { // 函数名 string method = YeelightMethod.set_power.ToString(); // 判断是否支持该函数 不支持直接抛异常 if (!this.DeviceIsSupportFunc(method)) { throw new Exception("不支持该函数"); } // 参数错误 if (effect == YeelightTransformEffect.smooth && duration < 30) { throw new Exception("参数错误"); } // 效果是突然时不需要渐变时间 if (effect == YeelightTransformEffect.sudden) { duration = 0; } // 组建参数 JArray param = new JArray(); param.Add(power.ToString()); param.Add(effect.ToString()); param.Add(duration); // 接收回应 string resp = await this.SendDataAsync(this.DeviceRequestBuildHelper(method, param)); return(this.DeviceResponseIsSuccess(resp)); }
/// <summary> /// 更新设备信息私有函数 /// </summary> /// <returns>更新的数据字段</returns> private async Task <IList <string> > UpdateDeviceInfoHelper() { // 获取设备数据 Dictionary <string, object> propList = await this.Device_get_prop_Async(); // 更新的数据字段 List <string> updateParamList = new List <string>(); if (propList["power"] != null && this.power != (YeelightPower)propList["power"]) { this.power = (YeelightPower)propList["power"]; updateParamList.Add("power"); } if (propList["bright"] != null && this.bright != (int)propList["bright"]) { this.bright = (int)propList["bright"]; updateParamList.Add("bright"); } if (propList["color_mode"] != null && this.color_mode != (YeelightColorMode)propList["color_mode"]) { this.color_mode = (YeelightColorMode)propList["color_mode"]; updateParamList.Add("color_mode"); } if (propList["ct"] != null && this.ct != (int)propList["ct"]) { this.ct = (int)propList["ct"]; updateParamList.Add("ct"); } if (propList["rgb"] != null && this.rgb != (int)propList["rgb"]) { this.rgb = (int)propList["rgb"]; this.r = (int)propList["r"]; this.g = (int)propList["g"]; this.b = (int)propList["b"]; updateParamList.Add("rgb"); updateParamList.Add("r"); updateParamList.Add("g"); updateParamList.Add("b"); } if (propList["hue"] != null && this.hue != (int)propList["hue"]) { this.hue = (int)propList["hue"]; updateParamList.Add("hue"); } if (propList["sat"] != null && this.sat != (int)propList["sat"]) { this.sat = (int)propList["sat"]; updateParamList.Add("sat"); } if (propList["name"] != null && this.name != (string)propList["name"]) { this.name = (string)propList["name"]; updateParamList.Add("name"); } return(updateParamList); }
/// <summary> /// 设置电源状态私有函数 /// </summary> /// <param name="power">电源状态</param> /// <returns>是否成功</returns> private async Task <bool> SetPowerHelper(YeelightPower power) { return(await this.Device_set_power_Async(power, YeelightTransformEffect.smooth, 300)); }
/// <summary> /// 解析并初始化设备信息 /// </summary> private void ParseRawDeviceInfo(string rawDevInfo) { try { // 保存 this.rawDevInfo = rawDevInfo; // 解析地址信息 var matchLocation = LOCATION_REGEX.Match(rawDevInfo); this.ip = matchLocation.Groups[1].ToString(); this.port = matchLocation.Groups[2].ToString(); // 解析ID var matchID = ID_REGEX.Match(rawDevInfo); this.id = matchID.Groups[1].ToString(); // 解析设备类型 var matchModel = MODEL_REGEX.Match(rawDevInfo); string rawModel = matchModel.Groups[1].ToString(); switch (rawModel) { case "mono": this.model = YeelightModel.mono; break; case "color": this.model = YeelightModel.color; break; case "stripe": this.model = YeelightModel.stripe; break; case "desklamp": this.model = YeelightModel.desklamp; break; } // 解析支持函数 var matchSupport = SUPPORT_REGEX.Match(rawDevInfo); string rawSupport = matchSupport.Groups[1].ToString(); this.support = rawSupport.Split(' ').ToDictionary(k => k, v => true); // 解析电源状态 var matchPower = POWER_REGEX.Match(rawDevInfo); string rawPower = matchPower.Groups[1].ToString(); this.power = rawPower == "on" ? YeelightPower.on : YeelightPower.off; // 解析亮度 var matchBright = BRIGHT_REGEX.Match(rawDevInfo); this.bright = Convert.ToInt32(matchBright.Groups[1].ToString()); // 解析颜色模式 var matchColorMode = COLOR_MODE_REGEX.Match(rawDevInfo); string rawColorMode = matchColorMode.Groups[1].ToString(); switch (rawColorMode) { case "1": this.color_mode = YeelightColorMode.color; break; case "2": this.color_mode = YeelightColorMode.temperature; break; case "3": this.color_mode = YeelightColorMode.hsv; break; } // 解析色温 var matchColorTemperature = BRIGHT_REGEX.Match(rawDevInfo); this.ct = Convert.ToInt32(matchColorTemperature.Groups[1].ToString()); // 解析RGB var matchRGB = RGB_REGEX.Match(rawDevInfo); this.rgb = Convert.ToInt32(matchRGB.Groups[1].ToString()); // RGB十六进制转换 var rgb16 = Convert.ToString(this.rgb, 16); // 补零 for (int i = 0, length = rgb16.Length; i < 6 - length; i++) { rgb16 = "0" + rgb16; } // 单独保存 this.r = Convert.ToInt32(rgb16.Substring(0, 2), 16); this.g = Convert.ToInt32(rgb16.Substring(2, 2), 16); this.b = Convert.ToInt32(rgb16.Substring(4, 2), 16); // 解析色调 var matchHue = HUE_REGEX.Match(rawDevInfo); this.hue = Convert.ToInt32(matchHue.Groups[1].ToString()); // 解析饱和度 var matchSat = SAT_REGEX.Match(rawDevInfo); this.sat = Convert.ToInt32(matchSat.Groups[1].ToString()); // 解析名字 var matchName = NAME_REGEX.Match(rawDevInfo); try { this.name = Encoding.UTF8.GetString(Convert.FromBase64String(matchName.Groups[1].ToString())); } catch (Exception) { this.name = null; } } catch (Exception) { throw; } }
/// <summary> /// 设置电源状态 /// </summary> /// <param name="power">电源状态</param> /// <returns>是否成功</returns> public IAsyncOperation <bool> SetPower(YeelightPower power) { return(this.SetPowerHelper(power).AsAsyncOperation()); }