WJC
2020-04-02 94e4e5b9fd3da964c44b7b14227d6fe2bbb426d7
ZigbeeApp/Shared/Common/Device.cs
@@ -45,12 +45,14 @@
            {
                lock (dicAllDevice)
                {
                    var list = new List<CommonDevice>();
                    foreach (var device in dicAllDevice.Values)
                    //先获取全部的Mac
                    var listMac = new List<string>();
                    foreach (var strMac in this.dicDeviceEpoint.Keys)
                    {
                        list.Add(device);
                        listMac.Add(strMac);
                    }
                    return list;
                    //根据MAC地址,获取全部回路的设备对象(强制排序)
                    return this.GetDevicesByMac(listMac);
                }
            }
        }
@@ -241,7 +243,16 @@
            //获取这个网关的本地所有设备
            string gwID = HdlGatewayLogic.Current.GetGatewayId(zbGateway);
            List<CommonDevice> listLocalDevices = this.GetDeviceByGatewayID(gwID, true);
            List<CommonDevice> listLocalDevices = this.GetDeviceByGatewayID(gwID);
            //获取ota设备
            foreach (var ota in this.dicOTADevice.Values)
            {
                if (ota.CurrentGateWayId == gwID)
                {
                    listLocalDevices.Add(ota);
                }
            }
            Dictionary<string, CommonDevice> dicExist = new Dictionary<string, CommonDevice>();
            foreach (var device in listLocalDevices)
            {
@@ -464,9 +475,28 @@
                //备份数据
                await HdlGatewayLogic.Current.UpLoadDeviceBackupDataToGateway(device, GatewayBackupEnum.AMac名称, newMacName);
                //如果它只有一个回路,则更改端点名字
                if (this.dicDeviceEpoint.ContainsKey(device.DeviceAddr) == true && this.dicDeviceEpoint[device.DeviceAddr].Count == 1)
                if (this.dicDeviceEpoint.ContainsKey(device.DeviceAddr) == true)
                {
                    return await this.ReName(device, newMacName);
                    //只有一个端点
                    if (this.dicDeviceEpoint[device.DeviceAddr].Count == 1)
                    {
                        return await this.ReName(device, newMacName);
                    }
                    //如果它有两个端点时,pir传感器特殊处理
                    else if (this.dicDeviceEpoint[device.DeviceAddr].Count == 2)
                    {
                        var myType = this.GetMyDeviceEnumInfo(listDevice);
                        if (myType.ConcreteType == DeviceConcreteType.Sensor_Pir)
                        {
                            foreach (var myDevice in listDevice)
                            {
                                if (myDevice.Type == DeviceType.IASZone)
                                {
                                    return await this.ReName(myDevice, newMacName);
                                }
                            }
                        }
                    }
                }
            }
            return true;
@@ -555,12 +585,12 @@
        /// <param name="deleteRoom">是否从房间删除</param>
        public void DeleteMemmoryDevice(CommonDevice device, bool deleteRoom = true)
        {
            if (deleteRoom == true && Room.CurrentRoom != null)
            if (deleteRoom == true && HdlRoomLogic.Current.CurrentRoom != null)
            {
                //从房间中删除
                Room.CurrentRoom.DeleteDevice(device);
                HdlRoomLogic.Current.DeleteDevice(device);
                //删除我的喜爱的设备
                Room.CurrentRoom.DeleteLoveDevice(device);
                HdlRoomLogic.Current.DeleteLoveDevice(device);
            }
            //删除缓存
@@ -702,36 +732,23 @@
        /// <summary>
        /// 根据网关ID获取所有的设备
        /// </summary>
        /// <param name="gwId">网关ID</param>
        /// <param name="getOtaDevice">是否获取ota设备</param>
        /// <param name="gwId">网关ID</param>>
        /// <returns></returns>
        public List<CommonDevice> GetDeviceByGatewayID(string gwId, bool getOtaDevice = false)
        public List<CommonDevice> GetDeviceByGatewayID(string gwId)
        {
            List<CommonDevice> list = new List<CommonDevice>();
            lock (dicAllDevice)
            {
                //各网关的所有设备
                var listMac = new List<string>();
                //各网关的所有设备的Mac
                foreach (CommonDevice device in this.dicAllDevice.Values)
                {
                    if (gwId == device.CurrentGateWayId)
                    {
                        list.Add(device);
                        listMac.Add(device.DeviceAddr);
                    }
                }
                if (getOtaDevice == true)
                {
                    //获取ota设备
                    foreach (var ota in this.dicOTADevice.Values)
                    {
                        if (ota.CurrentGateWayId == gwId)
                        {
                            list.Add(ota);
                        }
                    }
                }
                return this.GetDevicesByMac(listMac);
            }
            return list;
        }
        /// <summary>
@@ -798,6 +815,39 @@
                }
                return -1;
            });
            return list;
        }
        /// <summary>
        /// 根据MAC地址,获取全部回路的设备对象(强制排序)
        /// </summary>
        /// <param name="DeviceAddr">Mac地址</param>
        /// <returns></returns>
        public List<CommonDevice> GetDevicesByMac(List<string> listMacAddr)
        {
            //先排序
            listMacAddr.Sort();
            var list = new List<CommonDevice>();
            foreach (string strMac in listMacAddr)
            {
                var listEpoint = new List<int>();
                //获取全部的端点
                foreach (int epoint in this.dicDeviceEpoint[strMac])
                {
                    listEpoint.Add(epoint);
                }
                //然后排序
                listEpoint.Sort();
                foreach (int epoint in listEpoint)
                {
                    var device = this.GetDevice(strMac, epoint);
                    if (device != null)
                    {
                        list.Add(device);
                    }
                }
            }
            return list;
        }
