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