using System;
using Shared.Common;
using Shared.Phone.Device.CommonForm;
using Shared.Phone.Device.DeviceLogic;
using ZigBee.Device;
namespace Shared.Phone.Device.Light
{
public class DimmableLightControl:FrameLayout, ZigBee.Common.IStatus
{
#region ◆ 变量__________________________
///
/// The action.
///
public Action action;
/////
///// The top view.
/////
private CommonForm.TopFrameLayout top;
///
/// 传过来的设备
///
private DeviceUI device;
///
/// 传过来的ac
///
private ZigBee.Device.DimmableLight dimmableLight;
///
/// 传过来的房间
///
private Shared.Common.Room room;
///
/// bodyFrameLayout
///
private FrameLayout bodyFrameLayout;
///
/// 收藏按钮
///
private Button collectionBtn;
///
/// 开关
///
private Button switchBtn;
///
/// 滑条
///
private WaveSeekBar levelSeekBar;
private Button StatuBtn;
///
/// 房间
///
private Button roomBtn;
///
/// 房间名
///
private Button roomName;
///
/// MaxLevel
///
private const int MaxLevel = 254;
///
/// IsDrawerLockMode
///
public bool IsDrawerLockMode;
///
/// ProgressBtn
///
Button ProgressBtn;
///
/// ProgressBtnY
///
int ProgressBtnY;
#endregion
#region ◆ 接口__________________________
///
/// 处理变化事件 --将弃用 改用DeviceInfoChange()
///
/// The changed.
/// Common.
public void Changed(CommonDevice common)
{
}
///
/// Changeds the IL ogic status.
///
/// Logic.
public void ChangedILogicStatus(ZigBee.Device.Logic logic)
{
//throw new NotImplementedException();
}
///
/// Changeds the IS cene status.
///
/// Scene.
public void ChangedISceneStatus(Scene scene)
{
//throw new NotImplementedException();
}
///
/// 设备状态更新接口
/// type:如果为 DeviceInComingRespon:设备新上报
/// type:如果为 IASInfoReport:RemoveDeviceRespon
/// type:如果为 DeviceStatusReport:设备上报
/// type:如果为 IASInfoReport:IAS安防信息上报
/// type:如果为 OnlineStatusChange: 设备在线状态更新
///
/// Common.
/// Type tag.
public void DeviceInfoChange(CommonDevice common, string typeTag)
{
if (typeTag == "DeviceStatusReport")
{
Application.RunOnMainThread(() =>
{
try
{
var deviceUI = device;
//设备为空
if (deviceUI.CommonDevice == null)
{
return;
}
//是否为当前设备
if (deviceUI.CommonDevice.DeviceEpoint != common.DeviceEpoint || deviceUI.CommonDevice.DeviceAddr != common.DeviceAddr)
{
return;
}
if (deviceUI.CommonDevice.Type == DeviceType.DimmableLight)
{
if (common.DeviceStatusReport.CluterID == 6)
{
dimmableLight = deviceUI.CommonDevice as DimmableLight;
dimmableLight.DeviceStatusReport = common.DeviceStatusReport;
if (dimmableLight.DeviceStatusReport.AttriBute == null || dimmableLight.DeviceStatusReport.AttriBute.Count == 0)
{
return;
}
dimmableLight.OnOffStatus = dimmableLight.DeviceStatusReport.AttriBute[0].AttriButeData;
switchBtn.IsSelected = dimmableLight.OnOffStatus == 1;
StatuBtn.Text = switchBtn.IsSelected ? $"{Language.StringByID(R.MyInternationalizationString.CurrentLightLevel)} { device.GetDeviceStatu()}" : $"{Language.StringByID(R.MyInternationalizationString.Current)} {Language.StringByID(R.MyInternationalizationString.Shut)}";
if (switchBtn.IsSelected == true)
{
levelSeekBar.IsClickable = true;
levelSeekBar.SetProgressBarColors(ZigbeeColor.Current.GXCWaveSeekBarColor_Start, ZigbeeColor.Current.GXCWaveSeekBarColor_End);
}
else
{
levelSeekBar.IsClickable = false;
levelSeekBar.SetProgressBarColors(ZigbeeColor.Current.GXCWaveSeekBarUnSelectedColor, ZigbeeColor.Current.GXCWaveSeekBarUnSelectedColor);
}
dimmableLight.LastDateTime = DateTime.Now;
}
//亮度
if (common.DeviceStatusReport.CluterID == 8)
{
dimmableLight = deviceUI.CommonDevice as ZigBee.Device.DimmableLight;
dimmableLight.DeviceStatusReport = common.DeviceStatusReport;
var attriButeList = dimmableLight.DeviceStatusReport.AttriBute;
if (attriButeList == null || attriButeList.Count == 0)
{
return;
}
switch (attriButeList[0].AttributeId)
{
case 0:
//此属性表明当前亮度程度
dimmableLight.Level = attriButeList[0].AttriButeData;
dimmableLight.LastDateTime = DateTime.Now;
levelSeekBar.Progress = (int)(dimmableLight.Level*1.0/MaxLevel*100);
StatuBtn.Text = switchBtn.IsSelected ? $"{Language.StringByID(R.MyInternationalizationString.CurrentLightLevel)} { device.GetDeviceStatu()}" : $"{Language.StringByID(R.MyInternationalizationString.Current)} {Language.StringByID(R.MyInternationalizationString.Shut)}";
break;
}
}
}
}
catch (Exception ex)
{
System.Console.WriteLine($"Error:{ex.Message}");
}
});
}
else if (typeTag == "OnlineStatusChange")
{
}
}
#endregion
#region ◆ 重写移除_______________________
///
/// Removes from parent.
///
public override void RemoveFromParent()
{
ZbGateway.StatusList.Remove(this);
UserView.HomePage.Instance.ScrollEnabled = true;
action(device, room);
action = null;
if (IsDrawerLockMode)
{
CommonPage.Instance.IsDrawerLockMode = false;
}
base.RemoveFromParent();
}
#endregion
#region ◆ 构造方法_______________________
///
/// 构造方法
///
public DimmableLightControl()
{
UserView.HomePage.Instance.ScrollEnabled = false;
BackgroundColor = ZigbeeColor.Current.GXCBackgroundColor;
ZbGateway.StatusList.Add(this);
}
#endregion
#region ◆ 显示界面_______________________
///
/// 显示界面
///
/// Device.
/// Room.
public void Show(DeviceUI device, Shared.Common.Room room)
{
this.device = device;
this.room = room;
this.dimmableLight = device.CommonDevice as ZigBee.Device.DimmableLight;
//添加topview
AddTopView();
//添加midview
AddBodyView(this.device);
//绑定事件
BindEvent();
//收藏
InitCollection();
if (dimmableLight.Gateway.IsVirtual)
{
//发送读取状态命令
UserView.UserHomeView.ReadStatus(dimmableLight, () =>
{
ReadDeviceAttributeLogic.Instance.SendDimmableLightStatuComand(device.CommonDevice);
});
}
else
{
//防止短时间内多次读取设备状态
if ((DateTime.Now - dimmableLight.LastDateTime).TotalSeconds > CommonPage.ReadDeviceStatuSpan)
{
ReadDeviceAttributeLogic.Instance.SendDimmableLightStatuComand(device.CommonDevice);
}
}
}
#endregion
#region ◆ topview_______________________
///
/// Adds the top view.
///
private void AddTopView()
{
top = new CommonForm.TopFrameLayout();
AddChidren(top);
top.InitTopview();
top.backButton.MouseUpEventHandler += (sender, e) =>
{
RemoveFromParent();
};
var moreBtn = new Button
{
X = Application.GetRealWidth(953),
Width = Application.GetMinReal(69),
Height = Application.GetMinReal(69),
Gravity = Gravity.CenterVertical,
UnSelectedImagePath = "Item/More.png"
};
top.topView.AddChidren(moreBtn);
moreBtn.MouseUpEventHandler += MoreEvent;
}
///
/// 更多设置
///
/// Sender.
/// E.
private void MoreEvent(object sender, MouseEventArgs e)
{
var detailInfo = new Device.CommonForm.DeviceDetailInfo { };
UserView.HomePage.Instance.AddChidren(detailInfo);
UserView.HomePage.Instance.PageIndex += 1;
detailInfo.Show(device, room);
detailInfo.EditAction += (curDev, curRoom) =>
{
Show(curDev, curRoom);
};
}
#endregion
#region ◆ midview_______________________
///
/// AddBodyView
///
public void AddBodyView(DeviceUI device)
{
bodyFrameLayout = new FrameLayout()
{
Y = Application.GetRealHeight(184),
Height = Application.GetRealHeight(1737),
BackgroundColor = ZigbeeColor.Current.GXCGrayBackgroundColor,
};
AddChidren(bodyFrameLayout);
AddItemview();
}
#endregion
#region ◆ itemview_______________________
///
/// Adds the itemview.
///
private void AddItemview()
{
var itemView = new FrameLayout()
{
Y = Application.GetRealHeight(115),
Width = Application.GetRealWidth(965),
Height = Application.GetRealHeight(1316),
Radius = (uint)Application.GetRealHeight(CommonFormResouce.BigFormRadius),
Gravity = Gravity.CenterHorizontal,
BackgroundColor = ZigbeeColor.Current.GXCBackgroundColor
};
bodyFrameLayout.AddChidren(itemView);
collectionBtn = new Button()
{
X = Application.GetRealWidth(850),
Y = Application.GetRealHeight(46),
Width = Application.GetMinReal(69),
Height = Application.GetMinReal(69),
UnSelectedImagePath = "Item/Collection.png",
SelectedImagePath = "Item/CollectionSelected.png"
};
itemView.AddChidren(collectionBtn);
var deviceNameBtn = new Button()
{
Y = Application.GetRealHeight(46),
Width = Application.GetRealWidth(500),
Height = Application.GetRealHeight(60),
Gravity = Gravity.CenterHorizontal,
Text = device.CommonDevice.DeviceEpointName,
TextColor = ZigbeeColor.Current.GXCTextBlackColor,
TextSize = 15,
IsBold=true
};
itemView.AddChidren(deviceNameBtn);
StatuBtn = new Button
{
Y = Application.GetRealHeight(118),
Width = Application.GetRealWidth(600),
Height = Application.GetRealHeight(60),
Gravity = Gravity.CenterHorizontal,
TextColor = ZigbeeColor.Current.GXCTextGrayColor,
Text = dimmableLight.Level>0?$"{Language.StringByID(R.MyInternationalizationString.CurrentLightLevel)} { device.GetDeviceStatu()}" :$"{Language.StringByID(R.MyInternationalizationString.Current)} {Language.StringByID(R.MyInternationalizationString.Shut)}",
TextSize=10
};
itemView.AddChidren(StatuBtn);
levelSeekBar = new WaveSeekBar()
{
Y = Application.GetRealHeight(377),
Width = Application.GetRealWidth(271),
Height = Application.GetRealHeight(533),
Gravity = Gravity.CenterHorizontal,
WavePadding = Application.GetRealWidth(8),
MaxValue=100,
Progress = (int)(dimmableLight.Level*1.0/MaxLevel*100),
CornerRadius= Application.GetRealHeight(58)
};
itemView.AddChidren(levelSeekBar);
if (dimmableLight.OnOffStatus == 1)
{
levelSeekBar.IsClickable = true;
levelSeekBar.SetProgressBarColors(ZigbeeColor.Current.GXCWaveSeekBarColor_Start, ZigbeeColor.Current.GXCWaveSeekBarColor_End);
}
else
{
levelSeekBar.IsClickable = false;
levelSeekBar.SetProgressBarColors(ZigbeeColor.Current.GXCWaveSeekBarUnSelectedColor, ZigbeeColor.Current.GXCWaveSeekBarUnSelectedColor);
}
levelSeekBar.OnStartTrackingTouchEvent += (sender, e) =>
{
OnProgressButtonMove(levelSeekBar.Progress);
levelSeekBar.IsProgressTextShow = false;
ProgressBtn.Visible = true;
};
levelSeekBar.OnStopTrackingTouchEvent += (sender, e) =>
{
ProgressBtn.Visible = false;
levelSeekBar.IsProgressTextShow = true;
dimmableLight.SetLevel((int)(levelSeekBar.Progress * MaxLevel / 100.0));
};
levelSeekBar.OnProgressChangedEvent += (send2, e2) =>
{
OnProgressButtonMove(e2);
//判断是否300ms屏蔽
//if (levelSeekBar.IsProgressChangeDelay()) return;
//dimmableLight.SetLevel((int)(levelSeekBar.Progress * MaxLevel / 100.0));
};
InitProgressBtn(itemView);
switchBtn = new Button()
{
Y = Application.GetRealHeight(1005),
Width = Application.GetMinRealAverage(81),
Height = Application.GetMinRealAverage(81),
Gravity = Gravity.CenterHorizontal,
UnSelectedImagePath = "Item/Switch.png",
SelectedImagePath = "Item/SwitchSelected.png",
IsSelected = (device.CommonDevice as DimmableLight).OnOffStatus == 1
};
itemView.AddChidren(switchBtn);
var roomBG = new Button
{
Y = Application.GetRealHeight(1178 - 50),
Height = Application.GetRealHeight(138 + 50),
BackgroundColor = ZigbeeColor.Current.GXCBlackBackgroundColor,
Radius = (uint)Application.GetRealHeight(CommonFormResouce.BigFormRadius)
};
itemView.AddChidren(roomBG);
var roomBG2 = new Button
{
Y = Application.GetRealHeight(1178 - 50),
Height = Application.GetRealHeight(50),
BackgroundColor = ZigbeeColor.Current.GXCBackgroundColor,
};
itemView.AddChidren(roomBG2);
roomBtn = new Button()
{
X = Application.GetRealWidth(CommonFormResouce.X_Left),
Y = Application.GetRealHeight(1207),
Width = Application.GetMinReal(80),
Height = Application.GetMinReal(80),
UnSelectedImagePath = "Item/Room.png"
};
itemView.AddChidren(roomBtn);
roomName = new Button()
{
X = Application.GetRealWidth(150),
Y = Application.GetRealHeight(1224),
Width = Application.GetRealWidth(400),
Height = Application.GetRealHeight(50),
Text = room.Name,
TextAlignment = TextAlignment.CenterLeft,
TextColor = ZigbeeColor.Current.GXCTextWhiteColor,
TextSize = 12
};
itemView.AddChidren(roomName);
}
///
/// OnProgressButtonMove
///
private void OnProgressButtonMove(int mProgress)
{
ProgressBtn.Y = ProgressBtnY + levelSeekBar.NowProgressY;
ProgressBtn.Text = mProgress + "%";
}
///
/// 添加进度按钮
///
private void InitProgressBtn(FrameLayout layout)
{
ProgressBtnY = levelSeekBar.Y - Application.GetMinReal(154);
ProgressBtn = new Button()
{
Y = ProgressBtnY,
Width = Application.GetMinReal(135),
Height = Application.GetMinReal(104),
UnSelectedImagePath = "Item/ProgressBubbles.png",
Visible = false,
Gravity = Gravity.CenterHorizontal,
TextSize = 14,
IsBold = true,
TextColor = ZigbeeColor.Current.GXCTextWhiteColor
};
layout.AddChidren(ProgressBtn);
}
#endregion
#region ◆ 绑定按钮_______________________
///
/// 绑定按钮的事件
///
private void BindEvent()
{
switchBtn.MouseUpEventHandler += Switch_MouseUpEvent;
collectionBtn.MouseUpEventHandler += Collection;
}
#endregion
#region ◆ 开关__________________________
///
/// 开关设置
///
/// Sender.
/// Event arguments.
private void Switch_MouseUpEvent(object sender, MouseEventArgs eventArgs)
{
switchBtn.IsSelected = !switchBtn.IsSelected;
StatuBtn.Text = switchBtn.IsSelected ? $"{Language.StringByID(R.MyInternationalizationString.CurrentLightLevel)} { device.GetDeviceStatu()}" : $"{Language.StringByID(R.MyInternationalizationString.Current)} {Language.StringByID(R.MyInternationalizationString.Shut)}";
if (switchBtn.IsSelected == true)
{
dimmableLight.SwitchControl(1);
levelSeekBar.IsClickable = true;
levelSeekBar.SetProgressBarColors(ZigbeeColor.Current.GXCWaveSeekBarColor_Start, ZigbeeColor.Current.GXCWaveSeekBarColor_End);
}
else
{
dimmableLight.SwitchControl(0);
levelSeekBar.IsClickable = false;
levelSeekBar.SetProgressBarColors(ZigbeeColor.Current.GXCWaveSeekBarUnSelectedColor, ZigbeeColor.Current.GXCWaveSeekBarUnSelectedColor);
}
}
#endregion
#region ◆ 收藏__________________________
///
/// Inits the collection.
///
private void InitCollection()
{
if (Common.Room.CurrentRoom.IsCollectInRoom(device.FileName) == false)
{
collectionBtn.IsSelected = false;
}
else
{
collectionBtn.IsSelected = true;
}
}
///
/// 收藏到主页
///
/// Sender.
/// E.
private void Collection(object sender, MouseEventArgs e)
{
//collection
if ((sender as Button).IsSelected)
{
Shared.Common.Room.CurrentRoom.GetLoveRoom().DeleteDevice(device.FileName);
(sender as Button).IsSelected = false;
}
else
{
Shared.Common.Room.CurrentRoom.GetLoveRoom().AddDevice(device.FileName);
(sender as Button).IsSelected = true;
}
}
#endregion
}
}