using Shared.Phone.UserCenter; using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using ZigBee.Device; namespace Shared.Common { /// /// 本地设备 /// public class LocalDevice { #region ■ 变量声明___________________________ /// /// 本地设备 /// private static LocalDevice m_Current = null; /// /// 本地设备 /// public static LocalDevice Current { get { if (m_Current == null) { m_Current = new LocalDevice(); } return m_Current; } set { m_Current = value; } } /// /// 本地所有设备的缓存 /// public List listAllDevice { get { var list = new List(); foreach (var device in dicAllDevice.Values) { list.Add(device); } return list; } } /// /// R文件里面设备默认名字的ID的前缀 /// public const string deviceDefultNameFlag = "uDeviceDefultName"; /// /// R文件里面设备默认名字的ID /// public Dictionary dicDeviceDefultNameID = null; /// /// 设备的模块ID的枚举(keys:模块ID(已翻译),value:设备具体类型值-设备所属类型值(自定义的值,嘛,只要不重复就可以) /// private Dictionary dicDeviceModelIdEnum = null; /// /// 本地所有设备的缓存(非公开) /// private Dictionary dicAllDevice = new Dictionary(); /// /// 本地所有设备UI的缓存(非公开) /// private Dictionary dicAllDeviceUI = new Dictionary(); /// /// 本地所有的顶点升级设备(非公开,主键是MAC+200端口) /// private Dictionary dicOTADevice = new Dictionary(); /// /// 设备计数 /// private Dictionary dicDeviceCount = new Dictionary(); /// /// 设备文件的前缀名字 /// public const string deviceFirstName = "Device_"; /// /// 备用设备文件的前缀标识名字(为设备删除所用,这个文件一旦设备删除后就会一直存在) /// private string deviceBackupName = "Backup"; /// /// 锁 /// private object objLock = new object(); #endregion #region ■ 刷新设备___________________________ /// /// 刷新本地设备信息 /// public void ReFreshByLocal() { this.dicAllDevice.Clear(); this.dicAllDeviceUI.Clear(); this.dicDeviceCount.Clear(); //初始化R文件里面设备默认名字的ID this.InitDeviceDefultNameIDList(); //获取本地全部的设备文件 List listFile = this.GetAllDeviceFile(); foreach (string file in listFile) { //反序列化为指定的类,不然数据会丢失而导致无法强转 var device = ZigBee.Device.CommonDevice.CommonDeviceByFilePath(file); if (device == null || device.CurrentGateWayId == null) { //失效的文件,没有网关id的都删除掉 Global.DeleteFilebyHomeId(file); continue; } //如果这个设备的网关ID不存在的话 if (Common.LocalGateway.Current.IsGatewayExist(device.CurrentGateWayId) == false) { if (UserCenterResourse.UserInfo.AuthorityNo == 3) { //如果他是成员的话,帮他新建一个网关 Common.LocalGateway.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; } //添加缓存 this.dicAllDevice[mainKey] = device; //回路计数 if (this.dicDeviceCount.ContainsKey(device.DeviceAddr) == false) { this.dicDeviceCount[device.DeviceAddr] = new DeviceCountInfo(); } this.dicDeviceCount[device.DeviceAddr].Count += 1; this.dicDeviceCount[device.DeviceAddr].hsEpoint.Add(device.DeviceEpoint); //检测Ui图片是否正确 var deviceUi = this.GetDeviceUIFromLocalFile(device); if (deviceUi == null) { deviceUi = new DeviceUI(); deviceUi.DeviceFileName = device.FilePath; deviceUi.Save(); HdlAutoBackupLogic.AddOrEditorFile(deviceUi.FileName); } else { //这个图片本地是否存在? if (string.IsNullOrEmpty(IO.FileUtils.GetImageFilePath(deviceUi.IconPath)) == true) { //不存在的话,重新生成 deviceUi.IconPath = string.Empty; deviceUi.ReSave(); HdlAutoBackupLogic.AddOrEditorFile(deviceUi.FileName); } } //添加缓存 this.dicAllDeviceUI[deviceUi.FileName] = deviceUi; } //成员身份的时候,删除掉非法的网关文件 this.DeleteGatewayFileByMemberModel(); } /// /// 成员身份的时候,删除掉非法的网关文件 /// private void DeleteGatewayFileByMemberModel() { if (UserCenterResourse.UserInfo.AuthorityNo != 3) { return; } var listId = new HashSet(); foreach (var device in this.dicAllDevice.Values) { if (listId.Contains(device.CurrentGateWayId) == false) { listId.Add(device.CurrentGateWayId); } } var listGateway = Common.LocalGateway.Current.GetAllLocalGateway(); foreach (var gateway in listGateway) { string gwId = Common.LocalGateway.Current.GetGatewayId(gateway); if (listId.Contains(gwId) == false) { //这个网关对于当前这个成员来说是非法的 Common.LocalGateway.Current.DeleteGatewayFile(gwId); } } } #endregion #region ■ 添加设备___________________________ /// /// 将指定网关的设备存入缓存中(从新获取镜像) /// /// 网关对象 /// 接收到设备时的事件,设备对象为null时,代表接收完成 /// 一直返回true public async Task SetDeviceToMemmoryByGateway(ZbGateway zbGateway, Action deviceComingAction = null) { //从网关获取全部的设备 List listDevice = new List(); List list = await this.GetDeviceListFromGateway(zbGateway, deviceComingAction); if (list == null) { return false; } listDevice.AddRange(list); //获取这个网关的本地所有设备 string gwID = Common.LocalGateway.Current.GetGatewayId(zbGateway); List listLocalDevices = this.GetDeviceByGatewayID(gwID); Dictionary dicExist = new Dictionary(); foreach (var device in listLocalDevices) { string maikey = this.GetDeviceMainKeys(device); dicExist[maikey] = device; } //添加设备的缓存 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); } } //如果本地和网关的设备不一致的时候,暂时删除本地的设备 //注意:只是删除设备文件,房间内容什么的还存在着 foreach (var device in dicExist.Values) { this.DeleteMemmoryDevice(device, false); } if (listDevice.Count > 0) { //等待一下设备的硬件信息 await Task.Delay(1500); //重新变更UI foreach (var device in listDevice) { if (device == null || device.DeviceAddr == null || (device is OTADevice)) { continue; } var ui = this.GetDeviceUI(device); if (ui.IsCustomizeImage == false) { ui.IconPath = string.Empty; ui.ReSave(); } } } return true; } /// /// 添加设备到缓存,存在时覆盖 /// /// 设备对象(这个东西有可能会被更改) /// 是否重置镜像类型 public void AddDeviceToMemory(ref CommonDevice device, bool ResetImageType = false) { //尝试恢复删除了的文件 bool backFile = this.RecoverBackupDeviceFile(ref device); string mainKeys = this.GetDeviceMainKeys(device); //如果它是升级的顶端端点,则不能让它加入到缓存,但是可以让他生成文件 if (device is OTADevice) { //尝试恢复文件成功,或者本地都没有这个设备 if (backFile == true || this.dicOTADevice.ContainsKey(mainKeys) == false) { this.dicOTADevice[mainKeys] = (OTADevice)device; } else { //交换属性 var tempDevice = this.dicOTADevice[mainKeys]; //将DeviceInfo的属性设置到主属性中 this.SetDeviceInfoToMain(tempDevice, device); device = tempDevice; } if (ResetImageType == true) { //ota设备的镜像类型初始值设置为-1 device.ImgTypeId = -1; device.ImgVersion = -1; } bool exists = Global.IsExistsByHomeId(device.FilePath); device.ReSave(); if (exists == false) { //添加自动备份 HdlAutoBackupLogic.AddOrEditorFile(device.FilePath); } //设置设备全部的镜像信息 this.SetAllImageInfoToOtaDevice((OTADevice)device); return; } if (this.dicAllDevice.ContainsKey(mainKeys) == true) { //交换属性 var tempDevice = this.dicAllDevice[mainKeys]; //将DeviceInfo的属性设置到主属性中 this.SetDeviceInfoToMain(tempDevice, device); device = tempDevice; } else { this.dicAllDevice[mainKeys] = device; } //获取设备的硬件信息 this.SetAllHardFirmwareInfoToDevice(device); //虚拟设备的话,附加回路号 if (device.DriveCode > 0 && this.GetSimpleEpointName(device) == string.Empty) { //根据设备类型获取名称 var dName = this.GetDeviceObjectText(new List() { device }, false); //在端点名字的后面附加【回路】字样 dName += "(" + device.DeviceEpoint + Language.StringByID(R.MyInternationalizationString.uDeviceCircuit) + ")"; this.SetEpointName(device, dName); new System.Threading.Thread(async () => { //因为虚拟设备不会经过设备入网这一步,所以直接给它一个默认名字 await this.ReName(this.dicAllDevice[mainKeys], dName, ShowErrorMode.NO); }) { IsBackground = true }.Start(); } //设备计数 if (this.dicDeviceCount.ContainsKey(device.DeviceAddr) == false) { this.dicDeviceCount[device.DeviceAddr] = new DeviceCountInfo(); } if (this.dicDeviceCount[device.DeviceAddr].hsEpoint.Contains(device.DeviceEpoint) == false) { this.dicDeviceCount[device.DeviceAddr].Count += 1; this.dicDeviceCount[device.DeviceAddr].hsEpoint.Add(device.DeviceEpoint); } //添加设备的时候,直接创建设备的UI图 DeviceUI deviceUI = new DeviceUI(); deviceUI.DeviceFileName = device.FilePath; bool exists2 = Global.IsExistsByHomeId(deviceUI.FileName); deviceUI.Save(); if (exists2 == false) { //添加自动备份 HdlAutoBackupLogic.AddOrEditorFile(deviceUI.FileName); this.dicAllDeviceUI[deviceUI.FileName] = deviceUI; } exists2 = Global.IsExistsByHomeId(device.FilePath); device.ReSave(); if (exists2 == false) { //添加自动备份 HdlAutoBackupLogic.AddOrEditorFile(device.FilePath); } } /// /// 恢复删除了的文件(仅限添加设备的时候) /// /// private bool RecoverBackupDeviceFile(ref CommonDevice device) { //如果本地存在备用的文件夹的话,则使用备份的文件夹作为对象 string backFile = deviceBackupName + device.FilePath; if (Global.IsExistsByHomeId(device.FilePath) == true || Global.IsExistsByHomeId(backFile) == false) { //只有在本地目标设备文件不存在,并且备份文件存在的时候才处理 return false; } //反序列化为指定的类,不然数据会丢失而导致无法强转(★这个东西保存着以前的网关备份数据★) var tempDevice = ZigBee.Device.CommonDevice.CommonDeviceByFilePath(backFile); if (tempDevice == null) { return false; } //不沿用名字,先这样吧 this.SetMacName(tempDevice, string.Empty); this.SetEpointName(tempDevice, string.Empty); //将DeviceInfo的属性设置到主属性中 this.SetDeviceInfoToMain(tempDevice, device); //替换对象 device = tempDevice; //这里继续保留着那个备份文件,好像也没什么问题的样子 return true; } #endregion #region ■ 修改设备___________________________ /// /// 更改名字并且刷新缓存(修改失败时,会显示信息) /// /// 设备对象 /// 新名字 /// 是否显示错误 public async Task ReName(CommonDevice device, string newName, ShowErrorMode mode = ShowErrorMode.YES) { //不再检测名字是否一样 //成员只能修改自己本地的名字 if (UserCenterResourse.UserInfo.AuthorityNo != 3) { var result = await device.RenameDeviceNameAsync(device.DeviceAddr, device.DeviceEpoint, newName); if (result == null || result.deviceRenameData == null || result.deviceRenameData.Result == 1) { //设备名称修改失败 string msg = Language.StringByID(R.MyInternationalizationString.uDeviceReNameFail); //拼接上【网关回复超时】的Msg msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result.errorMessageBase); if (mode == ShowErrorMode.YES) { this.ShowErrorMsg(msg); } return false; } //备份数据 await HdlGatewayLogic.Current.UpLoadDeviceBackupDataToGateway(device, GatewayBackupEnum.A端点名称, newName); } //更改名字后,刷新设备缓存 this.SetEpointName(device, newName); this.BackupDeviceAfterReName(device); return true; } /// /// 更改Mac名字并且刷新缓存(修改失败时,会显示信息) /// /// 设备对象 /// 新名字 /// 是否显示错误 public async Task ReMacName(List listDevice, string newMacName, ShowErrorMode mode = ShowErrorMode.YES) { if (listDevice.Count == 0) { return true; } CommonDevice device = listDevice[0]; //不再检测名字是否一样 //成员只能修改自己本地的名字 if (UserCenterResourse.UserInfo.AuthorityNo != 3) { //修改物理名字 var result = await device.RenameDeviceMacNameAsync(device.DeviceAddr, device.DeviceEpoint, newMacName); if (result == null || result.renameDeviceMacNameData == null || result.renameDeviceMacNameData.Result == 1) { //设备名称修改失败 string msg = Language.StringByID(R.MyInternationalizationString.uDeviceReNameFail); //拼接上【网关回复超时】的Msg msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result.errorMessageBase); if (mode == ShowErrorMode.YES) { this.ShowErrorMsg(msg); } return false; } //备份数据 await HdlGatewayLogic.Current.UpLoadDeviceBackupDataToGateway(device, GatewayBackupEnum.AMac名称, newMacName); } //修改缓存 for (int i = 0; i < listDevice.Count; i++) { var device2 = listDevice[i]; //这两个东西很特殊 this.SetMacName(device2, newMacName); //更改名字后,刷新设备缓存 this.BackupDeviceAfterReName(device2); } return true; } /// /// 更改名字后,刷新设备缓存 /// /// private void BackupDeviceAfterReName(CommonDevice device) { string mainKeys = this.GetDeviceMainKeys(device); if (this.dicAllDevice.ContainsKey(mainKeys) == false) { return; } this.dicAllDevice[mainKeys] = device; device.ReSave(); //添加自动备份 HdlAutoBackupLogic.AddOrEditorFile(device.FilePath); } #endregion #region ■ 删除设备___________________________ /// /// 删除设备并且刷新缓存(删除失败时,会显示信息) /// /// 设备对象(MAC地址必须要相同) public async Task DeleteDevice(List listdevice) { 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.errorMessageBase); this.ShowErrorMsg(msg); return false; } //删除文件 foreach (CommonDevice device in listdevice) { this.DeleteMemmoryDevice(device); } return true; } /// /// 删除缓存的设备 /// /// 设备对象 /// 是否从房间删除 public void DeleteMemmoryDevice(CommonDevice device, bool deleteRoom = true) { //删除缓存 string mainKeys = this.GetDeviceMainKeys(device); if (this.dicAllDevice.ContainsKey(mainKeys) == true) { this.dicAllDevice.Remove(mainKeys); } //删除设备文件 string filePath = device.FilePath; if (Global.IsExistsByHomeId(filePath) == true) { if (UserCenterResourse.UserInfo.AuthorityNo == 3) { //成员的话,直接删除,没有商量的余地 Global.DeleteFilebyHomeId(filePath); } else { //不删除文件,直接改名字 string oldFile = UserCenterLogic.CombinePath(filePath); string newFile = UserCenterLogic.CombinePath(deviceBackupName + filePath); Global.MoveFileToDirectory(oldFile, newFile); //删除自动备份 HdlAutoBackupLogic.DeleteFile(device.FilePath); } } //删除200端口文件 string otaKeys = this.GetDeviceMainKeys(device.DeviceAddr, 200); if (this.dicOTADevice.ContainsKey(otaKeys) == true) { string otaFile = this.dicOTADevice[otaKeys].FilePath; if (Global.IsExistsByHomeId(otaFile) == true) { if (UserCenterResourse.UserInfo.AuthorityNo == 3) { //成员的话,直接删除,没有商量的余地 Global.DeleteFilebyHomeId(filePath); } else { //不删除文件,直接改名字 string oldFile = UserCenterLogic.CombinePath(otaFile); string newFile = UserCenterLogic.CombinePath(deviceBackupName + otaFile); Global.MoveFileToDirectory(oldFile, newFile); //删除自动备份 HdlAutoBackupLogic.DeleteFile(otaFile); } } this.dicOTADevice.Remove(otaKeys); } if (deleteRoom == true && Room.CurrentRoom != null) { //从房间中删除 Room.CurrentRoom.DeleteDevice(device); } } /// /// 删除设备的备份文件(应该只有在删除网关的时候使用吧) /// /// public void DeleteDeviceBackupFile(CommonDevice device) { string backFile = deviceBackupName + device.FilePath; if (Global.IsExistsByHomeId(backFile) == true) { Global.DeleteFilebyHomeId(backFile); HdlAutoBackupLogic.DeleteFile(backFile); } } #endregion #region ■ 测试设备___________________________ /// /// 发送测试指令到设备 /// /// public void SetTestCommand(CommonDevice device) { device.IdentifyControl(device.DeviceAddr, device.DeviceEpoint, 5); } /// /// 检测设备是否拥有测试的功能 /// /// /// public bool DeviceIsCanTest(CommonDevice device) { foreach (var data in device.InClusterList) { //拥有on/off功能的,才支持测试 if (data.InCluster == 3) { return true; } } return false; } /// /// 检测设备是否拥有开关的功能(输出簇) /// /// /// public bool OutDeviceIsCanOnOff(CommonDevice device) { foreach (var data in device.OutClusterList) { //拥有on/off功能的,才支持测试 if (data.OutCluster == 6) { return true; } } return false; } /// /// 检测设备是否拥有开关的功能(输入簇) /// /// /// public bool InDeviceIsCanOnOff(CommonDevice device) { foreach (var data in device.InClusterList) { //拥有on/off功能的,才支持测试 if (data.InCluster == 6) { return true; } } return false; } #endregion #region ■ 获取设备___________________________ /// /// 根据网关ID获取所有的设备 /// /// 网关ID /// public List GetDeviceByGatewayID(string gwId) { List list = new List(); //各网关的所有设备 foreach (CommonDevice device in this.dicAllDevice.Values) { if (gwId == device.CurrentGateWayId) { list.Add(device); } } return list; } /// /// 获取指定设备(主键是:Mac地址+端口号),不存在时,返回null /// /// Mac地址+端口号 /// public CommonDevice GetDevice(string mainKeys) { if (this.dicAllDevice.ContainsKey(mainKeys) == true) { return this.dicAllDevice[mainKeys]; } return null; } /// /// 获取指定设备,不存在时,返回null /// /// Mac地址 /// 端口号 /// public CommonDevice GetDevice(string DeviceAddr, int DeviceEpoint) { string mainkeys = this.GetDeviceMainKeys(DeviceAddr, DeviceEpoint); return this.GetDevice(mainkeys); } /// /// 获取本地全部的设备文件 /// /// public List GetAllDeviceFile() { List listDeviceFile = new List(); List listAllFile = Global.FileListByHomeId(); foreach (string file in listAllFile) { if (file.StartsWith(deviceFirstName) == false) { //如果不是设备文件 continue; } listDeviceFile.Add(file); } return listDeviceFile; } /// /// 附加设备的版本代号(返回Ver.XXX) /// /// 版本号 /// public string AppendVersion(int versionValue) { //转为16进制 string txt64 = Convert.ToString(versionValue, 16).PadLeft(4, '0'); //这个是小数点前面的值 int value1 = Convert.ToInt32(txt64.Substring(0, 2), 16); //这个是小数点后面的值 int value2 = Convert.ToInt32(txt64.Substring(2, 2), 16); //Ver. string ver = Language.StringByID(R.MyInternationalizationString.uVersionAbbreviation); return ver + value1 + "." + value2.ToString().PadLeft(3, '0'); } /// /// 获取OTA设备(200端口的) /// /// /// public OTADevice GetOTADevice(string macAdrr) { string mainkeys = this.GetDeviceMainKeys(macAdrr, 200); if (this.dicOTADevice.ContainsKey(mainkeys) == false) { return null; } return this.dicOTADevice[mainkeys]; } #endregion #region ■ 设备镜像(私有类型)_________________ /// /// OTA设备获取镜像后的回调函数 /// private Dictionary> dicOtaBackAction = new Dictionary>(); /// /// 设置设备全部的镜像信息(反复调用请使用【DeviceAttributeLogic.Current.SetFirmwareVersionComand】函数) /// /// /// public void SetAllImageInfoToOtaDevice(OTADevice tADevice, Action backAction = null) { if (tADevice == null) { return; } lock (objLock) { if (backAction != null) { this.dicOtaBackAction[tADevice.DeviceAddr] = backAction; } //设置设备的固件版本号(需要等待推送后才会更改) this.SetFirmwareVersionToOtaDevice(tADevice); } } /// /// 设置设备的固件版本号(需要等待推送后才会更改) /// /// private void SetFirmwareVersionToOtaDevice(OTADevice tADevice) { if (DeviceAttributeLogic.Current.IsEsixt("DeviceAutoGetFirmwareVersion") == false) { //添加事件 DeviceAttributeLogic.Current.AddFirmwareVersionEvent("DeviceAutoGetFirmwareVersion", this.SetFirmwareVersionByInterfaceResult); } //发送命令 DeviceAttributeLogic.Current.SetFirmwareVersionComand(tADevice); } /// /// 根据接口推送的信息,设置固件版本号 /// /// private void SetFirmwareVersionByInterfaceResult(CommonDevice device) { string mainKeys = this.GetDeviceMainKeys(device); if (this.dicOTADevice.ContainsKey(mainKeys) == false) { return; } //设置固件版本信息 DeviceAttributeLogic.Current.SetFirmwareVersion(device.DeviceStatusReport, this.dicOTADevice[mainKeys]); this.dicOTADevice[mainKeys].ReSave(); lock (objLock) { if (this.dicOtaBackAction.ContainsKey(device.DeviceAddr) == true) { //调用回调函数 this.dicOtaBackAction[device.DeviceAddr](device, device.DeviceStatusReport); //然后移除 this.dicOtaBackAction.Remove(device.DeviceAddr); } } } #endregion #region ■ 读取以及设置设备硬件信息___________ /// /// 设备获取硬件信息后的回调函数 /// private Dictionary> dicDeviceHardInfoBackAction = new Dictionary>(); /// 获取硬件信息的对象设备 /// private HashSet hsGetHardInfoDevice = new HashSet(); /// /// 读取以及设置设备硬件信息 /// /// 设备回路 /// 是否重置(不重置的情况时,如果已经有值了,则不再获取) /// 回调函数 public void SetAllHardFirmwareInfoToDevice(CommonDevice device, bool Reset = false, Action backAction = null) { if (device == null) { return; } if (Reset == false) { if (device.ModelIdentifier != string.Empty) { return; } } lock (objLock) { string mainkeys = this.GetDeviceMainKeys(device); this.hsGetHardInfoDevice.Add(mainkeys); if (backAction != null) { this.dicDeviceHardInfoBackAction[mainkeys] = backAction; } //设置设备的硬件信息(需要等待推送后才会更改) this.SetHardFirmwareInfoToDevice(device); } } /// /// 设置设备的硬件信息(需要等待推送后才会更改) /// /// private void SetHardFirmwareInfoToDevice(CommonDevice device) { if (DeviceAttributeLogic.Current.IsEsixt("DeviceGetHardFirmwareInfo") == false) { //添加事件 DeviceAttributeLogic.Current.AddFirmwareVersionEvent("DeviceGetHardFirmwareInfo", this.SetHardFirmwareInfoByInterfaceResult); } //发送命令 DeviceAttributeLogic.Current.SetHardFirmwareInfoComand(device); } /// /// 根据接口推送的信息,设置设备硬件信息 /// /// private void SetHardFirmwareInfoByInterfaceResult(CommonDevice device) { string mainKeys = this.GetDeviceMainKeys(device); if (this.hsGetHardInfoDevice.Contains(mainKeys) == false) { return; } //设置设备硬件信息 var localDevice = this.GetDevice(mainKeys); if (localDevice == null) { return; } lock (objLock) { DeviceAttributeLogic.Current.SetHardFirmwareInfo(device.DeviceStatusReport, localDevice); localDevice.ReSave(); this.hsGetHardInfoDevice.Remove(mainKeys); if (this.dicDeviceHardInfoBackAction.ContainsKey(mainKeys) == true) { //调用回调函数 this.dicDeviceHardInfoBackAction[mainKeys](device, device.DeviceStatusReport); //然后移除 this.dicDeviceHardInfoBackAction.Remove(mainKeys); } } } /// /// 移除获取设备硬件信息的监听线程 /// /// public void RemoveDeviceHardInfoThread(CommonDevice device) { lock (objLock) { string mainKeys = this.GetDeviceMainKeys(device); this.hsGetHardInfoDevice.Remove(mainKeys); if (this.dicDeviceHardInfoBackAction.ContainsKey(mainKeys) == true) { //然后移除 this.dicDeviceHardInfoBackAction.Remove(mainKeys); } } } #endregion #region ■ 设置图标___________________________ /// /// 变更设备的图标 /// /// 设备对象 /// 图片地址(非选择) public void ChangedDeviceIcon(CommonDevice device, string unSelPath) { if (unSelPath == string.Empty) { return; } DeviceUI deviceUI = this.GetDeviceUI(device); deviceUI.IconPath = unSelPath; deviceUI.IsCustomizeImage = true; deviceUI.ReSave(); HdlAutoBackupLogic.AddOrEditorFile(deviceUI.FileName); } /// /// 设置设备【图标】到指定的控件 /// /// 控件对象 /// 设备对象 /// public void SetDeviceIconToControl(ButtonCommon btnIcon, CommonDevice device) { //获取这个设备的UI文件 DeviceUI deviceUI = this.GetDeviceUI(device); if (deviceUI == null) { return; } if (string.IsNullOrEmpty(deviceUI.IconPath) == true) { return; } btnIcon.UnSelectedImagePath = deviceUI.IconPath; string selPath = deviceUI.OnlineIconPath; if (string.IsNullOrEmpty(selPath) == false) { btnIcon.SelectedImagePath = selPath; } } /// /// 设置设备的【大图标】到指定的控件 /// /// 控件对象 /// 设备对象 public void SetDeviceBigIconToControl(ButtonCommon btnIcon, CommonDevice device) { //获取这个设备的UI文件 DeviceUI deviceUI = this.GetDeviceUI(device); if (deviceUI == null) { return; } string deviceIcon = deviceUI.OnlineIconPath; if (string.IsNullOrEmpty(deviceIcon) == true) { return; } //按键面板的回路图标 if (deviceIcon == "Device/DryContactSelected.png") { //因为按键面板是一个点,所以需要特殊处理 this.SetDeviceBeloneIconToControl(btnIcon, new List() { device }); btnIcon.UnSelectedImagePath = btnIcon.SelectedImagePath; btnIcon.SelectedImagePath = null; } else { //这里是自定义图标 btnIcon.UnSelectedImagePath = deviceIcon; } } /// /// 设置设备的真实图片到指定的控件 /// /// 控件对象 /// 设备对象 public void SetRealDeviceIconToControl(ButtonCommon btnIcon, List listdevice) { //获取它属于什么类型的设备 var myDeviceType = this.GetMyDeviceEnumInfo(listdevice); string strConcrete = Enum.GetName(typeof(DeviceConcreteType), myDeviceType.ConcreteType); 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; } imageFilePath = "RealDevice/" + arry[1] + ".png"; } btnIcon.UnSelectedImagePath = imageFilePath; } /// /// 设置【设备所属类型】的图标到指定的控件 /// /// 控件对象 /// 设备对象 /// public void SetDeviceBeloneIconToControl(ButtonCommon btnIcon, List listdevice) { //获取自定义设备类型 var myDeviceType = this.GetMyDeviceEnumInfo(listdevice); string imageUnSelectFilePath = string.Empty; string imageSelectFilePath = string.Empty; //获取【设备所属类型】的图标 this.GetDeviceBeloneIcon(myDeviceType.ConcreteType, ref imageUnSelectFilePath, ref imageSelectFilePath); //设置图片 btnIcon.UnSelectedImagePath = imageUnSelectFilePath; btnIcon.SelectedImagePath = imageSelectFilePath; } /// /// 获取【设备所属类型】的图标 /// /// 设备对象 /// 图片地址 /// 图片地址 /// public void GetDeviceBeloneIcon(List listdevice, ref string unSelectPath, ref string selectPath) { //获取自定义设备类型 var myDeviceType = this.GetMyDeviceEnumInfo(listdevice); //获取【设备所属类型】的图标 this.GetDeviceBeloneIcon(myDeviceType.ConcreteType, ref unSelectPath, ref selectPath); } /// /// 获取【设备所属类型】的图标 /// /// 自定义设备类型 /// 图片地址 /// 图片地址 /// private void GetDeviceBeloneIcon(DeviceConcreteType specificType, ref string unSelectPath, ref string selectPath) { //将具体类型转字符串 string strSpecific = Enum.GetName(typeof(DeviceConcreteType), specificType); 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) { //如果它自己就是共通图片的话,不再处理 return; } imageFilePath = "Device/" + arry[1] + ".png"; imageSelectFilePath = "Device/" + arry[1] + "Selected.png"; } //设置图片 unSelectPath = imageFilePath; selectPath = imageSelectFilePath; } #endregion #region ■ 设备UI相关_________________________ /// /// 获取设备所匹配的设备UI对象 /// /// /// public DeviceUI GetDeviceUI(CommonDevice device) { return this.GetDeviceUI("DeviceUI_" + device.FilePath); } /// /// 获取设备所匹配的设备UI对象 /// /// /// public DeviceUI GetDeviceUI(string filePath) { if (this.dicAllDeviceUI.ContainsKey(filePath) == true) { return this.dicAllDeviceUI[filePath]; } var deviceUi = new DeviceUI(); deviceUi.DeviceFileName = filePath.Replace("DeviceUI_", string.Empty); deviceUi.ReSave(); this.dicAllDeviceUI[filePath] = deviceUi; return deviceUi; } /// /// 从文件中获取指定设备的UI对象(有可能返回null) /// /// 设备对象 /// private DeviceUI GetDeviceUIFromLocalFile(CommonDevice device) { DeviceUI deviceUI = new DeviceUI(); deviceUI.DeviceFileName = device.FilePath; if (Global.IsExistsByHomeId(deviceUI.FileName) == false) { return null; } var jsonInfo = Encoding.UTF8.GetString(Global.ReadFileByHomeId(deviceUI.FileName)); var tempCommon = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonInfo); return tempCommon; } #endregion #region ■ 获取自定义的设备类型_______________ /// /// 获取【自定义的设备类型】,两种类型都设置了 /// /// 设备对象 /// public DeviceEnumInfo GetMyDeviceEnumInfo(List listdevice) { //获取河东设备的设备类型 DeviceEnumInfo info = this.GetHdlMyDeviceEnumInfo(listdevice[0]); if (info != null) { return info; } //获取第三方设备的【设备类型】 return this.GetNotHdlMyDeviceEnumInfo(listdevice); } /// /// 获取设备的【设备类型】的翻译文本(优先镜像) /// /// /// 第三方或者虚拟设备的时候,是否添加标识 /// public string GetDeviceObjectText(List listDevice, bool ApendFalge = true) { string strName = string.Empty; if (this.dicDeviceModelIdEnum.ContainsKey(listDevice[0].ModelIdentifier) == true) { //根据模块ID,获取设备名字 strName = this.GetNameByModelId(listDevice[0]); } else { //获取第三方设备的【设备类型】 var myDeviceType = this.GetNotHdlMyDeviceEnumInfo(listDevice); strName = Language.StringByID(myDeviceType.TextId); } if (ApendFalge == true && listDevice[0].DriveCode > 0) { //虚拟设备加个标识 strName += "✩"; } else if (ApendFalge == true && this.IsHdlDevice(listDevice[0]) == false) { //第三方设备加个标识 strName += "★"; } return strName; } #endregion #region ■ 获取河东设备的设备类型_____________ /// /// 获取Hdl设备的【自定义的设备类型】 /// /// 随便某一回路 /// 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; if (this.dicDeviceModelIdEnum.ContainsKey(modelKeys) == false) { //没有匹配到模块ID,则直接走第三方设备的判断 return null; } string[] strValue = this.dicDeviceModelIdEnum[modelKeys].Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries); int ConcreteValue = Convert.ToInt32(strValue[0]); int BeloneValue = Convert.ToInt32(strValue[1]); //设备具体类型 info.ConcreteType = (DeviceConcreteType)ConcreteValue; if (info.ConcreteType.ToString() == ConcreteValue.ToString()) { info.ConcreteType = DeviceConcreteType.UnKownDevice; return info; } //设置设备的【设备所属类型】 info.BeloneType = (DeviceBeloneType)BeloneValue; return info; } #endregion #region ■ 获取第三方设备的设备类型___________ /// /// 获取第三方设备的【设备类型】 /// /// /// private DeviceEnumInfo GetNotHdlMyDeviceEnumInfo(List listdevice) { var dicType = new Dictionary(); 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) { info.TextId = R.MyInternationalizationString.uButtonControl; info.BeloneType = DeviceBeloneType.A按键面板; info.ConcreteType = DeviceConcreteType.ButtonPanel; } //2包含传感器的话,当传感器处理 else if (dicType.ContainsKey(DeviceType.IASZone) == true) { //设置传感器具体的类型 info.BeloneType = DeviceBeloneType.A传感器; this.SetSensorDeviceSpecificType(ref info, listdevice); } //3包含窗帘的话,当窗帘处理 else if (dicType.ContainsKey(DeviceType.WindowCoveringDevice) == true) { info.TextId = R.MyInternationalizationString.uCurtain; info.BeloneType = DeviceBeloneType.A窗帘; info.ConcreteType = DeviceConcreteType.Curtain; } //4空气开关 else if (dicType.ContainsKey(DeviceType.AirSwitch) == true) { info.TextId = R.MyInternationalizationString.uAirSwitch; info.BeloneType = DeviceBeloneType.A空气开关; info.ConcreteType = DeviceConcreteType.AirSwitch; } //5继电器 else if (dicType.ContainsKey(DeviceType.OnOffOutput) == true) { info.TextId = R.MyInternationalizationString.uRelay; info.BeloneType = DeviceBeloneType.A继电器; info.ConcreteType = DeviceConcreteType.Relay; } //6调光器 else if (dicType.ContainsKey(DeviceType.DimmableLight) == true) { info.TextId = R.MyInternationalizationString.uDimmableLight; info.BeloneType = DeviceBeloneType.A调光器; info.ConcreteType = DeviceConcreteType.DimmableLight; } //7彩灯 else if (dicType.ContainsKey(DeviceType.ColorDimmableLight) == true) { info.TextId = R.MyInternationalizationString.uColorDimmableLight; info.BeloneType = DeviceBeloneType.A彩灯; info.ConcreteType = DeviceConcreteType.ColorDimmableLight; } //8空调 else if (dicType.ContainsKey(DeviceType.Thermostat) == true) { info.TextId = R.MyInternationalizationString.Thermostat; info.BeloneType = DeviceBeloneType.A空调; info.ConcreteType = DeviceConcreteType.AirConditioner; } //9中继器 else if (dicType.ContainsKey(DeviceType.Repeater) == true) { info.TextId = R.MyInternationalizationString.Repeater; info.BeloneType = DeviceBeloneType.A中继器; info.ConcreteType = DeviceConcreteType.Repeater; } //10转换器 else if (dicType.ContainsKey(DeviceType.Transverter) == true) { info.TextId = R.MyInternationalizationString.uConverter; info.BeloneType = DeviceBeloneType.A转换器; info.ConcreteType = DeviceConcreteType.Converter; } return info; } #endregion #region ■ 传感器具体的类型___________________ /// /// 设置传感器具体的类型 /// /// 自定义设备枚举信息 /// 设备对象 private void SetSensorDeviceSpecificType(ref DeviceEnumInfo info, List listdevice) { //默认名字:传感器 info.ConcreteType = DeviceConcreteType.Sensor; info.TextId = R.MyInternationalizationString.uSensor; //如果这个设备拥有多个回路的话,我也不知道怎么命名,只能给个默认名字 if (listdevice.Count > 1) { return; } var iasZone = (IASZone)listdevice[0]; if (iasZone.IasDeviceType == 13) { //运动传感器 info.ConcreteType = DeviceConcreteType.Motion_Sensor; info.TextId = R.MyInternationalizationString.uMotionSensor; } else if (iasZone.IasDeviceType == 40) { //烟雾传感器 info.ConcreteType = DeviceConcreteType.Fire_Sensor; info.TextId = R.MyInternationalizationString.uDeviceDefultName1302; } else if (iasZone.IasDeviceType == 42) { //水侵传感器 info.ConcreteType = DeviceConcreteType.Water_Sensor; info.TextId = R.MyInternationalizationString.uDeviceDefultName1304; } else if (iasZone.IasDeviceType == 43) { //燃气传感器 info.ConcreteType = DeviceConcreteType.CarbonMonoxide_Sensor; info.TextId = R.MyInternationalizationString.uDeviceDefultName1300; } else if (iasZone.IasDeviceType == 44) { //紧急按钮 info.ConcreteType = DeviceConcreteType.EmergencyButton_Sensor; info.TextId = R.MyInternationalizationString.uDeviceDefultName1305; } else if (iasZone.IasDeviceType == 277) { //钥匙扣 info.ConcreteType = DeviceConcreteType.Keyfob_Sensor; info.TextId = R.MyInternationalizationString.uKeyfob; } else if (iasZone.IasDeviceType == 21 || iasZone.IasDeviceType == 22) { //门窗传感器 info.ConcreteType = DeviceConcreteType.DoorWindowSensor_Sensor; info.TextId = R.MyInternationalizationString.uDeviceDefultName1301; } } #endregion #region ■ 获取设备名称_______________________ /// /// 获取设备端点的名称(有特效) /// /// 设备对象 /// public string GetDeviceEpointName(CommonDevice device) { string dName = this.GetSimpleEpointName(device); if (string.IsNullOrEmpty(dName) == false) { return dName; } //根据设备类型获取名称 dName = this.GetDeviceObjectText(new List() { device }, false); //如果是虚拟设备 if (device.DriveCode > 0 || (this.dicDeviceCount.ContainsKey(device.DeviceAddr) == true && this.dicDeviceCount[device.DeviceAddr].Count > 1)) { var arry = dName.Split(new string[] { "(" }, StringSplitOptions.RemoveEmptyEntries); dName = arry[0].Trim(); //在端点名字的后面附加【回路】字样 dName += "(" + device.DeviceEpoint + Language.StringByID(R.MyInternationalizationString.uDeviceCircuit) + ")"; } return dName; } /// /// 获取设备MAC名称 /// /// 设备对象 /// public string GetDeviceMacName(CommonDevice device) { string dName = this.GetSimpleMacName(device); if (string.IsNullOrEmpty(dName) == false) { return dName; } //是否拥有配置的模块ID if (this.dicDeviceModelIdEnum.ContainsKey(device.ModelIdentifier) == true) { //获取模块ID名字 return this.GetNameByModelId(device); } else { //获取第三方设备的翻译名字 var myDeviceType = this.GetMyDeviceEnumInfo(new List() { device }); return Language.StringByID(myDeviceType.TextId); } } /// /// 在端点名字的后面附加【回路】字样,返回格式:XXXX(NN回路),如果没有名字,则返回:NN回路 /// /// 设备对象 /// public string AppendRoadTextToEpointName(CommonDevice device) { string pName = this.GetSimpleEpointName(device); //回路 string txtValue = Language.StringByID(R.MyInternationalizationString.uDeviceCircuit); if (string.IsNullOrEmpty(pName) == true) { //NN回路 return device.DeviceEpoint + txtValue; } else if (pName.Trim().EndsWith(txtValue) == true) { return pName; } //XXXX(NN回路) return pName + "(" + device.DeviceEpoint + txtValue + ")"; } /// /// 根据模块ID,获取翻译名字 /// /// /// public string GetNameByModelId(CommonDevice device) { if (device.ModelIdentifier == string.Empty) { //未知设备 return Language.StringByID(R.MyInternationalizationString.UnknowDevice); } string modelKeys = device.ModelIdentifier; if (this.dicDeviceModelIdEnum.ContainsKey(modelKeys) == false) { //未知设备 return Language.StringByID(R.MyInternationalizationString.UnknowDevice); } string[] strValue = this.dicDeviceModelIdEnum[modelKeys].Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries); int ConcreteValue = Convert.ToInt32(strValue[0]); string keyName = deviceDefultNameFlag + ConcreteValue; if (this.dicDeviceDefultNameID.ContainsKey(keyName) == true) { //R文件里面设置的名字 return Language.StringByID(this.dicDeviceDefultNameID[keyName]); } //未知设备 return Language.StringByID(R.MyInternationalizationString.UnknowDevice); } /// /// 非公开,设置设备的Mac名字(此方法只是单存的变更缓存) /// /// 设备对象 /// Mac名字 /// private void SetMacName(CommonDevice device, string macName) { device.DeviceName = macName; } /// /// 非公开,设置设备的端点名字(此方法只是单存的变更缓存) /// /// 设备对象 /// 端点名字 /// private void SetEpointName(CommonDevice device, string epointName) { device.DeviceEpointName = epointName; } /// /// 单纯获取设备的Mac名字 /// /// 设备对象 /// public string GetSimpleMacName(CommonDevice device) { return device.DeviceName; } /// /// 单纯获取设备的端点名字 /// /// 设备对象 /// public string GetSimpleEpointName(CommonDevice device) { return device.DeviceEpointName; } /// /// 初始化R文件里面设备默认名字的ID /// private void InitDeviceDefultNameIDList() { if (this.dicDeviceDefultNameID != null) { return; } this.dicDeviceDefultNameID = new Dictionary(); Type type = typeof(R.MyInternationalizationString); var PropertyList = type.GetFields(); foreach (var item in PropertyList) { if (item.Name.StartsWith(deviceDefultNameFlag) == true) { this.dicDeviceDefultNameID[item.Name] = Convert.ToInt32(item.GetValue(null)); } } //初始化设备枚举 this.InitDeviceModelIdEnum(); } #endregion #region ■ 设备排序___________________________ /// /// 设备排序 /// /// 设备对象 /// public List SortDeviceList(List listDevice) { List listSort = new List(); var list = this.SortDeviceListByRule(listDevice); listSort.AddRange(list); return listSort; } /// /// /// /// 设备对象 /// private List SortDeviceListByRule(List listDevice) { //设备排序的规则(Keys:设备类型(DeviceType) value:存放设备的容器) var dic = this.GetDeviceSortRule(); dic["对象外"] = new List(); foreach (CommonDevice device in listDevice) { if (device is IASZone) { var iasZone = (IASZone)device; //运动传感器 if (iasZone.DeviceInfo.DeviceType == 13) { dic["运动传感器"].Add(device); } //一氧化碳传感器 else if (iasZone.DeviceInfo.DeviceType == 43) { dic["一氧化碳传感器"].Add(device); } //紧急按钮 else if (iasZone.DeviceInfo.DeviceType == 44) { dic["紧急按钮"].Add(device); } else { dic[DeviceType.IASZone.ToString()].Add(device); } continue; } string checkKeys = device.Type.ToString(); if (dic.ContainsKey(checkKeys) == true) { dic[checkKeys].Add(device); } else { dic["对象外"].Add(device); } } List lstSort = new List(); foreach (var list in dic.Values) { lstSort.AddRange(list); } return lstSort; } /// /// 获取设备排序的规则(Keys:设备类型(DeviceType) value:存放设备的容器) /// /// private Dictionary> GetDeviceSortRule() { var dic = new Dictionary>(); //一氧化碳传感器 dic["一氧化碳传感器"] = new List(); //紧急按钮 dic["紧急按钮"] = new List(); //运动传感器 dic["运动传感器"] = new List(); //传感器 dic[DeviceType.IASZone.ToString()] = new List(); //控制面板 dic[DeviceType.OnOffSwitch.ToString()] = new List(); //继电器 dic[DeviceType.OnOffOutput.ToString()] = new List(); //卷帘 dic[DeviceType.WindowCoveringDevice.ToString()] = new List(); //调光灯 dic[DeviceType.ColorDimmableLight.ToString()] = new List(); //空气开关 dic[DeviceType.AirSwitch.ToString()] = new List(); return dic; } #endregion #region ■ 设备目标绑定_______________________ /// /// 获取设备下面绑定的设备(错误时返回null) /// /// 设备对象 /// public async Task> GetBindTargetDevice(CommonDevice mainDevice) { var result = (BindObj.GetDeviceBindResponseAllData)await this.LoadDeviceMethodByNameAsync(mainDevice, "GetDeviceBindAsync"); if (result == null || result.getAllBindResponseData == null) { //获取设备的绑定目标失败 string msg = Language.StringByID(R.MyInternationalizationString.uGetDeviceBindTargetFail); //拼接上【网关回复超时】的Msg msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result.errorMessageBase); this.ShowErrorMsg(msg); return null; } var listDevice = new List(); foreach (var data in result.getAllBindResponseData.BindList) { CommonDevice deviceTemp = this.GetDevice(data.BindMacAddr, data.BindEpoint); if (deviceTemp == null) { continue; } listDevice.Add(deviceTemp); } return listDevice; } /// /// 绑定设备的目标(返回成功设置的设备,错误时,返回null) /// /// 设备对象 /// 要绑定的目标设备 /// 要绑定的目标设备 /// public async Task> BindDeviceTarget(CommonDevice mainDevice, List listDevice, int BindCluster = 6) { if (listDevice.Count == 0) { return new List(); } var dicDevice = new Dictionary(); //组装数据 var addData = new IASZone.AddBindData(); addData.DeviceAddr = mainDevice.DeviceAddr; addData.Epoint = mainDevice.DeviceEpoint; foreach (var device in listDevice) { var info = new IASZone.AddBindListObj(); info.BindCluster = BindCluster; info.BindMacAddr = device.DeviceAddr; info.BindEpoint = device.DeviceEpoint; info.BindType = 0; addData.BindList.Add(info); //返回成功设备的时候使用 string mainkeys = Common.LocalDevice.Current.GetDeviceMainKeys(device); dicDevice[mainkeys] = device; } var result = (BindObj.AddedDeviceBindResponseAllData)await this.LoadDeviceMethodByNameAsync(mainDevice, "AddDeviceBindAsync", addData); if (result == null || result.addedDeviceBindResponseData == null) { //绑定目标设置失败 string msg = Language.StringByID(R.MyInternationalizationString.uSetBindTargetsFail); //拼接上【网关回复超时】的Msg msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result.errorMessageBase); this.ShowErrorMsg(msg); return null; } var listSuccess = new List(); foreach (var data in result.addedDeviceBindResponseData.BindList) { string mainkeys = Common.LocalDevice.Current.GetDeviceMainKeys(data.BindMacAddr, data.BindEpoint); //0:添加成功 3:已经存在,也可以代表成功 if (data.Result == 0 || data.Result == 3) { if (dicDevice.ContainsKey(mainkeys) == true) { listSuccess.Add(dicDevice[mainkeys]); } } //1:失败,节点设备或场景不存在 else if (data.Result == 1) { if (dicDevice.ContainsKey(mainkeys) == true) { //设备名称 绑定失败 string msg = this.GetDeviceEpointName(dicDevice[mainkeys]) + "\r\n"; msg += Language.StringByID(R.MyInternationalizationString.BindFailed); this.ShowTipMsg(msg); } } //2:未知,由节点设备反馈发送“Bind/BindResult”主题消息确定是否成功 else if (data.Result == 2) { if (result.addBindResultResponseData == null) { //设备名称 绑定失败 string msg = this.GetDeviceEpointName(dicDevice[mainkeys]) + "\r\n"; msg += Language.StringByID(R.MyInternationalizationString.BindFailed); this.ShowTipMsg(msg); } else { //添加成功 if (result.addBindResultResponseData.Result == 0) { if (dicDevice.ContainsKey(mainkeys) == true) { listSuccess.Add(dicDevice[mainkeys]); } } //设备名称 绑定列表已满 else if (result.addBindResultResponseData.Result == 140) { string msg = this.GetDeviceEpointName(dicDevice[mainkeys]) + "\r\n"; msg += Language.StringByID(R.MyInternationalizationString.uBindListIsFull); this.ShowTipMsg(msg); } else { //设备名称 绑定失败 string msg = this.GetDeviceEpointName(dicDevice[mainkeys]) + "\r\n"; msg += Language.StringByID(R.MyInternationalizationString.BindFailed); this.ShowTipMsg(msg); } } } } return listSuccess; } /// /// 删除设备绑定的目标 /// /// 设备对象 /// 要删除的绑定目标设备 /// 要绑定的目标设备 /// public async Task DeleteDeviceTarget(CommonDevice mainDevice, CommonDevice deleteDevice, int BindCluster = 6) { //组装数据 var deleteData = new IASZone.DelDeviceBindData(); deleteData.DeviceAddr = mainDevice.DeviceAddr; deleteData.Epoint = mainDevice.DeviceEpoint; var info = new IASZone.RemoveBindListObj(); info.BindCluster = 6; info.BindMacAddr = deleteDevice.DeviceAddr; info.BindEpoint = deleteDevice.DeviceEpoint; info.BindType = 0; deleteData.RemoveBindList.Add(info); var result = (BindObj.DelDeviceBindResponseAllData)await this.LoadDeviceMethodByNameAsync(mainDevice, "DelDeviceBindAsync", deleteData); if (result == null || result.delDeviceBindResponseData == null) { //删除绑定目标失败 string msg = Language.StringByID(R.MyInternationalizationString.uDeleteBindTargetsFail); //拼接上【网关回复超时】的Msg msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result.errorMessageBase); this.ShowErrorMsg(msg); return false; } foreach (var data in result.delDeviceBindResponseData.RemoveBindList) { //0:成功 1:设备不在绑定列表中 ,也可以代表成功 if (data.Result == 0 || data.Result == 1) { return true; } //3:失败,在等待节点设备确认是否解除绑定成功 else if (data.Result == 3) { //其他绑定目标正在删除中,请稍后再试 string msg = Language.StringByID(R.MyInternationalizationString.uOtherBindTargetsIsDelettingPleaseWait); this.ShowErrorMsg(msg); return false; } //4:未知,由节点设备反馈发送“Bind/BindResult”主题消息确定是否成功 else if (data.Result == 4) { if (result.removeBindResultResponseData == null) { //删除绑定目标失败 string msg = Language.StringByID(R.MyInternationalizationString.uDeleteBindTargetsFail); this.ShowErrorMsg(msg); return false; } else { //成功 if (result.removeBindResultResponseData.Result == 0) { return true; } //136:控制设备本地绑定列表中无此绑定 else if (result.removeBindResultResponseData.Result == 136) { //这个可以当做成功 return true; } else { //删除绑定目标失败 string msg = Language.StringByID(R.MyInternationalizationString.uDeleteBindTargetsFail); this.ShowErrorMsg(msg); return false; } } } } return false; } /// /// 执行指定设备对象类里面的方法(注意:这个是专门调用异步,并且等待异步完成的高科技函数,不调用异步的情况,别使用此函数) /// /// 需要执行的设备的设备对象 /// 指定要加载的方法名 /// 启动参数 private async Task LoadDeviceMethodByNameAsync(CommonDevice device, string method, params object[] parameter) { var task = device.GetType().InvokeMember(method, System.Reflection.BindingFlags.InvokeMethod, null, device, parameter) as Task; await task; var result = task.GetType().GetProperty("Result").GetValue(task, null); return result; } #endregion #region ■ PIR传感器__________________________ /// /// 获取PIR传感器的【光感等级总刻度】,错误时返回-1 /// /// /// public async Task GetPirLightAbilitySize(IASZone iASZone) { var data = await iASZone.GetPIRLightAbilitySizeAsync(); //共通错误检测 string error = UserCenterLogic.CheckCommonErrorCode(data); if (error != null) { this.ShowErrorMsg(error); return -1; } if (data == null || data.errorMessageBase != null || data.LightLevelCount == -1) { //获取传感器光感等级失败 string msg = Language.StringByID(R.MyInternationalizationString.uGetPirSensorLightPerceptionRegulationFail); //拼接上【网关回复超时】的Msg msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, data.errorMessageBase); this.ShowErrorMsg(msg); return -1; } return data.LightLevelCount; } /// /// 获取PIR传感器的【灯光配置】,错误时返回null /// /// /// public async Task GetPirSensorLightSettion(IASZone iASZone) { var data = await iASZone.GetPIRSensorParamateAsync(); if (data == null || data.configureParamates == null) { //获取传感器灯光设置信息失败 string msg = Language.StringByID(R.MyInternationalizationString.uGetPirSensorLightSettionFail); //拼接上【网关回复超时】的Msg msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, data.errorMessageBase); this.ShowErrorMsg(msg); return null; } return data.configureParamates; } /// /// 设置PIR传感器的【灯光配置】 /// /// 传感器对象 /// 灯光配置 /// public async Task SetPirSensorLightSettion(IASZone iASZone, IASZone.ConfigureParamates configure) { var result = await iASZone.SetPIRSensorParamateAsync(configure); if (result == null || result.responseData == null) { //设置传感器灯光配置失败 string msg = Language.StringByID(R.MyInternationalizationString.uSetPirSensorLightSettionFail); //拼接上【网关回复超时】的Msg msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result.errorMessageBase); this.ShowErrorMsg(msg); return false; } if (result.responseData.status != 0) { //设置传感器灯光配置失败 string msg = Language.StringByID(R.MyInternationalizationString.uSetPirSensorLightSettionFail); this.ShowErrorMsg(msg); return false; } //备份设置 await HdlGatewayLogic.Current.UpLoadDeviceBackupDataToGateway(iASZone, GatewayBackupEnum.APir灯光配置, configure); return true; } #endregion #region ■ 按键面板功能系_____________________ #region 颜色调节 /// /// 获取按键面板指定端点的【指示灯开关颜色】的信息(出错会返回null) /// /// 按键面板的某一个回路 /// public async Task GetPanelEpointColorInfo(Panel panel) { Panel.KeyNum keyNum = (Panel.KeyNum)panel.DeviceEpoint; var result = await panel.GetPanelColorInfoAsync(keyNum); //共通错误检测 string error = UserCenterLogic.CheckCommonErrorCode(result); if (error != null) { this.ShowErrorMsg(error); return null; } if (result == null || result.keyColorData == null) { //获取按键面板颜色调节信息失败 string msg = Language.StringByID(R.MyInternationalizationString.uGetPanelColorRegulationInfoFail); this.ShowErrorMsg(msg); return null; } return result.keyColorData; } /// /// 设置按键面板指定端点的【指示灯开关颜色】的信息 /// /// 按键面板的某一个回路 /// 开和关的颜色都需要一起设置 /// public async Task SetPanelEpointColorInfo(Panel panel, Panel.KeyColorData colorData) { var keyNum = new Panel.KeyNumStatus(); Type type = keyNum.GetType(); type.InvokeMember("Key" + panel.DeviceEpoint, System.Reflection.BindingFlags.SetField, null, keyNum, new object[] { true }); var result = await panel.SetPanelColorInfoAsync(colorData, keyNum); if (result == null || result.responseData == null) { //设置按键面板指示灯颜色失败 string msg = Language.StringByID(R.MyInternationalizationString.uSetPanelPilolightSettionFail); this.ShowErrorMsg(msg); return false; } if (result.responseData.status != 0) { //设置按键面板指示灯颜色失败 string msg = Language.StringByID(R.MyInternationalizationString.uSetPanelPilolightSettionFail); this.ShowErrorMsg(msg); return false; } //备份设备 await HdlGatewayLogic.Current.UpLoadDeviceBackupDataToGateway(panel, GatewayBackupEnum.A按键面板颜色调节, colorData); return true; } #endregion #region 亮度调节 /// /// 获取按键面板亮度配置(ui叫亮度调节,使用返回值的panelDirectionsLevel) /// /// 按键面板的某一个回路 /// public async Task GetPanelSwitchLightSettion(Panel panel) { var result = await panel.GetPanelSwitchLevelAsync(); //共通错误检测 string error = UserCenterLogic.CheckCommonErrorCode(result); if (error != null) { this.ShowErrorMsg(error); return null; } if (result == null || string.IsNullOrEmpty(result.errorMessageBase) == false) { //获取按键面板亮度调节信息失败 string msg = Language.StringByID(R.MyInternationalizationString.uGetPanelLightRegulationInfoFail); this.ShowErrorMsg(msg); return null; } return result; } /// /// 设置按键面板亮度(ui叫亮度调节) /// /// 按键面板的某一个回路 /// 指示灯亮度 0-100(现阶段不用这个) /// 背光灯亮度 0-100(ui叫亮度调节) /// public async Task SetPanelSwitchLightSettion(Panel panel, int directionsLevel, int backlightLevel) { var result = await panel.SetKeyLevelAsync(directionsLevel, backlightLevel); //共通错误检测 string error = UserCenterLogic.CheckCommonErrorCode(result); if (error != null) { this.ShowTipMsg(error); return false; } if (result == null || result.responseData == null) { //设置亮度调节失败 string msg = Language.StringByID(R.MyInternationalizationString.uSetLightRegulationFail); //拼接上【网关回复超时】的Msg msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result.errorMessageBase); this.ShowTipMsg(msg); return false; } if (result.responseData.status != 0) { //设置亮度调节失败 string msg = Language.StringByID(R.MyInternationalizationString.uSetLightRegulationFail); this.ShowTipMsg(msg); return false; } //备份设备 var backData = new Newtonsoft.Json.Linq.JObject { { "directionsLevel",directionsLevel }, { "backlightLevel", backlightLevel } }; await HdlGatewayLogic.Current.UpLoadDeviceBackupDataToGateway(panel, GatewayBackupEnum.A按键面板亮度调节, backData); return true; } #endregion #region 节能模式 /// /// 获取按键面板节能模式的配置状态(ui叫节能模式) /// /// 按键面板的某一个回路 /// public async Task GetPanelEnergyConservationMode(Panel panel) { var result = await panel.GetPanelSaveEnergyModeAsync(); //共通错误检测 string error = UserCenterLogic.CheckCommonErrorCode(result); if (error != null) { this.ShowErrorMsg(error); return null; } if (result == null || result.panelSaveEnergyModeInfo == null) { //获取按键面板节能模式信息失败 string msg = Language.StringByID(R.MyInternationalizationString.uGetPanelEnergyConservationInfoFail); //拼接上【网关回复超时】的Msg msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result.errorMessageBase); this.ShowErrorMsg(msg); return null; } return result.panelSaveEnergyModeInfo; } /// /// 设置按键面板的节能模式(ui叫节能模式) /// /// 按键面板的某一个回路 /// 节能模式是否有效 /// 无操作进入节能模式时间 0-255 /// 节能模式亮度:0-100 /// public async Task SetPanelEnergyConservationMode(Panel panel, bool modeEnable, int modeTime, int level) { var result = await panel.SetKeyModeAsync(modeEnable, modeTime, level); //共通错误检测 string error = UserCenterLogic.CheckCommonErrorCode(result); if (error != null) { this.ShowErrorMsg(error); return false; } if (result == null || result.responseData == null) { //节能模式配置失败 string msg = Language.StringByID(R.MyInternationalizationString.uSetEnergyConservationFail); //拼接上【网关回复超时】的Msg msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result.errorMessageBase); this.ShowTipMsg(msg); return false; } if (result.responseData.status != 0) { //节能模式配置失败 string msg = Language.StringByID(R.MyInternationalizationString.uSetEnergyConservationFail); this.ShowTipMsg(msg); return false; } //备份设备 var backData = new Newtonsoft.Json.Linq.JObject { { "modeEnable",modeEnable }, { "modeTime", modeTime }, { "level", level } }; await HdlGatewayLogic.Current.UpLoadDeviceBackupDataToGateway(panel, GatewayBackupEnum.A按键面板节能模式, backData); return true; } #endregion /// /// 检测控制面板(灯类)所拥有的功能,现支持的有以下几种(必定存在键值,出错会返回null) /// 键值1:亮度调节(ui叫亮度调节,针对整个设备) -> true:拥有此功能 false:无此功能 /// 键值2:节能模式(ui叫节能模式,针对整个设备) -> true:拥有此功能 false:无此功能 /// 键值3:颜色调节(ui叫颜色调节,只能单回路配置) -> true:拥有此功能 false:无此功能 /// /// 按键面板的某一个回路 /// public async Task> CheckPanelLightFunctionLevel2(Panel panel) { var dicCheck = new Dictionary(); dicCheck["亮度调节"] = false; dicCheck["节能模式"] = false; dicCheck["颜色调节"] = false; //获取第一级功能 var result = await this.GetPanelDeviceFunctionLevel1(panel); if (result == null) { return null; } if (result.Contains(1024) == false) { //此控制面板没有灯这个东西 return dicCheck; } //获取第二级功能 result = await this.GetPanelDeviceFunctionLevel2(panel, 1024); if (result == null) { return null; } if (result.Contains(102) == true) { //亮度调节:102 dicCheck["亮度调节"] = true; } if (result.Contains(101) == true) { //颜色调节:101 dicCheck["颜色调节"] = true; } if (result.Contains(0) == true) { //节能模式:0 dicCheck["节能模式"] = true; } return dicCheck; } /// /// 获取按键面板的第一级别功能功能系(1024:灯类,256:按键类,768:PIR类) /// /// 按键面板的某一个回路 /// public async Task> GetPanelDeviceFunctionLevel1(Panel panel) { var result = await this.GetPanelDeviceFunction(panel); if (result == null) { return null; } return result.privateFuncTypeLevelFirstList; } /// /// 获取按键面板的第二级别功能功能系(以下为返回值) /// 100:Switch,开关(按键类) /// 200:Dimmer,调光(按键类) /// 300:Curtain,窗帘(按键类) /// 0:EnergySavingMode,节能模式(灯类) /// 1:SleepMode,睡眠模式(灯类) /// 100:WhiteBalance,白平衡(灯类) /// 101:RGBColor,RGB指示灯颜色(灯类) /// 102:RGBLevel,RGB指示灯亮度(灯类) /// /// 按键面板的某一个回路 /// 第一级别的ID(1024:灯类,256:按键类,768:PIR类) /// public async Task> GetPanelDeviceFunctionLevel2(Panel panel, int levelNo) { var result = await this.GetPanelDeviceFunction(panel, new int[] { levelNo }); if (result == null) { return null; } return result.privateFuncTypeLevelSecondList; } /// /// 获取按键面板的功能 /// /// 按键面板的某一个回路 /// /// 方法1:当int[]传空,返回值是“面板具有的功能大类,即返回“第一级别。1024:灯类,256:按键类,768:PIR类 /// 方法2:、当int[]值为第一级别PrivateFuncTypeFir中选择一个。 /// 返回值是“面按键发送功能类”,即返回“第二级别。 /// 100:Switch,开关(按键类);200:Dimmer,调光(按键类);300:Curtain,窗帘(按键类) /// 0:EnergySavingMode,节能模式(灯类);1:SleepMode,睡眠模式(灯类);100:WhiteBalance,白平衡(灯类);101:RGBColor,RGB指示灯颜色(灯类);102:RGBLevel,RGB指示灯亮度(灯类) /// 方法3:当int[]值为第一级别PrivateFuncTypeFir中选择一个,接着再选第二级别PrivateFunTypeSec中选择一个 /// 返回值是“面按键具体功能配置”,即返回“第二级别。 /// 100:SwitchOpen,开关开(按键类);101:SwitchClose,开关关(按键类);102:SwitchChange,开关切换(按键类) /// 200:DimmerStepUp,增大调光(按键类);201:DimmerStepDown,降低调光(按键类);202:DimmerStepChange,调光切换(按键类) /// 300:CurtainOpen,窗帘开(按键类);301:CurtainClose,窗帘关(按键类);302:CurtainStop,窗帘停;303:CurtainUpStop,窗帘上升停;304:CurtainDownstop,窗帘下降停 /// /// private async Task GetPanelDeviceFunction(Panel panel, params int[] parameter) { var result = await panel.GetPanelPrivateFunctionsAsync(parameter); //共通错误检测 string error = UserCenterLogic.CheckCommonErrorCode(result); if (error != null) { this.ShowErrorMsg(error); return null; } if (result == null || result.panelPrivateFunctionsResponseInfo == null) { //获取按键面板功能类信息失败 string msg = Language.StringByID(R.MyInternationalizationString.uGetPanelFunctionInfoFail); //拼接上【网关回复超时】的Msg msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result.errorMessageBase); this.ShowErrorMsg(msg); return null; } return result.panelPrivateFunctionsResponseInfo; } #endregion #region ■ 一般方法___________________________ /// /// 判断是不是河东的设备 /// /// /// public bool IsHdlDevice(CommonDevice device) { return device.ManufacturerName == "HDL"; } /// /// 获取设备的唯一主键 /// /// /// public string GetDeviceMainKeys(CommonDevice device) { return device.DeviceAddr + device.DeviceEpoint; } /// /// 获取设备的唯一主键 /// /// MAC地址 /// 端口号 /// public string GetDeviceMainKeys(string DeviceAddr, int DeviceEpoint) { return DeviceAddr + DeviceEpoint; } /// /// 显示错误信息窗口 /// /// private void ShowErrorMsg(string msg) { Application.RunOnMainThread(() => { var contr = new Phone.UserCenter.ErrorMsgControl(msg); contr.Show(); }); } /// /// 显示Tip信息窗口 /// /// private void ShowTipMsg(string msg) { Application.RunOnMainThread(() => { var contr = new Phone.UserCenter.TipViewControl(msg); contr.ShowView(); }); } #endregion #region ■ 结构体_____________________________ /// /// 设备计数的信息 /// private class DeviceCountInfo { /// /// 计数 /// public int Count = 0; /// /// 端口号 /// public HashSet hsEpoint = new HashSet(); } #endregion //----------------------------------分割线(自定义接口)--------------------------------------------- #region ■ 获取设备列表的接口_________________ /// /// 从网关重新获取设备列表(★★★★★★★接收到设备时的事件,设备对象为null时,代表接收完成★★★★★★★) /// /// 网关对象 /// 接收到设备时的事件,设备对象为null时,代表接收完成 /// 是否显示错误 /// public async Task> GetDeviceListFromGateway(ZbGateway zbGateway, Action deviceComingAction = null, ShowErrorMode mode = ShowErrorMode.YES) { //如果切换到了别的界面,则不显示错误信息 string nowFormId = UserCenterResourse.NowActionFormID; ZbGateway realWay = null; if (Common.LocalGateway.Current.GetRealGateway(ref realWay, zbGateway) == false) { if (nowFormId == UserCenterResourse.NowActionFormID && mode == ShowErrorMode.YES) { //错误:网关对象丢失 string msg = Language.StringByID(R.MyInternationalizationString.uErrorGatewayLostMsg); this.ShowTipMsg(msg); } return null; } //是否达成中断的时机 bool canBreak = false; //网关ID string gatewayID = Common.LocalGateway.Current.GetGatewayId(zbGateway); //超时时间 int TimeOut = 0; //设备列表 var listDevice = new List(); Action getDeviceAction = (topic, message) => { try { if (topic == gatewayID + "/" + "DeviceInfoRespon") { TimeOut = 0; var jobject = Newtonsoft.Json.Linq.JObject.Parse(message); var totalNum = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"]["TotalNum"].ToString()); if (totalNum == 0) { //这个网关没有设备 canBreak = true; return; } var deviceID = (DeviceType)jobject.Value("Device_ID"); //根据设备类型创建设备对象的实例 var device = this.NewDeviceObjectByDeviceId(deviceID, jobject, zbGateway); if (device != null) { //回调函数 deviceComingAction?.Invoke(device); listDevice.Add(device); } } else if (topic == gatewayID + "/" + "DeviceInfoResponEnd") { canBreak = true; } } catch { } }; realWay.Actions += getDeviceAction; try { var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 93 } }; await realWay.Send("GetDeviceInfo", jObject.ToString()); } catch { canBreak = true; } while (canBreak == false && TimeOut < 60) { await Task.Delay(100); TimeOut++; } realWay.Actions -= getDeviceAction; if (TimeOut >= 60) { if (listDevice.Count == 0) { if (nowFormId == UserCenterResourse.NowActionFormID && mode == ShowErrorMode.YES) { //获取设备列表失败 //[XXXX]网关回复超时,请稍后再试 string msg = Language.StringByID(R.MyInternationalizationString.uGetDeviceListFail); msg += "\r\n[" + Common.LocalGateway.Current.GetGatewayName(zbGateway).ToString() + "]"; msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, "回复超时", false); this.ShowErrorMsg(msg); } return null; } else { if (nowFormId == UserCenterResourse.NowActionFormID && mode == ShowErrorMode.YES) { //网络不稳定,设备列表信息缺损 string msg = Language.StringByID(R.MyInternationalizationString.uNetworkUnStableAndDeviceInfoIsNotFull); this.ShowTipMsg(msg); } } } //回调函数(接收完成) deviceComingAction?.Invoke(null); return listDevice; } #endregion #region ■ 创建新设备对象相关_________________ /// /// 根据设备类型创建设备对象的实例 /// /// 设备类型 /// 主题Data /// 网关对象 /// private CommonDevice NewDeviceObjectByDeviceId(DeviceType deviceType, Newtonsoft.Json.Linq.JObject jobject, ZbGateway zbGateway) { string gwId = Common.LocalGateway.Current.GetGatewayId(zbGateway); //根据设备类型创建设备对象的实例 CommonDevice device = this.NewDeviceObjectByDeviceId(deviceType); if (device == null) { return null; } //设置设备属性类 device.DeviceInfo = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString()); if (device.DeviceInfo == null) { return null; } this.SetMacName(device, string.Empty); this.SetEpointName(device, string.Empty); //设置设备主键类 this.SetNewDeviceMainKeys(device, jobject); device.CurrentGateWayId = gwId; //将DeviceInfo的属性设置到主属性中 this.SetDeviceInfoToMain(device, device); return device; } /// /// 将DeviceInfo的属性设置到主属性中 /// /// 主设备对象 /// 设置源设备对象 public void SetDeviceInfoToMain(CommonDevice mainDevice, CommonDevice device) { if (string.IsNullOrEmpty(device.DeviceInfo.MacName) == false) { mainDevice.DeviceName = device.DeviceInfo.MacName; } if (string.IsNullOrEmpty(device.DeviceInfo.DeviceName) == false) { mainDevice.DeviceEpointName = device.DeviceInfo.DeviceName; } mainDevice.CurrentGateWayId = device.CurrentGateWayId; mainDevice.ImgVersion = device.DeviceInfo.ImgVersion; mainDevice.HwVersion = device.DeviceInfo.HwVersion; mainDevice.IsOnline = device.DeviceInfo.IsOnline; mainDevice.DriveCode = device.DeviceInfo.DriveCode; mainDevice.IasDeviceType = device.DeviceInfo.DeviceType; mainDevice.Profile = device.DeviceInfo.Profile; mainDevice.ImgTypeId = device.DeviceInfo.ImgTypeId; mainDevice.InClusterList = device.DeviceInfo.InClusterList; mainDevice.OutClusterList = device.DeviceInfo.OutClusterList; mainDevice.AttributeStatus = device.DeviceInfo.AttributeStatus; } /// /// 根据设备Type创建对应的设备对象 /// /// 设备Type /// public CommonDevice NewDeviceObjectByDeviceId(DeviceType deviceType) { CommonDevice device = null; //根据设备类型创建设备对象的实例 if (deviceType == DeviceType.ColorDimmableLight) { device = new ColorDimmableLight(); } else if (deviceType == DeviceType.DimmableLight) { device = new DimmableLight(); } else if (deviceType == DeviceType.OnOffSwitch) { device = new Panel(); } else if (deviceType == DeviceType.OnOffOutput) { device = new ToggleLight(); } else if (deviceType == DeviceType.AirSwitch) { device = new AirSwitch(); } else if (deviceType == DeviceType.WindowCoveringDevice) { device = new Rollershade(); } else if (deviceType == DeviceType.IASZone) { device = new IASZone(); } else if (deviceType == DeviceType.Repeater) { device = new Repeater(); } else if (deviceType == DeviceType.Thermostat) { device = new AC(); } else if (deviceType == DeviceType.OtaDevice || deviceType == DeviceType.OtaPanelDevice) { device = new OTADevice(); } else { return null; } device.DeviceEpointName = string.Empty; device.DeviceName = string.Empty; return device; } /// /// 给新设备设置主键属性 /// /// 设备对象 /// 主题Data public void SetNewDeviceMainKeys(CommonDevice device, Newtonsoft.Json.Linq.JObject jobject) { //设置设备主键类 device.Time = jobject.Value("Time"); device.DeviceID = jobject.Value("Device_ID"); device.DeviceAddr = jobject.Value("DeviceAddr"); device.DeviceEpoint = jobject.Value("Epoint"); device.DataID = jobject.Value("Data_ID"); } #endregion //----------------------------------分割线(设备模块ID)--------------------------------------------- #region ■ 自定义设备模块ID___________________ /// /// 初始化设备的模块ID的枚举 /// private void InitDeviceModelIdEnum() { if (this.dicDeviceModelIdEnum != null) { return; } this.dicDeviceModelIdEnum = new Dictionary(); //定义规则:模块ID(已翻译)=设备具体类型值-设备所属类型值(自定义的值,嘛,只要不重复就可以) //第一个值是:DeviceConcreteType 第二个值是:DeviceBeloneType //=========★★开合帘类(100-199)★★========= this.dicDeviceModelIdEnum["MWM65B-ZB.20"] = "100-100";//智能开合帘电机 this.dicDeviceModelIdEnum["MVSM35B-ZB.20"] = "101-100";//智能管状电机 //=========★★按键面板类(200-1199)★★========= this.dicDeviceModelIdEnum["MPT4/R4-ZB.18"] = "200-200";//4按键触摸面板(带4路继电器底座) this.dicDeviceModelIdEnum["MPT3/R3-ZB.18"] = "201-200";//3按键触摸面板 this.dicDeviceModelIdEnum["MPT2/R2-ZB.18"] = "202-200";//2按键触摸面板 this.dicDeviceModelIdEnum["MPT1/R1-ZB.18"] = "203-200";//12按键触摸面板 this.dicDeviceModelIdEnum["MPT4-ZB.18"] = "210-200";//4按键触摸面板(只带电源底座) //=========★★PIR传感器类(1200-1299)★★========= this.dicDeviceModelIdEnum["MSPIR01-ZB.10"] = "1200-1200";//pir传感器220 //=========★★安防类传感器类(1300-2299)★★========= this.dicDeviceModelIdEnum["MULTI-GASE--EA07"] = "1300-1300";//燃气传感器 this.dicDeviceModelIdEnum["MULTI-MECI--EA01"] = "1301-1300";//门窗磁传感器 this.dicDeviceModelIdEnum["MULTI-FIRE--EA05"] = "1302-1300";//烟雾传感器 this.dicDeviceModelIdEnum["MULTI-MOTI--EA04"] = "1303-1300";//红外传感器 this.dicDeviceModelIdEnum["MULTI-WATE--EA02"] = "1304-1300";//水浸传感器 this.dicDeviceModelIdEnum["MULTI-BURO--EA06"] = "1305-1300";//紧急按键 //=========★★继电器类(2300-2499)★★========= this.dicDeviceModelIdEnum["MPR0310-ZB.10"] = "2300-2300";//3路继电器小模块 //=========★★调光器类(2500-2799)★★========= this.dicDeviceModelIdEnum["MPD0101-ZB.10"] = "2500-2500";//1路调光器小模块 //=========★★空调类(3600-3899)★★========= this.dicDeviceModelIdEnum["MAC/GW-ZB.431"] = "3600-3600";//zigbee空调网关模块 //=========★★中继器类(3900-3999)★★========= this.dicDeviceModelIdEnum["MSR-ZB.10"] = "3900-3900"; //zigbee中继器 //=========★★空气开关类(4100-4199)★★========= this.dicDeviceModelIdEnum["MBCI01-ZB.10"] = "4100-4100";//zigbee微断云控制器 //=========★★转换器类(4200-4699)★★========= this.dicDeviceModelIdEnum["MBUS/GW-ZB.10"] = "4200-4200";//zigbee转485协议转换器 this.dicDeviceModelIdEnum["M485/GW-ZB.10"] = "4201-4200";//zigbee转buspro协议转换器 } #endregion } }