@@ -1103,9 +1153,41 @@
            //获取设备【图标】
            this.GetDeviceIcon(device, ref unSelectFilePath, ref selectFilePath);
            if (btnIcon.UnSelectedImagePath != unSelectFilePath)
            {
                btnIcon.UnSelectedImagePath = unSelectFilePath;
            }
            if (btnIcon.SelectedImagePath != selectFilePath)
            {
                btnIcon.SelectedImagePath = selectFilePath;
            }
        }
            btnIcon.UnSelectedImagePath = unSelectFilePath;
            btnIcon.SelectedImagePath = selectFilePath;
        /// <summary>
        /// 设置设备【图标】到指定的控件(注意,此函数设置的选择状态的图片是白色的)
        /// </summary>
        /// <param name="btnIcon">控件对象</param>
        /// <param name="device">设备对象</param>
        /// <returns></returns>
        public void SetDeviceIconToControl2(Button btnIcon, CommonDevice device)
        {
            if (device == null)
            {
                btnIcon.UnSelectedImagePath = "Device/ThirdPartyDevice.png";
                return;
            }
            string unSelectFilePath = device.IconPath;
            string selectFilePath = unSelectFilePath.Replace(".png", "Selected2.png");
            //获取设备【图标】
            if (btnIcon.UnSelectedImagePath != unSelectFilePath)
            {
                btnIcon.UnSelectedImagePath = unSelectFilePath;
            }
            if (btnIcon.SelectedImagePath != selectFilePath)
            {
                btnIcon.SelectedImagePath = selectFilePath;
            }
        }
        /// <summary>
@@ -1266,43 +1348,6 @@
        #endregion
        #region ■ 设备UI相关_________________________
        /// <summary>
        /// 获取设备所匹配的设备UI对象
        /// </summary>
        /// <param name="device"></param>
        /// <returns></returns>
        public DeviceUI GetDeviceUI(CommonDevice device)
        {
            //创建一个新的东西给过去
            var deviceUi = new DeviceUI();
            deviceUi.DeviceAddr = device.DeviceAddr;
            deviceUi.DeviceEpoint = device.DeviceEpoint;
            return deviceUi;
        }
        /// <summary>
        /// 获取设备所匹配的设备UI对象
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public DeviceUI GetDeviceUI(string filePath)
        {
            string[] arry = filePath.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries);
            if (arry.Length != 4)
            {
                return null;
            }
            //创建一个新的东西给过去
            var deviceUi = new DeviceUI();
            deviceUi.DeviceAddr = arry[2];
            deviceUi.DeviceEpoint = Convert.ToInt32(arry[3]);
            return deviceUi;
        }
        #endregion
        #region ■ 获取自定义的设备类型_______________
        /// <summary>
@@ -1329,7 +1374,11 @@
                return info;
            }
            //获取第三方设备的【设备类型】
            return this.GetNotHdlMyDeviceEnumInfo(listdevice);
            info = this.GetNotHdlMyDeviceEnumInfo(listdevice);
            //这里再次判断是否是河东设备,有可能它的模块ID写错了
            info.IsHdlDevice = this.IsHdlDevice(checkDevice);
            return info;
        }
        /// <summary>
