using System;
using System.Collections.Generic;
using Shared.Common;
using Shared.Phone.Device.Light;
using Shared.Phone.UserCenter.Device;
using ZigBee.Device;
using static ZigBee.Device.BindObj;
using static ZigBee.Device.Panel;
namespace Shared.Phone.UserCenter.Bind
{
public class PanelBindPage : FrameLayout, ZigBee.Common.IStatus
{
///
/// 按键模式接收
///
/// Common.
public void Changed(CommonDevice common)
{
if (common.Type != ZigBee.Device.DeviceType.OnOffSwitch)
{
return;
}
Shared.Application.RunOnMainThread(() =>
{
var dev = common as Panel;
UpdateData(btnMode, dev);
});
}
///
/// 加载绑定界面
///
/// Gateway.
/// Key.
public void Show(ZigBee.Device.ZbGateway gateway, Panel key)
{
curentKey = key;
curGateway = gateway;
InitConfigurePage(gateway, key);
}
#region 绑定变量
//显示被绑定设备或场景的view
VerticalScrolViewLayout midVerticalScrolViewLayout;
int currentClusterID = 0;
int currentMode = 0;
Panel curentKey;
ZbGateway curGateway;
///
/// 按键中被绑定的表
///
public static System.Collections.Generic.List bindList = new System.Collections.Generic.List { };
//Spinner spinnerMode;//当前模式显示控件
RowSecondRightTextView btnMode;//当前模式显示控件
FrameLayout inVerticalscrolView;//加场景或设备的view
bool isBindScene = false;
///
/// 获取所有房间的所有场景
///
/// All room scene UIL ist.
///
/// 本地设备列表
///
public static System.Collections.Generic.List list = new System.Collections.Generic.List();
//本地场景列表
public static System.Collections.Generic.List scList = new System.Collections.Generic.List { };
//按键支持当私有模式列表
private static System.Collections.Generic.List priFunList = new System.Collections.Generic.List { };
///
/// 按键配置的目标列表
///
List targetList = new List();
#endregion
///
/// 按键配置
///
/// Gateway.
/// Key.
async void InitConfigurePage(ZigBee.Device.ZbGateway gateway, Panel key)
{
bindList.Clear();
#region topFrameLayout (只有UI,无数据处理)
var topFrameLayout = new FrameLayout()
{
Height = Application.GetRealHeight(220),
BackgroundColor = Shared.Common.ZigbeeColor.Current.GXCTopViewBackgroundColor,
};
this.AddChidren(topFrameLayout);
var titleFrameLayout = new FrameLayout()
{
Y = Application.GetRealHeight(80),
Height = Application.GetRealHeight(140),
BackgroundColor = Shared.Common.ZigbeeColor.Current.GXCTopViewBackgroundColor,
};
topFrameLayout.AddChidren(titleFrameLayout);
var title = new Button()
{
TextAlignment = TextAlignment.Center,
Text = key.DeviceInfo.DeviceName,
TextColor = Shared.Common.ZigbeeColor.Current.GXCTextBlackColor,
Width = Application.GetRealWidth(1080 - 500),
Gravity = Gravity.CenterHorizontal,
};
titleFrameLayout.AddChidren(title);
var back = new Button()
{
X = Application.GetRealWidth(50),
Height = Application.GetRealHeight(100),
Width = Application.GetRealWidth(100),
UnSelectedImagePath = "Item/Back.png",
SelectedImagePath = "Item/BackOn.png",
Gravity = Gravity.CenterVertical,
};
titleFrameLayout.AddChidren(back);
#endregion
#region middleFrameLayout (只有UI,无数据处理)
#region modeFrameLayout
var midFrameLayout = new FrameLayout()
{
Width = LayoutParams.MatchParent,
Height = Application.GetRealHeight(1920 - 220),
Y = topFrameLayout.Bottom,
BackgroundColor = Shared.Common.ZigbeeColor.Current.GXCBackgroundColor,
};
this.AddChidren(midFrameLayout);
var modeFrameLayout = new FrameLayout()
{
Width = LayoutParams.MatchParent,
Height = Application.GetRealHeight(170),
BackgroundColor = Shared.Common.ZigbeeColor.Current.GXCBackgroundColor,
};
midFrameLayout.AddChidren(modeFrameLayout);
var btnModeText = new Button()
{
X = Application.GetRealWidth(50),
Width = Application.GetRealWidth(700),
Height = Application.GetRealHeight(110),
TextColor = ZigbeeColor.Current.GXCTextBlackColor,
Text = Language.StringByID(R.MyInternationalizationString.ModeType),
TextAlignment = TextAlignment.CenterLeft,
TextSize = 16,
Gravity = Gravity.CenterVertical,
};
modeFrameLayout.AddChidren(btnModeText);
btnMode = new RowSecondRightTextView();
btnMode.TextID = R.MyInternationalizationString.uNotHadSettion;
btnMode.TextColor = UserCenterColor.Current.Gray;
modeFrameLayout.AddChidren(btnMode);
var btnRight = new RowRightIconView();
modeFrameLayout.AddChidren(btnRight);
var line = new Button()
{
Y = modeFrameLayout.Bottom,
Height = 1,
BackgroundColor = ZigbeeColor.Current.GXCLineColor,
};
midFrameLayout.AddChidren(line);
#endregion
#region addBindDevice FrameLayout (只有UI,无数据处理)
var addBindDeviceFrameLayout = new FrameLayout()
{
Width = LayoutParams.MatchParent,
Height = Application.GetRealHeight(170),
Y = line.Bottom,
BackgroundColor = Shared.Common.ZigbeeColor.Current.GXCBackgroundColor,
};
midFrameLayout.AddChidren(addBindDeviceFrameLayout);
var btnAddBindText = new Button()
{
X = Application.GetRealWidth(50),
Width = Application.GetRealWidth(700),
Height = Application.GetRealHeight(110),
TextColor = ZigbeeColor.Current.GXCTextBlackColor,
TextID = R.MyInternationalizationString.AddBindTargets,
TextAlignment = TextAlignment.CenterLeft,
TextSize = 16,
Gravity = Gravity.CenterVertical
};
addBindDeviceFrameLayout.AddChidren(btnAddBindText);
var btnAddBind = new Button()
{
X = Application.CurrentWidth - Application.GetRealWidth(150),
Width = Application.GetMinRealAverage(110),
Height = Application.GetMinRealAverage(110),
UnSelectedImagePath = "Bind/BindAdd.png",
SelectedImagePath = "Bind/BindAddOn.png",
Gravity = Gravity.CenterVertical
};
addBindDeviceFrameLayout.AddChidren(btnAddBind);
var line1 = new Button()
{
Y = addBindDeviceFrameLayout.Bottom,
Height = 1,
BackgroundColor = ZigbeeColor.Current.GXCLineColor,
};
midFrameLayout.AddChidren(line1);
midVerticalScrolViewLayout = new VerticalScrolViewLayout()
{
Y = line1.Bottom,
Height = Application.GetRealHeight(1920 - 620),
};
midFrameLayout.AddChidren(midVerticalScrolViewLayout);
#endregion
#endregion
#region 处理事件 (数据处理)
back.MouseUpEventHandler += (sender, e) =>
{
list.Clear();
scList.Clear();
this.RemoveFromParent();
};
EventHandler hander = async (sender, e) =>
{
var oldCluster = currentClusterID;
if(priFunList==null)
{
try
{
CommonPage.Loading.Start("");
KeyModeList(key);
}
catch { }
finally
{
CommonPage.Loading.Hide();
}
}
if (priFunList != null)
{
PickerView.Show(priFunList, ModeSelectAction);
}else{
new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.NoModeSupport), Direction = AMPopTipDirection.Up, CloseTime = 2 }.Show(btnMode);
}
};
btnMode.MouseUpEventHandler += hander;
btnRight.MouseUpEventHandler += hander;
btnModeText.MouseUpEventHandler += hander;
modeFrameLayout.MouseUpEventHandler += hander;
btnAddBind.MouseUpEventHandler += (sender, e) =>
{
if (currentClusterID == 0)
{
new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.KeyConfiguration), Direction = AMPopTipDirection.Up, CloseTime = 2 }.Show(btnAddBind);
return;
}
bool isExistScene = false;
bool isExistDevice = false;
foreach (var sc in PanelBindPage.bindList)
{
if (sc.BindType == 2)
{
isExistScene = true;
}
else
{
isExistDevice = true;
}
}
if (currentMode != 100)
{
var selectDeviceList = new List();
foreach (var dev in PanelBindPage.bindList)
{
string mainkey = Common.LocalDevice.Current.GetDeviceMainKeys(dev.BindMacAddr, dev.BindEpoint);
selectDeviceList.Add(mainkey);
}
var listShowDevice = new List();
foreach (var device in PanelBindPage.list)
{
if (SupportDevice(device, currentClusterID) == false)
{
continue;
}
//如果已经被绑定过了的设备,则不在列表中进行显示
string mainkey = Common.LocalDevice.Current.GetDeviceMainKeys(device);
if (selectDeviceList.Contains(mainkey) == true)
{
continue;
}
listShowDevice.Add(device);
}
var form = new SelectDeviceForm();
form.AddForm(form, listShowDevice, selectDeviceList, false);
form.SetTitleText(Language.StringByID(R.MyInternationalizationString.AddBindTargets));
form.ActionSelectDevice += (async (devList) => {
if (devList == null || devList.Count == 0)
{
return;
}
//执行目标绑定
var result = await Common.LocalDevice.Current.BindDeviceTarget(key, devList, currentClusterID);
if (result != null && result.Count > 0)
{
bindList.Clear();
try
{
var getBindList = await key.GetDeviceBindAsync();
if (getBindList != null)
{
if (getBindList.getAllBindResponseData.BindList != null)
{
foreach (var b in getBindList.getAllBindResponseData.BindList)
{
if (b.BindCluster == currentClusterID)
{
bindList.Add(b);
}
}
}
}
}
catch { }
//绑定表刷新
RefreshList(gateway, key);
}
});
}
else
{
if (isExistScene)
{
var addBindTargetsPage = new Shared.Phone.UserCenter.Bind.AddBindTargetsPage();
Shared.Phone.UserView.HomePage.Instance.AddChidren(addBindTargetsPage);
Shared.Phone.UserView.HomePage.Instance.PageIndex += 1;
addBindTargetsPage.Show(key, currentClusterID, false);
}
else
{
BindInfo.AddBindTarget(key, currentClusterID, currentMode);
}
}
};
//模式刷新
UpdateData(btnMode, key);
//绑定表数据初始化(被绑定的设备列表,场景列表和设备列表)
try
{
list.Clear();
scList.Clear();
priFunList.Clear();
foreach (var dev in Shared.Common.LocalDevice.Current.listAllDevice)
{
list.Add(dev);
}
scList = Shared.Common.Room.AllRoomSceneUIList;
CommonPage.Loading.Start("");
KeyModeList(key);
}
catch { }
finally
{
CommonPage.Loading.Hide();
}
//绑定表刷新
RefreshList(gateway, key);
#endregion
}
///
/// 按键绑定表显示
///
/// Gateway.
/// Key.
void RefreshList(ZigBee.Device.ZbGateway gateway, Panel key)
{
midVerticalScrolViewLayout.RemoveAll();
AddBindTargetsPage.action += (obj) =>
{
bindList = obj;
RefreshList(gateway, key);
};
foreach (var bindDevice in bindList)
{
#region 绑定表UI显(无数据处理)
var rowLayout = new RowLayout()
{
Height = Application.GetRealHeight(170),
};
midVerticalScrolViewLayout.AddChidren(rowLayout);
var devicePic = new Button()
{
X = Application.GetRealWidth(50),
Width = Application.GetMinRealAverage(110),
Height = Application.GetMinRealAverage(110),
Gravity = Gravity.CenterVertical,
BackgroundColor = ZigbeeColor.Current.DialogTitleColor,
};
rowLayout.AddChidren(devicePic);
var btnBindName = new Button()
{
Width = Application.GetRealWidth(500 - 120),
X = devicePic.Right + Application.GetRealWidth(20),
TextColor = Shared.Common.ZigbeeColor.Current.TextBlack,
TextAlignment = TextAlignment.CenterLeft,
};
rowLayout.AddChidren(btnBindName);
var btnDel = new Button()
{
TextColor = Shared.Common.ZigbeeColor.Current.GXCTextWhiteColor,
X = btnBindName.Right,
TextID = R.MyInternationalizationString.Delete,
BackgroundColor = Shared.Common.ZigbeeColor.Current.DelRowLayout,
};
rowLayout.AddRightView(btnDel);
#endregion
#region 绑定数据处理
//被绑定设备图片
foreach (var dev in list)
{
if (bindDevice.BindMacAddr == dev.DeviceAddr && bindDevice.BindEpoint == dev.DeviceEpoint)
{
devicePic.UnSelectedImagePath = $"Device/{dev.Type.ToString()}.png";
}
}
//被绑定设备的场景图片
foreach (var sc in scList)
//foreach (var sc in ZbGateway.AllSceneList)
{
if (sc.ScenesId == bindDevice.BindScenesId)
{
devicePic.UnSelectedImagePath = "Item/Scene.png";
}
}
//被绑定目标名称
if (bindDevice.ESName == "")
{
btnBindName.TextID = R.MyInternationalizationString.Unknown;
}
else
{
btnBindName.Text = bindDevice.ESName;
}
//删除事件处理
btnDel.MouseUpEventHandler += async (sender, e) =>
{
var delDevice = new DelDeviceBindData();
delDevice.DeviceAddr = key.DeviceAddr;
delDevice.Epoint = key.DeviceEpoint;
if (bindDevice.BindType == 0 || bindDevice.BindType == 1)
{
var removeDevice = new RemoveBindListObj();
removeDevice.BindCluster = bindDevice.BindCluster;
removeDevice.BindType = 0;
removeDevice.BindMacAddr = bindDevice.BindMacAddr;
removeDevice.BindEpoint = bindDevice.BindEpoint;
delDevice.RemoveBindList.Add(removeDevice);
}
else if (bindDevice.BindType == 2)
{
var removeDevice = new RemoveBindListObj();
removeDevice.BindCluster = bindDevice.BindCluster;
removeDevice.BindType = 1;
removeDevice.BindScenesId = bindDevice.BindScenesId;
delDevice.RemoveBindList.Add(removeDevice);
}
try
{
CommonPage.Loading.Start("");
var delResult = new DelDeviceBindResponseAllData();
delResult = await key.DelDeviceBindAsync(delDevice);
if (delResult.delDeviceBindResponseData?.RemoveBindList != null)
{
foreach (var re in delResult.delDeviceBindResponseData.RemoveBindList)
{
switch (re.Result)
{
case 0:
bindList.Remove(bindDevice);
RefreshList(gateway, key);
break;
case 1:
bindList.Clear();
try
{
CommonPage.Loading.Start("");
var getBindList = await key.GetDeviceBindAsync();
if (getBindList != null)
{
if (getBindList.getAllBindResponseData.BindList != null)
{
foreach (var b in getBindList.getAllBindResponseData.BindList)
{
if (b.BindCluster == currentClusterID)
{
bindList.Add(b);
}
}
}
}
}
catch { }
finally
{
CommonPage.Loading.Hide();
}
RefreshList(gateway, key);
break;
case 4:
if (delResult.removeBindResultResponseData != null)
{
if (delResult.removeBindResultResponseData.Result == 0)
{
bindList.Remove(bindDevice);
RefreshList(gateway, key);
}
else
{
new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.RemoveFailed), Direction = AMPopTipDirection.Up, CloseTime = 2 }.Show(btnDel);
}
}
break;
default:
break;
}
}
}
}
catch { }
finally
{
CommonPage.Loading.Hide();
}
};
#endregion
}
}
///
/// 选择需要绑定的设备和场景
///
/// Gateway.
/// Key.
/// Action.
void BindDevice(ZigBee.Device.ZbGateway gateway, Panel key)
{
#region 绑定的设备和场景Dialog UI
var dialog = new Dialog();
var dialogBodyView = new FrameLayout()
{
Width = Application.GetRealWidth(753),
Height = Application.GetRealHeight(946 - 50),
BackgroundColor = ZigbeeColor.Current.DialogColor,
Radius = 5,
BorderWidth = 0,
Gravity = Gravity.Center
};
dialog.AddChidren(dialogBodyView);
var btnTitle = new Button()
{
Height = Application.GetRealHeight(119),
TextAlignment = TextAlignment.Center,
TextID = R.MyInternationalizationString.BindDevice,
BackgroundColor = ZigbeeColor.Current.DialogTitleColor,
TextSize = 20,
};
dialogBodyView.AddChidren(btnTitle);
var BindTypeView = new FrameLayout()
{
Height = Application.GetRealHeight(110),
BackgroundColor = ZigbeeColor.Current.DialogSubTitleColor,
Y = btnTitle.Bottom,
};
dialogBodyView.AddChidren(BindTypeView);
var bindVerticalScrolView = new VerticalScrolViewLayout()
{
Height = Application.GetRealHeight(946 - 240 - 180),
Width = Application.GetRealWidth(753),
Y = BindTypeView.Bottom,
};
dialogBodyView.AddChidren(bindVerticalScrolView);
inVerticalscrolView = new FrameLayout();
bindVerticalScrolView.AddChidren(inVerticalscrolView);
var btnDevice = new Button()
{
Width = Application.GetRealWidth(376),
TextSize = 16,
SelectedTextColor = Shared.Common.ZigbeeColor.Current.SelectedTextColor,
TextColor = Shared.Common.ZigbeeColor.Current.TextColor,
TextID = R.MyInternationalizationString.BindDevice,
IsSelected = !isBindScene,
};
BindTypeView.AddChidren(btnDevice);
var btnScene = new Button()
{
Width = Application.GetRealWidth(376),
X = btnDevice.Right,
TextSize = 16,
SelectedTextColor = Shared.Common.ZigbeeColor.Current.SelectedTextColor,
TextColor = Shared.Common.ZigbeeColor.Current.TextColor,
TextID = R.MyInternationalizationString.BindScene,
IsSelected = isBindScene,
};
BindTypeView.AddChidren(btnScene);
var btnLine = new Button()
{
Width = 1,
Height = Application.GetRealHeight(119) - 1,
BackgroundColor = Shared.Common.ZigbeeColor.Current.DialogTitleLine,
X = btnDevice.Right,
Y = 1,
};
BindTypeView.AddChidren(btnLine);
var bottomView = new FrameLayout()
{
Y = dialogBodyView.Height - Application.GetRealHeight(119),
Height = Application.GetRealHeight(119),
BackgroundColor = Shared.Common.ZigbeeColor.Current.DialogTitleColor,
};
dialogBodyView.AddChidren(bottomView);
var btnBack = new Button()
{
Width = Application.GetRealWidth(119),
Height = Application.GetRealHeight(119),
UnSelectedImagePath = "Bind/DialogBack.png",
SelectedImagePath = "Bind/DialogBackOn.png",
};
bottomView.AddChidren(btnBack);
btnBack.MouseUpEventHandler += (sender1, e1) =>
{
dialog.Close();
};
var btnLineH = new Button()
{
Width = 1,
Height = LayoutParams.MatchParent,
X = btnBack.Right,
BackgroundColor = Shared.Common.ZigbeeColor.Current.DialogColor,
};
bottomView.AddChidren(btnLineH);
var btnConfigureSave = new Button()
{
Width = Application.GetRealWidth(753 - 120),
X = btnLineH.Right,
TextID = R.MyInternationalizationString.Save,
BackgroundColor = ZigbeeColor.Current.DialogTitleColor,
TextAlignment = TextAlignment.Center,
TextSize = 16,
};
bottomView.AddChidren(btnConfigureSave);
InitDeviceOrSceneView(true, key, inVerticalscrolView, (obj) =>
{
Refresh();
});
dialog.Show();
#endregion
#region 事件和数据处理
//保存处理事件(添加场景和设备)
btnConfigureSave.MouseUpEventHandler += async (sender, e) =>
{
var addBindeDev = new AddBindData();
addBindeDev.DeviceAddr = key.DeviceAddr;
addBindeDev.Epoint = key.DeviceEpoint;
foreach (var de in targetList)
{
if (de.DeviceAddr != null)
{
var addBindInfo = new AddBindListObj();
addBindInfo.BindType = 0;
addBindInfo.BindCluster = currentClusterID;
addBindInfo.BindMacAddr = de.DeviceAddr;
addBindInfo.BindEpoint = de.DeviceEpoint;
addBindeDev.BindList.Add(addBindInfo);
}
else
{
var addBindInfo = new AddBindListObj();
addBindInfo.BindType = 1;
addBindInfo.BindCluster = currentClusterID;
var sc1 = (SceneUI)de;
addBindInfo.BindScenesId = sc1.ScenesId;
addBindeDev.BindList.Add(addBindInfo);
}
}
try
{
CommonPage.Loading.Start("");
var dev = new AddedDeviceBindResponseAllData();
dev = await key.AddDeviceBindAsync(addBindeDev);
foreach (var ab in dev.addedDeviceBindResponseData?.BindList)
{
switch (ab.Result)
{
case 0:
bindList.Add((BindListResponseObj)ab);
for (int i = 0; i < targetList.Count; i++)
{
var de = targetList[i];
var sc1 = new SceneUI();
if (de.DeviceAddr == null)
{
sc1 = (SceneUI)de;
if (sc1.ScenesId == ab.BindScenesId)
{
targetList.RemoveAt(i);
i--;
}
}
}
RefreshList(gateway, key);
break;
case 1:
new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.BindErrorAbsence), Direction = AMPopTipDirection.Up, CloseTime = 2 }.Show(btnConfigureSave);
break;
case 2:
switch (dev.addBindResultResponseData?.Result)
{
case 0:
bindList.Add((BindListResponseObj)ab);
for (int i = 0; i < targetList.Count; i++)
{
var de = targetList[i];
if (de.DeviceAddr == ab.BindMacAddr && de.DeviceEpoint == ab.BindEpoint)
{
targetList.RemoveAt(i);
i--;
}
}
RefreshList(gateway, key);
break;
case 140:
new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.BindFull), Direction = AMPopTipDirection.Up, CloseTime = 2 }.Show(btnConfigureSave);
break;
default:
new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.BindFailed), Direction = AMPopTipDirection.Up, CloseTime = 2 }.Show(btnConfigureSave);
break;
}
break;
case 3:
new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.BindErrorExisted), Direction = AMPopTipDirection.Up, CloseTime = 2 }.Show(btnConfigureSave);
break;
case 4:
new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.BindFailed), Direction = AMPopTipDirection.Up, CloseTime = 2 }.Show(btnConfigureSave);
break;
default:
break;
}
}
}
catch { }
finally
{
CommonPage.Loading.Hide();
}
};
//设备事件
btnDevice.MouseUpEventHandler += (sender, e) =>
{
btnDevice.IsSelected = true;
btnScene.IsSelected = false;
isBindScene = false;
InitDeviceOrSceneView(true, key, inVerticalscrolView, (obj) =>
{
Refresh();
});
};
//场景事件
btnScene.MouseUpEventHandler += (sender, e) =>
{
btnScene.IsSelected = true;
btnDevice.IsSelected = false;
isBindScene = false;
InitDeviceOrSceneView(false, key, inVerticalscrolView, (obj) =>
{
Refresh();
});
};
#endregion
}
///
/// 匹配当前模式的设备和场景,并选择绑定
///
/// If set to true is device.
/// Hdl button.
/// In verticalscrol view.
/// Action.
async void InitDeviceOrSceneView(bool isDevice, Panel hdlButton, FrameLayout inVerticalscrolView, Action