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; }); } /// /// 加载绑定界面 /// /// Gateway. /// Key. public void Show(ZigBee.Device.ZbGateway gateway, Panel key) { currentKey = key; curGateway = gateway; InitConfigurePage(gateway); } #region 绑定变量 //显示被绑定设备或场景的view VerticalScrolViewLayout midVerticalScrolViewLayout; int currentClusterID = 0; int currentMode = 0; Panel currentKey; ZbGateway curGateway; Action> action;//按键绑定表通知 RowSecondRightTextView btnMode;//当前模式显示控件 FrameLayout inVerticalscrolView;//加场景或设备的view bool isBindScene = false; //本地设备列表 public static System.Collections.Generic.List localDeviceList = 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 { }; //按键配置的目标列表 private List targetList = new List(); #endregion /// /// 按键配置 /// /// Gateway. /// Key. async void InitConfigurePage(ZigBee.Device.ZbGateway gateway) { currentKey.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 = currentKey.DeviceEpointName, 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) => { localDeviceList.Clear(); scList.Clear(); this.RemoveFromParent(); }; EventHandler hander = async (sender, e) => { var oldCluster = currentClusterID; if (currentKey. priFunList == null) { try { CommonPage.Loading.Start(); KeyModeList(currentKey); } catch { } finally { CommonPage.Loading.Hide(); } } if (currentKey.priFunList != null) { PickerView.Show(currentKey.priFunList, ModeSelectAction); } else { new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.NoModeSupport), Direction = AMPopTipDirection.Up, CloseTime = 2 }.Show(btnMode); } currentKey.ReSave(); }; 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; } if (currentMode != 1) { var selectDeviceList = new List(); foreach (var dev in currentKey.bindList) { string mainkey = Common.LocalDevice.Current.GetDeviceMainKeys(dev.BindMacAddr, dev.BindEpoint); selectDeviceList.Add(mainkey); } var listShowDevice = new List(); foreach (var device in localDeviceList) { 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) => { try { if (devList == null || devList.Count == 0) { return; } CommonPage.Loading.Start(); //执行目标绑定 var result = await Common.LocalDevice.Current.BindDeviceTarget(currentKey, devList, currentClusterID); if (result != null && result.Count > 0) { currentKey.bindList.Clear(); var getBindList = await currentKey.GetDeviceBindAsync(); var bindDeviceListTemp = new System.Collections.Generic.List { }; var bindSceneListTemp = new System.Collections.Generic.List { }; if (getBindList != null) { if (getBindList.getAllBindResponseData != null) { foreach (var b in getBindList.getAllBindResponseData.BindList) { if (b.BindCluster == currentClusterID) { if (b.BindCluster == 6) { if (b.BindType == 2) { bindSceneListTemp.Add(b); } else { bindDeviceListTemp.Add(b); } } else { currentKey.bindList.Add(b); } } } } } if (currentKey.panelMode == 1) { currentKey.bindList = bindSceneListTemp; } else if (currentKey.panelMode == 100 || currentKey.panelMode == 101 || currentKey.panelMode == 102) { currentKey.bindList = bindDeviceListTemp; } } }catch { } finally { //绑定表刷新 RefreshList(gateway, currentKey); CommonPage.Loading.Hide(); } }); } else { if (currentKey.bindList.Count != 0) { new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.SneneModeExistTargets), Direction = AMPopTipDirection.Up, CloseTime = 1 }.Show(btnAddBind); } else { var addBindTargetsPage = new Shared.Phone.UserCenter.Bind.AddBindTargetsPage(); Shared.Phone.UserView.HomePage.Instance.AddChidren(addBindTargetsPage); Shared.Phone.UserView.HomePage.Instance.PageIndex += 1; addBindTargetsPage.Show(currentKey, currentClusterID, false); Action> action = (obj) => { currentKey.bindList = obj; RefreshList(curGateway, currentKey); }; addBindTargetsPage.action += action; } } currentKey.ReSave(); }; //绑定表数据初始化(被绑定的设备列表,场景列表和设备列表) try { localDeviceList.Clear(); scList.Clear(); foreach (var dev in Shared.Common.LocalDevice.Current.listAllDevice) { localDeviceList.Add(dev); } scList = Shared.Common.Room.AllRoomSceneUIList; KeyModeList(currentKey); } catch { } #endregion } /// /// 按键绑定表显示 /// /// Gateway. /// Key. void RefreshList(ZigBee.Device.ZbGateway gateway, Panel key) { midVerticalScrolViewLayout.RemoveAll(); foreach (var bindDevice in key.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, }; 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 绑定数据处理 bool isExistScene = false; foreach (var sc in key.bindList) { if (sc.BindType == 2) { isExistScene = true; } } if (isExistScene) { devicePic.UnSelectedImagePath = "Item/Scene.png"; devicePic.Width = Application.GetMinRealAverage(110); if (bindDevice.ESName == "") { btnBindName.Text = bindDevice.BindScenesId.ToString(); } else { btnBindName.Text = bindDevice.ESName; } } else { //被绑定设备图片 foreach (var dev in localDeviceList) { var tempDev = localDeviceList.Find(obj => obj.DeviceAddr == bindDevice.BindMacAddr && obj.DeviceEpoint == bindDevice.BindEpoint); if (tempDev != null) { var deviceUi = Common.LocalDevice.Current.GetDeviceUI(tempDev); devicePic.UnSelectedImagePath = deviceUi.IconPath; devicePic.Width = Application.GetRealWidth(110); devicePic.Text = ""; if (tempDev.DeviceEpointName == "") { btnBindName.Text = tempDev.DeviceEpoint + Language.StringByID(R.MyInternationalizationString.uDeviceCircuit); } else { btnBindName.Text = tempDev.DeviceEpointName; } } else { btnBindName.Text = bindDevice.BindMacAddr + "_" + bindDevice.BindEpoint; devicePic.Width = Application.GetMinRealAverage(200); devicePic.TextAlignment = TextAlignment.CenterLeft; devicePic.TextColor = UserCenterColor.Current.Gray; devicePic.Text = Language.StringByID(R.MyInternationalizationString.uOffLine); } } } //删除事件处理 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: key.bindList.Remove(bindDevice); RefreshList(gateway, key); break; case 1: key.bindList.Clear(); try { CommonPage.Loading.Start(); var getBindList = await key.GetDeviceBindAsync(); var bindDeviceListTemp = new System.Collections.Generic.List { }; var bindSceneListTemp = new System.Collections.Generic.List { }; if (getBindList != null) { if (getBindList.getAllBindResponseData != null) { foreach (var b in getBindList.getAllBindResponseData.BindList) { if (b.BindCluster == currentClusterID) { if (b.BindCluster == 6) { if (b.BindType == 2) { bindSceneListTemp.Add(b); } else { bindDeviceListTemp.Add(b); } } else { key.bindList.Add(b); } } } } } if (key.panelMode == 1) { key.bindList = bindSceneListTemp; } else if (key.panelMode == 100 || key.panelMode == 101 || key.panelMode == 102) { key.bindList = bindDeviceListTemp; } } catch { } finally { CommonPage.Loading.Hide(); RefreshList(gateway, key); } break; case 4: if (delResult.removeBindResultResponseData != null) { if (delResult.removeBindResultResponseData.Result == 0) { key.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; } } } else { new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.RemoveFailed), Direction = AMPopTipDirection.Up, CloseTime = 2 }.Show(btnDel); } } 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.Id; 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: key.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.Id == 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: key.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 key, FrameLayout inVerticalscrolView, Action action) { inVerticalscrolView.RemoveAll(); if (isDevice) { inVerticalscrolView.Height = Application.GetRealHeight(946 - 240); if (ZbGateway.MainGateWay == null) { return; } int btnDeviceIndex = 0; int i = 0; foreach (var device in localDeviceList) { foreach (var clu in device.DeviceInfo?.InClusterList) { if (clu.InCluster == currentClusterID) { i++; var btnEquipment = new Button() { Width = (inVerticalscrolView.Width - Application.GetRealWidth(29) - Application.GetRealWidth(50)) / 2, Height = Application.GetRealHeight(90), X = Application.GetRealHeight(30), Y = Application.GetRealHeight(10), TextAlignment = TextAlignment.Center, BackgroundColor = Shared.Common.ZigbeeColor.Current.ButtonColor, SelectedBackgroundColor = Shared.Common.ZigbeeColor.Current.SelectedColor, TextColor = Shared.Common.ZigbeeColor.Current.TextColor, Text = i.ToString(), Radius = 10, }; //本地设备掉线,不显示 //if (device.DeviceInfo.IsOnline == 0) //{ // Console.WriteLine("length loss"); // continue; //} if (btnDeviceIndex < 2) { btnEquipment.X = btnDeviceIndex * (btnEquipment.Width + Application.GetRealWidth(15)) + Application.GetRealWidth(32); btnEquipment.Y = Application.GetRealHeight(10); btnDeviceIndex++; } else { btnEquipment.X = btnDeviceIndex % 2 * (btnEquipment.Width + Application.GetRealWidth(15)) + Application.GetRealWidth(31); btnEquipment.Y = (int)btnDeviceIndex / 2 * (btnEquipment.Height + Application.GetRealHeight(10)) + Application.GetRealHeight(10); btnDeviceIndex++; } if (device.DeviceInfo.DeviceName == null) { btnEquipment.Text = device.DeviceAddr + device.DeviceEpoint; } else { btnEquipment.Text = device.DeviceInfo.DeviceName; } btnEquipment.Tag = device; inVerticalscrolView.AddChidren(btnEquipment); foreach (var bindedDev in key.bindList) { if (device.DeviceAddr == bindedDev.BindMacAddr && device.DeviceEpoint == bindedDev.BindEpoint) { btnEquipment.IsSelected = true; btnEquipment.TextColor = ZigbeeColor.Current.SelectedTextColor; } } if (btnEquipment.IsSelected == true) { if (targetList != null) { targetList.Remove(device); } } btnEquipment.MouseUpEventHandler += (sendere, ee) => { btnEquipment.IsSelected = !btnEquipment.IsSelected; if (btnEquipment.IsSelected) { targetList.Add(device); } else { if (targetList != null) { targetList.Remove(device); } } }; } } } } else { inVerticalscrolView.Height = Application.GetRealHeight(946 - 240); int btnDeviceIndex = 0; int i = 0; foreach (var scene in scList) { i++; var btnEquipment = new Button() { Width = (inVerticalscrolView.Width - Application.GetRealWidth(29) - Application.GetRealWidth(50)) / 2, Height = Application.GetRealHeight(90), X = Application.GetRealHeight(30), Y = Application.GetRealHeight(10), TextAlignment = TextAlignment.Center, BackgroundColor = Shared.Common.ZigbeeColor.Current.ButtonColor, SelectedBackgroundColor = Shared.Common.ZigbeeColor.Current.SelectedColor, TextColor = Shared.Common.ZigbeeColor.Current.TextColor, Text = i.ToString(), Radius = 10, }; if (btnDeviceIndex < 2) { btnEquipment.X = btnDeviceIndex * (btnEquipment.Width + Application.GetRealWidth(15)) + Application.GetRealWidth(32); btnEquipment.Y = Application.GetRealHeight(10); btnDeviceIndex++; } else { btnEquipment.X = btnDeviceIndex % 2 * (btnEquipment.Width + Application.GetRealWidth(15)) + Application.GetRealWidth(31); btnEquipment.Y = (int)btnDeviceIndex / 2 * (btnEquipment.Height + Application.GetRealHeight(10)) + Application.GetRealHeight(10); btnDeviceIndex++; } if (scene.Name == null) { btnEquipment.Text = Language.StringByID(R.MyInternationalizationString.Unknown); } else { btnEquipment.Text = scene.Name; } btnEquipment.Tag = scene; inVerticalscrolView.AddChidren(btnEquipment); foreach (var bindedSc in key.bindList) { if (scene.Id == bindedSc.BindScenesId) { btnEquipment.IsSelected = true; btnEquipment.TextColor = ZigbeeColor.Current.SelectedTextColor; } } if (btnEquipment.IsSelected == true) { if (targetList != null) { targetList.Remove(scene); } } btnEquipment.MouseUpEventHandler += (sendere, ee) => { btnEquipment.IsSelected = !btnEquipment.IsSelected; if (btnEquipment.IsSelected) { targetList.Add(scene); } else { if (targetList != null) { targetList.Remove(scene); } } }; } } } /// /// 按键模式更新 /// /// Button mode text. /// Key. public void UpdateType(Button btnText, int curID) { switch (curID) { case 1: btnText.TextID = R.MyInternationalizationString.SceneTrigger; currentClusterID = 6; currentMode = 1; break; case 100: btnText.TextID = R.MyInternationalizationString.OnOffOn; currentClusterID = 6; currentMode = 100; break; case 101: btnText.TextID = R.MyInternationalizationString.OnOffoFF; currentClusterID = 6; currentMode = 101; break; case 102: btnText.TextID = R.MyInternationalizationString.OnOffOnOff; currentClusterID = 6; currentMode = 102; break; case 200: btnText.TextID = R.MyInternationalizationString.LevelStepUp; currentClusterID = 8; currentMode = 200; break; case 201: btnText.TextID = R.MyInternationalizationString.LevelStepDown; currentClusterID = 8; currentMode = 201; break; case 202: btnText.TextID = R.MyInternationalizationString.LevelStepUpDown; currentClusterID = 8; currentMode = 202; break; case 300: btnText.TextID = R.MyInternationalizationString.WcdUp; currentClusterID = 258; currentMode = 300; break; case 301: btnText.TextID = R.MyInternationalizationString.WcdDown; currentClusterID = 258; currentMode = 301; break; case 302: btnText.TextID = R.MyInternationalizationString.WcdStop; currentClusterID = 258; currentMode = 302; break; case 303: btnText.TextID = R.MyInternationalizationString.WcdUpStop; currentClusterID = 258; currentMode = 303; break; case 304: btnText.TextID = R.MyInternationalizationString.WcdDownStop; currentClusterID = 258; currentMode = 304; break; default: btnText.TextID = R.MyInternationalizationString.PanelCurrentMode; currentClusterID = 258; break; } } /// /// 模式选择Action事件 /// /// private async void ModeSelectAction(string value) { var temp = new Panel.SetWritableValueResponAllData(); var oldCluster = currentClusterID; if (currentKey == null || curGateway == null) { return; } if (value == Language.StringByID(R.MyInternationalizationString.SceneTrigger)) { btnMode.TextID = R.MyInternationalizationString.SceneTrigger; temp = await currentKey.ConfigureHdlKeyValueAsync(KeyMode.Scene_Trigger); currentClusterID = 6; currentMode = 1; } if (value == Language.StringByID(R.MyInternationalizationString.OnOffOn)) { btnMode.TextID = R.MyInternationalizationString.OnOffOn; temp = await currentKey.ConfigureHdlKeyValueAsync(KeyMode.OnOff_On); currentClusterID = 6; currentMode = 100; } else if (value == Language.StringByID(R.MyInternationalizationString.OnOffoFF)) { btnMode.TextID = R.MyInternationalizationString.OnOffoFF; temp = await currentKey.ConfigureHdlKeyValueAsync(KeyMode.OnOff_oFF); currentClusterID = 6; currentMode = 101; } else if (value == Language.StringByID(R.MyInternationalizationString.OnOffOnOff)) { btnMode.TextID = R.MyInternationalizationString.OnOffOnOff; temp = await currentKey.ConfigureHdlKeyValueAsync(KeyMode.OnOff_OnOff); currentClusterID = 6; currentMode = 102; } else if (value == Language.StringByID(R.MyInternationalizationString.LevelStepUp)) { btnMode.TextID = R.MyInternationalizationString.LevelStepUp; temp = await currentKey.ConfigureHdlKeyValueAsync(KeyMode.Level_Step_Up); currentClusterID = 8; currentMode = 200; } else if (value == Language.StringByID(R.MyInternationalizationString.LevelStepDown)) { btnMode.TextID = R.MyInternationalizationString.LevelStepDown; temp = await currentKey.ConfigureHdlKeyValueAsync(KeyMode.Level_Step_Down); currentClusterID = 8; currentMode = 201; } else if (value == Language.StringByID(R.MyInternationalizationString.LevelStepUpDown)) { btnMode.TextID = R.MyInternationalizationString.LevelStepUpDown; temp = await currentKey.ConfigureHdlKeyValueAsync(KeyMode.Level_Step_Up_Down); currentClusterID = 8; currentMode = 202; } else if (value == Language.StringByID(R.MyInternationalizationString.WcdUp)) { btnMode.TextID = R.MyInternationalizationString.WcdUp; temp = await currentKey.ConfigureHdlKeyValueAsync(KeyMode.Wcd_Up); currentClusterID = 258; currentMode = 300; } else if (value == Language.StringByID(R.MyInternationalizationString.WcdDown)) { btnMode.TextID = R.MyInternationalizationString.WcdDown; temp = await currentKey.ConfigureHdlKeyValueAsync(KeyMode.Wcd_Down); currentClusterID = 258; currentMode = 301; } else if (value == Language.StringByID(R.MyInternationalizationString.WcdStop)) { btnMode.TextID = R.MyInternationalizationString.WcdStop; temp = await currentKey.ConfigureHdlKeyValueAsync(KeyMode.Wcd_Stop); currentClusterID = 258; currentMode = 302; } else if (value == Language.StringByID(R.MyInternationalizationString.WcdUpStop)) { btnMode.TextID = R.MyInternationalizationString.WcdUpStop; temp = await currentKey.ConfigureHdlKeyValueAsync(KeyMode.Wcd_Up_Stop); currentClusterID = 258; currentMode = 303; } else if (value == Language.StringByID(R.MyInternationalizationString.WcdDownStop)) { btnMode.TextID = R.MyInternationalizationString.WcdDownStop; temp = await currentKey.ConfigureHdlKeyValueAsync(KeyMode.Wcd_Down_Stop); currentClusterID = 258; currentMode = 304; } currentKey.panelMode = currentMode; System.Threading.Thread.Sleep(1000); if (temp.setWritableValueResponData?.Status != 0) { new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.ChangeBindMode), Direction = AMPopTipDirection.Up, CloseTime = 2 }.Show(btnMode); } else { //if (oldCluster != currentClusterID) //{ currentKey.bindList.Clear(); try { CommonPage.Loading.Start(); var getBindList = await currentKey.GetDeviceBindAsync(); var bindDeviceListTemp = new System.Collections.Generic.List { }; var bindSceneListTemp = new System.Collections.Generic.List { }; if (getBindList != null) { if (getBindList.getAllBindResponseData != null) { foreach (var b in getBindList.getAllBindResponseData.BindList) { if (b.BindCluster == currentClusterID) { if (b.BindCluster == 6) { if (b.BindType == 2) { bindSceneListTemp.Add(b); } else { bindDeviceListTemp.Add(b); } } else { currentKey.bindList.Add(b); } } } } } if (currentKey.panelMode == 1) { currentKey.bindList = bindSceneListTemp; } else if (currentKey.panelMode == 100 || currentKey.panelMode == 101 || currentKey.panelMode == 102) { currentKey.bindList = bindDeviceListTemp; } } catch { } finally { CommonPage.Loading.Hide(); } RefreshList(curGateway, currentKey); //} } } /// /// 按键支持的模式列表 /// /// Key. private async void KeyModeList(Panel key) { try { CommonPage.Loading.Start(); key.priFunList.Clear(); var resultMode = await BindInfo.CheckPanelKeyFunctionLevel3(key); var tempDeata = new PanelConfigureInfoResponAllData(); foreach (var mo in resultMode) { if (mo.Value == true) { switch (mo.Key) { case "场景:触发": currentKey.priFunList.Add(Language.StringByID(R.MyInternationalizationString.SceneTrigger)); break; case "开关:开": currentKey.priFunList.Add(Language.StringByID(R.MyInternationalizationString.OnOffOn)); break; case "开关:关": currentKey.priFunList.Add(Language.StringByID(R.MyInternationalizationString.OnOffoFF)); break; case "开关:切换": currentKey.priFunList.Add(Language.StringByID(R.MyInternationalizationString.OnOffOnOff)); break; case "亮度:调大": currentKey.priFunList.Add(Language.StringByID(R.MyInternationalizationString.LevelStepUp)); break; case "亮度:调小": currentKey.priFunList.Add(Language.StringByID(R.MyInternationalizationString.LevelStepDown)); break; case "亮度:切换": currentKey.priFunList.Add(Language.StringByID(R.MyInternationalizationString.LevelStepUpDown)); break; case "窗帘:开": currentKey.priFunList.Add(Language.StringByID(R.MyInternationalizationString.WcdUp)); break; case "窗帘:关": currentKey.priFunList.Add(Language.StringByID(R.MyInternationalizationString.WcdDown)); break; case "窗帘:停": currentKey.priFunList.Add(Language.StringByID(R.MyInternationalizationString.WcdStop)); break; case "窗帘:上升停": currentKey.priFunList.Add(Language.StringByID(R.MyInternationalizationString.WcdUpStop)); break; case "窗帘:下降停": currentKey.priFunList.Add(Language.StringByID(R.MyInternationalizationString.WcdDownStop)); break; default: break; } } } tempDeata = await key.ReadPanelConfigureInfoAsync(); if (tempDeata!=null && tempDeata.deviceStatusReportData != null) { if (tempDeata.deviceStatusReportData.CluterID == 6) { foreach (var attr in tempDeata.deviceStatusReportData.AttriBute) { var curID = attr.AttriButeData; key.panelMode = curID; key.ReSave(); UpdateType(btnMode, curID); var getBindList = await key.GetDeviceBindAsync(); var bindDeviceListTemp = new System.Collections.Generic.List { }; var bindSceneListTemp = new System.Collections.Generic.List { }; if (getBindList != null) { if (getBindList.getAllBindResponseData != null) { foreach (var b in getBindList.getAllBindResponseData.BindList) { if (b.BindCluster == currentClusterID) { if (b.BindCluster == 6) { if (b.BindType == 2) { bindSceneListTemp.Add(b); } else { bindDeviceListTemp.Add(b); } } else { key.bindList.Add(b); } } } } } if(key.panelMode==1){ key.bindList = bindSceneListTemp; }else if(key.panelMode == 100|| key.panelMode == 101||key.panelMode == 102) { key.bindList = bindDeviceListTemp; } } } } else { new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.GetDataFailed), Direction = AMPopTipDirection.Up, CloseTime = 2 }.Show(btnMode); } } catch { } finally{ //绑定表刷新 RefreshList(curGateway, key); currentKey = key; currentKey.ReSave(); CommonPage.Loading.Hide(); } } /// /// 能绑定到按键的设备 /// /// /// public bool SupportDevice(CommonDevice device, int currentClusterID) { foreach (var data in device.InClusterList) { if (data.InCluster == currentClusterID) { return true; } } return false; } /// /// 添加绑定目标 /// /// Key. /// Current cluster identifier. /// Current mode. private void AddBindTarget(Panel key, int currentClusterID, int currentMode) { var selectDialog = new Dialog() { }; selectDialog.Show(); var itemFL = new FrameLayout() { Y = Application.GetRealHeight(1920 - 600 + 130), Width = Application.GetRealWidth(900), Height = Application.GetRealHeight(550 - 130), Radius = 10, Gravity = Gravity.CenterHorizontal }; selectDialog.AddChidren(itemFL); var selectedDeviceBtn = new Button() { Height = Application.GetRealHeight(130) - 1, TextID = R.MyInternationalizationString.Device, TextColor = ZigbeeColor.Current.GXCTextBlackColor, TextAlignment = TextAlignment.Center, BackgroundColor = ZigbeeColor.Current.GXCTextWhiteColor }; itemFL.AddChidren(selectedDeviceBtn); var selectedDeviceLine = new Button() { Y = selectedDeviceBtn.Bottom, Height = 1, BackgroundColor = ZigbeeColor.Current.GXCLineColor, }; itemFL.AddChidren(selectedDeviceLine); var selectedSceneBtn = new Button() { Y = selectedDeviceLine.Bottom, Height = Application.GetRealHeight(130) - 1, TextID = R.MyInternationalizationString.Scence, TextColor = ZigbeeColor.Current.GXCTextBlackColor, TextAlignment = TextAlignment.Center, BackgroundColor = ZigbeeColor.Current.GXCTextWhiteColor }; itemFL.AddChidren(selectedSceneBtn); var cancelBtn = new Button() { Y = selectedSceneBtn.Bottom + Application.GetRealHeight(30), Height = Application.GetRealHeight(150), TextID = R.MyInternationalizationString.Cancel, TextColor = ZigbeeColor.Current.GXCTextWhiteColor, BackgroundColor = ZigbeeColor.Current.GXCButtonBlueColor, TextAlignment = TextAlignment.Center, }; itemFL.AddChidren(cancelBtn); //添加设备 selectedDeviceBtn.MouseUpEventHandler += (send, ee) => { selectDialog.Close(); var selectDeviceList = new List(); foreach (var dev in key.bindList) { string mainkey = Common.LocalDevice.Current.GetDeviceMainKeys(dev.BindMacAddr, dev.BindEpoint); selectDeviceList.Add(mainkey); } var listShowDevice = new List(); foreach (var device in PanelBindPage.localDeviceList) { 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) { key.bindList.Clear(); try { var getBindList = await key.GetDeviceBindAsync(); var bindDeviceListTemp = new System.Collections.Generic.List { }; var bindSceneListTemp = new System.Collections.Generic.List { }; if (getBindList != null) { if (getBindList.getAllBindResponseData != null) { foreach (var b in getBindList.getAllBindResponseData.BindList) { if (b.BindCluster == currentClusterID) { if (b.BindCluster == 6) { if (b.BindType == 2) { bindSceneListTemp.Add(b); } else { bindDeviceListTemp.Add(b); } } else { key.bindList.Add(b); } } } } } if (key.panelMode == 1) { key.bindList = bindSceneListTemp; } else if (key.panelMode == 100 || key.panelMode == 101 || key.panelMode == 102) { key.bindList = bindDeviceListTemp; } } catch { } //绑定表刷新 RefreshList(curGateway, key); } }); }; //添加场景 selectedSceneBtn.MouseUpEventHandler += (send, ee) => { selectDialog.Close(); //被绑定设备的场景图片 bool isExistScene = false; bool isExistDevice = false; foreach (var sc in key.bindList) { if (sc.BindType == 2) { isExistScene = true; } else { isExistDevice = true; } } if (isExistScene) { new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.ChangeBindDeviceToScene), Direction = AMPopTipDirection.Up, CloseTime = 2 }.Show(selectedSceneBtn); } else if (isExistDevice) { var alert = new Alert(Language.StringByID(R.MyInternationalizationString.Tip), Language.StringByID(R.MyInternationalizationString.ChangeBindDeviceToScene), Language.StringByID(R.MyInternationalizationString.Cancel), Language.StringByID(R.MyInternationalizationString.Confrim)); alert.Show(); alert.ResultEventHandler += (sender1, e) => { if (e) { var editScenePage = new Shared.Phone.Device.Category.Category(); Shared.Phone.UserView.HomePage.Instance.AddChidren(editScenePage); Shared.Phone.UserView.HomePage.Instance.PageIndex += 1; editScenePage.Show(); } }; } else { 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); Action> action = (obj) => { key.bindList = obj; RefreshList(curGateway, key); }; addBindTargetsPage.action += action; } }; cancelBtn.MouseUpEventHandler += (send, ee) => { selectDialog.Close(); }; } public void ChangedILogicStatus(ZigBee.Device.Logic logic) { } public void ChangedISceneStatus(Scene scene) { } /// /// 设备状态更新接口 /// public void DeviceInfoChange(CommonDevice common, string typeTag) { } } }