HDL Home App 第二版本 旧平台金堂用 正在使用
hxb
2022-08-30 25429f085093d89d543a0b90e30d0d62d1b7dac9
ZigbeeApp/Shared/Common/Device.cs
New file
@@ -0,0 +1,4068 @@
using Shared.Phone.UserCenter;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using ZigBee.Device;
namespace Shared.Common
{
    /// <summary>
    /// 本地设备
    /// </summary>
    public class LocalDevice
    {
        #region ■ 变量声明___________________________
        /// <summary>
        /// 本地设备
        /// </summary>
        private static LocalDevice m_Current = null;
        /// <summary>
        /// 本地设备
        /// </summary>
        public static LocalDevice Current
        {
            get
            {
                if (m_Current == null)
                {
                    m_Current = new LocalDevice();
                }
                return m_Current;
            }
            set
            {
                m_Current = value;
            }
        }
        /// <summary>
        /// 本地所有设备的缓存
        /// </summary>
        public List<CommonDevice> listAllDevice
        {
            get
            {
                lock (dicAllDevice)
                {
                    //先获取全部的Mac
                    var listMac = new List<string>();
                    foreach (var strMac in this.dicDeviceEpoint.Keys)
                    {
                        listMac.Add(strMac);
                    }
                    //根据MAC地址,获取全部回路的设备对象(强制排序)
                    return this.GetDevicesByMac(listMac);
                }
            }
        }
        /// <summary>
        /// 设备的模块ID的枚举 keys:模块ID (Axxx代表是第三方设备)
        /// </summary>
        private Dictionary<string, DeviceNameContent> dicDeviceModelIdEnum = null;
        /// <summary>
        /// 需要转换的设备的模块ID(keys:旧模块ID,value:新模块ID)
        /// </summary>
        private Dictionary<string, string> dicDeviceModelIdChanged = null;
        /// <summary>
        /// 图片共有(keys:指定设备的具体类型 value:指定共有对象的具体类型)
        /// </summary>
        private Dictionary<string, string> dicPictrueShard = null;
        /// <summary>
        /// 本地所有设备的缓存(非公开)
        /// </summary>
        private Dictionary<string, CommonDevice> dicAllDevice = new Dictionary<string, CommonDevice>();
        /// <summary>
        /// 本地所有的顶点升级设备(非公开,主键是MAC+200端口)
        /// </summary>
        private Dictionary<string, OTADevice> dicOTADevice = new Dictionary<string, OTADevice>();
        /// <summary>
        /// 设备的总回路(keys:Mac地址  value:全部端口号)
        /// </summary>
        private Dictionary<string, HashSet<int>> dicDeviceEpoint = new Dictionary<string, HashSet<int>>();
        #endregion
        #region ■ 刷新设备___________________________
        /// <summary>
        /// 刷新本地设备信息
        /// </summary>
        public void ReFreshByLocal()
        {
            this.dicAllDevice.Clear();
            this.dicDeviceEpoint.Clear();
            //初始化设备枚举
            this.InitDeviceModelIdEnum();
            //获取本地全部的设备文件
            List<string> listFile = this.GetAllDeviceFile();
            foreach (string file in listFile)
            {
                CommonDevice device = null;
                //反序列化为指定的类,不然数据会丢失而导致无法强转
                try
                {
                    //读取它的内容
                    var fileData = HdlFileLogic.Current.ReadFileTextContent(System.IO.Path.Combine(Config.Instance.FullPath, file));
                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(fileData);
                    if (jobject.Property("DeviceID") == null)
                    {
                        continue;
                    }
                    device = CommonDevice.CommonDeviceByByteString(Convert.ToInt32(jobject["DeviceID"].ToString()), fileData);
                }
                catch (Exception ex) { HdlLogLogic.Current.WriteLog(ex); }
                if (device == null || device.CurrentGateWayId == null)
                {
                    //失效的文件,没有网关id的都删除掉
                    Global.DeleteFilebyHomeId(file);
                    continue;
                }
                //如果这个设备的网关ID不存在的话
                if (HdlGatewayLogic.Current.IsGatewayExist(device.CurrentGateWayId) == false)
                {
                    if (UserCenterResourse.UserInfo.AuthorityNo == 3)
                    {
                        //如果他是成员的话,帮他新建一个网关
                        HdlGatewayLogic.Current.AddVirtualGateway(device.CurrentGateWayId);
                    }
                    else
                    {
                        //如果是主人,或者管理员,那么这个文件是非法的,直接删除
                        Global.DeleteFilebyHomeId(file);
                        continue;
                    }
                }
                string mainKey = this.GetDeviceMainKeys(device);
                if (device is OTADevice)
                {
                    //200端口不需要处理
                    this.dicOTADevice[mainKey] = (OTADevice)device;
                    continue;
                }
                //如果设备是晾衣架,则一端点,其他端点(2~5)过滤,
                //因为其他端点只有控制作用,(2:控制照明,3:控制风干,4:控制烘干,5:控制消毒)
                //产品经理不让显示
                if (device is Airer)
                {
                    if (device.DeviceEpoint == 2 || device.DeviceEpoint == 3 || device.DeviceEpoint == 4 || device.DeviceEpoint == 5)
                        continue;
                }
                //添加缓存
                this.dicAllDevice[mainKey] = device;
                //回路收集
                if (this.dicDeviceEpoint.ContainsKey(device.DeviceAddr) == false)
                {
                    this.dicDeviceEpoint[device.DeviceAddr] = new HashSet<int>();
                }
                this.dicDeviceEpoint[device.DeviceAddr].Add(device.DeviceEpoint);
                //对应旧版本的设备文件 Device_OnoffOutput_abcdfrtgg_01
                bool bolSave = file.Split(new string[] { "_" }, StringSplitOptions.None).Length == 4;
                if (bolSave == true)
                {
                    //删掉这个文件,重新生成
                    HdlAutoBackupLogic.DeleteFile(file);
                    Global.DeleteFilebyHomeId(file);
                }
                //检测Ui图片是否正确,这个图片本地是否存在?
                if (string.IsNullOrEmpty(IO.FileUtils.GetImageFilePath(device.IconPath)) == true)
                {
                    //不存在的话,重新生成
                    device.IconPath = string.Empty;
                    device.ReSave();
                    HdlAutoBackupLogic.AddOrEditorFile(device.FilePath);
                    bolSave = false;
                }
                //重新生成文件
                if (bolSave == true)
                {
                    device.ReSave();
                    HdlAutoBackupLogic.AddOrEditorFile(device.FilePath);
                }
            }
            //成员身份的时候,删除掉非法的网关文件
            this.DeleteGatewayFileByMemberModel();
            //初始化物理设备所属房间的记录
            HdlRoomLogic.Current.InitRealDeviceRoomId();
#if DEBUG
            if (UserCenterResourse.HideOption.CenterHideMenu == 1)
            {
                //检测一些代码配置(debug)
                this.CheckOhtherSettion();
            }
#endif
        }
        /// <summary>
        /// 成员身份的时候,删除掉非法的网关文件
        /// </summary>
        private void DeleteGatewayFileByMemberModel()
        {
            if (UserCenterResourse.UserInfo.AuthorityNo != 3)
            {
                return;
            }
            var listId = new HashSet<string>();
            lock (dicAllDevice)
            {
                foreach (var device in this.dicAllDevice.Values)
                {
                    if (listId.Contains(device.CurrentGateWayId) == false)
                    {
                        listId.Add(device.CurrentGateWayId);
                    }
                }
            }
            var listGateway = HdlGatewayLogic.Current.GetAllLocalGateway();
            foreach (var gateway in listGateway)
            {
                string gwId = gateway.GwId;
                if (listId.Contains(gwId) == false)
                {
                    //这个网关对于当前这个成员来说是非法的
                    HdlGatewayLogic.Current.DeleteGatewayFile(gwId);
                }
            }
        }
        /// <summary>
        /// 检测一些代码配置(debug)
        /// </summary>
        private void CheckOhtherSettion()
        {
            foreach (var item in Enum.GetValues(typeof(DeviceType)))
            {
                string itemName = item.ToString();
                int itemValue = (int)item;
                if (itemName == "UnKown" || itemName == "ZbGateway")
                {
                    continue;
                }
                var device = this.NewDeviceObjectByDeviceId((DeviceType)itemValue, 1);
                string strMsg = string.Empty;
                if (device == null)
                {
                    strMsg = itemName + "没有实装(1)";
                }
                device = CommonDevice.CommonDeviceByByteString(itemValue, "{}");
                if (device == null)
                {
                    strMsg += "\r\n" + itemName + "没有实装(2)";
                }
                if (strMsg != string.Empty)
                {
                    this.ShowErrorMsg(strMsg);
                    return;
                }
            }
        }
        #endregion
        #region ■ 添加设备___________________________
        /// <summary>
        /// <para>将指定网关的设备存入缓存中(从新获取镜像)</para>
        /// <para>-1:异常 1:正常 2:设备信息缺损</para>
        /// </summary>
        /// <param name="gatewayId">网关Id</param>
        /// <param name="useLocalConnect">是否强制使用本地连接发送</param>
        public int SetDeviceToMemmoryByGateway(string gatewayId, bool useLocalConnect)
        {
            //从网关获取全部的设备
            int statu = 0;
            List<CommonDevice> listDevice = new List<CommonDevice>();
            List<CommonDevice> list = this.GetDeviceListFromGateway(gatewayId, ref statu, useLocalConnect);
            if (list == null)
            {
                return -1;
            }
            listDevice.AddRange(list);
            //获取这个网关的本地所有设备
            List<CommonDevice> listLocalDevices = this.GetDeviceByGatewayID(gatewayId);
            //获取ota设备
            foreach (var ota in this.dicOTADevice.Values)
            {
                if (ota.CurrentGateWayId == gatewayId)
                {
                    listLocalDevices.Add(ota);
                }
            }
            Dictionary<string, CommonDevice> dicExist = new Dictionary<string, CommonDevice>();
            foreach (var device in listLocalDevices)
            {
                string maikey = this.GetDeviceMainKeys(device);
                dicExist[maikey] = device;
            }
            //添加设备的缓存
            var listDriveDevice = new List<CommonDevice>();
            for (int i = 0; i < listDevice.Count; i++)
            {
                var device = listDevice[i];
                if (device == null || device.DeviceAddr == null)
                {
                    continue;
                }
                //添加缓存
                this.AddDeviceToMemory(ref device);
                //移除存在的设备内存
                string maikey = this.GetDeviceMainKeys(device);
                if (dicExist.ContainsKey(maikey) == true)
                {
                    dicExist.Remove(maikey);
                }
                //获取设备的固定属性
                if (HdlDeviceFixedAttributeLogic.Current.SetAllFixedAttributeToDevice(device) == true)
                {
                    System.Threading.Thread.Sleep(200);
                }
                //对未命名的虚拟设备重新命名
                if (device.DriveCode > 0 && this.GetSimpleEpointName(device) == string.Empty)
                {
                    listDriveDevice.Add(device);
                }
            }
            //设置虚拟设备的默认名字
            this.SetDriveDeviceDefultName(listDriveDevice);
            //只有完全获取的时候,才会去处理删除的问题
            if (statu != 1)
            {
                return statu;
            }
            //如果本地和网关的设备不一致的时候,删除本地的设备
            var listDeleteMac = new List<string>();
            foreach (var device in dicExist.Values)
            {
                if (device is OTADevice)
                {
                    this.DeleteMemmoryOtaDevice(device.DeviceAddr);
                }
                else
                {
                    this.DeleteMemmoryDevice(device, true);
                }
                if (listDeleteMac.Contains(device.DeviceAddr) == false)
                {
                    //收集被删除的Mac
                    listDeleteMac.Add(device.DeviceAddr);
                }
            }
            if (listDeleteMac.Count > 0)
            {
                //将真实物理设备从房间中移除
                HdlRoomLogic.Current.DeleteRealDeviceFromRoom(listDeleteMac);
            }
            return statu;
        }
        /// <summary>
        /// 添加设备到缓存,存在时覆盖
        /// </summary>
        /// <param name="device">设备对象(这个东西有可能会被更改)</param>
        public void AddDeviceToMemory(ref CommonDevice device)
        {
            string mainKeys = this.GetDeviceMainKeys(device);
            //如果它是升级的顶端端点,则不能让它加入到缓存,但是可以让他生成文件
            if (device is OTADevice)
            {
                if (this.dicOTADevice.ContainsKey(mainKeys) == false)
                {
                    this.dicOTADevice[mainKeys] = (OTADevice)device;
                }
                else
                {
                    //交换属性
                    var tempDevice = this.dicOTADevice[mainKeys];
                    //将DeviceInfo的属性设置到主属性中
                    this.SetDeviceInfoToMain(tempDevice, device);
                    //刷新设备功能类型(这个东西需要独立出来)
                    this.RefreshDeviceFunctionType(tempDevice, device, false);
                    device = tempDevice;
                }
                bool exists = Global.IsExistsByHomeId(device.FilePath);
                device.ReSave();
                if (exists == false)
                {
                    //添加自动备份
                    HdlAutoBackupLogic.AddOrEditorFile(device.FilePath);
                }
                return;
            }
            //如果设备是晾衣架,则一端点,其他端点(2~5)过滤,
            //因为其他端点只有控制作用,(2:控制照明,3:控制风干,4:控制烘干,5:控制消毒)
            //产品经理不让显示
            if (device is Airer)
            {
                if (device.DeviceEpoint == 2 || device.DeviceEpoint == 3 || device.DeviceEpoint == 4 || device.DeviceEpoint == 5)
                    return;
            }
            lock (dicAllDevice)
            {
                if (this.dicAllDevice.ContainsKey(mainKeys) == true)
                {
                    //交换属性
                    var tempDevice = this.dicAllDevice[mainKeys];
                    //将DeviceInfo的属性设置到主属性中
                    this.SetDeviceInfoToMain(tempDevice, device);
                    //刷新设备功能类型(这个东西需要独立出来)
                    this.RefreshDeviceFunctionType(tempDevice, device, false);
                    device = tempDevice;
                }
                else
                {
                    this.dicAllDevice[mainKeys] = device;
                }
            }
            //设备回路收集
            if (this.dicDeviceEpoint.ContainsKey(device.DeviceAddr) == false)
            {
                this.dicDeviceEpoint[device.DeviceAddr] = new HashSet<int>();
            }
            if (this.dicDeviceEpoint[device.DeviceAddr].Contains(device.DeviceEpoint) == false)
            {
                this.dicDeviceEpoint[device.DeviceAddr].Add(device.DeviceEpoint);
            }
            bool exists2 = Global.IsExistsByHomeId(device.FilePath);
            device.ReSave();
            if (exists2 == false)
            {
                //添加自动备份
                HdlAutoBackupLogic.AddOrEditorFile(device.FilePath);
            }
        }
        /// <summary>
        /// 添加虚拟设备到缓存
        /// </summary>
        /// <param name="device">设备对象</param>
        public void AddVirtualDeviceToMemory(CommonDevice device)
        {
            string mainKeys = this.GetDeviceMainKeys(device);
            if (device is OTADevice)
            {
                this.dicOTADevice[mainKeys] = (OTADevice)device;
                device.ReSave();
                return;
            }
            //如果设备是晾衣架,则一端点,其他端点(2~5)过滤,
            //因为其他端点只有控制作用,(2:控制照明,3:控制风干,4:控制烘干,5:控制消毒)
            //产品经理不让显示
            if (device is Airer)
            {
                if (device.DeviceEpoint == 2 || device.DeviceEpoint == 3 || device.DeviceEpoint == 4 || device.DeviceEpoint == 5)
                    return;
            }
            this.dicAllDevice[mainKeys] = device;
            //设备回路收集
            if (this.dicDeviceEpoint.ContainsKey(device.DeviceAddr) == false)
            {
                this.dicDeviceEpoint[device.DeviceAddr] = new HashSet<int>();
            }
            if (this.dicDeviceEpoint[device.DeviceAddr].Contains(device.DeviceEpoint) == false)
            {
                this.dicDeviceEpoint[device.DeviceAddr].Add(device.DeviceEpoint);
            }
            device.ReSave();
        }
        #endregion
        #region ■ 修改设备___________________________
        /// <summary>
        /// 更改端点名字并且刷新缓存(修改失败时,会显示信息)
        /// </summary>
        /// <param name="device">设备对象</param>
        /// <param name="newName">新名字</param>
        /// <param name="mode">是否显示错误</param>
        public bool ReName(CommonDevice device, string newName, ShowErrorMode mode = ShowErrorMode.YES)
        {
            //先别管那么多,更改名字后,刷新设备缓存
            this.SetEpointName(device, newName);
            this.BackupDeviceAfterReName(device);
            //如果住宅为虚拟住宅
            if (Common.Config.Instance.Home.IsVirtually == true)
            {
                //修改设备名字的话,主页需要重新刷新
                Phone.UserView.UserPage.Instance.RefreshAllForm = true;
                return true;
            }
            //不再检测名字是否一样
            //成员只能修改自己本地的名字
            if (UserCenterResourse.UserInfo.AuthorityNo != 3)
            {
                var result = this.RenameDeviceNameAsync(device, newName);
                if (result == null || result.deviceRenameData == null || result.deviceRenameData.Result == 1)
                {
                    //设备名称修改失败
                    string msg = Language.StringByID(R.MyInternationalizationString.uDeviceReNameFail);
                    //拼接上【网关回复超时】的Msg
                    msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result);
                    if (mode == ShowErrorMode.YES)
                    {
                        this.ShowErrorMsg(msg);
                    }
                    return false;
                }
            }
            //修改设备名字的话,主页需要重新刷新
            Phone.UserView.UserPage.Instance.RefreshAllForm = true;
            return true;
        }
        /// <summary>
        /// 更改Mac名字并且刷新缓存(修改失败时,会显示信息)
        /// </summary>
        /// <param name="listDevice">设备对象</param>
        /// <param name="newMacName">新名字</param>
        /// <param name="mode">是否显示错误</param>
        public bool ReMacName(List<CommonDevice> listDevice, string newMacName, ShowErrorMode mode = ShowErrorMode.YES)
        {
            if (listDevice.Count == 0)
            {
                return true;
            }
            //先别管那么多,先修改缓存
            for (int i = 0; i < listDevice.Count; i++)
            {
                var device2 = listDevice[i];
                //这两个东西很特殊
                this.SetMacName(device2, newMacName);
                //更改名字后,刷新设备缓存
                this.BackupDeviceAfterReName(device2);
            }
            //如果住宅为虚拟住宅
            if (Common.Config.Instance.Home.IsVirtually == true)
            {
                return true;
            }
            CommonDevice device = listDevice[0];
            //不再检测名字是否一样
            //成员只能修改自己本地的名字
            if (UserCenterResourse.UserInfo.AuthorityNo != 3)
            {
                //修改物理名字
                var result = this.RenameDeviceMacNameAsync(device, newMacName);
                if (result == null || result.renameDeviceMacNameData == null || result.renameDeviceMacNameData.Result != 0)
                {
                    //设备名称修改失败
                    string msg = Language.StringByID(R.MyInternationalizationString.uDeviceReNameFail);
                    //拼接上【网关回复超时】的Msg
                    msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result);
                    if (mode == ShowErrorMode.YES)
                    {
                        this.ShowErrorMsg(msg);
                    }
                    return false;
                }
                //如果它只有一个回路,则更改端点名字
                if (this.dicDeviceEpoint.ContainsKey(device.DeviceAddr) == true)
                {
                    //只有一个端点
                    if (this.dicDeviceEpoint[device.DeviceAddr].Count == 1)
                    {
                        return 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 this.ReName(myDevice, newMacName);
                                }
                            }
                        }
                    }
                }
            }
            return true;
        }
        /// <summary>
        /// 设置虚拟设备的默认名字
        /// </summary>
        /// <param name="listDriveDevice">虚拟设备列表</param>
        private void SetDriveDeviceDefultName(List<CommonDevice> listDriveDevice)
        {
            if (listDriveDevice.Count == 0)
            {
                return;
            }
            //如果虚拟设备还没有名字的话
            HdlThreadLogic.Current.RunThread(() =>
            {
                //如果不这样放在一个线程里,有可能对Dictionary产生影响
                foreach (var myDevice in listDriveDevice)
                {
                    string dName = string.Empty;
                    if (this.IsMiniLight(myDevice) == true)
                    {
                        //Mini夜灯
                        dName = Language.StringByID(R.MyInternationalizationString.uMiniNightLight);
                    }
                    else
                    {
                        //根据设备类型获取名称
                        dName = this.GetDeviceObjectText(new List<CommonDevice>() { myDevice }, false);
                        //在端点名字的后面附加【回路】字样
                        dName += "(" + myDevice.DeviceEpoint + Language.StringByID(R.MyInternationalizationString.uDeviceCircuit) + ")";
                    }
                    this.ReName(myDevice, dName, ShowErrorMode.NO);
                    System.Threading.Thread.Sleep(100);
                }
            });
        }
        ///<summary >
        /// 修改设备mac名称
        /// <para>macName:设备名称</para>
        /// </summary>
        private CommonDevice.RenameDeviceMacNameAllData RenameDeviceMacNameAsync(CommonDevice device, string macName)
        {
            //如果当前是虚拟住宅
            if (Common.Config.Instance.Home.IsVirtually == true)
            {
                return Phone.TemplateData.TemplateDeviceDataLogic.Current.ReDeviceMacName(device, macName);
            }
            //获取编辑设备Mac名字的命令字符
            var sendData = this.GetReDeviceMacNameCommandText(device.DeviceAddr, device.DeviceEpoint, macName);
            var result = HdlDeviceCommonLogic.Current.SendJobjectDataToGateway(device, "MacRename", sendData, "MacRename_Respon", 8);
            if (result.ErrorMsg != null || result.ErrorMsgDiv == 0)
            {
                return null;
            }
            //加缓存
            Phone.TemplateData.TemplateDeviceDataLogic.Current.ReDeviceMacName(device, macName);
            var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonDevice.RenameDeviceMacNameData>(result.ReceiptData);
            return new CommonDevice.RenameDeviceMacNameAllData { renameDeviceMacNameData = tempData };
        }
        /// <summary>
        /// 修改设备端口(按键)名称
        /// </summary>
        /// <param name="device">设备对象</param>
        /// <param name="deviceName">设备端点名字</param>
        /// <returns></returns>
        private CommonDevice.DeviceRenameAllData RenameDeviceNameAsync(CommonDevice device, string deviceName)
        {
            //如果当前是虚拟住宅
            if (Common.Config.Instance.Home.IsVirtually == true)
            {
                return Phone.TemplateData.TemplateDeviceDataLogic.Current.ReDeviceEpointName(device, deviceName);
            }
            //获取编辑设备端点名字的命令字符
            var sendData = this.GetReDeviceEpointNameCommandText(device.DeviceAddr, device.DeviceEpoint, deviceName);
            var result = HdlDeviceCommonLogic.Current.SendJobjectDataToGateway(device, "DeviceRename", sendData, "DeviceRenameRespon", 8);
            if (result.ErrorMsg != null || result.ErrorMsgDiv == 0)
            {
                return null;
            }
            //加缓存
            Phone.TemplateData.TemplateDeviceDataLogic.Current.ReDeviceEpointName(device, deviceName);
            var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonDevice.DeviceRenameResponseData>(result.ReceiptData);
            return new CommonDevice.DeviceRenameAllData { deviceRenameData = tempData };
        }
        /// <summary>
        /// 获取编辑设备Mac名字的命令字符
        /// </summary>
        /// <param name="deviceAddr"></param>
        /// <param name="deviceEpoint"></param>
        /// <param name="deviceName"></param>
        /// <returns></returns>
        public string GetReDeviceMacNameCommandText(string deviceAddr, int deviceEpoint, string deviceName)
        {
            var bytes = new byte[64];
            var reamarkGwBytes = Encoding.UTF8.GetBytes(deviceName);
            System.Array.Copy(reamarkGwBytes, 0, bytes, 0, 64 < reamarkGwBytes.Length ? 64 : reamarkGwBytes.Length);
            deviceName = Encoding.UTF8.GetString(bytes);
            var jObject = new Newtonsoft.Json.Linq.JObject { { "DeviceAddr", deviceAddr }, { "Epoint", deviceEpoint }, { "Cluster_ID", 0 }, { "Command", 100 } };
            var data = new Newtonsoft.Json.Linq.JObject { { "MacName", deviceName } };
            jObject.Add("Data", data);
            return jObject.ToString();
        }
        /// <summary>
        /// 获取编辑设备端点名字的命令字符
        /// </summary>
        /// <param name="deviceAddr"></param>
        /// <param name="deviceEpoint"></param>
        /// <param name="deviceName"></param>
        /// <returns></returns>
        public string GetReDeviceEpointNameCommandText(string deviceAddr, int deviceEpoint, string deviceName)
        {
            var bytes = new byte[64];
            var reamarkGwBytes = Encoding.UTF8.GetBytes(deviceName);
            System.Array.Copy(reamarkGwBytes, 0, bytes, 0, 64 < reamarkGwBytes.Length ? 64 : reamarkGwBytes.Length);
            deviceName = Encoding.UTF8.GetString(bytes);
            var jObject = new Newtonsoft.Json.Linq.JObject { { "DeviceAddr", deviceAddr }, { "Epoint", deviceEpoint }, { "Cluster_ID", 0 }, { "Command", 96 } };
            var data = new Newtonsoft.Json.Linq.JObject { { "DeviceName", deviceName } };
            jObject.Add("Data", data);
            return jObject.ToString();
        }
        /// <summary>
        /// 更改名字后,刷新设备缓存
        /// </summary>
        /// <param name="device"></param>
        private void BackupDeviceAfterReName(CommonDevice device)
        {
            lock (dicAllDevice)
            {
                string mainKeys = this.GetDeviceMainKeys(device);
                if (this.dicAllDevice.ContainsKey(mainKeys) == true)
                {
                    //一般设备
                    this.dicAllDevice[mainKeys] = device;
                    device.ReSave();
                    //添加自动备份
                    HdlAutoBackupLogic.AddOrEditorFile(device.FilePath);
                }
                else if (this.dicOTADevice.ContainsKey(mainKeys) == true)
                {
                    //Ota设备
                    this.dicOTADevice[mainKeys] = (OTADevice)device;
                    device.ReSave();
                    //添加自动备份
                    HdlAutoBackupLogic.AddOrEditorFile(device.FilePath);
                }
            }
        }
        #endregion
        #region ■ 删除设备___________________________
        /// <summary>
        /// 删除设备并且刷新缓存(删除失败时,会显示信息)
        /// </summary>
        /// <param name="listdevice">设备对象(MAC地址必须要相同)</param>
        public async Task<bool> DeleteDevice(List<CommonDevice> listdevice)
        {
            //虚拟住宅的话,不需要删除网关的设备
            if (Config.Instance.Home.IsVirtually == false)
            {
                var data = new CommonDevice.RemoveDeviceData();
                var info = new CommonDevice.RemoveDeviceListInfo();
                info.DeviceAddr = listdevice[0].DeviceAddr;
                data.DeviceAddrList.Add(info);
                //删一次的时候,它会把MAC地址下面全部的设备都删除
                var result = await listdevice[0].DeleteDeviceAsync(data);
                if (result == null || result.removeDeviceResponseData == null || result.removeDeviceResponseData.Result != 0)
                {
                    //设备删除失败
                    string msg = Language.StringByID(R.MyInternationalizationString.uDeviceDeleteFail);
                    //拼接上【网关回复超时】的Msg
                    msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result);
                    this.ShowErrorMsg(msg);
                    return false;
                }
            }
            //删除缓存的Ota设备
            this.DeleteMemmoryOtaDevice(listdevice[0].DeviceAddr);
            //删除一般设备文件
            foreach (CommonDevice device in listdevice)
            {
                this.DeleteMemmoryDevice(device);
            }
            //移除真实设备的房间索引
            HdlRoomLogic.Current.DeleteRealDeviceFromRoom(listdevice[0]);
            return true;
        }
        /// <summary>
        /// 删除缓存的一般设备
        /// </summary>
        /// <param name="device">设备对象</param>
        /// <param name="deleteRoom">是否从房间删除</param>
        public void DeleteMemmoryDevice(CommonDevice device, bool deleteRoom = true)
        {
            //删除缓存设备的话,主页需要重新刷新
            Phone.UserView.UserPage.Instance.RefreshAllForm = true;
            if (deleteRoom == true)
            {
                //从房间中删除
                HdlRoomLogic.Current.DeleteDevice(device);
                //删除我的喜爱的设备
                HdlRoomLogic.Current.DeleteLoveDevice(device);
            }
            //删除缓存
            string mainKeys = this.GetDeviceMainKeys(device);
            lock (dicAllDevice)
            {
                if (this.dicAllDevice.ContainsKey(mainKeys) == true)
                {
                    this.dicAllDevice.Remove(mainKeys);
                }
                if (this.dicDeviceEpoint.ContainsKey(device.DeviceAddr) == true)
                {
                    //变更端点数
                    this.dicDeviceEpoint[device.DeviceAddr].Remove(device.DeviceEpoint);
                }
            }
            //删除模板数据
            Phone.TemplateData.TemplateCommonLogic.Current.DeleteDevice(device);
            //删除设备文件
            string filePath = device.FilePath;
            if (Global.IsExistsByHomeId(filePath) == true)
            {
                if (UserCenterResourse.UserInfo.AuthorityNo == 3)
                {
                    //成员的话,直接删除,没有商量的余地
                    Global.DeleteFilebyHomeId(filePath);
                }
                else
                {
                    //变更:搞掉它,不留了
                    Global.DeleteFilebyHomeId(filePath);
                    //删除自动备份
                    HdlAutoBackupLogic.DeleteFile(device.FilePath);
                }
            }
        }
        /// <summary>
        /// 删除缓存的Ota设备
        /// </summary>
        /// <param name="macAdrr"></param>
        /// <param name="ePoint"></param>
        public void DeleteMemmoryOtaDevice(string macAdrr, int ePoint = 200)
        {
            //删除200端口文件
            string otaKeys = this.GetDeviceMainKeys(macAdrr, ePoint);
            if (this.dicOTADevice.ContainsKey(otaKeys) == true)
            {
                string otaFile = this.dicOTADevice[otaKeys].FilePath;
                if (Global.IsExistsByHomeId(otaFile) == true)
                {
                    if (UserCenterResourse.UserInfo.AuthorityNo == 3)
                    {
                        //成员的话,直接删除,没有商量的余地
                        Global.DeleteFilebyHomeId(otaFile);
                    }
                    else
                    {
                        //变更:搞掉它,不留了
                        Global.DeleteFilebyHomeId(otaFile);
                        //删除自动备份
                        HdlAutoBackupLogic.DeleteFile(otaFile);
                    }
                }
                this.dicOTADevice.Remove(otaKeys);
            }
        }
        #endregion
        #region ■ 同步设备___________________________
        /// <summary>
        ///  同步设备并且刷新缓存(同步失败时,会显示信息)
        /// </summary>
        /// <param name="litdevice">设备对象(MAC地址必须要相同)</param>
        public async Task<bool> SynchronizationDevice(List<CommonDevice> listdevice)
        {
            //虚拟住宅的话,不需要删除网关的设备
            if (Config.Instance.Home.IsVirtually == false)
            {
                //同步
                var result = await listdevice[0].SyncMsgToBindSource(listdevice[0].DeviceAddr, listdevice[0].DeviceEpoint);
                if (result == null || result.result != 0)
                {
                    //同步删除失败
                    string msg = Language.StringByID(R.MyInternationalizationString.SynchronizationFailed);
                    //拼接上【网关回复超时】的Msg
                    msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result);
                    this.ShowErrorMsg(msg);
                    return false;
                }
            }
            return true;
        }
        #endregion
        #region ■ 测试设备___________________________
        /// <summary>
        /// 发送定位指令到设备
        /// </summary>
        /// <param name="device">设备回路</param>
        /// <param name="isRealDevice">是否是定位真实设备</param>
        public void SetFixedPositionCommand(CommonDevice device, bool isRealDevice = false)
        {
            //如果当前住宅不是虚拟住宅
            if (Common.Config.Instance.Home.IsVirtually == false)
            {
                int epoint = device.DeviceEpoint;
                //如果是定位真实设备的话
                if (isRealDevice == true)
                {
                    var myType = this.GetMyDeviceEnumInfo(new List<CommonDevice> { device });
                    if (myType.ConcreteType == DeviceConcreteType.DimmableLight_OneLoad)
                    {
                        //一路调光器需要发8回路
                        epoint = 8;
                    }
                }
                device.IdentifyControl(device.DeviceAddr, epoint, 5);
            }
        }
        /// <summary>
        /// 检测设备是否拥有定位的功能
        /// </summary>
        /// <param name="device"></param>
        /// <returns></returns>
        public bool DeviceIsCanFixedPosition(CommonDevice device)
        {
            if (device.Type == DeviceType.DoorLock || device.Type == DeviceType.Airer || device.Type == DeviceType.PMSensor)
            {
                //门锁和晾衣架没有定位功能;
                return false;
            }
            var myTypeInfo = this.GetMyDeviceEnumInfo(new List<CommonDevice>() { device });
            if (device.Type == DeviceType.IASZone)
            {
                if (myTypeInfo.ConcreteType == DeviceConcreteType.Sensor_Pir)
                {
                    //传感器除了Pir都没有定位功能
                    return true;
                }
                else if (myTypeInfo.ConcreteType == DeviceConcreteType.Sensor_SphericalMotion)
                {
                    //球型移动传感器虽然是电池设备,但是它有定位功能
                    return true;
                }
                return false;
            }
            //温湿度传感器没有定位功能
            if (myTypeInfo.ConcreteType == DeviceConcreteType.Sensor_TemperatureHumidity)
            {
                return false;
            }
            foreach (var data in device.InClusterList)
            {
                //拥有on/off功能的,才支持测试
                if (data.InCluster == 3)
                {
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 检测设备是否拥有一键同步功能
        /// </summary>
        /// <param name="device"></param>
        /// <returns></returns>
        public bool DeviceIsCanSynchronization(CommonDevice device)
        {
            //获取设备类型的
            var deviceEnumInfo = Common.LocalDevice.Current.GetMyDeviceEnumInfo(new List<CommonDevice>() { device });
            if (deviceEnumInfo.ConcreteType == DeviceConcreteType.ButtonPanel_SimpleMultifunction)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 检测设备是否拥有开关的功能(输出簇)
        /// </summary>
        /// <param name="device"></param>
        /// <returns></returns>
        public bool OutDeviceIsCanOnOff(CommonDevice device)
        {
            foreach (var data in device.OutClusterList)
            {
                //拥有on/off功能的,才支持测试
                if (data.OutCluster == 6)
                {
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 检测设备是否拥有开关的功能(输入簇)
        /// </summary>
        /// <param name="device"></param>
        /// <returns></returns>
        public bool InDeviceIsCanOnOff(CommonDevice device)
        {
            foreach (var data in device.InClusterList)
            {
                //拥有on/off功能的,才支持测试
                if (data.InCluster == 6)
                {
                    return true;
                }
            }
            return false;
        }
        #endregion
        #region ■ 获取设备___________________________
        /// <summary>
        /// 根据网关ID获取所有的设备
        /// </summary>
        /// <param name="gwId">网关ID</param>>
        /// <returns></returns>
        public List<CommonDevice> GetDeviceByGatewayID(string gwId)
        {
            lock (dicAllDevice)
            {
                var listMac = new List<string>();
                //各网关的所有设备的Mac
                foreach (CommonDevice device in this.dicAllDevice.Values)
                {
                    if (gwId == device.CurrentGateWayId)
                    {
                        listMac.Add(device.DeviceAddr);
                    }
                }
                return this.GetDevicesByMac(listMac);
            }
        }
        /// <summary>
        /// 获取指定设备(主键是:Mac地址+端口号),不存在时,返回null
        /// </summary>
        /// <param name="mainKeys">Mac地址+端口号</param>
        /// <returns></returns>
        public CommonDevice GetDevice(string mainKeys)
        {
            lock (dicAllDevice)
            {
                if (this.dicAllDevice.ContainsKey(mainKeys) == true)
                {
                    return this.dicAllDevice[mainKeys];
                }
            }
            return null;
        }
        /// <summary>
        /// 获取指定设备,不存在时,返回null
        /// </summary>
        /// <param name="DeviceAddr">Mac地址</param>
        /// <param name="DeviceEpoint">端口号</param>
        /// <returns></returns>
        public CommonDevice GetDevice(string DeviceAddr, int DeviceEpoint)
        {
            string mainkeys = this.GetDeviceMainKeys(DeviceAddr, DeviceEpoint);
            return this.GetDevice(mainkeys);
        }
        /// <summary>
        /// 根据MAC地址,获取全部回路的设备对象
        /// </summary>
        /// <param name="DeviceAddr">Mac地址</param>
        /// <param name="sort">是否排序</param>
        /// <returns></returns>
        public List<CommonDevice> GetDevicesByMac(string DeviceAddr, bool sort = true)
        {
            var list = new List<CommonDevice>();
            if (dicDeviceEpoint.ContainsKey(DeviceAddr) == false)
            {
                return list;
            }
            foreach (var point in dicDeviceEpoint[DeviceAddr])
            {
                var device = this.GetDevice(DeviceAddr, point);
                if (device != null)
                {
                    list.Add(device);
                }
            }
            if (sort == false)
            {
                return list;
            }
            //排序
            list.Sort((obj1, obj2) =>
            {
                if (obj1.DeviceEpoint > obj2.DeviceEpoint)
                {
                    return 1;
                }
                return -1;
            });
            return list;
        }
        /// <summary>
        /// 根据MAC地址,获取简约面板全部回路的设备对象
        /// </summary>
        /// <param name="listDevice"></param>
        /// <returns></returns>
        public List<CommonDevice> GetMutilfunctionPanelByMac(List<CommonDevice> listDevice, bool sort = true)
        {
            //和彪哥、设备和产品部同事确认:
            //简约多功能面板[不显示多余的回路,只显示携带的2个继电器,1个温度传感器,1个湿度传感器
            var list = new List<CommonDevice>();
            foreach (var dev in listDevice)
            {
                if (dev.Type == DeviceType.TemperatureSensor)
                {
                    if (dev.DeviceEpoint == 64)
                    {
                        list.Add(dev);
                    }
                }
                else if (dev.Type == DeviceType.FreshAirHumiditySensor)
                {
                    if (dev.DeviceEpoint == 65)
                    {
                        list.Add(dev);
                    }
                }
                else if (dev.Type == DeviceType.OnOffOutput)
                {
                    list.Add(dev);
                }
            }
            if (sort == false)
            {
                return list;
            }
            list.Sort((obj1, obj2) =>
            {
                if (obj1.DeviceEpoint > obj2.DeviceEpoint)
                {
                    return 1;
                }
                return -1;
            });
            return list;
        }
        /// <summary>
        /// 根据MAC地址,获取新风、简约环境面板全部回路的设备对象
        /// </summary>
        /// <param name="listDevice"></param>
        /// <returns></returns>
        public List<CommonDevice> GetPanelMatchEpointByMac(List<CommonDevice> listDevice, bool sort = true)
        {
            var list = new List<CommonDevice>();
            foreach (var dev in listDevice)
            {
                if (dev.Type == DeviceType.TemperatureSensor)
                {
                    list.Add(dev);
                }
                else if (dev.Type == DeviceType.FreshAirHumiditySensor)
                {
                    list.Add(dev);
                }
            }
            if (sort == false)
            {
                return list;
            }
            list.Sort((obj1, obj2) =>
            {
                if (obj1.DeviceEpoint > obj2.DeviceEpoint)
                {
                    return 1;
                }
                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;
        }
        /// <summary>
        /// 根据MAC地址,获取全部回路的数量
        /// </summary>
        /// <param name="DeviceAddr">Mac地址</param>
        /// <returns></returns>
        public int GetDevicesCountByMac(string DeviceAddr)
        {
            if (dicDeviceEpoint.ContainsKey(DeviceAddr) == false)
            {
                return 0;
            }
            return dicDeviceEpoint[DeviceAddr].Count;
        }
        /// <summary>
        /// 获取本地全部的设备文件
        /// </summary>
        /// <returns></returns>
        public List<string> GetAllDeviceFile()
        {
            List<string> listDeviceFile = new List<string>();
            List<string> listAllFile = HdlFileLogic.Current.GetRootPathListFile();
            foreach (string file in listAllFile)
            {
                if (file.StartsWith("Device_") == false)
                {
                    //如果不是设备文件
                    continue;
                }
                listDeviceFile.Add(file);
            }
            return listDeviceFile;
        }
        /// <summary>
        /// 获取OTA设备(200端口的)
        /// </summary>
        /// <param name="macAdrr"></param>
        /// <param name="ePoint"></param>
        /// <returns></returns>
        public OTADevice GetOTADevice(string macAdrr, int ePoint = 200)
        {
            string mainkeys = this.GetDeviceMainKeys(macAdrr, ePoint);
            if (this.dicOTADevice.ContainsKey(mainkeys) == false)
            {
                return null;
            }
            return this.dicOTADevice[mainkeys];
        }
        /// <summary>
        /// 获取特殊的,没有其他回路,单纯只有200端点的OTA设备
        /// </summary>
        /// <param name="gwId">网关ID</param>
        /// <returns></returns>
        public List<OTADevice> GetSpecialOtaDevice(string gwId)
        {
            var list = new List<OTADevice>();
            foreach (var ota in this.dicOTADevice.Values)
            {
                if (ota.CurrentGateWayId != gwId)
                {
                    //不是同一个网关
                    continue;
                }
                //没有其他回路
                if (dicDeviceEpoint.ContainsKey(ota.DeviceAddr) == false
                    || dicDeviceEpoint[ota.DeviceAddr].Count == 0)
                {
                    //目前只针对中央空调
                    if (ota.ModelIdentifier == "MAC/GW-ZB.10")
                    {
                        list.Add(ota);
                    }
                }
            }
            return list;
        }
        #endregion
        #region ■ 获取设备信息_______________________
        /// <summary>
        /// 从网关读取单个端点回路设备信息
        /// </summary>
        /// <param name="device">设备对象</param>
        /// <returns></returns>
        public CommonDevice.DeviceInfoData ReadDeviceEpointDeviceInfo(CommonDevice device)
        {
            var jObject = new Newtonsoft.Json.Linq.JObject { { "DeviceAddr", device.DeviceAddr }, { "Epoint", device.DeviceEpoint }, { "Cluster_ID", 0 }, { "Command", 80 } };
            var result = HdlDeviceCommonLogic.Current.SendJobjectDataToGateway(device, "GetStatusRecord", jObject.ToString(), "GetStatusRecord_Respon");
            if (result.ErrorMsg != null || result.ErrorMsgDiv == 0)
            {
                return null;
            }
            var info = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonDevice.DeviceInfoData>(result.ReceiptData);
            return info;
        }
        /// <summary>
        /// 从网关读取指定Mac下的设备列表(返回的是网关回复的设备Json,调用ConvertJObjectToDevice方法转为Device对象)
        /// </summary>
        /// <param name="realGateway">真实网关对象</param>
        /// <param name="i_deviceMac">设备Mac</param>
        /// <param name="useLocalConnect">是否使用本地连接发送</param>
        /// <param name="listFucDevice">需要变更功能类型的回路</param>
        /// <param name="statu">状态-> -1:异常,会返回null, 1:没有异常, 2:数据接收不全</param>
        /// <returns></returns>
        public List<Newtonsoft.Json.Linq.JObject> ReadDeviceListByMacFromGateway(string gatewayId, string i_deviceMac, bool useLocalConnect, ref int statu)
        {
            //是否达成中断的时机
            bool canBreak = false;
            //超时时间
            int TimeOut = 0;
            //设备总数
            int deviceCount = -1;
            //接收数
            int receiveCount = 0;
            //设备列表
            var listDeviceJson = new List<Newtonsoft.Json.Linq.JObject>();
            HdlGatewayReceiveLogic.Current.AddGatewayReceiveEvent(gatewayId, (topic, message) =>
            {
                if (topic == gatewayId + "/GetStatusRecord_Respon")
                {
                    lock (listDeviceJson)
                    {
                        //设备接收数
                        receiveCount++;
                        TimeOut = 0;
                        var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
                        var totalNum = Newtonsoft.Json.JsonConvert.DeserializeObject<int>(jobject["Data"]["TotalNum"].ToString());
                        if (totalNum == 0)
                        {
                            //这个网关没有设备
                            canBreak = true;
                            return;
                        }
                        if (deviceCount == -1)
                        {
                            //设置需要接收多少个设备
                            deviceCount = totalNum;
                        }
                        listDeviceJson.Add(jobject);
                    }
                    if (receiveCount == deviceCount && deviceCount != -1)
                    {
                        //设备全部接收完成
                        canBreak = true;
                    }
                }
            });
            var jObject = new Newtonsoft.Json.Linq.JObject { { "DeviceAddr", i_deviceMac }, { "Epoint", 255 }, { "Cluster_ID", 0 }, { "Command", 80 } };
            HdlGatewayLogic.Current.SendJobjectData(gatewayId, "GetStatusRecord", jObject.ToString(), useLocalConnect);
            while (canBreak == false && TimeOut <= 150)
            {
                System.Threading.Thread.Sleep(20);
                TimeOut++;
            }
            //移除监听
            HdlGatewayReceiveLogic.Current.RemoveGatewayReceiveEvent();
            if (TimeOut > 150)
            {
                statu = listDeviceJson.Count == 0 ? -1 : 2;
            }
            else
            {
                statu = 1;
            }
            return listDeviceJson;
        }
        /// <summary>
        /// 将设备的Json转换为设备对象
        /// </summary>
        /// <param name="listIObjects">设备Json</param>
        /// <param name="gwId">网关id</param>
        /// <param name="listFucDevice">需要变更功能类型的回路</param>
        /// <returns></returns>
        public List<CommonDevice> ConvertJObjectToDevice(List<Newtonsoft.Json.Linq.JObject> listIObjects, string gwId, ref List<CommonDevice> listFucDevice)
        {
            //设备列表
            var listDevice = new List<CommonDevice>();
            //网关里面有可能会有重复的回路
            var listCheck = new HashSet<string>();
            foreach (var jobject in listIObjects)
            {
                var deviceID = (DeviceType)jobject.Value<int>("Device_ID");
                //根据设备类型创建设备对象的实例
                var device = this.NewDeviceObjectByDeviceId(deviceID, jobject, gwId);
                if (device != null)
                {
                    string mainkeys = this.GetDeviceMainKeys(device);
                    //网关里面有可能会有重复的回路
                    if (listCheck.Contains(mainkeys) == false)
                    {
                        listDevice.Add(device);
                        listCheck.Add(mainkeys);
                        //刷新一下本地缓存
                        var localDevice = this.GetDevice(mainkeys);
                        var tempDevice = localDevice == null ? device : localDevice;
                        //如果这个设备ID变更了的话
                        bool typeNotEquals = localDevice != null && deviceID != localDevice.Type;
                        if (typeNotEquals == true)
                        {
                            //重新New这个对象
                            typeNotEquals = this.ReNewDeviceOnTypeIsChanged(localDevice, deviceID);
                            //重新再次获取对象
                            tempDevice = this.GetDevice(mainkeys);
                        }
                        //刷新属性
                        this.SetDeviceInfoToMain(tempDevice, device);
                        if (this.RefreshDeviceFunctionType(tempDevice, device, false) == true)
                        {
                            //需要发送功能类型给网关
                            listFucDevice.Add(tempDevice);
                        }
                        if (typeNotEquals == true)
                        {
                            //重新生成缓存
                            tempDevice.ReSave();
                            HdlAutoBackupLogic.AddOrEditorFile(tempDevice.FilePath);
                            //全部主页菜单需要刷新
                            Phone.UserView.UserPage.Instance.RefreshAllForm = true;
                        }
                    }
                }
            }
            return listDevice;
        }
        #endregion
        #region ■ 设置设备功能类型___________________
        /// <summary>
        /// 设置设备功能类型到网关
        /// </summary>
        /// <param name="device">设备回路</param>
        /// <param name="functionType">功能类型</param>
        /// <returns></returns>
        public bool SendDeviceFunctionTypeToGateway(CommonDevice device, DeviceFunctionType functionType)
        {
            //如果是虚拟住宅
            if (Common.Config.Instance.Home.IsVirtually == true)
            {
                device.DfunctionType = functionType;
                return true;
            }
            //获取命令字符
            var sendCommond = this.GetDeviceFunctionTypeCommandText(device, functionType);
            var result = HdlDeviceCommonLogic.Current.SendJobjectDataToGateway(device, "Device/SetEPDeviceFunctionType", sendCommond, "Device/SetEPDeviceFunctionTypeRespon");
            if (result.ErrorMsg != null || result.ErrorMsgDiv == 0)
            {
                return false;
            }
            var resultData = Newtonsoft.Json.Linq.JObject.Parse(result.ReceiptData);
            if (resultData.Property("Result") != null)
            {
                //0:修改成功 1:修改失败
                return resultData["Result"].ToString() == "0";
            }
            return false;
        }
        /// <summary>
        /// 获取更改设备功能类型的命令文本
        /// </summary>
        /// <param name="device"></param>
        /// <param name="functionType"></param>
        /// <returns></returns>
        public string GetDeviceFunctionTypeCommandText(CommonDevice device, DeviceFunctionType functionType)
        {
            var jObject = new Newtonsoft.Json.Linq.JObject { { "DeviceAddr", device.DeviceAddr }, { "Epoint", device.DeviceEpoint }, { "Cluster_ID", 0 }, { "Command", 110 } };
            var data = new Newtonsoft.Json.Linq.JObject { { "FunctionType", (int)functionType } };
            jObject.Add("Data", data);
            return jObject.ToString();
        }
        #endregion
        #region ■ 获取设备名称_______________________
        /// <summary>
        /// 获取设备端点的名称(有特效)
        /// </summary>
        /// <param name="device">设备对象</param>
        /// <returns></returns>
        public string GetDeviceEpointName(CommonDevice device)
        {
            string dName = this.GetSimpleEpointName(device);
            if (string.IsNullOrEmpty(dName) == false)
            {
                return dName;
            }
            if (this.IsMiniLight(device) == true)
            {
                //Mini夜灯
                return Language.StringByID(R.MyInternationalizationString.uMiniNightLight);
            }
            //如果这个设备只有一个回路的话,返回Mac名字给它
            if (this.GetDevicesCountByMac(device.DeviceAddr) <= 1)
            {
                return this.GetDeviceMacName(device);
            }
            //同一设备里面,不同类型的回路,它的命名都从1开始
            int epointNo = 0;
            var listSort = this.GetDevicesByMac(device.DeviceAddr);
            foreach (var myDevice in listSort)
            {
                if (myDevice.Type == device.Type)
                {
                    //同一类型编号+1
                    epointNo++;
                    if (myDevice.DeviceEpoint == device.DeviceEpoint)
                    {
                        //已经到达它自己
                        break;
                    }
                }
            }
            if (device.Type == DeviceType.OnOffOutput
                || device.Type == DeviceType.DimmableLight
                || device.Type == DeviceType.ColorDimmableLight
                || device.Type == DeviceType.ColorTemperatureLight)
            {
                //继电器,掉光器都叫回路
                return Language.StringByID(R.MyInternationalizationString.uDeviceCircuit) + epointNo;
            }
            if (device.Type == DeviceType.Thermostat)
            {
                //空调都叫室内机
                return Language.StringByID(R.MyInternationalizationString.uIndoorUnit) + epointNo;
            }
            //获取设备类型
            var deviceInfoType = this.GetMyDeviceEnumInfo(new List<CommonDevice>() { device });
            if (device.Type == DeviceType.OnOffSwitch)
            {
                //面板的干接点叫按键
                if (deviceInfoType.BeloneType == DeviceBeloneType.A按键面板)
                {
                    return Language.StringByID(R.MyInternationalizationString.uPanelButton) + epointNo;
                }
                //其他的干接点叫干接点
                else
                {
                    var infoContent = this.GetDeviceModelIdNameInfo("A419");
                    return infoContent != null ? infoContent.A官方名字 + epointNo : string.Empty;
                }
            }
            else if (device.Type == DeviceType.FreshAirHumiditySensor)
            {
                //新风面板/简约多功能/简约环境面板湿度传感器
                return deviceInfoType.DeviceDefultName + Language.StringByID(R.MyInternationalizationString.HumiditySensor);
            }
            else if (device.Type == DeviceType.TemperatureSensor)
            {
                if (deviceInfoType.ConcreteType == DeviceConcreteType.ButtonPanel_FangyueFreshAir
                    || deviceInfoType.ConcreteType == DeviceConcreteType.ButtonPanel_SimpleEnvironment
                    || deviceInfoType.ConcreteType == DeviceConcreteType.ButtonPanel_FangyueEnvironment
                    || deviceInfoType.ConcreteType == DeviceConcreteType.ButtonPanel_SimpleMultifunction)
                {
                    //新风面板/简约多功能/简约/方悦环境面板 温度传感器
                    return deviceInfoType.DeviceDefultName + Language.StringByID(R.MyInternationalizationString.TemperatureSensor);
                }
                else if (deviceInfoType.BeloneType == DeviceBeloneType.A按键面板)
                {
                    //面板的温度探头叫  面板名字+温度
                    return deviceInfoType.DeviceDefultName + Language.StringByID(R.MyInternationalizationString.uTemperature);
                }
                else if (deviceInfoType.BeloneType == DeviceBeloneType.A空气质量)
                {
                    //空气质量传感器的温度探头叫空气质量传感器+温度
                    return deviceInfoType.DeviceDefultName + Language.StringByID(R.MyInternationalizationString.TemperatureSensor);
                }
                else if (((TemperatureSensor)device).SensorDiv == 1)
                {
                    //温度传感器
                    return Language.StringByID(R.MyInternationalizationString.TemperatureSensor);
                }
                else if (((TemperatureSensor)device).SensorDiv == 2)
                {
                    //湿度传感器
                    return Language.StringByID(R.MyInternationalizationString.HumiditySensor);
                }
            }
            //其他情况,使用它的默认名称
            return deviceInfoType.DeviceDefultName + epointNo;
        }
        /// <summary>
        /// 获取设备MAC名称
        /// </summary>
        /// <param name="device">设备对象</param>
        /// <returns></returns>
        public string GetDeviceMacName(CommonDevice device)
        {
            string dName = this.GetSimpleMacName(device);
            if (string.IsNullOrEmpty(dName) == false)
            {
                return dName;
            }
            if (this.IsMiniLight(device) == true)
            {
                //Mini夜灯
                return Language.StringByID(R.MyInternationalizationString.uMiniNightLight);
            }
            //获取设备类型
            var deviceInfoType = this.GetMyDeviceEnumInfo(new List<CommonDevice>() { device });
            return deviceInfoType.DeviceDefultName;
        }
        /// <summary>
        /// 非公开,设置设备的Mac名字(此方法只是单存的变更缓存)
        /// </summary>
        /// <param name="device">设备对象</param>
        /// <param name="macName">Mac名字</param>
        /// <returns></returns>
        public void SetMacName(CommonDevice device, string macName)
        {
            device.DeviceName = macName;
        }
        /// <summary>
        /// 非公开,设置设备的端点名字(此方法只是单存的变更缓存)
        /// </summary>
        /// <param name="device">设备对象</param>
        /// <param name="epointName">端点名字</param>
        /// <returns></returns>
        public void SetEpointName(CommonDevice device, string epointName)
        {
            device.DeviceEpointName = epointName;
        }
        /// <summary>
        /// 单纯获取设备的Mac名字
        /// </summary>
        /// <param name="device">设备对象</param>
        /// <returns></returns>
        public string GetSimpleMacName(CommonDevice device)
        {
            return device.DeviceName;
        }
        /// <summary>
        /// 单纯获取设备的端点名字
        /// </summary>
        /// <param name="device">设备对象</param>
        /// <returns></returns>
        public string GetSimpleEpointName(CommonDevice device)
        {
            return device.DeviceEpointName;
        }
        #endregion
        #region ■ 设置图标___________________________
        /// <summary>
        /// 变更设备的图标
        /// </summary>
        /// <param name="device">设备对象</param>
        /// <param name="unSelPath">图片地址(非选择)</param>
        public void ChangedDeviceIcon(CommonDevice device, string unSelPath)
        {
            if (unSelPath == string.Empty)
            {
                return;
            }
            device.IconPath = unSelPath;
            device.IsCustomizeImage = true;
            device.ReSave();
            HdlAutoBackupLogic.AddOrEditorFile(device.FilePath);
        }
        /// <summary>
        /// 设置设备【图标】到指定的控件
        /// </summary>
        /// <param name="btnIcon">控件对象</param>
        /// <param name="device">设备对象</param>
        /// <returns></returns>
        public void SetDeviceIconToControl(Button btnIcon, CommonDevice device)
        {
            if (device == null)
            {
                btnIcon.UnSelectedImagePath = "Device/ThirdPartyDevice.png";
                return;
            }
            string unSelectFilePath = string.Empty;
            string selectFilePath = string.Empty;
            //获取设备【图标】
            this.GetDeviceIcon(device, ref unSelectFilePath, ref selectFilePath);
            if (btnIcon.UnSelectedImagePath != unSelectFilePath)
            {
                btnIcon.UnSelectedImagePath = unSelectFilePath;
            }
            if (btnIcon.SelectedImagePath != selectFilePath)
            {
                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>
        /// 获取设备【图标】
        /// </summary>
        /// <param name="listdevice">设备对象</param>
        /// <param name="unSelectPath">图片地址</param>
        /// <param name="selectPath">图片地址</param>
        /// <returns></returns>
        public void GetDeviceIcon(CommonDevice device, ref string unSelectPath, ref string selectPath)
        {
            if (string.IsNullOrEmpty(device.IconPath) == true)
            {
                return;
            }
            unSelectPath = device.IconPath;
            string selPath = device.OnlineIconPath;
            if (string.IsNullOrEmpty(selPath) == false)
            {
                selectPath = selPath;
            }
        }
        /// <summary>
        /// 设置设备的真实图片到指定的控件
        /// </summary>
        /// <param name="btnIcon">控件对象</param>
        /// <param name="listdevice">设备对象</param>
        public void SetRealDeviceIconToControl(Button btnIcon, List<CommonDevice> listdevice)
        {
            //获取设备的真实图片
            string imagePath = this.GetRealDeviceIcon(listdevice);
            btnIcon.UnSelectedImagePath = imagePath;
        }
        /// <summary>
        /// 获取设备的真实图片
        /// </summary>
        /// <param name="listdevice">设备对象</param>
        public string GetRealDeviceIcon(List<CommonDevice> listdevice)
        {
            //获取它属于什么类型的设备
            var myDeviceType = this.GetMyDeviceEnumInfo(listdevice);
            string strConcrete = Enum.GetName(typeof(DeviceConcreteType), myDeviceType.ConcreteType);
            //图片共有
            if (this.dicPictrueShard.ContainsKey(strConcrete) == true)
            {
                strConcrete = this.dicPictrueShard[strConcrete];
            }
            string strType = strConcrete.Replace("_", string.Empty);
            //将类型转为图片地址
            string imageFilePath = "RealDevice/" + strType + ".png";
            //这个图片本地是否存在?
            if (string.IsNullOrEmpty(IO.FileUtils.GetImageFilePath(imageFilePath)) == true)
            {
                //不存在则使用共通图片
                string[] arry = strConcrete.Split(new string[] { "_" }, StringSplitOptions.None);
                if (arry.Length == 1)
                {
                    //如果它自己就是共通图片的话,不再处理
                    return "RealDevice/CommonDevice.png";
                }
                imageFilePath = "RealDevice/" + arry[0] + ".png";
                //如果它自己的共通图片还是不存在的话,则直接使用所有设备的共通图片
                if (string.IsNullOrEmpty(IO.FileUtils.GetImageFilePath(imageFilePath)) == true)
                {
                    imageFilePath = "RealDevice/CommonDevice.png";
                }
            }
            return imageFilePath;
        }
        /// <summary>
        /// 设置【设备类型】的图标到指定的控件(此方法不能用在设备功能类型菜单的图标)
        /// </summary>
        /// <param name="btnIcon">控件对象</param>
        /// <param name="listdevice">设备对象</param>
        /// <returns></returns>
        public void SetDeviceObjectIconToControl(Button btnIcon, List<CommonDevice> listdevice)
        {
            //获取自定义设备类型
            var myDeviceType = this.GetMyDeviceEnumInfo(listdevice);
            string imageUnSelectFilePath = string.Empty;
            string imageSelectFilePath = string.Empty;
            //获取【设备类型】的图标
            this.GetDeviceObjectIcon(myDeviceType.ConcreteType, ref imageUnSelectFilePath, ref imageSelectFilePath);
            //设置图片
            btnIcon.UnSelectedImagePath = imageUnSelectFilePath;
            btnIcon.SelectedImagePath = imageSelectFilePath;
        }
        /// <summary>
        /// 获取【设备类型】的图标(此方法不能用在设备功能类型菜单的图标)
        /// </summary>
        /// <param name="listdevice">设备对象</param>
        /// <param name="unSelectPath">图片地址</param>
        /// <param name="selectPath">图片地址</param>
        /// <returns></returns>
        public void GetDeviceObjectIcon(List<CommonDevice> listdevice, ref string unSelectPath, ref string selectPath)
        {
            //获取自定义设备类型
            var myDeviceType = this.GetMyDeviceEnumInfo(listdevice);
            //获取【设备类型】的图标
            this.GetDeviceObjectIcon(myDeviceType.ConcreteType, ref unSelectPath, ref selectPath);
        }
        /// <summary>
        /// 获取【设备功能类型】的菜单图标
        /// </summary>
        /// <param name="deviceEnumInfo">自定义设备类型</param>
        /// <param name="unSelectPath">图片地址</param>
        /// <param name="selectPath">图片地址</param>
        /// <returns></returns>
        public void GetDeviceFunctionTypeMenuIcon(DeviceEnumInfo deviceEnumInfo, ref string unSelectPath, ref string selectPath)
        {
            //新风小模块
            if (deviceEnumInfo.ConcreteType == DeviceConcreteType.Relay_FangyueFreshAirModul)
            {
                unSelectPath = "Device/FreshAirEpoint.png";
                selectPath = "Device/FreshAirEpointSelected.png";
                return;
            }
            //PM2.5空气质量传感器
            else if (deviceEnumInfo.ConcreteType == DeviceConcreteType.Sensor_PMTwoPointFive)
            {
                unSelectPath = "Device/AirQualitySensorEpoint.png";
                selectPath = "Device/AirQualitySensorEpointSelected.png";
                return;
            }
            else if (deviceEnumInfo.BeloneType == DeviceBeloneType.A窗帘)
            {
                unSelectPath = "ZigeeLogic/curtain.png";
                selectPath = "ZigeeLogic/selectedcurtain.png";
                return;
            }
            //上面需要特殊处理
            //获取【设备类型】的图标
            this.GetDeviceObjectIcon(deviceEnumInfo.ConcreteType, ref unSelectPath, ref selectPath);
        }
        /// <summary>
        /// 获取【设备类型】的图标 2020.05.13:次函数不再公开
        /// </summary>
        /// <param name="specificType">自定义设备类型</param>
        /// <param name="unSelectPath">图片地址</param>
        /// <param name="selectPath">图片地址</param>
        /// <returns></returns>
        private void GetDeviceObjectIcon(DeviceConcreteType specificType, ref string unSelectPath, ref string selectPath)
        {
            //将具体类型转字符串
            string strSpecific = Enum.GetName(typeof(DeviceConcreteType), specificType);
            //图片共有
            if (this.dicPictrueShard.ContainsKey(strSpecific) == true)
            {
                strSpecific = this.dicPictrueShard[strSpecific];
            }
            string strType = strSpecific.Replace("_", string.Empty);
            //将类型转为图片地址
            string imageFilePath = "Device/" + strType + ".png";
            string imageSelectFilePath = "Device/" + strType + "Selected.png";
            //这个图片本地是否存在?
            if (string.IsNullOrEmpty(IO.FileUtils.GetImageFilePath(imageFilePath)) == true)
            {
                //不存在则使用共通图片
                string[] arry = strSpecific.Split(new string[] { "_" }, StringSplitOptions.None);
                //如果它自己就是共通图片的话,不再处理
                if (arry.Length > 1)
                {
                    imageFilePath = "Device/" + arry[0] + ".png";
                    imageSelectFilePath = "Device/" + arry[0] + "Selected.png";
                }
            }
            //如果那款设备连共通图片都没有的话
            if (string.IsNullOrEmpty(IO.FileUtils.GetImageFilePath(imageFilePath)) == true)
            {
                imageFilePath = "Device/ThirdPartyDevice.png";
                imageSelectFilePath = "Device/ThirdPartyDeviceSelected.png";
            }
            //设置图片
            unSelectPath = imageFilePath;
            selectPath = imageSelectFilePath;
        }
        #endregion
        #region ■ 获取自定义的设备类型_______________
        /// <summary>
        /// 获取【自定义的设备类型】,两种类型都设置了
        /// </summary>
        /// <param name="listdevice">设备对象</param>
        /// <returns></returns>
        public DeviceEnumInfo GetMyDeviceEnumInfo(List<CommonDevice> listdevice)
        {
            CommonDevice checkDevice = listdevice[0];
            foreach (var temp in listdevice)
            {
                //拿拥有模块ID的那个回路来判断
                if (temp.ModelIdentifier != string.Empty)
                {
                    checkDevice = temp;
                }
            }
            //获取河东设备的设备类型
            DeviceEnumInfo info = this.GetHdlMyDeviceEnumInfo(checkDevice);
            if (info != null)
            {
                return info;
            }
            //获取第三方设备的【设备类型】
            info = this.GetNotHdlMyDeviceEnumInfo(listdevice);
            //这里再次判断是否是河东设备,有可能它的模块ID写错了
            info.IsHdlDevice = this.IsHdlDevice(checkDevice);
            return info;
        }
        /// <summary>
        /// 获取设备的【设备类型】的翻译文本(优先镜像)
        /// </summary>
        /// <param name="listDevice"></param>
        /// <param name="ApendFalge">第三方或者虚拟设备的时候,是否添加标识</param>
        /// <returns></returns>
        public string GetDeviceObjectText(List<CommonDevice> listDevice, bool ApendFalge = true)
        {
            CommonDevice checkDevice = listDevice[0];
            foreach (var temp in listDevice)
            {
                //拿拥有模块ID的那个回路来判断
                if (temp.ModelIdentifier != string.Empty)
                {
                    checkDevice = temp;
                }
            }
            //获取自定义设备类型
            var myInfoType = this.GetMyDeviceEnumInfo(listDevice);
            //获取设备类型的翻译名字
            string strName = myInfoType.ObjectTypeName;
            if (strName == string.Empty)
            {
                //加一层保险,未知设备
                strName = Language.StringByID(R.MyInternationalizationString.UnknowDevice);
            }
            if (ApendFalge == true)
            {
                if (listDevice[0].DriveCode > 0)
                {
                    //虚拟设备加个标识
                    strName += "✩";
                }
                else
                {
                    foreach (var temp in listDevice)
                    {
                        //拿拥有模块ID的那个回路来判断
                        if (temp.ModelIdentifier != string.Empty)
                        {
                            if (this.IsHdlDevice(checkDevice) == false)
                            {
                                //第三方设备加个标识
                                strName += "★";
                                break;
                            }
                        }
                    }
                }
            }
            return strName;
        }
        #endregion
        #region ■ 获取河东设备的设备类型_____________
        /// <summary>
        /// 获取Hdl设备的【自定义的设备类型】
        /// </summary>
        /// <param name="device">随便某一回路</param>
        /// <returns></returns>
        private DeviceEnumInfo GetHdlMyDeviceEnumInfo(CommonDevice device)
        {
            //设备具体类型
            var info = new DeviceEnumInfo();
            info.IsHdlDevice = this.IsHdlDevice(device);
            if (device.ModelIdentifier == string.Empty)
            {
                return null;
            }
            string modelKeys = device.ModelIdentifier;
            //交换一下模块ID(麦乐克那边的传感器)
            this.ChangedDeviceModeId(ref modelKeys);
            if (this.dicDeviceModelIdEnum.ContainsKey(modelKeys) == false)
            {
                //没有匹配到模块ID,则直接走第三方设备的判断
                return null;
            }
            var nameContent = this.dicDeviceModelIdEnum[modelKeys];
            //设置设备的【设备所属类型】
            info.BeloneType = (DeviceBeloneType)nameContent.BeloneTypeValue;
            info.BeloneText = nameContent.A所属名字;
            //设备具体类型
            info.ConcreteType = (DeviceConcreteType)nameContent.ConcreteTypeValue;
            if (info.ConcreteType.ToString() == nameContent.ConcreteTypeValue.ToString())
            {
                info.ConcreteType = DeviceConcreteType.UnKownDevice;
            }
            //设备的官方名称
            info.ConcreteText = nameContent.A官方名字;
            //设备的类型翻译名称
            info.ObjectTypeName = nameContent.A类型名字;
            //设备默认名字
            info.DeviceDefultName = nameContent.A备注名字;
            return info;
        }
        #endregion
        #region ■ 获取第三方设备的设备类型___________
        /// <summary>
        /// 获取设备的【所属类型信息】,此方法会把所有的传感器都归为【传感器】(包括温湿度传感器)
        /// </summary>
        /// <param name="device">设备回路</param>
        /// <returns></returns>
        public DeviceEnumInfo GetDeviceBelongEnumInfo(CommonDevice device)
        {
            var info = this.GetNotHdlMyDeviceEnumInfo(new List<CommonDevice>() { device });
            if (info.BeloneType == DeviceBeloneType.A调光器
                || info.BeloneType == DeviceBeloneType.A彩灯)
            {
                //归为灯光
                info.ConcreteType = DeviceConcreteType.Light;
                info.ConcreteText = this.dicDeviceModelIdEnum["A418"].A官方名字;
                info.BeloneType = DeviceBeloneType.A灯光;
                info.BeloneText = this.dicDeviceModelIdEnum["A418"].A官方名字;
                info.ObjectTypeName = this.dicDeviceModelIdEnum["A418"].A类型名字;//调光模块
            }
            else if (info.BeloneType == DeviceBeloneType.A传感器
                || device.Type == DeviceType.TemperatureSensor
                || device.Type == DeviceType.FreshAirHumiditySensor)
            {
                //传感器合并
                info.ConcreteType = DeviceConcreteType.Sensor;
                info.ConcreteText = this.dicDeviceModelIdEnum["A402"].A官方名字;
                info.BeloneType = DeviceBeloneType.A传感器;
                info.BeloneText = this.dicDeviceModelIdEnum["A402"].A官方名字;
                info.ObjectTypeName = this.dicDeviceModelIdEnum["A402"].A类型名字;//传感器
            }
            return info;
        }
        /// <summary>
        /// 获取第三方设备的【设备类型】(不建议使用)
        /// </summary>
        /// <param name="listdevice">Mac都一样的设备列表</param>
        /// <returns></returns>
        public DeviceEnumInfo GetNotHdlMyDeviceEnumInfo(List<CommonDevice> listdevice)
        {
            var dicType = new Dictionary<DeviceType, CommonDevice>();
            foreach (CommonDevice device in listdevice)
            {
                if (dicType.ContainsKey(device.Type) == false)
                {
                    dicType[device.Type] = device;
                }
            }
            var info = new DeviceEnumInfo();
            info.IsHdlDevice = false;
            //1包含面板的话,当面板处理
            if (dicType.ContainsKey(DeviceType.OnOffSwitch) == true)
            {
                if (listdevice.Count > 1)
                {
                    info.ConcreteText = this.dicDeviceModelIdEnum["A401"].A官方名字;
                    info.BeloneType = DeviceBeloneType.A按键面板;
                    info.ConcreteType = DeviceConcreteType.ButtonPanel;
                }
                else
                {
                    info.ConcreteText = this.dicDeviceModelIdEnum["A419"].A官方名字;
                    info.BeloneType = DeviceBeloneType.A干接点;
                    info.ConcreteType = DeviceConcreteType.DryContact;
                }
                info.ObjectTypeName = this.dicDeviceModelIdEnum["A401"].A类型名字;//智能面板
            }
            //3包含窗帘的话,当窗帘处理
            else if (dicType.ContainsKey(DeviceType.WindowCoveringDevice) == true)
            {
                info.ConcreteText = this.dicDeviceModelIdEnum["A400"].A官方名字;
                info.BeloneType = DeviceBeloneType.A窗帘;
                info.ConcreteType = DeviceConcreteType.Curtain;
                info.ObjectTypeName = this.dicDeviceModelIdEnum["A400"].A类型名字;//遮阳模块
            }
            //4空气开关
            else if (dicType.ContainsKey(DeviceType.AirSwitch) == true)
            {
                //默认值
                info.ConcreteText = this.dicDeviceModelIdEnum["A408"].A官方名字;
                info.BeloneType = DeviceBeloneType.A开关;
                info.ConcreteType = DeviceConcreteType.AirSwitch;
                info.ObjectTypeName = this.dicDeviceModelIdEnum["A408"].A类型名字;//开关模块
                if (dicType[DeviceType.AirSwitch].DfunctionType == DeviceFunctionType.A开关)
                {
                    info.ConcreteText = this.dicDeviceModelIdEnum["A416"].A官方名字;
                    info.BeloneType = DeviceBeloneType.A开关;
                    info.ConcreteType = DeviceConcreteType.Switch;
                }
                else if (dicType[DeviceType.AirSwitch].DfunctionType == DeviceFunctionType.A插座)
                {
                    info.ConcreteText = this.dicDeviceModelIdEnum["A417"].A官方名字;
                    info.BeloneType = DeviceBeloneType.A插座;
                    info.ConcreteType = DeviceConcreteType.Socket1;
                }
                else if (dicType[DeviceType.AirSwitch].DfunctionType == DeviceFunctionType.A灯光)
                {
                    info.ConcreteText = this.dicDeviceModelIdEnum["A418"].A官方名字;
                    info.BeloneType = DeviceBeloneType.A灯光;
                    info.ConcreteType = DeviceConcreteType.Light;
                }
            }
            //5继电器
            else if (dicType.ContainsKey(DeviceType.OnOffOutput) == true)
            {
                //默认值
                info.ConcreteText = this.dicDeviceModelIdEnum["A403"].A官方名字;
                info.BeloneType = DeviceBeloneType.A继电器;
                info.ConcreteType = DeviceConcreteType.Relay;
                info.ObjectTypeName = this.dicDeviceModelIdEnum["A403"].A类型名字;//开关模块
                if (dicType[DeviceType.OnOffOutput].DfunctionType == DeviceFunctionType.A开关)
                {
                    info.ConcreteText = this.dicDeviceModelIdEnum["A416"].A官方名字;
                    info.BeloneType = DeviceBeloneType.A开关;
                    info.ConcreteType = DeviceConcreteType.Switch;
                }
                else if (dicType[DeviceType.OnOffOutput].DfunctionType == DeviceFunctionType.A插座)
                {
                    info.ConcreteText = this.dicDeviceModelIdEnum["A417"].A官方名字;
                    info.BeloneType = DeviceBeloneType.A插座;
                    info.ConcreteType = DeviceConcreteType.Socket1;
                }
                else if (dicType[DeviceType.OnOffOutput].DfunctionType == DeviceFunctionType.A灯光)
                {
                    info.ConcreteText = this.dicDeviceModelIdEnum["A418"].A官方名字;
                    info.BeloneType = DeviceBeloneType.A灯光;
                    info.ConcreteType = DeviceConcreteType.Light;
                }
            }
            //6调光器
            else if (dicType.ContainsKey(DeviceType.DimmableLight) == true)
            {
                info.ConcreteText = this.dicDeviceModelIdEnum["A404"].A官方名字;
                info.BeloneType = DeviceBeloneType.A调光器;
                info.ConcreteType = DeviceConcreteType.DimmableLight;
                info.ObjectTypeName = this.dicDeviceModelIdEnum["A404"].A类型名字;//调光模块
            }
            //7彩灯
            else if (dicType.ContainsKey(DeviceType.ColorDimmableLight) == true)
            {
                info.ConcreteText = this.dicDeviceModelIdEnum["A410"].A官方名字;
                info.BeloneType = DeviceBeloneType.A彩灯;
                info.ConcreteType = DeviceConcreteType.ColorLight;
                info.ObjectTypeName = this.dicDeviceModelIdEnum["A410"].A类型名字;//调光模块
            }
            //8空调
            else if (dicType.ContainsKey(DeviceType.Thermostat) == true)
            {
                info.ConcreteText = this.dicDeviceModelIdEnum["A406"].A官方名字;
                info.BeloneType = DeviceBeloneType.A空调;
                info.ConcreteType = DeviceConcreteType.AirConditioner;
                info.ObjectTypeName = this.dicDeviceModelIdEnum["A406"].A类型名字;//空调模块
            }
            //9中继器
            else if (dicType.ContainsKey(DeviceType.Repeater) == true)
            {
                info.ConcreteText = this.dicDeviceModelIdEnum["A407"].A官方名字;
                info.BeloneType = DeviceBeloneType.A中继器;
                info.ConcreteType = DeviceConcreteType.Repeater;
                info.ObjectTypeName = this.dicDeviceModelIdEnum["A407"].A类型名字;//系统设备
            }
            //10转换器
            else if (dicType.ContainsKey(DeviceType.Transverter) == true)
            {
                info.ConcreteText = this.dicDeviceModelIdEnum["A409"].A官方名字;
                info.BeloneType = DeviceBeloneType.A转换器;
                info.ConcreteType = DeviceConcreteType.Converter;
                info.ObjectTypeName = this.dicDeviceModelIdEnum["A409"].A类型名字;//转换器
            }
            //11智能门锁
            else if (dicType.ContainsKey(DeviceType.DoorLock) == true)
            {
                info.ConcreteText = this.dicDeviceModelIdEnum["A405"].A官方名字;
                info.BeloneType = DeviceBeloneType.A智能门锁;
                info.ConcreteType = DeviceConcreteType.IntelligentLocks;
                info.ObjectTypeName = this.dicDeviceModelIdEnum["A405"].A类型名字;//智能门锁
            }
            //12包含传感器的话,当传感器处理
            else if (dicType.ContainsKey(DeviceType.IASZone) == true)
            {
                info.BeloneType = DeviceBeloneType.A传感器;
                info.ConcreteType = DeviceConcreteType.Sensor;
                info.ConcreteText = this.dicDeviceModelIdEnum["A402"].A官方名字;
                info.ObjectTypeName = this.dicDeviceModelIdEnum["A402"].A类型名字;//传感器
                //设置传感器具体的类型
                this.SetSensorDeviceSpecificType(ref info, listdevice);
            }
            //13包含温度传感器的话
            else if (dicType.ContainsKey(DeviceType.TemperatureSensor) == true)
            {
                bool temperatrue = false;
                bool humidity = false;
                bool airQualitySensor = false;
                //获取全部的回路
                var listTemp = this.GetDevicesByMac(listdevice[0].DeviceAddr, false);
                foreach (var device in listTemp)
                {
                    if (device is TemperatureSensor)
                    {
                        //空气质量传感器
                        if (device.isAirQualitySensor(device))
                        {
                            airQualitySensor = true;
                        }
                        //温度传感器
                        if (((TemperatureSensor)device).SensorDiv == 1)
                        {
                            temperatrue = true;
                        }
                        //湿度传感器
                        else if (((TemperatureSensor)device).SensorDiv == 2)
                        {
                            humidity = true;
                        }
                    }
                }
                if (temperatrue == true && humidity == true)
                {
                    //设置传感器具体的类型
                    info.BeloneType = DeviceBeloneType.A温湿度传感器;
                    info.ConcreteText = this.dicDeviceModelIdEnum["A411"].A官方名字;
                    info.ConcreteType = DeviceConcreteType.Sensor_TemperatureHumidity;
                }
                else if (temperatrue == true && humidity == false)
                {
                    //设置传感器具体的类型
                    info.BeloneType = DeviceBeloneType.A温度传感器;
                    info.ConcreteText = this.dicDeviceModelIdEnum["A412"].A官方名字;
                    info.ConcreteType = DeviceConcreteType.Sensor_Temperature;
                }
                else if (temperatrue == false && humidity == true)
                {
                    //设置传感器具体的类型
                    info.BeloneType = DeviceBeloneType.A湿度传感器;
                    info.ConcreteText = this.dicDeviceModelIdEnum["A413"].A官方名字;
                    info.ConcreteType = DeviceConcreteType.Sensor_Humidity;
                }
                info.ObjectTypeName = this.dicDeviceModelIdEnum["A411"].A类型名字;//传感器
                //空气质量传感器
                if (airQualitySensor == true)
                {
                    info.ConcreteText = this.dicDeviceModelIdEnum["A422"].A官方名字;
                    info.BeloneType = DeviceBeloneType.A空气质量;
                    info.ConcreteType = DeviceConcreteType.AirQualitySensor;
                    info.ObjectTypeName = this.dicDeviceModelIdEnum["A422"].A类型名字;
                }
            }
            //14新风设备
            else if (dicType.ContainsKey(DeviceType.FreshAir) == true)
            {
                info.ConcreteText = this.dicDeviceModelIdEnum["A414"].A官方名字;
                info.BeloneType = DeviceBeloneType.A新风;
                info.ConcreteType = DeviceConcreteType.Relay_FangyueFreshAirModul;
                info.ObjectTypeName = this.dicDeviceModelIdEnum["A414"].A类型名字;//新风
            }
            //15 PM2.5传感器设备
            else if (dicType.ContainsKey(DeviceType.PMSensor) == true)
            {
                info.ConcreteText = this.dicDeviceModelIdEnum["A415"].A官方名字;
                info.BeloneType = DeviceBeloneType.APM2点5空气质量传感器;
                info.ConcreteType = DeviceConcreteType.Sensor_PMTwoPointFive;
                info.ObjectTypeName = this.dicDeviceModelIdEnum["A415"].A类型名字;//传感器
            }
            //16色温灯
            else if (dicType.ContainsKey(DeviceType.ColorTemperatureLight) == true)
            {
                info.ConcreteText = this.dicDeviceModelIdEnum["A420"].A官方名字;
                info.BeloneType = DeviceBeloneType.A彩灯;
                info.ConcreteType = DeviceConcreteType.ColorLight_Temperature;
                info.ObjectTypeName = this.dicDeviceModelIdEnum["A420"].A类型名字;//调光模块
            }
            //17晾衣架
            else if (dicType.ContainsKey(DeviceType.Airer) == true)
            {
                info.ConcreteText = this.dicDeviceModelIdEnum["A421"].A官方名字;
                info.BeloneType = DeviceBeloneType.A晾衣架;
                info.ConcreteType = DeviceConcreteType.Airer;
                info.ObjectTypeName = this.dicDeviceModelIdEnum["A421"].A类型名字;
            }
            //设备所属类型的翻译名字
            info.BeloneText = info.ConcreteText;
            info.DeviceDefultName = info.ConcreteText;
            return info;
        }
        #endregion
        #region ■ 传感器具体的类型___________________
        /// <summary>
        /// 设置传感器具体的类型
        /// </summary>
        /// <param name="info">自定义设备枚举信息</param>
        /// <param name="listdevice">设备对象</param>
        private void SetSensorDeviceSpecificType(ref DeviceEnumInfo info, List<CommonDevice> listdevice)
        {
            //如果这个设备拥有多个回路的话,我也不知道怎么命名,只能给个默认名字
            if (listdevice.Count > 1)
            {
                return;
            }
            var iasZone = (IASZone)listdevice[0];
            if (iasZone.IasDeviceType == 13)
            {
                //运动传感器
                info.ConcreteType = DeviceConcreteType.Sensor_Motion;
                info.ConcreteText = Language.StringByID(R.MyInternationalizationString.uMotionSensor);
            }
            else if (iasZone.IasDeviceType == 40)
            {
                //烟雾传感器
                info.ConcreteType = DeviceConcreteType.Sensor_Fire;
                info.ConcreteText = Language.StringByID(R.MyInternationalizationString.uSensorFire);
            }
            else if (iasZone.IasDeviceType == 42)
            {
                //水侵传感器
                info.ConcreteType = DeviceConcreteType.Sensor_Water;
                info.ConcreteText = Language.StringByID(R.MyInternationalizationString.uSensorWater);
            }
            else if (iasZone.IasDeviceType == 43)
            {
                //燃气传感器
                info.ConcreteType = DeviceConcreteType.Sensor_CarbonMonoxide;
                info.ConcreteText = Language.StringByID(R.MyInternationalizationString.uSensorCarbonMonoxide);
            }
            else if (iasZone.IasDeviceType == 44)
            {
                //紧急按钮
                info.ConcreteType = DeviceConcreteType.Sensor_EmergencyButton;
                info.ConcreteText = Language.StringByID(R.MyInternationalizationString.uSensorEmergencyButton);
            }
            else if (iasZone.IasDeviceType == 277)
            {
                //钥匙扣
                info.ConcreteType = DeviceConcreteType.Sensor_Keyfob;
                info.ConcreteText = Language.StringByID(R.MyInternationalizationString.uKeyfob);
            }
            else if (iasZone.IasDeviceType == 21 || iasZone.IasDeviceType == 22)
            {
                //门窗传感器
                info.ConcreteType = DeviceConcreteType.Sensor_DoorWindow;
                info.ConcreteText = Language.StringByID(R.MyInternationalizationString.uSensorDoorWindow);
            }
        }
        #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传感器);
            //其他的看着办呗,都是排在后面的,都归为这个属性
            list.Add(DeviceBeloneType.A未知设备);
            return list;
        }
        #endregion
        #region ■ 晾衣架设备时间配置___________________________
        /// <summary>
        ///  设置晾衣架风干,烘干和消毒时间(设置失败时,会显示信息)
        ///  time:设置的分钟
        /// </summary>
        /// <param name="litdevice">设备对象(MAC地址必须要相同)</param>
        public async Task<bool> SetAirerTimeDevice(List<CommonDevice> listdevice, Airer airer, int time, int controlEpoint)
        {
            //虚拟住宅的话,不需要
            if (Config.Instance.Home.IsVirtually == false)
            {
                foreach (var dev in listdevice)
                {
                    //设置属性信息
                    CommonDevice.SetWritableValueData dataT = new CommonDevice.SetWritableValueData();
                    dataT.Undivided = 0;
                    dataT.AttributeId = 16385;
                    dataT.AttributeDataType = 33;
                    dataT.AttributeData = time;
                    dev.DeviceEpoint = controlEpoint;//暂时换成晾衣架对应的控制端点
                    var result = await dev.SetWritableValueAsync(6, dataT);
                    if (result == null || result.setWritableValueResponData == null || result.setWritableValueResponData.Status != 0)
                    {
                        //设置失败
                        if (dev.DeviceEpoint == 3)
                        {
                            if (airer.WindTime != 0)
                            {
                                airer.WindTime = 0;
                            }
                            string msg = Language.StringByID(R.MyInternationalizationString.AirerWindTimeFailed);
                            //拼接上【网关回复超时】的Msg
                            msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result);
                            dev.DeviceEpoint = 1;//将晾衣架对应的控制端点还原成1端点
                            this.ShowErrorMsg(msg);
                            return false;
                        }
                        if (dev.DeviceEpoint == 4)
                        {
                            if (airer.DryTime != 0)
                            {
                                airer.DryTime = 0;
                            }
                            string msg = Language.StringByID(R.MyInternationalizationString.AirerDryTimeFailed);
                            //拼接上【网关回复超时】的Msg
                            msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result);
                            dev.DeviceEpoint = 1;//将晾衣架对应的控制端点还原成1端点
                            this.ShowErrorMsg(msg);
                            return false;
                        }
                        if (dev.DeviceEpoint == 5)
                        {
                            if (airer.DisinfectTime != 0)
                            {
                                airer.DisinfectTime = 0;
                            }
                            string msg = Language.StringByID(R.MyInternationalizationString.AirerDisinfectTimeFailed);
                            //拼接上【网关回复超时】的Msg
                            msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result);
                            this.ShowErrorMsg(msg);
                            dev.DeviceEpoint = 1;//将晾衣架对应的控制端点还原成1端点
                            return false;
                        }
                    }
                }
            }
            return true;
        }
        #endregion
        #region ■ 一般方法___________________________
        /// <summary>
        /// 从缓存变量检测设备是否在线
        /// </summary>
        /// <param name="i_device"></param>
        /// <param name="checkAll">是否检测全部回路</param>
        /// <returns></returns>
        public bool CheckDeviceIsOnline(CommonDevice i_device, bool checkAll = true)
        {
            if (checkAll == false)
            {
                //不检测全部回路
                return i_device.IsOnline == 1 || i_device.IsOnline == 2;
            }
            var listDevice = this.GetDevicesByMac(i_device.DeviceAddr, false);
            foreach (var device in listDevice)
            {
                //0:离线 1:在线 2:正在刷新状态
                bool statu = i_device.IsOnline == 1 || i_device.IsOnline == 2;
                if (statu == true)
                {
                    //有一个回路在线,即在线
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 判断该设备是否可以显示在主页
        /// </summary>
        /// <param name="i_device"></param>
        /// <returns></returns>
        public bool CanShowInHomeHomeMainPage(CommonDevice i_device)
        {
            if (i_device == null || i_device.Type == DeviceType.OnOffSwitch)//干接点
            {
                //这个设备不见了
                return false;
            }
            if (i_device.Type == DeviceType.OnOffOutput || i_device.Type == DeviceType.AirSwitch)
            {
                //2020.03.23追加式样:未指定类型的继电器,不显示
                if (i_device.DfunctionType == DeviceFunctionType.A未定义
                    || i_device.DfunctionType == DeviceFunctionType.A不指定)
                {
                    return false;
                }
            }
            //2020.03.30追加式样:如果是面板的温度探头,不显示
            else if (i_device.Type == DeviceType.TemperatureSensor && ((TemperatureSensor)i_device).SensorDiv == 1)
            {
                var myInfoType = LocalDevice.Current.GetMyDeviceEnumInfo(new List<CommonDevice>() { i_device });
                if (myInfoType.BeloneType == DeviceBeloneType.A按键面板)
                {
                    return false;
                }
            }
            else if (i_device.Type == DeviceType.FreshAirHumiditySensor)
            {
                //新风的湿度传感器不显示
                return false;
            }
            if (((int)i_device.Type).ToString() == i_device.Type.ToString())
            {
                //此设备id在DeviceType里面还没有注册,则不能显示出来
                return false;
            }
            //如果是新风面板或环境面板,则都不显示任何回路
            var myInfoTypeTemp = LocalDevice.Current.GetMyDeviceEnumInfo(new List<CommonDevice>() { i_device });
            if (myInfoTypeTemp.ConcreteType == DeviceConcreteType.ButtonPanel_FangyueFreshAir || myInfoTypeTemp.ConcreteType == DeviceConcreteType.ButtonPanel_SimpleEnvironment)
            {
                return false;
            }
            else if (myInfoTypeTemp.ConcreteType == DeviceConcreteType.ButtonPanel_SimpleMultifunction)
            {
                if (i_device.Type != DeviceType.OnOffOutput)
                {
                    return false;
                }
            }
            return true;
        }
        /// <summary>
        /// 判断是不是河东的设备
        /// </summary>
        /// <param name="device"></param>
        /// <returns></returns>
        public bool IsHdlDevice(CommonDevice device)
        {
            return device.ManufacturerName == "HDL";
        }
        /// <summary>
        /// 是否是Mini夜灯
        /// </summary>
        /// <param name="device"></param>
        /// <returns></returns>
        public bool IsMiniLight(CommonDevice device)
        {
            return device.DriveCode != 0 && device.Type == DeviceType.ColorTemperatureLight;
        }
        /// <summary>
        /// 获取设备的唯一主键
        /// </summary>
        /// <param name="device"></param>
        /// <returns></returns>
        public string GetDeviceMainKeys(CommonDevice device)
        {
            return this.GetDeviceMainKeys(device.DeviceAddr, device.DeviceEpoint);
        }
        /// <summary>
        /// 获取设备的唯一主键
        /// </summary>
        /// <param name="DeviceAddr">MAC地址</param>
        /// <param name="DeviceEpoint">端口号</param>
        /// <returns></returns>
        public string GetDeviceMainKeys(string DeviceAddr, int DeviceEpoint)
        {
            return DeviceAddr + "_" + DeviceEpoint;
        }
        /// <summary>
        /// 交换设备的模块ID
        /// </summary>
        /// <param name="modeId">模块ID</param>
        /// <returns></returns>
        public bool ChangedDeviceModeId(ref string modeId)
        {
            if (this.dicDeviceModelIdChanged.ContainsKey(modeId) == false)
            {
                return false;
            }
            modeId = this.dicDeviceModelIdChanged[modeId];
            return true;
        }
        /// <summary>
        /// 获取模块ID所指向的各种名字信息
        /// </summary>
        /// <param name="strKey"></param>
        /// <returns></returns>
        public DeviceNameContent GetDeviceModelIdNameInfo(string strKey)
        {
            if (this.dicDeviceModelIdEnum.ContainsKey(strKey) == false)
            {
                return new DeviceNameContent();
            }
            return this.dicDeviceModelIdEnum[strKey];
        }
        /// <summary>
        /// 附加设备的版本代号(返回Ver.XXX)
        /// </summary>
        /// <param name="versionValue">版本号</param>
        /// <returns></returns>
        public string AppendVersion(int versionValue)
        {
            //10101 3个byte  显示为:ver.1.01.01
            //中间那个byte为奇数时,代表是测试版本,在显示最新版本时,不需要显示,但是在历史版本那里可以显示
            //中间那个byte为偶数时, 代表是正式版本
            //直接是10进制
            string txt10 = Convert.ToString(versionValue).PadLeft(6, '0');
            //这个是第一位
            int value1 = Convert.ToInt32(txt10.Substring(0, 2));
            //这个是第二位
            string value2 = txt10.Substring(2, 2);
            //这个是第三位
            string value3 = txt10.Substring(4, 2);
            //Ver.
            string ver = Language.StringByID(R.MyInternationalizationString.uVersionAbbreviation);
            return ver + value1 + "." + value2 + "." + value3;
        }
        /// <summary>
        /// 显示错误信息窗口
        /// </summary>
        /// <param name="msg"></param>
        private void ShowErrorMsg(string msg)
        {
            HdlThreadLogic.Current.RunMain(() =>
            {
                var contr = new ShowMsgControl(Phone.ShowMsgType.Error, msg);
                contr.Show();
            });
        }
        /// <summary>
        /// 显示Tip信息窗口
        /// </summary>
        /// <param name="msg"></param>
        private void ShowTipMsg(string msg)
        {
            HdlThreadLogic.Current.RunMain(() =>
            {
                var contr = new ShowMsgControl(Phone.ShowMsgType.Tip, msg);
                contr.Show();
            });
        }
        /// <summary>
        /// 处理网关返回的湿度值
        /// </summary>
        /// <param name="value">网关返回的属性值</param>
        public decimal AdjustHumidityValue(int value)
        {
            if (value == 0)
            {
                //0%
                return 0;
            }
            //不会出现负数
            else
            {
                //小数点需要一位
                string strValue = value.ToString();
                strValue = strValue.Substring(0, strValue.Length - 1);
                //记录温度
                return strValue == string.Empty ? 0m : Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
            }
        }
        /// <summary>
        /// 处理网关返回的温度值
        /// </summary>
        /// <param name="value">网关返回的属性值</param>
        public decimal AdjustTemperatrueValue(int value)
        {
            if (value == 0)
            {
                //0℃
                return 0;
            }
            else if (value > 32767)
            {
                //负数(特殊处理)
                string strValue = (value - 65536).ToString();
                //小数点需要一位
                strValue = strValue.Substring(0, strValue.Length - 1);
                //记录温度
                return strValue == string.Empty ? 0m : Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
            }
            else
            {
                //小数点需要一位
                string strValue = value.ToString();
                strValue = strValue.Substring(0, strValue.Length - 1);
                //记录温度
                return strValue == string.Empty ? 0m : Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
            }
        }
        #endregion
        //----------------------------------分割线(自定义接口)---------------------------------------------
        #region ■ 获取设备列表的接口_________________
        /// <summary>
        /// <para>从网关重新获取设备列表(返回的设备为虚拟出来的),一次性全部获取</para>
        /// <para>statu状态 -1:异常,会返回null, 1:没有异常, 2:数据接收不全</para>
        /// </summary>
        /// <param name="gatewayId">网关id</param>
        /// <param name="statu">状态-> -1:异常,会返回null, 1:没有异常, 2:数据接收不全</param>
        /// <param name="useLocalConnect">是否使用本地连接发送</param>
        /// <param name="mode">是否显示错误</param>
        /// <returns></returns>
        public List<CommonDevice> GetDeviceListFromGateway(string gatewayId, ref int statu, bool useLocalConnect, ShowErrorMode mode = ShowErrorMode.YES)
        {
            //需要变更功能类型的回路
            var listFucDevice = new List<CommonDevice>();
            //是否达成中断的时机
            bool canBreak = false;
            //超时时间
            int TimeOut = 0;
            //当前设备接收数
            int nowReceiveCount = 0;
            //设备总数
            int deviceCount = -1;
            //设备列表
            var listDevice = new List<CommonDevice>();
            //网关里面有可能会有重复的回路
            var listCheck = new HashSet<string>();
            ///Mac的端点总数
            var dicMacPointCount = new Dictionary<string, int>();
            HdlGatewayReceiveLogic.Current.AddGatewayReceiveEvent(gatewayId, (topic, message) =>
            {
                if (topic == gatewayId + "/DeviceInfoRespon")
                {
                    lock (listDevice)
                    {
                        TimeOut = 0;
                        var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
                        var totalNum = Convert.ToInt32(jobject["Data"]["TotalNum"].ToString());
                        if (totalNum == 0)
                        {
                            //这个网关没有设备
                            canBreak = true;
                            return;
                        }
                        if (deviceCount == -1)
                        {
                            //设置需要接收多少个设备
                            deviceCount = totalNum;
                        }
                        nowReceiveCount++;
                        //设备回路数收集
                        string deviceMac = jobject.Value<string>("DeviceAddr");
                        if (dicMacPointCount.ContainsKey(deviceMac) == false)
                        {
                            dicMacPointCount[deviceMac] = 0;
                        }
                        dicMacPointCount[deviceMac]++;
                        var deviceID = (DeviceType)jobject.Value<int>("Device_ID");
                        //根据设备类型创建设备对象的实例
                        var device = this.NewDeviceObjectByDeviceId(deviceID, jobject, gatewayId);
                        if (device != null)
                        {
                            string mainkeys = this.GetDeviceMainKeys(device);
                            //网关里面有可能会有重复的回路
                            if (listCheck.Contains(mainkeys) == false)
                            {
                                listDevice.Add(device);
                                listCheck.Add(mainkeys);
                                //刷新一下本地缓存
                                var localDevice = this.GetDevice(mainkeys);
                                var tempDevice = localDevice == null ? device : localDevice;
                                //如果这个设备ID变更了的话
                                bool typeNotEquals = localDevice != null && deviceID != localDevice.Type;
                                if (typeNotEquals == true)
                                {
                                    //重新New这个对象
                                    typeNotEquals = this.ReNewDeviceOnTypeIsChanged(localDevice, deviceID);
                                    //重新再次获取对象
                                    tempDevice = this.GetDevice(mainkeys);
                                }
                                //刷新属性
                                this.SetDeviceInfoToMain(tempDevice, device);
                                if (this.RefreshDeviceFunctionType(tempDevice, device, false) == true)
                                {
                                    //需要发送功能类型给网关
                                    listFucDevice.Add(tempDevice);
                                }
                                if (typeNotEquals == true)
                                {
                                    //重新生成缓存
                                    tempDevice.ReSave();
                                    HdlAutoBackupLogic.AddOrEditorFile(tempDevice.FilePath);
                                    //全部主页菜单需要刷新
                                    Phone.UserView.UserPage.Instance.RefreshAllForm = true;
                                }
                            }
                        }
                    }
                    if (nowReceiveCount >= deviceCount)
                    {
                        //设备全部接收完成
                        canBreak = true;
                    }
                }
            });
            //发送命令
            var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 93 } };
            HdlGatewayLogic.Current.SendJobjectData(gatewayId, "GetDeviceInfo", jObject.ToString(), useLocalConnect);
            while (canBreak == false && TimeOut <= 30)
            {
                System.Threading.Thread.Sleep(100);
                TimeOut++;
            }
            //移除网关监听事件
            HdlGatewayReceiveLogic.Current.RemoveGatewayReceiveEvent();
            if (TimeOut > 30)
            {
                if (listDevice.Count > 0)
                {
                    //网络不稳定,设备列表信息缺损,则采用按序号获取设备
                    statu = this.GetDeviceListFromGatewayByMac(gatewayId, ref listDevice, ref listFucDevice, dicMacPointCount, useLocalConnect);
                    if (statu == 2)
                    {
                        //网络不稳定,设备列表信息缺损
                        string msg = Language.StringByID(R.MyInternationalizationString.uNetworkUnStableAndDeviceInfoIsNotFull);
                        this.ShowTipMsg(msg);
                    }
                }
                else
                {
                    if (mode == ShowErrorMode.YES)
                    {
                        //获取设备列表失败
                        //[XXXX]网关回复超时,请稍后再试
                        string msg = Language.StringByID(R.MyInternationalizationString.uGetDeviceListFail);
                        var localGw = HdlGatewayLogic.Current.GetLocalGateway(gatewayId);
                        if (localGw != null)
                        {
                            msg += "\r\n[" + HdlGatewayLogic.Current.GetGatewayName(localGw).ToString() + "]";
                        }
                        msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, null, "回复超时", false);
                        this.ShowTipMsg(msg);
                    }
                    statu = -1;
                    return null;
                }
            }
            else
            {
                statu = 1;
            }
            if (listFucDevice.Count > 0)
            {
                //如果存在需要变更的功能类型
                HdlThreadLogic.Current.RunThread(() =>
                {
                    for (int i = 0; i < listFucDevice.Count; i++)
                    {
                        this.SendDeviceFunctionTypeToGateway(listFucDevice[i], listFucDevice[i].DfunctionType);
                        System.Threading.Thread.Sleep(100);
                    }
                });
            }
            return listDevice;
        }
        /// <summary>
        /// 根据序号获取设备列表信息(只有在设备缺损的时候才调用这个函数) 1:正常 2:设备缺损
        /// </summary>
        /// <param name="gatewayId">网关id</param>
        /// <param name="listDevice">存储的设备对象集合</param>
        /// <param name="listFucDevice">需要变更功能类型的回路</param>
        /// <param name="dicNowPointCount">当前已经获取的Mac的端点总数</param>
        /// <param name="useLocalConnect">是否使用本地连接发送</param>
        /// <returns></returns>
        public int GetDeviceListFromGatewayByMac(string gatewayId, ref List<CommonDevice> listDevice, ref List<CommonDevice> listFucDevice, Dictionary<string, int> dicNowPointCount, bool useLocalConnect)
        {
            //首先先获取设备的mac列表
            var jObject33 = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 87 } };
            var result = HdlGatewayLogic.Current.SendJobjectDataToGateway(gatewayId, "GetStatusRecord", jObject33.ToString(), "DeviceMACDeviceListRespon", 3, useLocalConnect);
            if (result.ErrorMsgDiv == -1 || result.ErrorMsgDiv == 0)
            {
                //如果网关没有对这个主题做出回应的话,则说明它是一个旧网关
                return 2;
            }
            var resultMac = Newtonsoft.Json.JsonConvert.DeserializeObject<ReceiptMacResult>(result.ReceiptData);
            //设备列表
            var listDeviceJson = new List<Newtonsoft.Json.Linq.JObject>();
            int statu = 0;
            foreach (var macInfo in resultMac.MACDeviceList)
            {
                if (dicNowPointCount.ContainsKey(macInfo.MacAddr) == true
                    && dicNowPointCount[macInfo.MacAddr] == macInfo.EPTotalNum)
                {
                    //如果这个mac的端点数一致,则说明已经接收齐了,不需要再次接收
                    continue;
                }
                //从网关读取指定Mac下的设备列表
                var jsonData = this.ReadDeviceListByMacFromGateway(gatewayId, macInfo.MacAddr, useLocalConnect, ref statu);
                foreach (var myData in jsonData)
                {
                    listDeviceJson.Add(myData);
                }
                if (statu != 1)
                {
                    //没有正常完成,则中断
                    break;
                }
            }
            //转化为设备列表
            var listTempDevice = this.ConvertJObjectToDevice(listDeviceJson, gatewayId, ref listFucDevice);
            for (int i = 0; i < listTempDevice.Count; i++)
            {
                listDevice.Add(listTempDevice[i]);
            }
            return statu == 1 ? 1 : 2;
        }
        /// <summary>
        /// 在设备Type变更时,重新New这个设备对象(仅限在刷新设备列表使用,并且本地需要存在)
        /// </summary>
        /// <param name="oldLocalDevice">原来的本地对象</param>
        /// <param name="newDeviceType">新的设备Type</param>
        private bool ReNewDeviceOnTypeIsChanged(CommonDevice oldLocalDevice, DeviceType newDeviceType)
        {
            //先获取本地这个旧设备对象的json
            string oldFile = System.IO.Path.Combine(Config.Instance.FullPath, oldLocalDevice.FilePath);
            var deviceData = HdlFileLogic.Current.ReadFileTextContent(oldFile);
            if (deviceData == null)
            {
                //应该不会
                return false;
            }
            //根据新Type重新New对象
            var newDevice = CommonDevice.CommonDeviceByByteString((int)newDeviceType, deviceData);
            if (newDevice == null)
            {
                //好像有点可能
                return false;
            }
            //删除本地文件
            HdlFileLogic.Current.DeleteFile(oldFile);
            HdlAutoBackupLogic.DeleteFile(oldLocalDevice.FilePath);
            //重新生成文件
            if (newDevice.IsCustomizeImage == false)
            {
                newDevice.IconPath = string.Empty;
            }
            string mainkey = this.GetDeviceMainKeys(newDevice);
            lock (dicAllDevice)
            {
                this.dicAllDevice[mainkey] = newDevice;
            }
            return true;
        }
        /// <summary>
        /// 接收设备Mac列表的类
        /// </summary>
        private class ReceiptMacResult
        {
            /// <summary>
            /// mac总数
            /// </summary>
            public int MACDeviceTotalNum = 0;
            /// <summary>
            /// 设备mac列表
            /// </summary>
            public List<MacResult> MACDeviceList = new List<MacResult>();
        }
        /// <summary>
        /// 设备Mac的内容
        /// </summary>
        private class MacResult
        {
            /// <summary>
            /// 设备Mac
            /// </summary>
            public string MacAddr = string.Empty;
            /// <summary>
            /// mac端点的总数
            /// </summary>
            public int EPTotalNum = 0;
        }
        #endregion
        #region ■ 创建新设备对象相关_________________
        /// <summary>
        /// 根据设备类型创建设备对象的实例(数据在DeviceInfo中)
        /// </summary>
        /// <param name="deviceType">设备类型</param>
        /// <param name="jobject">主题Data</param>
        /// <param name="gwid">网关id</param>
        /// <returns></returns>
        private CommonDevice NewDeviceObjectByDeviceId(DeviceType deviceType, Newtonsoft.Json.Linq.JObject jobject, string gwid)
        {
            //根据设备类型创建设备对象的实例
            CommonDevice device = this.NewDeviceObjectByDeviceId(deviceType, jobject.Value<int>("Epoint"));
            if (device == null)
            {
                return null;
            }
            //设置设备属性类
            device.DeviceInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonDevice.DeviceInfoData>(jobject["Data"].ToString());
            if (device.DeviceInfo == null)
            {
                return null;
            }
            //设置设备主键类
            this.SetNewDeviceMainKeys(device, jobject);
            device.CurrentGateWayId = gwid;
            return device;
        }
        /// <summary>
        /// 将DeviceInfo的属性设置到主属性中
        /// </summary>
        /// <param name="mainDevice">主设备对象</param>
        /// <param name="device">设置源设备对象</param>
        public void SetDeviceInfoToMain(CommonDevice mainDevice, CommonDevice device)
        {
            mainDevice.DeviceName = device.DeviceInfo.MacName;
            mainDevice.DeviceEpointName = device.DeviceInfo.DeviceName;
            mainDevice.CurrentGateWayId = device.CurrentGateWayId;
            mainDevice.ZigbeeType = device.DeviceInfo.ZigbeeType;
            mainDevice.IsOnline = device.DeviceInfo.IsOnline;
            mainDevice.DriveCode = device.DeviceInfo.DriveCode;
            mainDevice.IasDeviceType = device.DeviceInfo.DeviceType;
            mainDevice.Profile = device.DeviceInfo.Profile;
            mainDevice.Type = device.Type;
            mainDevice.DeviceID = (int)device.Type;
            //固件版本
            mainDevice.ImgVersion = device.DeviceInfo.ImgVersion;
            //硬件版本
            mainDevice.HwVersion = device.DeviceInfo.HwVersion;
            //镜像ID
            mainDevice.ImgTypeId = device.DeviceInfo.ImgTypeId;
            //厂商名称
            mainDevice.ManufacturerName = device.DeviceInfo.ManufacturerName;
            //模块ID
            mainDevice.ModelIdentifier = device.DeviceInfo.ModelIdentifier;
            //序列号
            mainDevice.SerialNumber = device.DeviceInfo.ProductCode;
            //设备功能类型
            mainDevice.DfunctionType = (DeviceFunctionType)device.DeviceInfo.FunctionType;
            if (mainDevice.DfunctionType == DeviceFunctionType.A不指定)
            {
                //式样变更:功能类型不允许选择为空 网关默认的FunctionType为0
                mainDevice.DfunctionType = DeviceFunctionType.A未定义;
            }
            mainDevice.InClusterList.Clear();
            mainDevice.InClusterList.AddRange(device.DeviceInfo.InClusterList);
            mainDevice.OutClusterList.Clear();
            mainDevice.OutClusterList.AddRange(device.DeviceInfo.OutClusterList);
            mainDevice.AttributeStatus.Clear();
            mainDevice.AttributeStatus.AddRange(device.DeviceInfo.AttributeStatus);
            //如果是温度传感器
            if (mainDevice.Type == DeviceType.TemperatureSensor)
            {
                //输出族 1026:温度传感器 1029:湿度传感器
                foreach (var data in mainDevice.OutClusterList)
                {
                    if (data.OutCluster == 1029)
                    {
                        ((TemperatureSensor)mainDevice).SensorDiv = 2;
                    }
                    else if (data.OutCluster == 1026)
                    {
                        ((TemperatureSensor)mainDevice).SensorDiv = 1;
                    }
                }
                if (((TemperatureSensor)mainDevice).SensorDiv == 1)
                {
                    //变更一下温度的值
                    foreach (var data in mainDevice.AttributeStatus)
                    {
                        if (data.ClusterId == 1026
                           && data.AttributeId == (int)AttriButeId.MeasuredValue)
                        {
                            //处理网关返回的温度值
                            decimal temperatrue = this.AdjustTemperatrueValue(data.AttributeData);
                            ((TemperatureSensor)mainDevice).currentTemperature = temperatrue;
                        }
                    }
                }
                else if (((TemperatureSensor)mainDevice).SensorDiv == 2)
                {
                    //变更一下湿度的值
                    foreach (var data in mainDevice.AttributeStatus)
                    {
                        if (data.ClusterId == 1029
                           && data.AttributeId == (int)AttriButeId.MeasuredValue)
                        {
                            //处理网关返回的温度值
                            decimal humidity = this.AdjustHumidityValue(data.AttributeData);
                            ((TemperatureSensor)mainDevice).currentHumidity = humidity;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 根据设备Type创建对应的设备对象
        /// </summary>
        /// <param name="deviceType">设备Type</param>
        /// <param name="DeviceEpoint">设备端点(没有什么特殊需求的话,填0即可)</param>
        /// <returns></returns>
        public CommonDevice NewDeviceObjectByDeviceId(DeviceType deviceType, int DeviceEpoint)
        {
            //根据设备类型创建设备对象的实例
            var device = CommonDevice.CommonDeviceByByteString((int)deviceType, "{\"DeviceID\":" + (int)deviceType + ",\"DeviceEpoint\":" + DeviceEpoint + "}");
            if (device == null)
            {
                return null;
            }
            device.DeviceEpointName = string.Empty;
            device.DeviceName = string.Empty;
            return device;
        }
        /// <summary>
        /// 给新设备设置主键属性
        /// </summary>
        /// <param name="device">设备对象</param>
        /// <param name="jobject">主题Data</param>
        public void SetNewDeviceMainKeys(CommonDevice device, Newtonsoft.Json.Linq.JObject jobject)
        {
            //设置设备主键类
            device.DeviceID = jobject.Value<int>("Device_ID");
            device.DeviceAddr = jobject.Value<string>("DeviceAddr");
            device.DeviceEpoint = jobject.Value<int>("Epoint");
        }
        #endregion
        #region ■ 刷新设备功能类型___________________
        /// <summary>
        /// 刷新设备的功能类型,如果返回true,代表需要另外调用【SendDeviceFunctionTypeToGateway】函数,sendFucType为true时自动调用
        /// </summary>
        /// <param name="mainDevice">主设备对象</param>
        /// <param name="device">设置源设备对象</param>
        /// <param name="sendFucType">当需要变更时,是否发送功能类型到网关(此变量给获取设备列表有特殊含义)</param>
        public bool RefreshDeviceFunctionType(CommonDevice mainDevice, CommonDevice device, bool sendFucType)
        {
            //如果是调光器
            if (mainDevice.Type == DeviceType.DimmableLight)
            {
                if (mainDevice.IsCustomizeImage == false)
                {
                    mainDevice.IconPath = "Device/Light.png";
                }
                mainDevice.DfunctionType = DeviceFunctionType.A灯光;
                if (device.DeviceInfo != null && device.DeviceInfo.FunctionType != (int)DeviceFunctionType.A灯光)
                {
                    //调光器固定灯光
                    if (sendFucType == true)
                    {
                        this.SendDeviceFunctionTypeToGateway(mainDevice, DeviceFunctionType.A灯光);
                    }
                    return true;
                }
            }
            //如果是色温灯
            else if (mainDevice.Type == DeviceType.ColorTemperatureLight)
            {
                if (mainDevice.IsCustomizeImage == false)
                {
                    mainDevice.IconPath = "Device/ColorLightTemperature.png";
                }
                mainDevice.DfunctionType = DeviceFunctionType.A灯光;
                if (device.DeviceInfo != null && device.DeviceInfo.FunctionType != (int)DeviceFunctionType.A灯光)
                {
                    //色温灯固定灯光
                    if (sendFucType == true)
                    {
                        this.SendDeviceFunctionTypeToGateway(mainDevice, DeviceFunctionType.A灯光);
                    }
                    return true;
                }
            }
            //如果是三路继电器的回路的话,默认为灯光
            else if (mainDevice.Type == DeviceType.OnOffOutput)
            {
                //根据功能类型,重新设置设备回路图标
                this.ResetIconPathByDeviceFunctionType(mainDevice);
                //2020.05.13变更:继电器都默认为灯光
                if (mainDevice.DfunctionType == DeviceFunctionType.A未定义)
                {
                    mainDevice.DfunctionType = DeviceFunctionType.A灯光;
                    if (device.DeviceInfo != null && device.DeviceInfo.FunctionType != (int)DeviceFunctionType.A灯光)
                    {
                        //继电器默认为灯光
                        if (sendFucType == true)
                        {
                            this.SendDeviceFunctionTypeToGateway(mainDevice, DeviceFunctionType.A灯光);
                        }
                        return true;
                    }
                }
            }
            //如果是空气开关的话
            else if (mainDevice.Type == DeviceType.AirSwitch)
            {
                //根据功能类型,重新设置设备回路图标
                this.ResetIconPathByDeviceFunctionType(mainDevice);
                //空气开关默认为开关
                if (mainDevice.DfunctionType == DeviceFunctionType.A未定义)
                {
                    mainDevice.DfunctionType = DeviceFunctionType.A开关;
                    if (device.DeviceInfo != null && device.DeviceInfo.FunctionType != (int)DeviceFunctionType.A开关)
                    {
                        //空气开关默认为开关
                        if (sendFucType == true)
                        {
                            this.SendDeviceFunctionTypeToGateway(mainDevice, DeviceFunctionType.A开关);
                        }
                        return true;
                    }
                }
            }
            //如果是彩灯的话
            else if (mainDevice.Type == DeviceType.ColorDimmableLight)
            {
                if (mainDevice.IsCustomizeImage == false)
                {
                    mainDevice.IconPath = "Device/ColorLight.png";
                }
                mainDevice.DfunctionType = DeviceFunctionType.A灯光;
                if (device.DeviceInfo != null && device.DeviceInfo.FunctionType != (int)DeviceFunctionType.A灯光)
                {
                    //彩灯默认为开关
                    if (sendFucType == true)
                    {
                        this.SendDeviceFunctionTypeToGateway(mainDevice, DeviceFunctionType.A开关);
                    }
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 根据功能类型,重新设置设备回路图标
        /// </summary>
        /// <param name="device"></param>
        private void ResetIconPathByDeviceFunctionType(CommonDevice device)
        {
            if (device.IsCustomizeImage == true)
            {
                return;
            }
            if (device.DfunctionType == DeviceFunctionType.A开关)
            {
                device.IconPath = "Device/Switch.png";
            }
            else if (device.DfunctionType == DeviceFunctionType.A插座)
            {
                device.IconPath = "Device/Socket1.png";
            }
            else
            {
                device.IconPath = "Device/Light.png";
            }
        }
        #endregion
        //----------------------------------分割线(设备模块ID)---------------------------------------------
        #region ■ 自定义设备模块ID___________________
        /// <summary>
        /// 初始化设备的模块ID的枚举
        /// </summary>
        private void InitDeviceModelIdEnum()
        {
            //*********************************************************************
            //新设备添加方法:
            //1、在最下面添加 DeviceConcreteType ,然后是 DeviceBeloneType
            //2、以设备的具体类型为名字DeviceConcreteType(去掉【-】)添加设备的【所属图片】,【真实物理图片】。回路图片需要特殊处理
            //3、如果需要共有图片,则在这个函数的最底下添加
            //4、显示的模块ID需要交换的话,则在这个函数的最底下添加
            //*********************************************************************
            if (this.dicDeviceModelIdEnum != null)
            {
                return;
            }
            this.dicDeviceModelIdEnum = new Dictionary<string, DeviceNameContent>();
            var listText = this.GetDeviceNameFileContent();
            foreach (var dataText in listText)
            {
                if (dataText == string.Empty || dataText.StartsWith(";") == true)
                {
                    //这是注释
                    continue;
                }
                string[] strArry1 = dataText.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                if (strArry1.Length != 2)
                {
                    //非法设置
                    continue;
                }
                string[] strArry2 = strArry1[1].Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
                if (strArry2.Length < 6)
                {
                    //非法设置
                    continue;
                }
                var content = new DeviceNameContent();
                content.ConcreteTypeValue = Convert.ToInt32(strArry2[0].Trim());
                content.A官方名字 = strArry2[1].Trim();
                content.A备注名字 = strArry2[2].Trim();
                content.BeloneTypeValue = Convert.ToInt32(strArry2[3].Trim());
                content.A所属名字 = strArry2[4].Trim();
                content.A类型名字 = strArry2[5].Trim();
                this.dicDeviceModelIdEnum[strArry1[0].Trim()] = content;
            }
            //✩✩✩✩✩需要交换的模块ID✩✩✩✩✩
            this.dicDeviceModelIdChanged = new Dictionary<string, string>();
            //=========★★安防类传感器类★★=========
            this.dicDeviceModelIdChanged["MULTI-GASE--EA07"] = "MSG01/M-ZB.10";//燃气传感器
            this.dicDeviceModelIdChanged["MULTI-MECI--EA01"] = "MSDC01/M-ZB.10";//门窗磁传感器
            this.dicDeviceModelIdChanged["MULTI-FIRE--EA05"] = "MSS01/M-ZB.10";//烟雾传感器
            this.dicDeviceModelIdChanged["MULTI-MOTI--EA04"] = "MSPIR01/M-ZB.10";//红外传感器
            this.dicDeviceModelIdChanged["MULTI-WATE--EA02"] = "MSW01/M-ZB.10";//水浸传感器
            this.dicDeviceModelIdChanged["MULTI-BURO--EA06"] = "MBU01/M-ZB.10";//紧急按键
            //✩✩✩✩✩需要共有的图片对象✩✩✩✩✩
            //两者都是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 Pro的门锁图片 沿用 H06C的图标
            this.dicPictrueShard["IntelligentLocks_AT1TF"] = "IntelligentLocks_H06C";//全自动智能门锁 AT-1TF图片 沿用 H06C的图标
            this.dicPictrueShard["IntelligentLocks_AT1F"] = "IntelligentLocks_H06C";//全自动智能门锁 AT-1F图片 沿用 H06C的图标
            this.dicPictrueShard["IntelligentLocks_AT1T"] = "IntelligentLocks_H06C";//全自动智能门锁 AT-1T图片 沿用 H06C的图标
            this.dicPictrueShard["IntelligentLocks_HAT1B"] = "IntelligentLocks_H06C";//智能门锁 HAT-1B图片 沿用 H06C的图标
            this.dicPictrueShard["IntelligentLocks_HAT1T"] = "IntelligentLocks_H06C";//智能门锁 HAT-1T图片 沿用 H06C的图标
            this.dicPictrueShard["IntelligentLocks_ATFT"] = "IntelligentLocks_H06C";//智能门锁 HAT-1T图片 沿用 H06C的图标
            this.dicPictrueShard["Relay_FangyueFreshAirModul"] = "Relay_ThreeLoad";//方悦新风小模块图片 沿用 3路继电器的图标
            this.dicPictrueShard["ButtonPanel_FourButtonScene"] = "ButtonPanel_Four";//简约4按键场景面板 沿用 4按键面板的图标
        }
        /// <summary>
        /// 获取设备名字文件的内容
        /// </summary>
        /// <returns></returns>
        private List<string> GetDeviceNameFileContent()
        {
            System.IO.StreamReader streamReader = null;
            var listText = new List<string>();
            try
            {
#if iOS
                string textFile = Foundation.NSBundle.MainBundle.PathForResource("DeviceName.ini", null);
                streamReader = new System.IO.StreamReader(textFile, Encoding.UTF8);
                string text;
                while ((text = streamReader.ReadLine()) != null)
                {
                    listText.Add(text.Trim());
                }
                return listText;
#endif
#if Android
                var stream = Application.Activity.Assets.Open("DeviceName.ini");
                streamReader = new System.IO.StreamReader(stream, Encoding.UTF8);
                string text;
                while ((text = streamReader.ReadLine()) != null)
                {
                    listText.Add(text.Trim());
                }
                stream.Close();
                return listText;
#endif
            }
            catch
            {
                return listText;
            }
            finally
            {
                try
                {
                    streamReader?.Close();
                }
                catch
                {
                }
            }
        }
        #endregion
    }
    #region ■ 自定义设备类型_________________________
    /// <summary>
    /// <para>设备的具体【设备类型】,变量名可以作为【设备类型】图片,这个值是瞎写的,没什么特殊意义</para>
    /// </summary>
    public enum DeviceConcreteType
    {
        /// <summary>
        /// 未知设备
        /// </summary>
        UnKownDevice = -1,
        //=========★★窗帘类(100-199)★★=========
        /// <summary>
        /// 窗帘
        /// </summary>
        Curtain = -100,
        /// <summary>
        /// 智能开合帘电机 镜像id:100
        /// </summary>
        Curtain_AutoOpen = 100,
        /// <summary>
        /// 智能管状电机 镜像id:101
        /// </summary>
        Curtain_Siphonate = 101,
        //=========★★按键面板类(200-1199)★★=========
        /// <summary>
        /// 按键面板
        /// </summary>
        ButtonPanel = -200,
        /// <summary>
        /// 4按键多功能触摸面板(带4路继电器底座) 镜像id:200
        /// </summary>
        ButtonPanel_Four = 200,
        /// <summary>
        /// 3按键多功能触摸面板(带3路继电器底座) 镜像id:201
        /// </summary>
        ButtonPanel_Three = 201,
        /// <summary>
        /// 2按键多功能触摸面板(带2路继电器底座) 镜像id:202
        /// </summary>
        ButtonPanel_Two = 202,
        /// <summary>
        /// 12按键多功能触摸面板(带1路继电器底座) 镜像id:203
        /// </summary>
        ButtonPanel_Twelve = 203,
        /// <summary>
        /// 4按键多功能触摸面板(只带电源底座) 镜像id:210
        /// </summary>
        ButtonPanel_FourNotPower = 210,
        /// <summary>
        /// 简约4按键面板 镜像id:220
        /// </summary>
        ButtonPanel_SimpleFour = 220,
        /// <summary>
        /// 简约3按键面板 镜像id:221
        /// </summary>
        ButtonPanel_SimpleThree = 221,
        /// <summary>
        /// 简约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,
        /// <summary>
        /// 简约多功能面板 镜像ID:212
        /// </summary>
        ButtonPanel_SimpleMultifunction = 212,
        /// <summary>
        /// 简约环境面板
        /// </summary>
        ButtonPanel_SimpleEnvironment = 230,
        //=========★★PIR传感器类(1200-1299)★★=========
        /// <summary>
        /// 传感器
        /// </summary>
        Sensor = -1200,
        /// <summary>
        /// pir传感器220 镜像id:1200
        /// </summary>
        Sensor_Pir = 1200,
        /// <summary>
        /// 球形移动传感器 镜像id:1205
        /// </summary>
        Sensor_SphericalMotion = 1205,
        //=========★★安防类传感器类(1300-2299)★★=========
        /// <summary>
        /// 燃气传感器
        /// </summary>
        Sensor_CarbonMonoxide = 1300,
        /// <summary>
        /// 门窗传感器
        /// </summary>
        Sensor_DoorWindow = 1301,
        /// <summary>
        /// 烟雾传感器
        /// </summary>
        Sensor_Fire = 1302,
        /// <summary>
        /// 红外传感器
        /// </summary>
        Sensor_Infrared = 1303,
        /// <summary>
        /// 水侵传感器
        /// </summary>
        Sensor_Water = 1304,
        /// <summary>
        /// 紧急按钮
        /// </summary>
        Sensor_EmergencyButton = 1305,
        /// <summary>
        /// 吸顶燃气传感器
        /// </summary>
        Sensor_CeilingGas = 1306,
        /// <summary>
        /// PM2.5空气质量传感器
        /// </summary>
        Sensor_PMTwoPointFive = 1307,
        /// <summary>
        /// 温湿度传感器(这个单词拼错了,但是错了就错了呗,就这样了)
        /// </summary>
        Sensor_TemperatureHumidity = 1308,
        /// <summary>
        /// 运动传感器
        /// </summary>
        Sensor_Motion = -1306,
        /// <summary>
        /// 钥匙扣
        /// </summary>
        Sensor_Keyfob = -1307,
        /// <summary>
        /// 温度传感器(这个单词拼错了,但是错了就错了呗,就这样了)
        /// </summary>
        Sensor_Temperature = -1309,
        /// <summary>
        /// 湿度传感器
        /// </summary>
        Sensor_Humidity = -1310,
        //=========★★继电器类(2300-2499)★★=========
        /// <summary>
        /// 继电器
        /// </summary>
        Relay = -2300,
        /// <summary>
        /// 三路继电器 镜像id:2300
        /// </summary>
        Relay_ThreeLoad = 2300,
        /// <summary>
        /// 方悦新风小模块 镜像id:2310
        /// </summary>
        Relay_FangyueFreshAirModul = 2310,
        /// <summary>
        /// 国标3路10A继电器小模块 镜像id:2301
        /// </summary>
        Relay_NationalThreeLoadTenA = 2301,
        /// <summary>
        /// 欧标2路5A继电器小模块 镜像id:2315
        /// </summary>
        Relay_EuropeanTwoLoadFiveA = 2315,
        /// <summary>
        /// 7路干接点输入输出模块 镜像id:2320
        /// </summary>
        Relay_SevenLoadInOutPutDryContact = 2320,
        //=========★★调光器类(2500-2799)★★=========
        /// <summary>
        /// 调光器
        /// </summary>
        DimmableLight = -2500,
        /// <summary>
        /// 1路调光器小模块
        /// </summary>
        DimmableLight_OneLoad = 2500,
        //=========★★智能门锁类(2800-????)★★=========
        /// <summary>
        /// 智能门锁
        /// </summary>
        IntelligentLocks = -2800,
        /// <summary>
        /// H06C
        /// </summary>
        IntelligentLocks_H06C = 2800,
        /// <summary>
        /// S-One Pro
        /// </summary>
        IntelligentLocks_Sone = 2802,
        /// <summary>
        /// AT-1TF
        /// </summary>
        IntelligentLocks_AT1TF = 2803,
        /// <summary>
        /// AT-1F
        /// </summary>
        IntelligentLocks_AT1F = 2804,
        /// <summary>
        /// AT-1T
        /// </summary>
        IntelligentLocks_AT1T = 2805,
        /// <summary>
        /// HAT-1B
        /// </summary>
        IntelligentLocks_HAT1B = 2806,
        /// <summary>
        /// HAT-1T
        /// </summary>
        IntelligentLocks_HAT1T = 2807,
        /// <summary>
        /// AT-FT
        /// </summary>
        IntelligentLocks_ATFT = 2808,
        //=========★★彩灯类(????-????)★★=========
        /// <summary>
        /// 彩灯
        /// </summary>
        ColorLight = -10,
        /// <summary>
        /// 色温灯(它的镜像ID未定,暂定20000)
        /// </summary>
        ColorLight_Temperature = 20000,
        //=========★★空调(3600-3899)★★=========
        /// <summary>
        /// 空调
        /// </summary>
        AirConditioner = -3600,
        /// <summary>
        /// zigbee空调网关模块
        /// </summary>
        AirConditioner_ZbGateway = 3600,
        //=========★★中继器(3900-3999)★★=========
        /// <summary>
        /// 中继器
        /// </summary>
        Repeater = -3900,
        /// <summary>
        /// zigbee中继器 镜像id:3900
        /// </summary>
        Repeater_Zigbee = 3900,
        //=========★★空气开关类(4100-????)★★=========
        /// <summary>
        /// 智能空开
        /// </summary>
        AirSwitch = -4100,
        /// <summary>
        /// 智能空开 镜像id:4100
        /// </summary>
        AirSwitch_CloudContr = 4100,
        //=========★★转换器类(4200-4699)★★=========
        /// <summary>
        /// 转换器
        /// </summary>
        Converter = -4200,
        /// <summary>
        /// zigbee转485协议转换器
        /// </summary>
        Converter_Zb485 = 4200,
        /// <summary>
        /// zigbee转buspro协议转换器
        /// </summary>
        Converter_ZbBuspro = 4201,
        //=========★★晾衣架类(5000-5199)★★=========
        /// <summary>
        /// 晾衣架
        /// </summary>
        Airer = 5000,
        //=========★★空气质量传感器类(5200-5399)★★=========
        /// <summary>
        /// 空气质量传感器
        /// </summary>
        AirQualitySensor = 5200,
        //=========★★其他类(????-????)★★=========
        /// <summary>
        /// 干接点(注意,它属于其他类,不是设备类型)
        /// </summary>
        DryContact = -10000,
        /// <summary>
        /// 灯光(注意,它属于其他类,不是设备类型)
        /// </summary>
        Light = -10001,
        /// <summary>
        /// 插座(注意,它属于其他类,不是设备类型)
        /// </summary>
        Socket1 = -10002,
        /// <summary>
        /// 开关(注意,它属于其他类,不是设备类型)
        /// </summary>
        Switch = -10003,
    }
    /// <summary>
    /// <para>设备所属枚举,这个值是瞎写的,没什么特殊意义</para>
    /// </summary>
    public enum DeviceBeloneType
    {
        /// <summary>
        /// 未知设备
        /// </summary>
        A未知设备 = 0,
        /// <summary>
        /// 窗帘(100-199)
        /// </summary>
        A窗帘 = 100,
        /// <summary>
        /// 按键面板(200-1199)
        /// </summary>
        A按键面板 = 200,
        /// <summary>
        /// 传感器(1200-2299)
        /// </summary>
        A传感器 = 1200,
        /// <summary>
        /// PM2.5空气质量传感器
        /// </summary>
        APM2点5空气质量传感器 = 1307,
        /// <summary>
        /// 继电器(2300-2499)
        /// </summary>
        A继电器 = 2300,
        /// <summary>
        /// 新风
        /// </summary>
        A新风 = 2310,
        /// <summary>
        /// 调光器(2500-2799)
        /// </summary>
        A调光器 = 2500,
        /// <summary>
        /// 智能门锁(2800-????)
        /// </summary>
        A智能门锁 = 2800,
        /// <summary>
        /// 空调(3600-3899)
        /// </summary>
        A空调 = 3600,
        /// <summary>
        /// 中继器(3900-3999)
        /// </summary>
        A中继器 = 3900,
        /// <summary>
        /// 智能空开(4100-4199)
        /// </summary>
        A智能空开 = 4100,
        /// <summary>
        /// 转换器(4200-4699)
        /// </summary>
        A转换器 = 4200,
        /// <summary>
        /// 晾衣架(4200-4699)
        /// </summary>
        A晾衣架 = 5000,
        /// <summary>
        /// 空气质量传感器(5200-5399)
        /// </summary>
        A空气质量 = 5200,
        /// <summary>
        /// 彩灯
        /// </summary>
        A彩灯 = 9,
        /// <summary>
        /// 温湿度传感器
        /// </summary>
        A温湿度传感器 = 10,
        /// <summary>
        /// 温度传感器
        /// </summary>
        A温度传感器 = 11,
        /// <summary>
        /// 湿度传感器
        /// </summary>
        A湿度传感器 = 12,
        /// <summary>
        /// 开关
        /// </summary>
        A开关 = 13,
        /// <summary>
        /// 插座
        /// </summary>
        A插座 = 14,
        /// <summary>
        /// 灯光
        /// </summary>
        A灯光 = 15,
        /// <summary>
        /// 干接点
        /// </summary>
        A干接点 = 16,
    }
    #endregion
}