@@ -1394,6 +1443,9 @@
                return null;
            }
            string modelKeys = device.ModelIdentifier;
            //交换一下模块ID(麦乐克那边的传感器)
            this.ChangedDeviceModeId(ref modelKeys);
            if (this.dicDeviceModelIdEnum.ContainsKey(modelKeys) == false)
            {
                //没有匹配到模块ID,则直接走第三方设备的判断
@@ -1774,7 +1826,7 @@
                    //单纯只是Ota设备则不处理
                    return;
                }
                Common.Room.CurrentRoom.ChangedRoom(listDevice[0], roomId, false);
                HdlRoomLogic.Current.ChangedRoom(listDevice[0], roomId, false);
            }
        }
@@ -1790,7 +1842,7 @@
                //未分配区域
                return Language.StringByID(R.MyInternationalizationString.uDeviceNotAssignedRoom);
            }
            var room = Room.CurrentRoom.GetRoomById(this.dicDeviceRoomId[device.DeviceAddr]);
            var room = HdlRoomLogic.Current.GetRoomById(this.dicDeviceRoomId[device.DeviceAddr]);
            if (room != null)
            {
                return room.Name;
@@ -1810,7 +1862,81 @@
            {
                return null;
            }
            return Room.CurrentRoom.GetRoomById(this.dicDeviceRoomId[device.DeviceAddr]);
            return HdlRoomLogic.Current.GetRoomById(this.dicDeviceRoomId[device.DeviceAddr]);
        }
        #endregion
        #region ■ 设备排序___________________________
        /// <summary>
        /// 设备排序
        /// </summary>
        /// <param name="i_listDevice">请确保这个东西已经按mac和端点排序了</param>
        /// <returns></returns>
        public List<CommonDevice> SortDeviceByBelongType(List<CommonDevice> i_listDevice)
        {
            //获取排序规则
            var listRule = this.GetBelongTypeSortRule();
            var dicDevice = new Dictionary<DeviceBeloneType, List<CommonDevice>>();
            dicDevice[DeviceBeloneType.A未知设备] = new List<CommonDevice>();
            foreach (var device in i_listDevice)
            {
                //获取所属类型
                var typeInfo = this.GetDeviceBelongEnumInfo(device);
                if (listRule.Contains(typeInfo.BeloneType) == false)
                {
                    //不在排序范围内,都丢在最后面
                    dicDevice[DeviceBeloneType.A未知设备].Add(device);
                    continue;
                }
                if (dicDevice.ContainsKey(typeInfo.BeloneType) == false)
                {
                    dicDevice[typeInfo.BeloneType] = new List<CommonDevice>();
                }
                dicDevice[typeInfo.BeloneType].Add(device);
            }
            var listSort = new List<CommonDevice>();
            foreach (var myType in listRule)
            {
                //根据规则顺序,添加设备
                if (dicDevice.ContainsKey(myType) == false || dicDevice[myType].Count == 0)
                {
                    continue;
                }
                listSort.AddRange(dicDevice[myType]);
            }
            return listSort;
        }
        /// <summary>
        /// 获取设备所属类型的的排序规则
        /// </summary>
        /// <returns></returns>
        public HashSet<DeviceBeloneType> GetBelongTypeSortRule()
        {
            //谁在前面,谁就优先显示
            var list = new HashSet<DeviceBeloneType>();
            list.Add(DeviceBeloneType.A灯光);
            list.Add(DeviceBeloneType.A彩灯);
            list.Add(DeviceBeloneType.A开关);
            list.Add(DeviceBeloneType.A插座);
            list.Add(DeviceBeloneType.A调光器);
            list.Add(DeviceBeloneType.A窗帘);
            list.Add(DeviceBeloneType.A空调);
            list.Add(DeviceBeloneType.A继电器);
            list.Add(DeviceBeloneType.A干接点);
            list.Add(DeviceBeloneType.A智能门锁);
            list.Add(DeviceBeloneType.A智能空开);
            list.Add(DeviceBeloneType.A传感器);
            //其他的看着办呗,都是排在后面的,都归为这个属性
            list.Add(DeviceBeloneType.A未知设备);
            return list;
        }
        #endregion
