using System;
using System.Collections.Generic;
using Shared.Common;
using Shared.Phone.UserView;
using ZigBee.Device;
namespace Shared.Phone.Device.Light
{
///
/// 灯光控制界面
///
public class LightControl:FrameLayout,ZigBee.Common.IStatus
{
#region ◆ 变量__________________________
///
/// 设备是否在线标识--Online
///
private readonly string DeviceStatus_Online = "Online";
///
/// 设备状态开关标识--Switch
///
private readonly string DeviceStatus_OnOffStatus = "Switch";
///
/// 开关
///
private Button switchBtn;
///
/// The action.
///
public Action action;
///
/// The light view.
///
private FrameLayout deviceView;
///
/// The light image.
///
private Button deviceIMG;
///
/// 收藏按钮
///
private Button collectionBtn;
///
/// 传过来的设备
///
private DeviceUI device;
///
/// 传过来的房间
///
private Shared.Common.Room room;
///
/// 更多设置
///
private Button moreBtn;
///
/// 房间
///
private Button roomBtn;
///
/// 房间名
///
private Button roomName;
///
/// 网关
///
private ZbGateway zbGateway = null;
///
/// 是否发送控制命令成功了
///
private bool sendedControlCommand = false;
#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 = deviceView.Tag as DeviceUI;
//设备为空
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 as ToggleLight).DeviceStatusReport.CluterID == 6)
{
var light = deviceUI.CommonDevice as ToggleLight;
light.DeviceStatusReport = (common as ToggleLight).DeviceStatusReport;
//记录、更新状态
if(light.DeviceStatusReport.AttriBute==null || light.DeviceStatusReport.AttriBute.Count==0)
{
return;
}
light.OnOffStatus = light.DeviceStatusReport.AttriBute[0].AttriButeData;
switchBtn.IsSelected = light.OnOffStatus == 1;
light.LastDateTime = DateTime.Now;
}
//***新改***设备状态上报中,当CluterID=3,证明设备在线,直接标记
else if ((common as ToggleLight).DeviceStatusReport.CluterID == 3)
{
var light = deviceUI.CommonDevice as ToggleLight;
light.IsOnline = 1;
deviceIMG.IsSelected = light.IsOnline == 1;
light.LastDateTime = DateTime.Now;
}
}
}
catch (Exception ex)
{
System.Console.WriteLine($"Error:{ex.Message}");
}
});
}
else if (typeTag == "OnlineStatusChange")
{
Application.RunOnMainThread(() =>
{
try
{
var deviceUI = deviceView.Tag as DeviceUI;
//设备为空
if (deviceUI.CommonDevice == null)
{
return;
}
//是否为当前设备
if (deviceUI.CommonDevice.DeviceEpoint != common.DeviceEpoint || deviceUI.CommonDevice.DeviceAddr != common.DeviceAddr)
{
return;
}
if (deviceUI.CommonDevice.Type == DeviceType.OnOffOutput)
{
var light = deviceUI.CommonDevice as ToggleLight;
light.IsOnline = (common as ToggleLight).IsOnline;
deviceIMG.IsSelected = light.IsOnline == 1;
light.LastDateTime = DateTime.Now;
}
}
catch (Exception ex)
{
System.Console.WriteLine($"Error:{ex.Message}");
}
});
}
}
#endregion
#region ◆ 初始化________________________
///
/// 显示界面
///
/// Device.
/// Room.
public void Show(DeviceUI device,Shared.Common.Room room)
{
#region topview
var topBGView = new FrameLayout()
{
Height = Application.GetRealHeight(CommonPage.Navigation_Height),
BackgroundColor = ZigbeeColor.Current.GXCTopViewBackgroundColor
};
AddChidren(topBGView);
var topView = new FrameLayout()
{
Y = Application.GetRealHeight(CommonPage.NavigationTitle_Y),
Height = Application.GetRealHeight(CommonPage.Navigation_Height - CommonPage.NavigationTitle_Y),
BackgroundColor = ZigbeeColor.Current.GXCTopViewBackgroundColor,
};
AddChidren(topView);
moreBtn = new Device.CommonForm.SelectedStatuButton()
{
X=Application.GetRealWidth(CommonPage.AppRealWidth-150),
Width=Application.GetMinReal(110),
Height=Application.GetMinReal(110),
Gravity=Gravity.CenterVertical,
UnSelectedImagePath = "Item/More.png",
SelectedImagePath = "Item/MoreSelected.png",
};
topView.AddChidren(moreBtn);
var back = new Device.CommonForm.BackButton() { };
topView.AddChidren(back);
back.MouseUpEventHandler += (sender, e) =>
{
this.RemoveFromParent();
};
if (device == null || device.CommonDevice == null || room == null)
{
return;
}
this.device = device;
this.zbGateway = this.device.CommonDevice.Gateway;
this.room = room;
var light = device.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);
}
}
#endregion
#region midFL
var midFL = new FrameLayout()
{
Height = Application.GetRealHeight(CommonPage.AppRealHeight - CommonPage.Navigation_Height),
Y = topView.Bottom,
BackgroundColor = ZigbeeColor.Current.GXCBackgroundColor,
};
this.AddChidren(midFL);
var itemView = new FrameLayout()
{
X = Application.GetRealWidth(50),
Y = Application.GetRealHeight(50),
Width = Application.GetRealWidth(CommonPage.AppRealWidth - 100),
Height = Application.GetRealHeight(850),
Radius = CommonPage.BigFormRadius,
Gravity = Gravity.CenterHorizontal,
BackgroundColor = ZigbeeColor.Current.GXCTopViewBackgroundColor
};
midFL.AddChidren(itemView);
deviceView = new FrameLayout()
{
X = 2,
Y = 2,
Width = itemView.Width - 4,
Height = itemView.Height - Application.GetRealHeight(130),
BackgroundColor = ZigbeeColor.Current.GXCBackgroundColor,
Tag = device
};
itemView.AddChidren(deviceView);
collectionBtn = new Button()
{
X = deviceView.Width - Application.GetRealWidth(130),
Y = Application.GetRealHeight(20),
Width = Application.GetMinReal(110),
Height = Application.GetMinReal(110),
UnSelectedImagePath = "Item/Collection.png",
SelectedImagePath = "Item/CollectionSelected.png"
};
deviceView.AddChidren(collectionBtn);
deviceIMG = new Button()
{
Y = Application.GetRealHeight(100),
Height = Application.GetMinRealAverage(240),
Width = Application.GetMinRealAverage(240),
Gravity = Gravity.CenterHorizontal,
UnSelectedImagePath = device.IconPath,
SelectedImagePath = device.OnlineIconPath,
IsSelected = light.IsOnline == 1,
Tag = DeviceStatus_Online
};
deviceView.AddChidren(deviceIMG);
var lightName = new Button()
{
Y = deviceIMG.Bottom,
Height = Application.GetRealHeight(85),
Gravity = Gravity.CenterHorizontal,
Text = device.CommonDevice.DeviceEpointName,
TextColor = ZigbeeColor.Current.GXCTextBlackColor
};
deviceView.AddChidren(lightName);
switchBtn = new Button()
{
Y = lightName.Bottom + Application.GetRealHeight(70),
Gravity = Gravity.CenterHorizontal,
Width = Application.GetMinRealAverage(180),
Height = Application.GetMinRealAverage(120),
UnSelectedImagePath = "Item/Switch.png",
SelectedImagePath = "Item/SwitchSelected.png",
Tag = DeviceStatus_OnOffStatus,
IsSelected = light.OnOffStatus == 1
};
deviceView.AddChidren(switchBtn);
roomBtn = new Button()
{
X = Application.GetRealWidth(50),
Y = Application.GetRealHeight(25) + deviceView.Bottom,
Width = Application.GetMinReal(80),
Height = Application.GetMinReal(80),
UnSelectedImagePath = "Item/Room.png",
SelectedImagePath = "Item/RoomSelected.png"
};
itemView.AddChidren(roomBtn);
roomName = new Button()
{
X = roomBtn.Right + Application.GetRealWidth(20),
Y = roomBtn.Y,
Width = Application.GetRealWidth(400),
Height = Application.GetRealHeight(80),
Text = room.Name,
TextAlignment = TextAlignment.CenterLeft,
TextColor = ZigbeeColor.Current.GXCTextBlackColor,
SelectedTextColor = ZigbeeColor.Current.GXCTextBlueColor
};
itemView.AddChidren(roomName);
//var moreBtn = new Button()
//{
// X = itemView.Width - Application.GetRealWidth(130),
// Y = roomBtn.Y,
// Width = Application.GetMinReal(80),
// Height = Application.GetMinReal(80),
// UnSelectedImagePath = "Item/More.png",
// SelectedImagePath = "Item/MoreSelected.png",
//};
//itemView.AddChidren(moreBtn);
var dev = Shared.Common.Room.LoveRoomDeviceUIFilePathList.Find((obj) => obj == device.FileName);
if (dev == null)
{
collectionBtn.IsSelected = false;
}
else
{
collectionBtn.IsSelected = true;
}
this.BindEvent();
#endregion
}
///
/// 重写移除方法
///
public override void RemoveFromParent()
{
ZbGateway.StatusList.Remove(this);
action();
RemoveUpdateControlDeviceStatuAction();
base.RemoveFromParent();
}
///
/// 构造方法
///
public LightControl()
{
BackgroundColor = ZigbeeColor.Current.GXCBackgroundColor;
ZbGateway.StatusList.Add(this);
}
#endregion
#region ◆ 绑定按钮_______________________
///
/// 绑定按钮的事件
///
private void BindEvent()
{
switchBtn.MouseUpEventHandler += Switch;
collectionBtn.MouseUpEventHandler += Collection;
moreBtn.MouseUpEventHandler += More;
roomBtn.MouseUpEventHandler += BackToRoomHandler;
roomName.MouseUpEventHandler += BackToRoomHandler;
}
#endregion
#region ◆ 开关__________________________
///
/// 开关设置
///
/// Sender.
/// Event arguments.
private void Switch(object sender, MouseEventArgs eventArgs)
{
sendedControlCommand = false;
zbGateway.ReportAction += UpdateDeviceControllStatu;
switchBtn.IsSelected = !switchBtn.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 (collectionBtn.IsSelected)
{
Shared.Common.Room.Lists[0].DeleteDevice(device.FileName);
collectionBtn.IsSelected = false;
}
else
{
Shared.Common.Room.Lists[0].AddDevice(device.FileName);
collectionBtn.IsSelected = true;
}
action?.Invoke();
}
#endregion
#region ◆ 回到主页________________________
///
/// 回到主页
///
/// Sender.
/// The ${ParameterType} instance containing the event data.
private void BackToRoomHandler(object sender, MouseEventArgs mouseEventArgs )
{
//backToRoom
}
#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
}
}