using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using ZigBee.Device; namespace Shared.Common { /// /// 安防 /// public class LocalSecurity { /// /// 本地安防数据 /// private static LocalSecurity m_Current = null; /// /// 本地安防数据 /// public static LocalSecurity Current { get { if (m_Current == null) { m_Current = new LocalSecurity(); } return m_Current; } set { m_Current = value; } } /// /// 安防文件 /// public const string LocalSecurityFile = "LocalSecurityFile.json"; /// /// 安防所有的设备路径(Keys:布防ID) /// private Dictionary> dicAllDevicePath = new Dictionary>(); /// /// 是否已经从网关刷新过 /// private bool IsRefreshed = false; /// /// 刷新本地网关信息 /// public void ReFreshByLocal() { if (Global.IsExistsByHomeId(LocalSecurityFile) == false) { return; } byte[] filebyte = Global.ReadFileByHomeId(LocalSecurityFile); string strvalue = System.Text.Encoding.UTF8.GetString(filebyte); m_Current = JsonConvert.DeserializeObject(strvalue); } /// /// 从新从网关那里获取数据 /// /// /// true:从新从网关获取数据 false:如果已经初始化一次后,不再刷新 public async Task ReFreshByGateway(bool reFresh = false) { if (reFresh == false && IsRefreshed == true) { return true; } this.IsRefreshed = true; //获取全部布防信息 var allData = await ZigBee.Device.Safeguard.GetZoneInfoAsync(); if (string.IsNullOrEmpty(allData.errorMessageBase) == false) { return false; } //获取各防区下面的全部设备 var dic = await GetZoneDeviceList(allData); return true; } // /// 获取各防区下面的全部设备 /// /// /// private async Task> GetZoneDeviceList(ZigBee.Device.Safeguard.GetZoneInfoResponAllData allData) { var dic = new Dictionary(); //先获取全部防区ID List listCheck = new List(); foreach (ZigBee.Device.Safeguard.ZoneInfoListData data in allData.getZoneInfoResponData.ZoneList) { //获取指定布防列表下面的设备 if (listCheck.Contains(data.ZoneId) == true) { continue; } listCheck.Add(data.ZoneId); //通过防区ID获取防区设备列表 var data2 = await ZigBee.Device.Safeguard.getZoneDeviceListByIdAsync(data.ZoneId); if (string.IsNullOrEmpty(data2.errorMessageBase) == false) { continue; } dic[data.ZoneId] = data2; } return dic; } /// /// 将防区设备列表加入缓存 /// /// 各防区数据 private void SetZoneDeviceToMemory(Dictionary dic) { foreach (int zoneId in dic.Keys) { if (this.dicAllDevicePath.ContainsKey(zoneId) == false) { this.dicAllDevicePath[zoneId] = new List(); } var listData = dic[zoneId].getZoneDeviceListByIdResponData.DeviceList; foreach (var data2 in listData) { //本地是否有这个设备 string mainKey = data2.MacAddr + data2.Epoint; CommonDevice device = LocalDevice.Current.GetDevice(mainKey); if (device == null) { continue; } this.dicAllDevicePath[zoneId].Add(device.FilePath); } } } /// /// 获取指定防区的所有设备 /// /// /// public List GetDevicesByZoonID(int zoonId) { var list = new List(); if (dicAllDevicePath.ContainsKey(zoonId) == false) { return list; } List listPath = dicAllDevicePath[zoonId]; foreach (string strPath in listPath) { var device = this.GetDevice(strPath); list.Add(device); } return list; } /// /// 获取全部的设备 /// /// public List GetAllDevices() { var listData = new List(); foreach (int ZoonId in dicAllDevicePath.Keys) { List list = this.GetDevicesByZoonID(ZoonId); listData.AddRange(list); } return listData; } /// /// 添加设备 /// /// 防区ID /// public void AddDevice(int zoonId, CommonDevice device) { if (dicAllDevicePath.ContainsKey(zoonId) == false) { dicAllDevicePath[zoonId] = new List(); } if (dicAllDevicePath[zoonId].Contains(device.FilePath) == true) { return; } dicAllDevicePath[zoonId].Add(device.FilePath); this.Save(); } /// /// 删除设备 /// /// 防区ID /// public void DeleteDevice(int zoonId, CommonDevice device) { if (dicAllDevicePath.ContainsKey(zoonId) == false) { return; } if (dicAllDevicePath[zoonId].Contains(device.FilePath) == true) { dicAllDevicePath[zoonId].Remove(device.FilePath); } this.Save(); } /// /// 获取设备 /// /// /// private CommonDevice GetDevice(string strPath) { byte[] myByte = Global.ReadFileByHomeId(strPath); var device = JsonConvert.DeserializeObject(System.Text.Encoding.UTF8.GetString(myByte)); return device; } /// /// 保存现阶段的网关信息到本地文件 /// public async void Save() { var file = await Task.Factory.StartNew(() => JsonConvert.SerializeObject(this)); var bytes = System.Text.Encoding.UTF8.GetBytes(file); Global.WriteFileByBytesByHomeId(LocalSecurityFile, bytes); } } }