using Newtonsoft.Json;
|
using System;
|
using System.Collections.Generic;
|
using System.Text;
|
using System.Threading.Tasks;
|
using ZigBee.Device;
|
|
namespace Shared.Common
|
{
|
/// <summary>
|
/// 安防
|
/// </summary>
|
public class LocalSecurity
|
{
|
/// <summary>
|
/// 本地安防数据
|
/// </summary>
|
private static LocalSecurity m_Current = null;
|
/// <summary>
|
/// 本地安防数据
|
/// </summary>
|
public static LocalSecurity Current
|
{
|
get
|
{
|
if (m_Current == null)
|
{
|
m_Current = new LocalSecurity();
|
}
|
return m_Current;
|
}
|
set
|
{
|
m_Current = value;
|
}
|
}
|
|
/// <summary>
|
/// 安防文件
|
/// </summary>
|
public const string LocalSecurityFile = "LocalSecurityFile.json";
|
/// <summary>
|
/// 安防所有的设备路径(Keys:布防ID)
|
/// </summary>
|
private Dictionary<int, List<string>> dicAllDevicePath = new Dictionary<int, List<string>>();
|
/// <summary>
|
/// 是否已经从网关刷新过
|
/// </summary>
|
private bool IsRefreshed = false;
|
|
/// <summary>
|
/// 刷新本地网关信息
|
/// </summary>
|
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<LocalSecurity>(strvalue);
|
}
|
|
/// <summary>
|
/// 从新从网关那里获取数据
|
/// </summary>
|
/// <returns></returns>
|
/// <param name="reFresh">true:从新从网关获取数据 false:如果已经初始化一次后,不再刷新</param>
|
public async Task<bool> 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;
|
}
|
|
// <summary>
|
/// 获取各防区下面的全部设备
|
/// </summary>
|
/// <param name="allData"></param>
|
/// <returns></returns>
|
private async Task<Dictionary<int, ZigBee.Device.Safeguard.GetZoneDeviceListByIdResponAllData>> GetZoneDeviceList(ZigBee.Device.Safeguard.GetZoneInfoResponAllData allData)
|
{
|
var dic = new Dictionary<int, ZigBee.Device.Safeguard.GetZoneDeviceListByIdResponAllData>();
|
//先获取全部防区ID
|
List<int> listCheck = new List<int>();
|
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;
|
}
|
|
/// <summary>
|
/// 将防区设备列表加入缓存
|
/// </summary>
|
/// <param name="dic">各防区数据</param>
|
private void SetZoneDeviceToMemory(Dictionary<int, ZigBee.Device.Safeguard.GetZoneDeviceListByIdResponAllData> dic)
|
{
|
foreach (int zoneId in dic.Keys)
|
{
|
if (this.dicAllDevicePath.ContainsKey(zoneId) == false)
|
{
|
this.dicAllDevicePath[zoneId] = new List<string>();
|
}
|
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);
|
}
|
}
|
}
|
|
/// <summary>
|
/// 获取指定防区的所有设备
|
/// </summary>
|
/// <param name="zoonId"></param>
|
/// <returns></returns>
|
public List<CommonDevice> GetDevicesByZoonID(int zoonId)
|
{
|
var list = new List<CommonDevice>();
|
if (dicAllDevicePath.ContainsKey(zoonId) == false)
|
{
|
return list;
|
}
|
List<string> listPath = dicAllDevicePath[zoonId];
|
foreach (string strPath in listPath)
|
{
|
var device = this.GetDevice(strPath);
|
|
list.Add(device);
|
}
|
|
return list;
|
}
|
|
/// <summary>
|
/// 获取全部的设备
|
/// </summary>
|
/// <returns></returns>
|
public List<CommonDevice> GetAllDevices()
|
{
|
var listData = new List<CommonDevice>();
|
|
foreach (int ZoonId in dicAllDevicePath.Keys)
|
{
|
List<CommonDevice> list = this.GetDevicesByZoonID(ZoonId);
|
listData.AddRange(list);
|
}
|
return listData;
|
}
|
|
/// <summary>
|
/// 添加设备
|
/// </summary>
|
/// <param name="zoonId">防区ID</param>
|
/// <param name="device"></param>
|
public void AddDevice(int zoonId, CommonDevice device)
|
{
|
if (dicAllDevicePath.ContainsKey(zoonId) == false)
|
{
|
dicAllDevicePath[zoonId] = new List<string>();
|
}
|
if (dicAllDevicePath[zoonId].Contains(device.FilePath) == true)
|
{
|
return;
|
}
|
dicAllDevicePath[zoonId].Add(device.FilePath);
|
|
this.Save();
|
}
|
|
/// <summary>
|
/// 删除设备
|
/// </summary>
|
/// <param name="zoonId">防区ID</param>
|
/// <param name="device"></param>
|
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();
|
}
|
|
/// <summary>
|
/// 获取设备
|
/// </summary>
|
/// <param name="strPath"></param>
|
/// <returns></returns>
|
private CommonDevice GetDevice(string strPath)
|
{
|
byte[] myByte = Global.ReadFileByHomeId(strPath);
|
var device = JsonConvert.DeserializeObject<CommonDevice>(System.Text.Encoding.UTF8.GetString(myByte));
|
return device;
|
}
|
|
/// <summary>
|
/// 保存现阶段的网关信息到本地文件
|
/// </summary>
|
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);
|
}
|
}
|
}
|