HDL Home App 第二版本 旧平台金堂用 正在使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
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);
        }
    }
}