using System;
using System.Collections.Generic;
using Shared.Common;
using Shared.Phone.Device.CommonForm;
using Shared.Phone.UserView;
using ZigBee.Device;
namespace Shared.Phone.Device.Light
{
///
/// 插座
///
public class PlugControl:FrameLayout, ZigBee.Common.IStatus
{
#region ◆ 变量__________________________
///
/// 开关
///
private Button switchBtn;
///
/// The action.
///
public Action action;
///
/// The light image.
///
private Button deviceIMG;
///
/// 收藏按钮
///
private Button collectionBtn;
///
/// 传过来的设备
///
private DeviceUI device;
///
/// 传过来的房间
///
private Shared.Common.Room room;
///
/// 房间
///
private Button roomBtn;
///
/// 房间名
///
private Button roomName;
///
/// 网关
///
private ZbGateway zbGateway = null;
///
/// 是否发送控制命令成功了
///
private bool sendedControlCommand = false;
///
/// bodyFrameLayout
///
private FrameLayout bodyFrameLayout;
private Button StatuBtn;
#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.OnOffOutput)
{
if (common.DeviceStatusReport.CluterID == 6)
{
var light = deviceUI.CommonDevice as ToggleLight;
light.DeviceStatusReport = common.DeviceStatusReport;
//记录、更新状态
if (light.DeviceStatusReport.AttriBute == null || light.DeviceStatusReport.AttriBute.Count == 0)
{
return;
}
light.OnOffStatus = light.DeviceStatusReport.AttriBute[0].AttriButeData;
deviceIMG.IsSelected = switchBtn.IsSelected = light.OnOffStatus == 1;
StatuBtn.Text = CommonFormResouce.GetSwitchStatu(deviceIMG.IsSelected);
light.LastDateTime = DateTime.Now;
}
}
}
catch (Exception ex)
{
System.Console.WriteLine($"Error:{ex.Message}");
}
});
}
else if (typeTag == "OnlineStatusChange")
{
Application.RunOnMainThread(() =>
{
try
{
var deviceUI = device;
//设备为空
if (deviceUI.CommonDevice == null)
{
return;
}
//是否为当前设备
if (deviceUI.CommonDevice.DeviceEpoint != common.DeviceEpoint || deviceUI.CommonDevice.DeviceAddr != common.DeviceAddr)
{
return;
}
deviceUI.CommonDevice.IsOnline = common.IsOnline;
deviceIMG.IsSelected = switchBtn.IsSelected = deviceUI.CommonDevice.IsOnline == 1;
deviceUI.CommonDevice.LastDateTime = DateTime.Now;
}
catch (Exception ex)
{
System.Console.WriteLine($"Error:{ex.Message}");
}
});
}
}
#endregion
#region ◆ 初始化_____________________
///
/// 重写移除方法
///
public override void RemoveFromParent()
{
ZbGateway.StatusList.Remove(this);
//action();
//action = null;
RemoveUpdateControlDeviceStatuAction();
base.RemoveFromParent();
}
///
/// 构造方法
///
public PlugControl()
{
BackgroundColor = ZigbeeColor.Current.GXCBackgroundColor;
ZbGateway.StatusList.Add(this);
}
///
/// 显示界面
///
/// Device.
/// Room.
public void Show(DeviceUI dev, Shared.Common.Room room)
{
device = dev;
zbGateway = this.device.CommonDevice.Gateway;
this.room = room;
AddTop();
AddBodyView(device);
var light = dev.CommonDevice as ToggleLight;
//补上非远程
if (light.Gateway == null)
{
return;
}
if (light.Gateway.IsVirtual)
{
UserHomeView.ReadStatus(light, () =>
{
light.ReadOnOffStatus();
light.ReadAttri(Cluster_ID.Identify, AttriButeId.Switch);
});
}
else
{
//防止短时间内多次读取状态
if (CommonPage.ReadDeviceStatuSpan < (DateTime.Now - light.LastDateTime).TotalSeconds)
{
light.ReadOnOffStatus();
light.ReadAttri(Cluster_ID.Identify, AttriButeId.Switch);
}
}
var de = Shared.Common.Room.LoveRoomDeviceUIFilePathList.Find((obj) => obj == device.FileName);
if (de == null)
{
collectionBtn.IsSelected = false;
}
else
{
collectionBtn.IsSelected = true;
}
BindEvent();
}
#endregion
#region ◆ Add______________________________
///
/// AddTop
///
public void AddTop()
{
var top = new TopFrameLayout();
AddChidren(top);
top.InitTopview();
top.backButton.MouseUpEventHandler += (sender, e) =>
{
RemoveFromParent();
};
var sharedBtn = new Button
{
X = Application.GetRealWidth(850),
Width = Application.GetMinReal(69),
Height = Application.GetMinReal(69),
Gravity = Gravity.CenterVertical,
UnSelectedImagePath = "Item/Shared.png"
};
top.topView.AddChidren(sharedBtn);
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 += More;
}
///
/// AddBodyView
///
public void AddBodyView(DeviceUI device)
{
bodyFrameLayout = new FrameLayout()
{
Y = Application.GetRealHeight(184),
Height = Application.GetRealHeight(1737),
BackgroundColor = ZigbeeColor.Current.GXCGrayBackgroundColor,
};
AddChidren(bodyFrameLayout);
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
};
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 = CommonFormResouce.GetSwitchStatu(device.CommonDevice.IsOnline == 1)
};
itemView.AddChidren(StatuBtn);
deviceIMG = new Button()
{
Y = Application.GetRealHeight(344),
Width = Application.GetMinRealAverage(567),
Height = Application.GetMinRealAverage(567),
Gravity = Gravity.CenterHorizontal,
UnSelectedImagePath = "Light/Plug.png",
SelectedImagePath = "Light/PlugSelected.png",
IsSelected = (device.CommonDevice as ToggleLight).OnOffStatus == 1
};
itemView.AddChidren(deviceIMG);
switchBtn = new Button()
{
Y = Application.GetRealHeight(996),
Width = Application.GetMinRealAverage(81),
Height = Application.GetMinRealAverage(81),
Gravity = Gravity.CenterHorizontal,
UnSelectedImagePath = "Item/Switch.png",
SelectedImagePath = "Item/SwitchSelected.png",
IsSelected = (device.CommonDevice as ToggleLight).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
};
itemView.AddChidren(roomName);
}
#endregion
#region ◆ 绑定按钮_______________________
///
/// 绑定按钮的事件
///
private void BindEvent()
{
switchBtn.MouseUpEventHandler += Switch;
deviceIMG.MouseUpEventHandler += Switch;
collectionBtn.MouseUpEventHandler += Collection;
}
#endregion
#region ◆ 开关__________________________
///
/// 开关设置
///
/// Sender.
/// Event arguments.
private void Switch(object sender, MouseEventArgs eventArgs)
{
sendedControlCommand = false;
zbGateway.ReportAction += UpdateDeviceControllStatu;
switchBtn.IsSelected = !switchBtn.IsSelected;
deviceIMG.IsSelected = !deviceIMG.IsSelected;
StatuBtn.Text = CommonFormResouce.GetSwitchStatu(deviceIMG.IsSelected);
if (switchBtn.IsSelected == true)
{
(device.CommonDevice as ToggleLight).SwitchControl(1);
}
else
{
(device.CommonDevice as ToggleLight).SwitchControl(0);
}
//控制延时回调
DeviceUI.SendCommandDelayAction(device.CommonDevice, () =>
{
if (Parent == null)
{
return;
}
RemoveUpdateControlDeviceStatuAction();
if (sendedControlCommand == false)
{
DeviceUI.ShowStatuTip(R.MyInternationalizationString.FAIL);
}
});
}
#endregion
#region ◆ 设置__________________________
///
/// 更多设置
///
/// Sender.
/// E.
private void More(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.action = () =>
{
Show(device, room);
};
}
#endregion
#region ◆ 收藏__________________________
///
/// 收藏到主页
///
/// 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
#region ◆ 设备控制状态_____________________
///
/// 显示设备控制状态
///
/// Command.
/// Object value.
private void UpdateDeviceControllStatu(string command, object objValue)
{
if (Parent == null)
{
return;
}
if (command != "DeviceDefaultAck" || objValue == null)
{
return;
}
var tempDevice = (CommonDevice)objValue;
if (tempDevice.DeviceEpoint != this.device.CommonDevice.DeviceEpoint || tempDevice.DeviceAddr != this.device.CommonDevice.DeviceAddr)
{
//不是当前设备的推送,则不处理
return;
}
//标记已经发送控制命令到网关
sendedControlCommand = true;
//DeviceUI.ShowStatuTip(R.MyInternationalizationString.Success);
}
///
/// 移除更新控制设备的action
///
private void RemoveUpdateControlDeviceStatuAction()
{
//移除action
if (zbGateway != null)
{
zbGateway.ReportAction -= UpdateDeviceControllStatu;
}
}
#endregion
}
}