using Shared;
using HDL_ON.Stan;
using System;
using System.Collections.Generic;
using System.Text;
using HDL_ON.UI.CSS;
using HDL_ON.Entity;
using HDL_ON.DriverLayer;
namespace HDL_ON.UI
{
///
/// 涂鸦风扇的控制界面
///
public class TuyaFanPage : DeviceFunctionCardCommonForm
{
#region ■ 变量声明___________________________
///
/// 当前档位控件
///
private NormalViewControl btnNowGear = null;
///
/// 滑动条控件
///
private SeekBarImageControl seekBarContr = null;
///
/// 开关控件
///
private IconViewControl btnSwitch = null;
///
/// 风扇数据
///
private FanData fanData = new FanData();
#endregion
#region ■ 初始化_____________________________
///
/// 初始化白色区域的内容
///
public override void InitFrameWhiteContent()
{
base.SetTitleText(Language.StringByID(StringId.Fan));
//刷新当前设备的状态缓存
this.RefreshNowDeviceStatuMemory(this.device);
//初始化第一个索引页的内容
this.InitFrameWhiteContent1();
}
///
/// 初始化第一个索引页的内容
///
private void InitFrameWhiteContent1()
{
//当前风速
var btnNowSpeed = new NormalViewControl(FrameWhiteCentet1.Width, Application.GetRealHeight(21), false);
btnNowSpeed.Y = Application.GetRealHeight(164);
btnNowSpeed.TextAlignment = TextAlignment.Center;
btnNowSpeed.TextID = StringId.CurrentWindSpeed;
FrameWhiteCentet1.AddChidren(btnNowSpeed);
//当前档位值
this.btnNowGear = new NormalViewControl(200, 38, true);
btnNowGear.TextAlignment = TextAlignment.Center;
btnNowGear.TextSize = CSS_FontSize.EmphasisFontSize_ExLevel;
btnNowGear.Gravity = Gravity.CenterHorizontal;
btnNowGear.Y = btnNowSpeed.Bottom + Application.GetRealHeight(8);
FrameWhiteCentet1.AddChidren(btnNowGear);
//滑动条
this.seekBarContr = new SeekBarImageControl(215);
seekBarContr.Enable = false;
seekBarContr.Gravity = Gravity.CenterHorizontal;
seekBarContr.Y = btnNowGear.Bottom + Application.GetRealHeight(30);
seekBarContr.MinValue = 1;
seekBarContr.MaxValue = 15;
FrameWhiteCentet1.AddChidren(seekBarContr);
seekBarContr.Progress = this.fanData.SpeedLevel;
this.btnNowGear.Text = this.fanData.SpeedLevel + Language.StringByID(StringId.Gear);
//开关图标
this.btnSwitch = new IconViewControl(40);
btnSwitch.Gravity = Gravity.CenterHorizontal;
btnSwitch.Y = Application.GetRealHeight(468);
btnSwitch.UnSelectedImagePath = "Public/PowerClose.png";
btnSwitch.SelectedImagePath = "Public/PowerOpen.png";
FrameWhiteCentet1.AddChidren(btnSwitch);
btnSwitch.ButtonClickEvent += (sender, e) =>
{
//发送开关命令
this.SendSwitchComand();
};
//刷新界面状态
this.RefreshFormStatu(false);
//档
var strView = Language.StringByID(StringId.Gear);
seekBarContr.ProgressChangedEvent += (div, value) =>
{
this.btnNowGear.Text = value + strView;
//滑动中
if (div == 0) { this.fanData.IsProgressing = true; }
//滑动结束
else
{
this.fanData.IsProgressing = false;
this.fanData.ProgressEndTime = DateTime.Now;
//发送风速命令
this.SendSpeedComand(value);
}
};
}
#endregion
#region ■ 设备状态反馈_______________________
///
/// 设备状态反馈
///
///
public override void DeviceStatuPush(Function i_LocalDevice)
{
//不是同一个东西
if (this.device.sid != i_LocalDevice.sid) { return; }
//刷新当前设备的状态缓存
this.RefreshNowDeviceStatuMemory(i_LocalDevice);
//刷新界面状态
this.RefreshFormStatu(true);
}
#endregion
#region ■ 刷新界面状态_______________________
///
/// 刷新界面状态
///
private void RefreshFormStatu(bool checkTime)
{
//开关
if (this.btnSwitch.IsSelected != this.fanData.Open)
{
//相同不变更
this.btnSwitch.IsSelected = this.fanData.Open;
this.seekBarContr.Enable = this.fanData.Open;
}
//如果是停止滑动的情况
if (this.fanData.IsProgressing == false)
{
if (checkTime == true && (DateTime.Now - this.fanData.ProgressEndTime).TotalSeconds <= 2)
{
//2秒之内不变更滑动条的值(为了不让它来回跳动)
return;
}
if (this.seekBarContr.Progress != this.fanData.SpeedLevel)
{
//变更进度条的值
this.seekBarContr.Progress = this.fanData.SpeedLevel;
this.btnNowGear.Text = this.fanData.SpeedLevel + Language.StringByID(StringId.Gear);
}
}
}
#endregion
#region ■ 发送各种命令_______________________
///
/// 发送开关命令
///
private void SendSwitchComand()
{
this.btnSwitch.CanClick = false;
string statu = this.btnSwitch.IsSelected == true ? "off" : "on";
HdlThreadLogic.Current.RunThread(() =>
{
var dic = new Dictionary();
dic.Add(FunctionAttributeKey.OnOff, statu);
Control.Ins.SendWriteCommand(this.device, dic, true);
HdlThreadLogic.Current.RunMain(() =>
{
this.btnSwitch.CanClick = true;
});
});
}
///
/// 发送风速命令
///
private void SendSpeedComand(int value)
{
var dic = new Dictionary();
dic.Add("fan_speed_percent", value.ToString());
Control.Ins.SendWriteCommand(this.device, dic, true);
}
#endregion
#region ■ 一般方法___________________________
///
/// 刷新当前设备的状态缓存
///
private void RefreshNowDeviceStatuMemory(Function i_LocalDevice)
{
for (int i = 0; i < i_LocalDevice.attributes.Count; i++)
{
var data = i_LocalDevice.attributes[i];
//开关
if (data.key == "on_off") { this.fanData.Open = data.state == "on"; }
//风速档位
else if (data.key == "fan_speed_percent")
{
var value = data.state;
if (value != string.Empty)
{
this.fanData.SpeedLevel = Convert.ToInt32(value);
}
}
}
}
#endregion
#region ■ 结构体_____________________________
///
/// 风扇的数据
///
private class FanData
{
///
/// 是否打开
///
public bool Open = true;
///
/// 风速档位(1-15)
///
public int SpeedLevel = 1;
///
/// 是否正在滑动中
///
public bool IsProgressing = false;
///
/// 滑动结束的时间
///
public DateTime ProgressEndTime = DateTime.Now;
}
#endregion
}
}