using System; using System.Collections.Generic; using System.Threading.Tasks; using Shared.Common; using Shared.Phone.UserCenter.Device; using ZigBee.Device; using static ZigBee.Device.BindObj; using static ZigBee.Device.Panel; namespace Shared.Phone.UserCenter.DeviceBind { public class BindInfo { #region 绑定设计的变量定义 /// /// 当前设备绑定类型 /// public enum BindType { /// /// 新风 /// FreshAir = 1, /// /// 温度传感器 /// Temperature = 2, /// /// 湿度传感器 /// Humidity = 3, /// /// PM2.5传感器 /// PM = 4, /// /// 空调目标 /// AC = 5, } #endregion #region 绑定涉及的数据处理 /// /// 获取当前楼层名称 /// /// public static string GetCurrentKeyAllRoomList() { var dicFloor = HdlRoomLogic.Current.GetFloorSortList(); foreach (var floorId in dicFloor.Keys) { //第一个楼层 return dicFloor[floorId]; break; } return null; } /// /// 获取当前楼层 /// /// public static string GetCurrentSelectFloorId() { var dicFloor = HdlRoomLogic.Current.GetFloorSortList();// foreach (var floorId in dicFloor.Keys) { //第一个楼层 return floorId; break; } return null; } /// /// 获取当前楼层名称 /// /// public static string GetCurrentSelectFloorIdName() { var dicFloor = HdlRoomLogic.Current.GetFloorSortList(); foreach (var floorId in dicFloor.Keys) { //第一个楼层 return dicFloor[floorId]; break; } return null; } /// /// 获取当前楼层名称 /// fllodID:楼层ID /// /// public static string GetBindTargetsFloorIdName(string curFllodID) { var dicFloor = HdlRoomLogic.Current.GetFloorSortList(); foreach (var floorId in dicFloor.Keys) { //当前楼层 if (curFllodID == floorId) { return dicFloor[floorId]; break; } } return null; } /// /// 获取当前房间中匹配的支持绑定的设备列表 /// currentRoomSupportBindDeviceList 当前房间中支持被绑定的设备列表 /// curRoom 当前房间 /// curDeviceBindType 当前设备绑定类型 /// /// public static List GetCurRoomSupportDeviceList(List currentRoomSupportBindDeviceList, Room curRoom, BindInfo.BindType curDeviceBindType) { currentRoomSupportBindDeviceList.Clear(); List curRoomDeviceListTemp = new List(); switch (curDeviceBindType) { case BindType.FreshAir: foreach (var deviceKeys in curRoom.ListDevice) { var device = LocalDevice.Current.GetDevice(deviceKeys); if (device != null) { if (BindInfo.checkRealFreshAirDevice(device) == false) { continue; } if (device.Type == DeviceType.FreshAir) { curRoomDeviceListTemp.Add(device); } } } break; case BindType.Temperature: curRoomDeviceListTemp = GetMatchTemperatureSensorDevice(curRoom); break; case BindType.Humidity: curRoomDeviceListTemp = GetMatchHumiditySensorDevice(curRoom); foreach (var deviceKeys in curRoom.ListDevice) { var device = LocalDevice.Current.GetDevice(deviceKeys); if (device != null) { if (device.Type == DeviceType.FreshAirHumiditySensor) { curRoomDeviceListTemp.Add(device); } } } break; case BindType.PM: foreach (var deviceKeys in curRoom.ListDevice) { var device = LocalDevice.Current.GetDevice(deviceKeys); if (device != null) { if (device.Type == DeviceType.PMSensor) { curRoomDeviceListTemp.Add(device); } } } break; case BindType.AC: foreach (var deviceKeys in curRoom.ListDevice) { var device = LocalDevice.Current.GetDevice(deviceKeys); if (device != null) { if (device.Type == DeviceType.Thermostat) { if (BindInfo.checkRealAcDevice(device) == false) { continue; } curRoomDeviceListTemp.Add(device); } } } break; } return curRoomDeviceListTemp; } /// /// 匹配温度传感器 /// /// public static List GetMatchTemperatureSensorDevice(Room room) { List roomIncludeMatchTempDevice = new List(); foreach (var de in room.ListDevice) { var device = LocalDevice.Current.GetDevice(de); if (device != null) { //电池设备不支持绑定 if (device.ZigbeeType != 1) { continue; } if (device.Type == DeviceType.TemperatureSensor) { var dev = device as TemperatureSensor; if (dev.SensorDiv == 1) { roomIncludeMatchTempDevice.Add(device); } } else if (device.Type == DeviceType.PMSensor) { var dev = device as PMSensor; foreach (var clu in dev.InClusterList) { if (clu.InCluster == 1026) { roomIncludeMatchTempDevice.Add(device); } } } } } return roomIncludeMatchTempDevice; } /// /// 匹配湿度传感器 /// /// public static List GetMatchHumiditySensorDevice(Room room) { List roomIncludeMatchHumpDevice = new List(); foreach (var de in room.ListDevice) { var device = LocalDevice.Current.GetDevice(de); if (device != null) { //电池设备不支持绑定 if (device.ZigbeeType != 1) { continue; } if (device.Type == DeviceType.TemperatureSensor) { var dev = device as TemperatureSensor; if (dev.SensorDiv == 2) { roomIncludeMatchHumpDevice.Add(device); } } else if (device.Type == DeviceType.PMSensor) { var dev = device as PMSensor; foreach (var clu in dev.InClusterList) { if (clu.InCluster == 1029) { roomIncludeMatchHumpDevice.Add(device); } } } } } return roomIncludeMatchHumpDevice; } /// /// 获取楼层对应的房间列表 /// /// public static List GetFloorRoomList() { var supportRoomListTemp = new List(); var listAllRoom = HdlRoomLogic.Current.GetAllListRooms(); if (Common.Config.Instance.Home.FloorDics.Count == 0) { // 获取没有楼层房间 foreach (var room in listAllRoom) { if (string.IsNullOrEmpty(room.FloorId)) { if (room.IsLove) { continue; } supportRoomListTemp.Add(room); } } } else { // 获取支持的房间 foreach (var room in listAllRoom) { if (room.IsLove) { continue; } supportRoomListTemp.Add(room); } } return supportRoomListTemp; } /// /// 能显示的房间列表 /// curControlDev 控制设备 /// supportRoomList 本地房间列表 /// curDeviceBindType 当前设备绑定类型 /// public static List GetSupportRoomList(Panel curControlDev, List supportRoomList, BindInfo.BindType curDeviceBindType) { var roomTempList = new List(); for (int i = 0; i < supportRoomList.Count; i++) { var room = supportRoomList[i]; //如果房间为喜爱[后来改名为常用房间],则不显示 if (room.IsLove == true) { continue; } //如果房间中没有设备,则不显示 if (room.ListDevice.Count == 0) { continue; } else { List roomIncludeMatchDevice = new List(); //房间中没有对应的支持绑定的目标 switch (curDeviceBindType) { case BindInfo.BindType.FreshAir: foreach (var de in room.ListDevice) { var device = LocalDevice.Current.GetDevice(de); if (device != null) { //获取设备类型的 var deviceEnumInfo = Common.LocalDevice.Current.GetMyDeviceEnumInfo(new List() { device }); //新风面板的新风设备,则不显示 if (deviceEnumInfo.ConcreteType == Common.DeviceConcreteType.ButtonPanel_FangyueFreshAir) { continue; } if (device.Type == DeviceType.FreshAir) { roomIncludeMatchDevice.Add(device); } } } break; case BindInfo.BindType.Temperature: roomIncludeMatchDevice = BindInfo.GetMatchTemperatureSensorDevice(room); break; case BindInfo.BindType.Humidity: roomIncludeMatchDevice = GetMatchHumiditySensorDevice(room); foreach (var de in room.ListDevice) { var device = LocalDevice.Current.GetDevice(de); if (device != null) { if (device.Type == DeviceType.FreshAirHumiditySensor) { roomIncludeMatchDevice.Add(device); } } } break; case BindInfo.BindType.PM: foreach (var de in room.ListDevice) { var device = LocalDevice.Current.GetDevice(de); if (device != null) { if (device.Type == DeviceType.PMSensor) { roomIncludeMatchDevice.Add(device); } } } break; case BindInfo.BindType.AC: foreach (var de in room.ListDevice) { var device = LocalDevice.Current.GetDevice(de); if (device != null) { if (device.Type == DeviceType.Thermostat) { if (BindInfo.checkRealAcDevice(device) == false) { continue; } roomIncludeMatchDevice.Add(device); } } } break; } if (roomIncludeMatchDevice.Count == 0) { continue; } if (!string.IsNullOrEmpty(room.FloorId)) { //有楼层 if (room.FloorId == curControlDev.currentSelectFloorId) { roomTempList.Add(room); } } else { //没有楼层 roomTempList.Add(room); } } } return roomTempList; } /// /// 获取本地未分配的支持当前类型的绑定设备列表 /// /// public static List GetUndistributeDeviceList(List undistruibuteDevList, DeviceBind.BindInfo.BindType curDeviceBindType) { undistruibuteDevList.Clear(); List UndistributeCommonDeviceListTemp = new List(); //获取本地设备列表 foreach (var tempDev in Shared.Common.LocalDevice.Current.listAllDevice) { //获取设备所属房间 var tempDevRoom = HdlRoomLogic.Current.GetRoomByDevice(tempDev); if (tempDevRoom == null) { UndistributeCommonDeviceListTemp.Add(tempDev); } } switch (curDeviceBindType) { case BindType.FreshAir: foreach (var device in UndistributeCommonDeviceListTemp) { if (BindInfo.checkRealFreshAirDevice(device) == false) { continue; } if (device.Type == DeviceType.FreshAir) { undistruibuteDevList.Add(device); } } break; case BindType.Temperature: foreach (var device in UndistributeCommonDeviceListTemp) { //电池设备不支持绑定 if (device.ZigbeeType != 1) { continue; } if (device.Type == DeviceType.TemperatureSensor) { var dev = device as TemperatureSensor; if (dev.SensorDiv == 1) { undistruibuteDevList.Add(device); } } else if (device.Type == DeviceType.PMSensor) { var dev = device as PMSensor; foreach (var clu in dev.InClusterList) { if (clu.InCluster == 1026) { undistruibuteDevList.Add(device); } } } } break; case BindType.Humidity: foreach (var device in UndistributeCommonDeviceListTemp) { //电池设备不支持绑定 if (device.ZigbeeType != 1) { continue; } if (device.Type == DeviceType.TemperatureSensor) { var dev = device as TemperatureSensor; if (dev.SensorDiv == 2) { undistruibuteDevList.Add(device); } } else if (device.Type == DeviceType.PMSensor) { var dev = device as PMSensor; foreach (var clu in dev.InClusterList) { if (clu.InCluster == 1029) { undistruibuteDevList.Add(device); } } } if (device.Type == DeviceType.FreshAirHumiditySensor) { undistruibuteDevList.Add(device); } } break; case BindType.PM: foreach (var device in UndistributeCommonDeviceListTemp) { if (device.Type == DeviceType.PMSensor) { undistruibuteDevList.Add(device); } } break; case BindType.AC: foreach (var device in UndistributeCommonDeviceListTemp) { if (device.Type == DeviceType.Thermostat) { if (BindInfo.checkRealAcDevice(device) == false) { continue; } undistruibuteDevList.Add(device); } } break; } return undistruibuteDevList; } /// /// 所有房间中匹配的支持绑定的所有目标列表 /// /// public static List GetAllRoomSupportDeviceList(List currentPanelSupportBindDeviceList, List supportRoomList, DeviceBind.BindInfo.BindType curDeviceBindType) { currentPanelSupportBindDeviceList.Clear(); List currentPanelBindSupportDeviceListTemp = new List(); switch (curDeviceBindType) { case BindInfo.BindType.FreshAir: foreach (var r in supportRoomList) { if (r.ListDevice.Count == 0) { continue; } foreach (var deviceKeys in r.ListDevice) { var device = LocalDevice.Current.GetDevice(deviceKeys); if (device != null) { if (BindInfo.checkRealFreshAirDevice(device) == false) { continue; } if (device.Type == DeviceType.FreshAir) { currentPanelBindSupportDeviceListTemp.Add(device); } } } } break; case BindInfo.BindType.Temperature: foreach (var r in supportRoomList) { if (r.ListDevice.Count == 0) { continue; } var deviceListTemp = GetMatchTemperatureSensorDevice(r); foreach (var dev in deviceListTemp) { currentPanelBindSupportDeviceListTemp.Add(dev); } } break; case BindInfo.BindType.Humidity: foreach (var r in supportRoomList) { if (r.ListDevice.Count == 0) { continue; } var deviceListTemp = GetMatchHumiditySensorDevice(r); foreach (var dev in deviceListTemp) { currentPanelBindSupportDeviceListTemp.Add(dev); } foreach (var deviceKeys in r.ListDevice) { var device = LocalDevice.Current.GetDevice(deviceKeys); if (device != null) { if (device.Type == DeviceType.FreshAirHumiditySensor) { currentPanelBindSupportDeviceListTemp.Add(device); } } } } break; case BindInfo.BindType.PM: foreach (var r in supportRoomList) { if (r.ListDevice.Count == 0) { continue; } foreach (var deviceKeys in r.ListDevice) { var device = LocalDevice.Current.GetDevice(deviceKeys); if (device != null) { if (device.Type == DeviceType.PMSensor) { currentPanelBindSupportDeviceListTemp.Add(device); } } } } break; case BindInfo.BindType.AC: foreach (var r in supportRoomList) { if (r.ListDevice.Count == 0) { continue; } foreach (var deviceKeys in r.ListDevice) { var device = LocalDevice.Current.GetDevice(deviceKeys); if (device != null) { if (device.Type == DeviceType.Thermostat) { if (BindInfo.checkRealAcDevice(device) == false) { continue; } currentPanelBindSupportDeviceListTemp.Add(device); } } } } break; } return currentPanelBindSupportDeviceListTemp; } /// /// 检测目标是否被绑定过 /// targetList 按键配置的目标列表 /// oldTargetList 面板中已经存在的目标列表 /// /// public static bool checkExistDevice(List targetList, List oldTargetList) { bool exist = false; foreach (var oldDev in oldTargetList) { var key = oldDev.DeviceAddr + oldDev.DeviceEpoint; var result = targetList.Find(obj => (obj != null) && (obj.DeviceAddr + obj.DeviceEpoint == key)); if (result != null) { exist = true; } else { exist = false; } } return exist; } /// /// 是否是真实的空调设备 【部分是能绑定空调的设备】 /// /// /// public static bool checkRealAcDevice(CommonDevice device) { bool result = true; //获取设备类型的 var clu = device.OutClusterList.Find((obj) => obj.OutCluster == 513); if (clu != null) { result = false; } return result; } /// /// 是否是真实的新风设备 【部分是能绑定新风的设备】 /// /// /// public static bool checkRealFreshAirDevice(CommonDevice device) { bool result = true; //获取设备类型的 var deviceEnumInfo = Common.LocalDevice.Current.GetMyDeviceEnumInfo(new List() { device }); if (deviceEnumInfo.ConcreteType == Common.DeviceConcreteType.ButtonPanel_FangyueFreshAir) { result = false; } if (deviceEnumInfo.ConcreteType == Common.DeviceConcreteType.ButtonPanel_SimpleEnvironment) { result = false; } return result; } /// /// 检测该设备能否显示 /// /// /// public static bool CheckCanShowDevice(ZigBee.Device.CommonDevice device, string curDeviceBindType = "AddSwitch") { if (device == null) { return false; } //如果是传感器,或者是没有开关簇的话(这里判断的是输入簇) if ((device.Type == ZigBee.Device.DeviceType.IASZone) || InMatchDevice(device, curDeviceBindType) == false) { return false; } return true; } /// /// 检测该房间能否显示 /// /// /// public static bool CheckCanShowRoom(Common.Room room, string curDeviceBindType = "AddSwitch") { if (room.ListDevice.Count == 0) { return false; } if (room.IsLove == true) { return false; } foreach (var deviceKeys in room.ListDevice) { var device = Common.LocalDevice.Current.GetDevice(deviceKeys); //检测该设备能否显示 if (CheckCanShowDevice(device, curDeviceBindType) == false) { continue; } //存在设备的话,此房间可以显示 return true; } return false; } /// /// 检测设备是否拥有开关的功能(输入簇) /// /// /// public static bool InMatchDevice(CommonDevice device, string curDeviceBindType = "AddSwitch") { foreach (var data in device.InClusterList) { switch (curDeviceBindType) { case "AddSwitch": //拥有on/off功能的,才支持测试 if (data.InCluster == 6) { return true; } break; case "AddDimmer": if (data.InCluster == 8) { return true; } break; case "AddCurtain": if (data.InCluster == 258) { return true; } break; } } return false; } /// /// 检测控制面板(按键类)所拥有的功能,现支持的有以下几种(必定存在键值,出错会返回null) /// /// The panel key function level2. /// Key. public static async System.Threading.Tasks.Task> CheckPanelKeyFunctionLevel3(Panel key) { Dictionary dicCheck = new Dictionary(); dicCheck["场景:触发"] = false; dicCheck["开关:开"] = false; dicCheck["开关:关"] = false; dicCheck["开关:切换"] = false; dicCheck["亮度:按等级调大"] = false; dicCheck["亮度:按等级调小"] = false; dicCheck["亮度:按等级切换"] = false; dicCheck["亮度:打开"] = false; dicCheck["亮度:关闭"] = false; dicCheck["亮度:切换"] = false; dicCheck["窗帘:开"] = false; dicCheck["窗帘:关"] = false; dicCheck["窗帘:停"] = false; dicCheck["窗帘:上升停"] = false; dicCheck["窗帘:下降停"] = false; List result = null; //获取第一级功能 if (key.privateFuncFirstLevelList.Count == 0 || key.privateFuncFirstLevelList.Contains(256) == false) { result = await key.GetPanelDeviceFunctionLevel1(); if (result == null) { return null; } key.privateFuncFirstLevelList = result; //面板没有按键类 if (result.Contains(256) == false) { return dicCheck; } } else { result = key.privateFuncFirstLevelList; } if (key.privateFuncSecondLevelList.Count == 0 || key.privateFuncSecondLevelList.Contains(1) == false || key.privateFuncSecondLevelList.Contains(100) == false || key.privateFuncSecondLevelList.Contains(200) == false || key.privateFuncSecondLevelList.Contains(300) == false) { //获取第二级功能 result = await key.GetPanelDeviceFunctionLevel2(256); if (result == null) { return null; } key.privateFuncSecondLevelList = result; } else { result = key.privateFuncSecondLevelList; } //特殊功能 if (result.Contains(1) == true) { List result3 = null; //获取第三级功能 if (key.privateFuncThirdLevelList.Count == 0 || key.privateFuncThirdLevelList.Contains(1) == false) { result3 = await key.GetPanelDeviceFunctionLevel3(256, 1); foreach (var l3 in result3) { key.privateFuncThirdLevelList.Add(l3); } } else { result3 = key.privateFuncThirdLevelList; } if (result3 != null) { if (result3.Contains(1) == true) { dicCheck["场景:触发"] = true; } } } //按键开关类 if (result.Contains(100) == true) { List result3 = null; //获取第三级功能 if (key.privateFuncThirdLevelList.Count == 0 || key.privateFuncThirdLevelList.Contains(100) == false || key.privateFuncThirdLevelList.Contains(101) == false || key.privateFuncThirdLevelList.Contains(102) == false) { result3 = await key.GetPanelDeviceFunctionLevel3(256, 100); foreach (var l3 in result3) { key.privateFuncThirdLevelList.Add(l3); } } else { result3 = key.privateFuncThirdLevelList; } if (result3 != null) { if (result3.Contains(100) == true) { dicCheck["开关:开"] = true; } if (result3.Contains(101) == true) { dicCheck["开关:关"] = true; } if (result3.Contains(102) == true) { dicCheck["开关:切换"] = true; } } } //按键调光类 if (result.Contains(200) == true) { List result3 = null; //获取第三级功能 if (key.privateFuncThirdLevelList.Count == 0 || key.privateFuncThirdLevelList.Contains(200) == false || key.privateFuncThirdLevelList.Contains(201) == false || key.privateFuncThirdLevelList.Contains(202) == false) { result3 = await key.GetPanelDeviceFunctionLevel3(256, 200); foreach (var l3 in result3) { key.privateFuncThirdLevelList.Add(l3); } } else { result3 = key.privateFuncThirdLevelList; } if (result3 != null) { if (result3.Contains(200) == true) { dicCheck["亮度:按等级调大"] = true; } if (result3.Contains(201) == true) { dicCheck["亮度:按等级调小"] = true; } if (result3.Contains(202) == true) { dicCheck["亮度:按等级切换"] = true; } if (result3.Contains(203) == true) { dicCheck["亮度:打开"] = true; } if (result3.Contains(204) == true) { dicCheck["亮度:关闭"] = true; } if (result3.Contains(205) == true) { dicCheck["亮度:切换"] = true; } } } //窗帘类 if (result.Contains(300) == true) { List result3 = null; //获取第三级功能 if (key.privateFuncThirdLevelList.Count == 0 || (key.privateFuncThirdLevelList.Contains(300) == false && key.privateFuncThirdLevelList.Contains(301) == false && key.privateFuncThirdLevelList.Contains(302) == false && key.privateFuncThirdLevelList.Contains(303) == false && key.privateFuncThirdLevelList.Contains(304) == false)) { result3 = await key.GetPanelDeviceFunctionLevel3(256, 300); foreach (var l3 in result3) { key.privateFuncThirdLevelList.Add(l3); } } else { result3 = key.privateFuncThirdLevelList; } if (result3 != null) { if (result3.Contains(300) == true) { dicCheck["窗帘:开"] = true; } if (result3.Contains(301) == true) { dicCheck["窗帘:关"] = true; } if (result3.Contains(302) == true) { dicCheck["窗帘:停"] = true; } if (result3.Contains(303) == true) { dicCheck["窗帘:上升停"] = true; } if (result3.Contains(304) == true) { dicCheck["窗帘:下降停"] = true; } } } return dicCheck; } /// /// 底部完成按钮显示 /// /// public static void FinishDisplay(List roomTempList, Button btnFinish) { if (roomTempList.Count == 0) { btnFinish.Enable = false; btnFinish.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMUnSelect; } else { btnFinish.Enable = true; btnFinish.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMBlack; } } #endregion } }