@@ -1834,7 +1960,7 @@
        /// <returns></returns>
        public string GetDeviceMainKeys(CommonDevice device)
        {
            return device.DeviceAddr + device.DeviceEpoint;
            return this.GetDeviceMainKeys(device.DeviceAddr, device.DeviceEpoint);
        }
        /// <summary>
@@ -1845,7 +1971,7 @@
        /// <returns></returns>
        public string GetDeviceMainKeys(string DeviceAddr, int DeviceEpoint)
        {
            return DeviceAddr + DeviceEpoint;
            return DeviceAddr + "_" + DeviceEpoint;
        }
        /// <summary>
@@ -2064,6 +2190,8 @@
                    statu = 2;
                }
            }
            statu = 1;
            return listDevice;
        }
@@ -2254,9 +2382,10 @@
            //*********************************************************************
            //新设备添加方法:
            //1、在这里填写上模块ID,然后是 DeviceConcreteType ,然后是 DeviceBeloneType
            //2、然后在最下面添加【设备的具体类型】,【设备的所属类型】
            //3、已设备的具体类型为名字(去掉【-】)添加设备的【所属图片】,【真实物理图片】。回路图片需要特殊处理
            //4、添加R文件,添加Language文件
            //2、然后在最下面的【自定义设备类型】折叠栏里添加【设备的具体类型】,【设备的所属类型】
            //3、以设备的具体类型为名字(去掉【-】)添加设备的【所属图片】,【真实物理图片】。回路图片需要特殊处理
            //4、添加R文件(uDeviceModelId),添加Language文件
            //5、如果需要共有图片,则在这个函数的最底下添加
            //*********************************************************************
            //=========★★开合帘类(100-199)★★=========
@@ -2272,6 +2401,14 @@
            this.dicDeviceModelIdEnum["MPT4R4L/S-ZB.18"] = "220-200";//简约4按键面板
            this.dicDeviceModelIdEnum["MPT3R3L/S-ZB.18"] = "221-200";//简约3按键面板
            this.dicDeviceModelIdEnum["MPT2R2L/S-ZB.18"] = "222-200";//简约2按键面板
            this.dicDeviceModelIdEnum["MPT4SC/S-ZB.18"] = "224-200";//简约4按键场景面板
            this.dicDeviceModelIdEnum["MPT2W/S-ZB.18"] = "226-200";//简约2路窗帘面板
            this.dicDeviceModelIdEnum["MP2B/TILE-ZB.18"] = "240-200";//方悦单开双控面板
            this.dicDeviceModelIdEnum["MP4B/TILE-ZB.18"] = "241-200";//方悦双开四控面板
            this.dicDeviceModelIdEnum["MP8B/TILE-ZB.18"] = "242-200";//方悦四开八控面板
            this.dicDeviceModelIdEnum["MPFA/TILE-ZB.18"] = "250-200";//方悦新风面板
            this.dicDeviceModelIdEnum["MPTE3/TILE-ZB.18"] = "253-200";//方悦环境面板
            this.dicDeviceModelIdEnum["MP2W/TILE-ZB.18"] = "256-200";//窗帘面板
            //=========★★PIR传感器类(1200-1299)★★=========
            this.dicDeviceModelIdEnum["MSPIR01-ZB.10"] = "1200-1200";//pir传感器220
@@ -2286,12 +2423,14 @@
            //=========★★继电器类(2300-2499)★★=========
            this.dicDeviceModelIdEnum["MPR0310-ZB.10"] = "2300-2300";//3路继电器小模块
            this.dicDeviceModelIdEnum["MFA01-ZB1.0"] = "2310-2300";//方悦新风小模块
            //=========★★调光器类(2500-2799)★★=========
            this.dicDeviceModelIdEnum["MPD0101-ZB.10"] = "2500-2500";//1路调光器小模块
            //=========★★智能门锁类(2800-????)★★=========
            this.dicDeviceModelIdEnum["H06C"] = "2800-2800";//智能门锁(H06C)
            this.dicDeviceModelIdEnum["S-one"] = "2802-2800";//智能门锁(S-one)
            //=========★★空调类(3600-3899)★★=========
            this.dicDeviceModelIdEnum["MAC/GW-ZB.10"] = "3600-3600";//zigbee空调网关模块
