using System;
using System.Collections.Generic;
using Shared.Common;
using ZigBee.Device;
using static ZigBee.Device.BindObj;
using static ZigBee.Device.Panel;
namespace Shared.Phone.UserCenter.Bind
{
public class AddBindTargetsPage : UserCenterCommonForm, ZigBee.Common.IStatus
{
public void Changed(CommonDevice common)
{
}
///
/// 列表控件
///
private VerticalScrolViewLayout midVerticalScrolViewLayout;
///
/// 按键配置的目标列表
///
List targetList = new List();
public static Action> action;
public System.Collections.Generic.List allDeviceList = new System.Collections.Generic.List();
public System.Collections.Generic.List allScList = new System.Collections.Generic.List { };
Action action1;
bool isChoose = false;
public override void RemoveFromParent()
{
base.RemoveFromParent();
}
///
/// 画面显示(底层会固定调用此方法,借以完成画面创建)
///
public void Show(Panel key, int currentClusterID, bool isDevice)
{
#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,
TextID = R.MyInternationalizationString.uAddDevice,
TextColor = Shared.Common.ZigbeeColor.Current.GXCTextBlackColor,
Width = Application.GetRealWidth(1080 - 500),
Gravity = Gravity.CenterHorizontal,
};
titleFrameLayout.AddChidren(title);
if (isDevice)
{
title.TextID = R.MyInternationalizationString.BindDevice;
}
else
{
title.TextID = R.MyInternationalizationString.BindScene;
}
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);
back.MouseUpEventHandler += (sender, e) =>
{
this.RemoveFromParent();
};
#endregion
#region addBindDevice FrameLayout (只有UI,无数据处理)
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 btnFinifh = new Button()
{
Width = Application.GetRealWidth(720),
Height = Application.GetRealHeight(120),
Y = Application.GetRealHeight(1920 - 220 - 270),
Gravity = Gravity.CenterHorizontal,
Radius = (uint)Application.GetRealHeight(120) / 2,
TextID = R.MyInternationalizationString.uFinish,
BackgroundColor = UserCenterColor.Current.ClickButtonColor,
};
btnFinifh.MouseUpEventHandler += async (sender, e) =>
{
var addBindeDev = new AddBindData();
addBindeDev.DeviceAddr = key.DeviceAddr;
addBindeDev.Epoint = key.DeviceEpoint;
if (!isDevice)
{
if (!isChoose)
{
targetList.Clear();
}
}
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);
}
}
bool isFinish = false;
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:
PanelBindPage.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--;
}
}
}
isFinish = true;
break;
case 1:
new Tip()
{
MaxWidth = 150,
Text = Language.StringByID(R.MyInternationalizationString.BindErrorAbsence),
Direction = AMPopTipDirection.Up,
CloseTime = 1
}.Show(btnFinifh);
break;
case 2:
switch (dev.addBindResultResponseData?.Result)
{
case 0:
PanelBindPage.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--;
}
}
isFinish = true;
break;
case 140:
new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.BindFull), Direction = AMPopTipDirection.Up, CloseTime = 1 }.Show(btnFinifh);
break;
default:
new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.BindFailed), Direction = AMPopTipDirection.Up, CloseTime = 1 }.Show(btnFinifh);
break;
}
break;
case 3:
new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.BindErrorExisted), Direction = AMPopTipDirection.Up, CloseTime = 1 }.Show(btnFinifh);
break;
case 4:
new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.BindFailed), Direction = AMPopTipDirection.Up, CloseTime = 1 }.Show(btnFinifh);
break;
default:
break;
}
}
}
catch { }
finally
{
if (isFinish)
{
if (action != null)
{
action(PanelBindPage.bindList);
}
this.RemoveFromParent();
}
else
{
this.RemoveFromParent();
}
CommonPage.Loading.Hide();
}
};
midFrameLayout.AddChidren(btnFinifh);
midVerticalScrolViewLayout = new VerticalScrolViewLayout()
{
Height = btnFinifh.Y - Application.GetRealHeight(120),
};
midFrameLayout.AddChidren(midVerticalScrolViewLayout);
SetMiddleFrameInfo(key, currentClusterID, isDevice);
#endregion
}
///
/// 设置中部信息
///
private void SetMiddleFrameInfo(Panel key, int currentClusterID, bool isDevice)
{
midVerticalScrolViewLayout.RemoveAll();
if (isDevice)
{
foreach (CommonDevice device in PanelBindPage.list)
{
foreach (var clu in device.DeviceInfo?.InClusterList)
{
if (clu.InCluster == currentClusterID)
{
//本地设备掉线,不显示
if (device.DeviceInfo.IsOnline == 0)
{
Console.WriteLine("length loss");
continue;
}
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 btnSelect = new Button()
{
Width = Application.GetMinRealAverage(110),
Height = Application.GetMinRealAverage(110),
X = Application.CurrentWidth - Application.GetRealWidth(150),
TextColor = Shared.Common.ZigbeeColor.Current.TextBlack,
TextAlignment = TextAlignment.CenterLeft,
//UnSelectedImagePath = "Item/Tick.png",
SelectedImagePath = "Item/TickSelected.png",
Gravity = Gravity.CenterVertical,
};
rowLayout.AddChidren(btnSelect);
if (device.DeviceInfo.DeviceName == "")
{
btnBindName.Text = device.DeviceAddr + device.DeviceEpoint;
}
else
{
btnBindName.Text = device.DeviceInfo.DeviceName;
}
//被绑定设备图片
//灯光
if (device.Type == DeviceType.ColorDimmableLight)
{
//图标
devicePic.SelectedImagePath = "Device/LightSelected.png";
devicePic.UnSelectedImagePath = "Device/Light.png";
}
//开关设备(这个东西也是个“灯光”)
else if (device.Type == DeviceType.OnOffOutput)
{
//图标
devicePic.SelectedImagePath = "Device/LightSelected.png";
devicePic.UnSelectedImagePath = "Device/Light.png";
}
//窗帘
else if (device.Type == DeviceType.WindowCoveringDevice)
{
//图标
devicePic.SelectedImagePath = "Device/WindowCoveringDeviceSelected.png";
devicePic.UnSelectedImagePath = "Device/WindowCoveringDevice.png";
}
//控制面板
else if (device.Type == DeviceType.OnOffSwitch)
{
//图标
devicePic.SelectedImagePath = "Device/ButtonControlSelected.png";
devicePic.UnSelectedImagePath = "Device/ButtonControl.png";
}
foreach (var bindedDev in PanelBindPage.bindList)
{
if (device.DeviceAddr == bindedDev.BindMacAddr && device.DeviceEpoint == bindedDev.BindEpoint)
{
devicePic.IsSelected = true;
btnBindName.TextColor = ZigbeeColor.Current.SelectedTextColor;
btnSelect.IsSelected = true;
}
}
if (devicePic.IsSelected == true)
{
if (targetList != null)
{
targetList.Remove(device);
}
}
bool IsTip = false;
EventHandler hander = (sender1, e1) =>
{
//绑定只能选中,不能取消;因为这里考虑到按键已经存在的绑定设备,删除可能会存在删除慢或这删除失败的问题,
//如果是新增的绑定,在临时表中取消有很快
if (PanelBindPage.bindList.Count != 0)
{
if (IsTip)
{
return;
}
IsTip = true;
if (devicePic.IsSelected)
{
new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.BindExist), Direction = AMPopTipDirection.Up, CloseTime = 1 }.Show(btnBindName);
return;
}
}
if (!devicePic.IsSelected)
{
devicePic.IsSelected = true;
btnSelect.IsSelected = true;
btnBindName.TextColor = ZigbeeColor.Current.SelectedTextColor;
targetList.Add(device);
}
//else{
// devicePic.IsSelected = false;
// btnSelect.IsSelected = false;
// btnBindName.TextColor = ZigbeeColor.Current.TextBlack;
// targetList.Remove(device);
//}
};
rowLayout.MouseUpEventHandler += hander;
devicePic.MouseUpEventHandler += hander;
btnBindName.MouseUpEventHandler += hander;
btnSelect.MouseUpEventHandler += hander;
}
}
}
}
else
{
int index = 0;
foreach (var scene in PanelBindPage.scList)
{
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,
UnSelectedImagePath = "Item/Scene.png",
SelectedImagePath = "Item/SceneSelected.png",
Tag = index,
};
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 btnSelect = new Button()
{
Width = Application.GetMinRealAverage(110),
Height = Application.GetMinRealAverage(110),
X = Application.CurrentWidth - Application.GetRealWidth(150),
TextColor = Shared.Common.ZigbeeColor.Current.TextBlack,
TextAlignment = TextAlignment.CenterLeft,
//UnSelectedImagePath = "Item/Tick.png",
SelectedImagePath = "Item/TickSelected.png",
Gravity = Gravity.CenterVertical,
};
rowLayout.AddChidren(btnSelect);
devicePic.IsSelected = false;
btnSelect.IsSelected = false;
btnBindName.TextColor = ZigbeeColor.Current.TextBlack;
if (scene.SceneName == null)
{
btnBindName.Text = Language.StringByID(R.MyInternationalizationString.Unknown);
}
else
{
btnBindName.Text = scene.SceneName;
}
foreach (var bindedSc in PanelBindPage.bindList)
{
if (scene.ScenesId == bindedSc.BindScenesId)
{
devicePic.IsSelected = true;
btnBindName.TextColor = ZigbeeColor.Current.SelectedTextColor;
btnSelect.IsSelected = true;
targetList.Add(scene);
}
}
foreach (var sc in targetList)
{
var sc1 = (SceneUI)sc;
if (scene.ScenesId == sc1.ScenesId)
{
devicePic.IsSelected = true;
btnBindName.TextColor = ZigbeeColor.Current.SelectedTextColor;
btnSelect.IsSelected = true;
}
}
bool IsTip = false;
EventHandler hander = (sender1, e1) =>
{
if (PanelBindPage.bindList.Count != 0)
{
if (IsTip)
{
return;
}
IsTip = true;
if (devicePic.IsSelected)
{
new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.BindExist), Direction = AMPopTipDirection.Up, CloseTime = 1 }.Show(btnBindName);
return;
}
}
if (targetList.Count != 0)
{
var alert = new Alert(Language.StringByID(R.MyInternationalizationString.Tip), Language.StringByID(R.MyInternationalizationString.SureChangeScene),
Language.StringByID(R.MyInternationalizationString.Cancel), Language.StringByID(R.MyInternationalizationString.Confrim));
alert.Show();
alert.ResultEventHandler += async (sender, e) =>
{
if (e)
{
if (!devicePic.IsSelected)
{
bool delBindedScene = false;
foreach (var bindedSc in PanelBindPage.bindList)
{
var delDevice = new DelDeviceBindData();
delDevice.DeviceAddr = key.DeviceAddr;
delDevice.Epoint = key.DeviceEpoint;
var removeDevice = new RemoveBindListObj();
removeDevice.BindCluster = bindedSc.BindCluster;
removeDevice.BindType = 1;
removeDevice.BindScenesId = bindedSc.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:
delBindedScene = true;
targetList.Clear();
isChoose = true;
targetList.Add(scene);
break;
default:
new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.BindErrorAbsence), Direction = AMPopTipDirection.Up, CloseTime = 1 }.Show(btnBindName);
break;
}
}
}
}
catch { }
finally
{
CommonPage.Loading.Hide();
}
}
if (PanelBindPage.bindList.Count == 0)
{
delBindedScene = true;
targetList.Clear();
isChoose = true;
targetList.Add(scene);
}
if (delBindedScene)
{
PanelBindPage.bindList.Clear();
}
if (action != null)
{
action(PanelBindPage.bindList);
}
SetMiddleFrameInfo(key, currentClusterID, isDevice);
}
else
{
targetList.Remove(scene);
SetMiddleFrameInfo(key, currentClusterID, isDevice);
}
}
};
}
else
{
if (!devicePic.IsSelected)
{
targetList.Add(scene);
isChoose = true;
devicePic.IsSelected = true;
btnSelect.IsSelected = true;
btnBindName.TextColor = ZigbeeColor.Current.SelectedTextColor;
}
else
{
//targetList.Remove(scene);
//devicePic.IsSelected = false;
//btnSelect.IsSelected = false;
//btnBindName.TextColor = ZigbeeColor.Current.TextBlack;
}
}
};
rowLayout.MouseUpEventHandler += hander;
devicePic.MouseUpEventHandler += hander;
btnBindName.MouseUpEventHandler += hander;
btnSelect.MouseUpEventHandler += hander;
}
}
}
public void ChangedILogicStatus(Logic logic)
{
//throw new NotImplementedException();
}
public void ChangedISceneStatus(Scene scene)
{
// throw new NotImplementedException();
}
///
/// 设备状态更新接口
///
public void DeviceInfoChange(CommonDevice common, string typeTag)
{
}
}
}