using System;
using System.Collections.Generic;
using System.Text;
using HDL_ON.DriverLayer;
using HDL_ON.Entity;
using HDL_ON.Stan;
using HDL_ON.UI.CSS;
using Shared;
namespace HDL_ON.UI
{
public class AirFreshRelayControlPage : DeviceFunctionCardCommonForm
{
#region ■ 变量声明___________________________
///
/// 风速图标控件
///
private IconViewControl btnFan = null;
///
/// 风速文本控件
///
private NormalViewControl btnFanView = null;
///
/// 开关图标
///
private IconViewControl btnSwitch = null;
#endregion
#region ■ 初始化_____________________________
///
/// 初始化白色区域的内容
///
public override void InitFrameWhiteContent()
{
base.SetTitleText(Language.StringByID(StringId.AirFresh));
//刷新当前设备的状态缓存
this.RefreshNowDeviceStatuMemory(this.device);
//初始化第一个索引页的内容
this.InitFrameWhiteContent1();
Control.Ins.SendReadCommand(device);
}
///
/// 初始化第一个索引页的内容
///
private void InitFrameWhiteContent1()
{
//一个背景图片
var framePic = new FrameLayout();
framePic.Y = Application.GetRealHeight(87);
framePic.Width = this.GetPictrueRealSize(247);
framePic.Height = this.GetPictrueRealSize(247);
framePic.Gravity = Gravity.CenterHorizontal;
framePic.BackgroundImagePath = "FunctionIcon/AirFresh/AirFreshBgIcon.png";
this.FrameWhiteCentet1.AddChidren(framePic);
//风速图标
this.btnFan = new IconViewControl(28);
btnFan.Gravity = Gravity.CenterHorizontal;
btnFan.Y = Application.GetRealHeight(367);
this.FrameWhiteCentet1.AddChidren(btnFan);
List fanSpeedList = new List();
fanSpeedList = device.GetAttribute(FunctionAttributeKey.FanSpeed).value;
btnFan.MouseUpEventHandler += (sender, e) =>
{
if( !btnSwitch.IsSelected){return;}
var curFanSpeedIndex = fanSpeedList.IndexOf(device.GetAttrState(FunctionAttributeKey.FanSpeed));
var sendFanSpeedIndex = curFanSpeedIndex == fanSpeedList.Count - 1 ? 0 : 1+curFanSpeedIndex;
device.SetAttrState(FunctionAttributeKey.FanSpeed, fanSpeedList[sendFanSpeedIndex]);
new System.Threading.Thread(() =>
{
Dictionary d = new Dictionary();
d.Add(FunctionAttributeKey.FanSpeed, fanSpeedList[sendFanSpeedIndex]);
Control.Ins.SendWriteCommand(device, d);
})
{ IsBackground = true }.Start();
RefreshFanSpeed();
};
//风速文本
this.btnFanView = new NormalViewControl(this.FrameWhiteCentet1.Width / 3, Application.GetRealHeight(18), false);
btnFanView.Gravity = Gravity.CenterHorizontal;
btnFanView.Y = btnFan.Bottom + Application.GetRealHeight(6);
btnFanView.TextAlignment = TextAlignment.Center;
btnFanView.TextSize = CSS_FontSize.PromptFontSize_FirstLevel;
btnFanView.TextColor = CSS_Color.TextualColor;
this.FrameWhiteCentet1.AddChidren(btnFanView);
//开关图标
this.btnSwitch = new IconViewControl(32);
btnSwitch.Gravity = Gravity.CenterHorizontal;
btnSwitch.Y = Application.GetRealHeight(468);
btnSwitch.UnSelectedImagePath = "Public/PowerClose.png";
btnSwitch.SelectedImagePath = "Public/PowerOpen.png";
FrameWhiteCentet1.AddChidren(btnSwitch);
btnSwitch.MouseUpEventHandler = (sender, e) =>
{
btnSwitch.IsSelected = !btnSwitch.IsSelected;
device.trait_on_off.curValue = btnSwitch.IsSelected ? "on" : "off";
new System.Threading.Thread(() =>
{
Dictionary d = new Dictionary();
d.Add(FunctionAttributeKey.OnOff, device.trait_on_off.curValue.ToString());
Control.Ins.SendWriteCommand(device, d);
})
{ IsBackground = true }.Start();
};
//刷新界面状态
this.RefreshFormStatu();
if (fanSpeedList.Count > 0)
{
this.btnFan.UnSelectedImagePath = "FunctionIcon/AirFresh/Fan1.png";
this.btnFan.SelectedImagePath = "FunctionIcon/AirFresh/Fan1Select.png";
this.btnFanView.Text = Language.StringByID(StringId.LowWindSpeed);
}
}
#endregion
#region ■ 显示风速选择界面___________________
///
/// 显示风速选择界面
///
private void ShowFanSelectView()
{
//整个灰色界面
var frameBack = new Dialog();
var dialogBody = new NormalFrameLayout();
frameBack.AddChidren(dialogBody);
dialogBody.ButtonClickEvent = (sender, e) =>
{
frameBack.Close();
};
frameBack.Show();
//菜单控件(风速)
var menuContr = new DialogTitleMenuControl(3, Language.StringByID(StringId.FanSpeed));
menuContr.X = Application.GetRealWidth(209);
menuContr.Y = Application.GetRealHeight(231);
menuContr.Width = Application.GetRealWidth(160);
menuContr.Height = Application.GetRealHeight(199);
dialogBody.AddChidren(menuContr);
//1档
var iconPath = this.device.GetAttrState(FunctionAttributeKey.FanSpeed) == "low" ? "FunctionIcon/AirFresh/Fan1Select.png" : "FunctionIcon/AirFresh/Fan1.png";
menuContr.AddRowMenu(Language.StringByID(StringId.FanOneGear), iconPath, this.device.GetAttrState(FunctionAttributeKey.FanSpeed) == "low", () =>
{
frameBack.Close();
//发送档位命令
this.SendOtherComand(this.btnFan, FunctionAttributeKey.FanSpeed, "low");
});
//2档
iconPath = this.device.GetAttrState(FunctionAttributeKey.FanSpeed) == "medium" ? "FunctionIcon/AirFresh/Fan2Select.png" : "FunctionIcon/AirFresh/Fan2.png";
menuContr.AddRowMenu(Language.StringByID(StringId.FanTwoGear), iconPath, this.device.GetAttrState(FunctionAttributeKey.FanSpeed) == "medium", () =>
{
frameBack.Close();
//发送档位命令
this.SendOtherComand(this.btnFan, FunctionAttributeKey.FanSpeed, "medium");
});
//3档
iconPath = this.device.GetAttrState(FunctionAttributeKey.FanSpeed) == "high" ? "FunctionIcon/AirFresh/Fan3Select.png" : "FunctionIcon/AirFresh/Fan3.png";
menuContr.AddRowMenu(Language.StringByID(StringId.FanThreeGear), iconPath, this.device.GetAttrState(FunctionAttributeKey.FanSpeed) == "high", () =>
{
frameBack.Close();
//发送档位命令
this.SendOtherComand(this.btnFan, FunctionAttributeKey.FanSpeed, "high");
});
}
#endregion
#region ■ 设备状态反馈_______________________
///
/// 设备状态反馈
///
///
public override void DeviceStatuPush(Function i_LocalDevice)
{
//不是同一个东西
if (this.device.sid != i_LocalDevice.sid) { return; }
//刷新当前设备的状态缓存
this.RefreshNowDeviceStatuMemory(i_LocalDevice);
//刷新界面状态
this.RefreshFormStatu();
}
#endregion
#region ■ 刷新界面状态_______________________
///
/// 刷新界面状态
///
private void RefreshFormStatu()
{
//开关
this.btnSwitch.IsSelected = this.device.GetAttrState(FunctionAttributeKey.OnOff) == "on";
this.RefreshFanSpeed();
}
///
/// 刷新风速状态
///
private void RefreshFanSpeed()
{
//风速
if (this.device.GetAttrState(FunctionAttributeKey.FanSpeed) == "low")
{
this.btnFan.UnSelectedImagePath = "FunctionIcon/AirFresh/Fan1.png";
this.btnFan.SelectedImagePath = "FunctionIcon/AirFresh/Fan1Select.png";
this.btnFanView.Text = Language.StringByID(StringId.LowWindSpeed);
}
else if (this.device.GetAttrState(FunctionAttributeKey.FanSpeed) == "medium")
{
this.btnFan.UnSelectedImagePath = "FunctionIcon/AirFresh/Fan2.png";
this.btnFan.SelectedImagePath = "FunctionIcon/AirFresh/Fan2Select.png";
this.btnFanView.Text = Language.StringByID(StringId.MiddleWindSpeed);
}
else if (this.device.GetAttrState(FunctionAttributeKey.FanSpeed) == "high")
{
this.btnFan.UnSelectedImagePath = "FunctionIcon/AirFresh/Fan3.png";
this.btnFan.SelectedImagePath = "FunctionIcon/AirFresh/Fan3Select.png";
this.btnFanView.Text = Language.StringByID(StringId.HighWindSpeed);
}
this.btnFan.CanClick = this.btnFan.IsSelected = this.device.GetAttrState(FunctionAttributeKey.OnOff) == "on";
}
#endregion
#region ■ 发送各种命令_______________________
///
/// 发送其他命令
///
/// 按钮
/// 命令主键
/// 命令
private void SendOtherComand(IconViewControl btnIcon, string comadKey, string comadValue)
{
btnIcon.CanClick = false;
HdlThreadLogic.Current.RunThread(() =>
{
//获取发送命令的样板(bus协议是需要一次性把全部命令一起发送的)
var dic = this.GetSendComandSample();
dic[comadKey] = comadValue;
Control.Ins.SendWriteCommand(this.device, dic);
HdlThreadLogic.Current.RunMain(() =>
{
btnIcon.CanClick = true;
});
});
}
///
/// 获取发送命令的样板(bus协议是需要一次性把全部命令一起发送的)
///
///
private Dictionary GetSendComandSample()
{
var dic = new Dictionary();
//开关
dic[FunctionAttributeKey.OnOff] = this.device.GetAttrState(FunctionAttributeKey.OnOff);
//风速
dic[FunctionAttributeKey.FanSpeed] = this.device.GetAttrState(FunctionAttributeKey.FanSpeed);
return dic;
}
#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 == FunctionAttributeKey.OnOff) { this.device.SetAttrState(FunctionAttributeKey.OnOff, data.state); }
//风速
else if (data.key == FunctionAttributeKey.FanSpeed) { this.device.SetAttrState(FunctionAttributeKey.FanSpeed, data.state); }
}
}
#endregion
}
}