@@ -2321,10 +2460,14 @@
            //✩✩✩✩✩需要共有的图片对象✩✩✩✩✩
            //两者都是DeviceConcreteType
            //Keys:指定的设备    value:沿用的图片是哪款设备的
            this.dicPictrueShard = new Dictionary<string, string>();
            this.dicPictrueShard["ButtonPanel_SimpleFour"] = "ButtonPanel_Four";//简约4按键面板 沿用 4按键的图标
            this.dicPictrueShard["ButtonPanel_SimpleThree"] = "ButtonPanel_Three";//简约3按键面板 沿用 3按键的图标
            this.dicPictrueShard["ButtonPanel_SimpleTwo"] = "ButtonPanel_Two";//简约2按键面板 沿用 2按键的图标
            this.dicPictrueShard["IntelligentLocks_Sone"] = "IntelligentLocks_H06C";//S-one的门锁图片沿用H06C的图标
        }
        #endregion
@@ -2333,12 +2476,12 @@
    #region ■ 自定义设备类型_________________________
    /// <summary>
    /// <para>仅限底层使用:设备的具体【设备类型】,自定义与模块id关联的枚举(值为LocalDevice里面dicDeviceModelIdEnum所指定的值)</para>
    /// <para>仅限底层使用:设备的具体【设备类型】,自定义与模块id关联的枚举(值为LocalDevice里面dicDeviceModelIdEnum所指定的DeviceConcreteType值)</para>
    /// <para>变量名可以作为【设备类型】图片,这个值是瞎写的,没什么特殊意义</para>
    /// </summary>
    public enum DeviceConcreteType
    {
        //定义规则:【设备类型】图片名字=LocalDevice里面dicDeviceModelIdEnum所指定的值
        //定义规则:【设备类型】图片名字=LocalDevice里面dicDeviceModelIdEnum所指定的DeviceConcreteType值
        /// <summary>
        /// 未知设备
@@ -2396,6 +2539,38 @@
        /// 简约2按键面板 镜像id:222
        /// </summary>
        ButtonPanel_SimpleTwo = 222,
        /// <summary>
        /// 简约4按键场景面板 镜像id:224
        /// </summary>
        ButtonPanel_FourButtonScene = 224,
        /// <summary>
        /// 简约2路窗帘面板 镜像id:226
        /// </summary>
        ButtonPanel_TwoButtonCurtain = 226,
        /// <summary>
        /// 方悦2按键轻触式面板 镜像id:240
        /// </summary>
        ButtonPanel_FangyueTwo = 240,
        /// <summary>
        /// 方悦4按键轻触式面板 镜像id:241
        /// </summary>
        ButtonPanel_FangyueFour = 241,
        /// <summary>
        /// 方悦8按键轻触式面板 镜像id:242
        /// </summary>
        ButtonPanel_FangyueEight = 242,
        /// <summary>
        /// 方悦新风面板 镜像id:250
        /// </summary>
        ButtonPanel_FangyueFreshAir = 250,
        /// <summary>
        /// 方悦环境面板 镜像id:253
        /// </summary>
        ButtonPanel_FangyueEnvironment = 253,
        /// <summary>
        /// 窗帘面板 镜像id:256
        /// </summary>
        ButtonPanel_Curtain = 256,
        //=========★★PIR传感器类(1200-1299)★★=========
        /// <summary>
@@ -2462,7 +2637,11 @@
        /// 三路继电器 镜像id:2300
        /// </summary>
        Relay_ThreeLoad = 2300,
        /// <summary>
        /// 方悦新风小模块 镜像id:2310
        /// </summary>
        Relay_FangyueFreshAirModul = 2310,
        //=========★★调光器类(2500-2799)★★=========
        /// <summary>
        /// 调光器
@@ -2482,6 +2661,10 @@
        /// H06C
        /// </summary>
        IntelligentLocks_H06C = 2800,
        /// <summary>
        /// S-one
        /// </summary>
        IntelligentLocks_Sone = 2802,
        //=========★★彩灯类(????-????)★★=========
        /// <summary>
@@ -2553,7 +2736,7 @@
    }
    /// <summary>
    /// <para>仅限底层使用:设备所属的【设备种类】,自定义与模块id关联的枚举(值为LocalDevice里面dicDeviceModelIdEnum所指定的值)</para>
    /// <para>仅限底层使用:设备所属的【设备种类】,自定义与模块id关联的枚举(值为LocalDevice里面dicDeviceModelIdEnum所指定的DeviceBeloneType值)</para>
    /// <para>这个值是瞎写的,没什么特殊意义</para>
    /// </summary>
    public enum DeviceBeloneType