using System;
using Shared.Common;
using Shared.Phone.Device.CommonForm;
using Shared.Phone.Device.DeviceLogic;
using Shared.Phone.UserView;
using ZigBee.Device;
namespace Shared.Phone.Device.Curtain
{
public class RollerShadeControl:FrameLayout,ZigBee.Common.IStatus
{
#region ◆ 变量__________________________
///
/// The action.
///
public Action action;
///
/// 收藏按钮
///
private Button collectionBtn;
///
/// 传过来的设备
///
private CommonDevice device;
///
/// 传过来的房间
///
private Shared.Common.Room room;
///
/// 房间
///
private Button roomBtn;
///
/// 房间名
///
private Button roomName;
///
/// 网关
///
private ZbGateway zbGateway = null;
///
/// 是否发送控制命令成功了
///
private bool sendedControlCommand = false;
///
/// bodyFrameLayout
///
private FrameLayout bodyFrameLayout;
///
/// StatuBtn
///
private Button StatuBtn;
///
/// OpenOrUpBtn
///
public Button OpenOrUpBtn;
///
/// OpenOrUpBtn
///
public Button CloseOrDownBtn;
///
/// StopBtn
///
public Button StopBtn;
///
/// 开合帘
///
private CurtainSeekBar curtainSeekBar = new CurtainSeekBar { };
///
/// 卷帘
///
private CurtainRollSeekBar curtainRollSeekBar = new CurtainRollSeekBar { };
///
/// IsDrawerLockMode
///
public bool IsDrawerLockMode;
///
/// ProgressBtn
///
Button ProgressBtn;
///
/// ProgressBtnY
///
int ProgressBtnY;
///
/// ProgressBtnX
///
int ProgressBtnX;
#endregion
#region ◆ 接口___________________________
///
/// 设备状态更新接口
/// 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 = this.device;
//设备为空
if (deviceUI == null)
{
return;
}
//是否为当前设备
if (deviceUI.DeviceEpoint != common.DeviceEpoint || deviceUI.DeviceAddr != common.DeviceAddr)
{
return;
}
if (common.DeviceStatusReport.CluterID == 258)
{
if (common.DeviceStatusReport.AttriBute[0].AttributeId == 8)
{
//窗帘百分比
var rollerShade = deviceUI as Rollershade;
rollerShade.DeviceStatusReport = common.DeviceStatusReport;
rollerShade.WcdCurrentPositionLiftPercentage = common.DeviceStatusReport.AttriBute[0].AttriButeData;
StatuBtn.Text = $"{Language.StringByID(R.MyInternationalizationString.Current)} { UserCenter.HdlDeviceOtherLogic.Current.GetDeviceStatu(deviceUI)}";
if(rollerShade.WcdType==0)
{
curtainRollSeekBar.Progress = rollerShade.WcdCurrentPositionLiftPercentage;
}
else if(rollerShade.WcdType==4)
{
curtainSeekBar.Progress = rollerShade.WcdCurrentPositionLiftPercentage;
}
rollerShade.LastDateTime = DateTime.Now;
}
else if (common.DeviceStatusReport.AttriBute[0].AttributeId == 0)
{
////窗帘类型
var rollerShade = device as Rollershade;
rollerShade.DeviceStatusReport = common.DeviceStatusReport;
rollerShade.WcdType = common.DeviceStatusReport.AttriBute[0].AttriButeData;
rollerShade.LastDateTime = DateTime.Now;
}
}
//***新改***设备状态上报中,当CluterID=3,证明设备在线,直接标记
else if (common.DeviceStatusReport.CluterID == 3)
{
//var rollerShade = deviceUI.CommonDevice as Rollershade;
//rollerShade.IsOnline = 1;
//rollerShadeIMG.IsSelected = rollerShade.IsOnline == 1;
//rollerShade.LastDateTime = DateTime.Now;
}
}
catch (Exception ex)
{
System.Console.WriteLine($"Error:{ex.Message}");
}
});
}
else if (typeTag == "OnlineStatusChange")
{
Application.RunOnMainThread(() =>
{
try
{
//var deviceUI = SeekBar.Tag as DeviceUI;
////设备为空
//if (deviceUI.CommonDevice == null)
//{
// return;
//}
////是否为当前设备
//if (deviceUI.CommonDevice.DeviceEpoint != common.DeviceEpoint || deviceUI.CommonDevice.DeviceAddr != common.DeviceAddr)
//{
// return;
//}
//var rollerShade = deviceUI.CommonDevice as Rollershade;
//rollerShade.IsOnline = (common as Rollershade).IsOnline;
//rollerShadeIMG.IsSelected = rollerShade.IsOnline == 1;
//rollerShade.LastDateTime = DateTime.Now;
}
catch (Exception ex)
{
System.Console.WriteLine($"Error:{ex.Message}");
}
});
}
}
#endregion
#region ◆ 构造方法________________________
///
/// 构造方法
///
public RollerShadeControl()
{
BackgroundColor = ZigbeeColor.Current.GXCBackgroundColor;
ZigBee.Device.ZbGateway.StatusList.Add(this);
}
#endregion
#region ◆ 重写移除方法____________________
///
/// Removes from parent.
///
public override void RemoveFromParent()
{
UserView.HomePage.Instance.ScrollEnabled = true;
ZigBee.Device.ZbGateway.StatusList.Remove(this);
action(device,room);
action = null;
RemoveUpdateControlDeviceStatuAction();
if (IsDrawerLockMode)
{
CommonPage.Instance.IsDrawerLockMode = false;
}
base.RemoveFromParent();
}
#endregion
#region ◆ 显示界面________________________
///
/// 显示界面
///
/// Device.
/// Room.
public void Show(CommonDevice dev, Shared.Common.Room room)
{
device = dev;
zbGateway = this.device.Gateway;
this.room = room;
AddTop();
AddBodyView(device);
var rollerShade = device as ZigBee.Device.Rollershade;
//不上非远程
if (rollerShade.Gateway == null)
{
return;
}
if (rollerShade.Gateway.IsVirtual)
{
UserHomeView.ReadStatus(rollerShade, () =>
{
ReadDeviceAttributeLogic.Instance.SendCurtainStatuComand(device);
});
}
else
{
//防止短时间内多次读取
if ((DateTime.Now - rollerShade.LastDateTime).TotalSeconds > CommonPage.ReadDeviceStatuSpan)
{
ReadDeviceAttributeLogic.Instance.SendCurtainStatuComand(device);
}
}
if (UserCenter.HdlRoomLogic.Current.IsCollectInRoom(device) == false)
{
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 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(CommonDevice 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 = Common.LocalDevice.Current.GetDeviceEpointName(device),
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 = $"{Language.StringByID(R.MyInternationalizationString.Current)} {UserCenter.HdlDeviceOtherLogic.Current.GetDeviceStatu(device)}",
TextSize = 10
};
itemView.AddChidren(StatuBtn);
OpenOrUpBtn = new Button
{
X = Application.GetRealWidth(253),
Y = Application.GetRealHeight(994),
Width = Application.GetMinRealAverage(81),
Height = Application.GetMinRealAverage(81)
};
itemView.AddChidren(OpenOrUpBtn);
OpenOrUpBtn.MouseUpEventHandler += (sender, e) =>
{
(device as Rollershade).CurtainUpDownStopControl(0);
};
StopBtn = new Button
{
X = Application.GetRealWidth(441),
Y=Application.GetRealHeight(994),
Width = Application.GetMinRealAverage(81),
Height = Application.GetMinRealAverage(81),
SelectedImagePath = "RollerShade/StopSelected.png",
UnSelectedImagePath = "RollerShade/Stop.png",
};
itemView.AddChidren(StopBtn);
StopBtn.MouseUpEventHandler += (sender, e) =>
{
(device as Rollershade).CurtainUpDownStopControl(2);
};
CloseOrDownBtn = new Button
{
X = Application.GetRealWidth(631),
Y = Application.GetRealHeight(994),
Width = Application.GetMinRealAverage(81),
Height = Application.GetMinRealAverage(81)
};
itemView.AddChidren(CloseOrDownBtn);
CloseOrDownBtn.MouseUpEventHandler += (sender, e) =>
{
(device as Rollershade).CurtainUpDownStopControl(1);
};
if ((device as Rollershade).WcdType == -1)
{
CommonPage.Loading.Start();
new System.Threading.Thread(() =>
{
Rollershade.ReadWcdTypeAction(device, () =>
{
Application.RunOnMainThread(() =>
{
SetRollerShadeIcon((device as Rollershade).WcdType);
CommonPage.Loading.Hide();
});
});
})
{ IsBackground = true }.Start();
}
SetCurtainType(itemView, (device as Rollershade).WcdType);
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);
}
#endregion
#region ◆ 绑定事件________________________
///
/// 绑定按钮的事件
///
private void BindEvent()
{
//upBtn.MouseUpEventHandler += Up;
//stopBtn.MouseUpEventHandler += Stop;
//downBtn.MouseUpEventHandler += Down;
collectionBtn.MouseUpEventHandler += Collection;
}
#endregion
#region ◆ 控制__________________________
///
/// 上、开
///
/// Sender.
/// Event arguments.
private void Up(object sender, MouseEventArgs eventArgs)
{
sendedControlCommand = false;
zbGateway.ReportAction += UpdateDeviceControllStatu;
(device as ZigBee.Device.Rollershade).CurtainUpDownStopControl(0);
//控制延时回调
UserCenter.HdlDeviceOtherLogic.Current.SendCommandDelayAction(device, () =>
{
if (Parent == null)
{
return;
}
RemoveUpdateControlDeviceStatuAction();
if (sendedControlCommand == false)
{
UserCenter.HdlDeviceOtherLogic.Current.ShowStatuTip(R.MyInternationalizationString.FAIL);
}
});
}
///
/// 下、关
///
/// Sender.
/// Event arguments.
private void Down(object sender, MouseEventArgs eventArgs)
{
sendedControlCommand = false;
zbGateway.ReportAction += UpdateDeviceControllStatu;
(device as ZigBee.Device.Rollershade).CurtainUpDownStopControl(1);
//控制延时回调
UserCenter.HdlDeviceOtherLogic.Current.SendCommandDelayAction(device, () =>
{
if (Parent == null)
{
return;
}
RemoveUpdateControlDeviceStatuAction();
if (sendedControlCommand == false)
{
UserCenter.HdlDeviceOtherLogic.Current.ShowStatuTip(R.MyInternationalizationString.FAIL);
}
});
}
///
/// 停
///
/// Sender.
/// Event arguments.
private void Stop(object sender, MouseEventArgs eventArgs)
{
sendedControlCommand = false;
zbGateway.ReportAction += UpdateDeviceControllStatu;
(device as ZigBee.Device.Rollershade).CurtainUpDownStopControl(2);
//控制延时回调
UserCenter.HdlDeviceOtherLogic.Current.SendCommandDelayAction(device, () =>
{
if (Parent == null)
{
return;
}
RemoveUpdateControlDeviceStatuAction();
if (sendedControlCommand == false)
{
UserCenter.HdlDeviceOtherLogic.Current.ShowStatuTip(R.MyInternationalizationString.FAIL);
}
});
}
#endregion
#region ◆ 控制反馈_________________________
///
/// 显示设备控制状态
///
/// Command.
/// Object value.
private void UpdateDeviceControllStatu(string command, object objValue)
{
if (command != "DeviceDefaultAck" || objValue == null)
{
return;
}
var tempDevice = (CommonDevice)objValue;
if (tempDevice.DeviceEpoint != this.device.DeviceEpoint || tempDevice.DeviceAddr != this.device.DeviceAddr)
{
//不是当前设备的推送,则不处理
return;
}
//标记已经发送控制命令到网关
sendedControlCommand = true;
//成功不提示
//DeviceUI.ShowStatuTip(R.MyInternationalizationString.Success);
}
///
/// 移除更新控制设备的action
///
private void RemoveUpdateControlDeviceStatuAction()
{
//移除action
if (zbGateway != null)
{
zbGateway.ReportAction -= UpdateDeviceControllStatu;
}
}
#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.EditAction += (curDev,curRoom) =>
{
Show(curDev, curRoom);
};
}
///
/// SetCurtainType
///
///
///
private void SetCurtainType(FrameLayout layout, int type)
{
SetRollerShadeIcon(type);
SetRollerSeekBar(layout, type);
}
///
/// WcdType=4:开合帘
/// WcdType=0:卷帘
///
///
private void SetRollerShadeIcon(int type)
{
if (type == 0)
{
OpenOrUpBtn.UnSelectedImagePath = "RollerShade/Up.png";
OpenOrUpBtn.SelectedImagePath = "RollerShade/UpSelected.png";
CloseOrDownBtn.UnSelectedImagePath = "RollerShade/Down.png";
CloseOrDownBtn.SelectedImagePath = "RollerShade/DownSelected.png";
}
else
{
OpenOrUpBtn.UnSelectedImagePath = "RollerShade/Open.png";
OpenOrUpBtn.SelectedImagePath = "RollerShade/OpenSelected.png";
CloseOrDownBtn.UnSelectedImagePath = "RollerShade/Close.png";
CloseOrDownBtn.SelectedImagePath = "RollerShade/CloseSelected.png";
}
}
///
/// SetRollerSeekBar
///
///
///
private void SetRollerSeekBar(FrameLayout layout,int type)
{
if(type == 0)
{
curtainRollSeekBar.Y = Application.GetRealHeight(300);
curtainRollSeekBar.Width = Application.GetRealWidth(438);
curtainRollSeekBar.Height = Application.GetRealHeight(576);
curtainRollSeekBar.Gravity = Gravity.CenterHorizontal;
curtainRollSeekBar.Progress = (device as Rollershade).WcdCurrentPositionLiftPercentage;
layout.AddChidren(curtainRollSeekBar);
curtainRollSeekBar.IsProgressTextShow = false;
curtainRollSeekBar.CurtainPaddingTop = Application.GetRealHeight(110);
curtainRollSeekBar.OnStopTrackingTouchEvent += (sender, e) =>
{
(device as Rollershade).WcdGoToTiltValue(curtainRollSeekBar.Progress);
};
curtainRollSeekBar.OnStartTrackingTouchEvent += (sender, e) =>
{
OnProgressButtonMove(curtainRollSeekBar.Progress);
ProgressBtn.Visible = true;
};
curtainRollSeekBar.OnStopTrackingTouchEvent += (sender, e) =>
{
ProgressBtn.Visible = false;
};
curtainRollSeekBar.OnProgressChangedEvent += (send2, e2) =>
{
OnProgressButtonMove(e2);
//判断是否300ms屏蔽
//if (curtainRollSeekBar.IsProgressChangeDelay()) return;
//(device.CommonDevice as Rollershade).WcdGoToTiltValue(curtainRollSeekBar.Progress);
};
InitProgressBtn(layout);
}
else
{
curtainSeekBar.Y = Application.GetRealHeight(334);
curtainSeekBar.Width = Application.GetRealWidth(570);
curtainSeekBar.Height = Application.GetRealHeight(513);
curtainSeekBar.Gravity = Gravity.CenterHorizontal;
curtainSeekBar.IsProgressTextShow = false;
curtainSeekBar.Progress = (device as Rollershade).WcdCurrentPositionLiftPercentage;
layout.AddChidren(curtainSeekBar);
curtainSeekBar.OnStopTrackingTouchEvent += (sender, e) =>
{
(device as Rollershade).WcdGoToTiltValue(curtainSeekBar.Progress);
};
curtainSeekBar.OnStartTrackingTouchEvent += (sender, e) =>
{
OnProgressButtonMoveForCurtain(curtainSeekBar.Progress);
ProgressBtn.Visible = true;
};
curtainSeekBar.OnStopTrackingTouchEvent += (sender, e) =>
{
ProgressBtn.Visible = false;
};
curtainSeekBar.OnProgressChangedEvent += (send2, e2) =>
{
OnProgressButtonMoveForCurtain(e2);
//判断是否300ms屏蔽
//if (curtainRollSeekBar.IsProgressChangeDelay()) return;
//(device.CommonDevice as Rollershade).WcdGoToTiltValue(curtainRollSeekBar.Progress);
};
InitProgressBtnForCurtain(layout);
}
}
///
/// 添加进度按钮
///
private void InitProgressBtn(FrameLayout layout)
{
ProgressBtnY = curtainRollSeekBar.Y - Application.GetMinReal(204);
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);
}
///
/// OnProgressButtonMove
///
private void OnProgressButtonMove(int mProgress)
{
ProgressBtn.Y = ProgressBtnY + curtainRollSeekBar.NowProgressY;
ProgressBtn.Text = mProgress + "%";
}
///
/// 添加进度按钮
///
private void InitProgressBtnForCurtain(FrameLayout layout)
{
ProgressBtnX = curtainSeekBar.X - Application.GetMinReal(20);
ProgressBtn = new Button()
{
X = ProgressBtnX,
Y = curtainSeekBar.Y + curtainSeekBar.Height / 2 - Application.GetMinReal(180),
Width = Application.GetMinReal(135),
Height = Application.GetMinReal(104),
UnSelectedImagePath = "Item/ProgressBubbles.png",
Visible = false,
TextSize = 14,
IsBold = true,
TextColor = ZigbeeColor.Current.GXCTextWhiteColor
};
layout.AddChidren(ProgressBtn);
}
///
/// OnProgressButtonMove
///
private void OnProgressButtonMoveForCurtain(int mProgress)
{
ProgressBtn.X = ProgressBtnX + curtainSeekBar.NowProgressX- Application.GetMinReal(5);
ProgressBtn.Text = mProgress + "%";
}
#endregion
#region ◆ 收藏到主页_______________________
///
/// 收藏到主页
///
/// Sender.
/// E.
private void Collection(object sender, MouseEventArgs e)
{
if (collectionBtn.IsSelected)
{
UserCenter.HdlRoomLogic.Current.DeleteLoveDevice(device);
collectionBtn.IsSelected = false;
}
else
{
UserCenter.HdlRoomLogic.Current.AddLoveDevice(device);
collectionBtn.IsSelected = true;
}
}
#endregion
#region ◆ 回到主页________________________
///
/// 回到主页
///
/// Sender.
/// The ${ParameterType} instance containing the event data.
private void BackToRoomHandler(object sender, MouseEventArgs mouseEventArgs)
{
//backToRoom
}
#endregion
}
}