wxr
2021-07-01 43b0d5870d528f23ecd6aeceb6cfd4325188b46f
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using HDL_ON.Common;
using HDL_ON.DAL;
using HDL_ON.DAL.Server;
using Shared;
 
namespace HDL_ON.Entity
{
    public class FunctionList
    {
        static FunctionList _FunctionList;
        public static FunctionList List
        {
            get
            {
                if (_FunctionList == null)
                {
                    _FunctionList = new FunctionList();
                    #region 恢复数据
                    var filePathList = FileUtlis.Files.ReadFiles();
                    foreach (var filePath in filePathList)
                    {
                        _FunctionList.IniFunctionList(filePath);
                    }
                    #endregion
                }
                return _FunctionList;
            }
        }
        /// <summary>
        /// 功能列表
        /// </summary>
        public List<Function> Functions = new List<Function>();
 
        /// <summary>
        /// 空调列表
        /// </summary>
        /// <returns></returns>
        public List<Function> GetAcList()
        {
            var spkList = SPK.AcSpkList();
            return Functions.FindAll((obj) => spkList.Contains(obj.spk));
        }
 
        /// <summary>
        /// 灯光列表
        /// </summary>
        public List<Function> GetLightList()
        {
            var spkList = SPK.LightSpkList();
            return Functions.FindAll((obj) => spkList.Contains(obj.spk));
        }
        /// <summary>
        /// 窗帘列表
        /// </summary>
        public List<Function> GetCurtainList()
        {
            var spkList = SPK.CurtainSpkList();
            return Functions.FindAll((obj) => spkList.Contains(obj.spk));
        }
 
        /// <summary>
        /// 地热列表
        /// </summary>
        public List<Function> GetFloorHeatingList()
        {
            var spkList = SPK.FhSpkList();
            return Functions.FindAll((obj) => spkList.Contains(obj.spk));
        }
 
        /// <summary>
        /// 新风列表
        /// </summary>
        /// <returns></returns>
        public List<Function> GetAirFreshList()
        {
            var spkList = SPK.AirFreshSpkList();
            return Functions.FindAll((obj) => spkList.Contains(obj.spk));
        }
 
        /// <summary>
        /// 能源列表
        /// </summary>
        /// <returns></returns>
        public List<Function> GetEnergyList()
        {
            var spkList = SPK.EnergySpkList();
            return Functions.FindAll((obj) => spkList.Contains(obj.spk));
        }
 
        #region 家电列表 electricals
        /// <summary>
        /// 家电列表
        /// </summary>
        public List<Function> GetElectricals()
        {
            var spkList = SPK.ElectricalSpkList();
            return Functions.FindAll((obj) => spkList.Contains(obj.spk));
        }
    
      
        /// <summary>
        /// 红外宝列表
        /// </summary>
        public List<Function> GetIrModuleList()
        {
            return Functions.FindAll((obj) => obj.spk == SPK.IrModule);
        }
        #endregion
 
        /// <summary>
        /// 音乐列表
        /// </summary>
        /// <returns></returns>
        public List<Function> GetMusicList()
        {
            var spkList = SPK.MusicSpkList();
            return Functions.FindAll((obj) => spkList.Contains(obj.spk));
        }
 
 
        /// <summary>
        /// 环境传感器列表
        /// </summary>
        public List<Function> GetEnvirSensorsList()
        {
            var spkList = SPK.EnvironSpkList();
            return Functions.FindAll((obj) => spkList.Contains(obj.spk));
        }
        /// <summary>
        /// 安防设备列表
        /// </summary>
        public List<Function> GetArmSensorList()
        {
            var spkList = SPK.ArmSensorSpkList();
            return Functions.FindAll((obj) => spkList.Contains(obj.spk));
        }
 
        /// <summary>
        /// 可视对讲列表,当为null或size为0则不显示可视对讲icon
        /// </summary>
        // public UI.UI2.FuntionControlView.Video.VideoClouds videoIntercom;
        public List<UI.UI2.FuntionControlView.Video.Video> videoIntercom = new List<UI.UI2.FuntionControlView.Video.Video>();
 
        List<Function> _OtherBrandFunction;
        /// <summary>
        /// 第三方品牌功能
        /// </summary>
        public List<Function> OtherBrandFunction
        {
            get
            {
                if(_OtherBrandFunction == null)
                {
                    _OtherBrandFunction = new List<Function>();
                    var spkList = SPK.GetAll3tySPK();
                    _OtherBrandFunction = Functions.FindAll((obj) => spkList.Contains(obj.spk));
                }
                return _OtherBrandFunction;
            }
        }
 
 
        /// <summary>
        /// 场景列表
        /// </summary>
        public List<Scene> scenes = new List<Scene>();
 
        /// <summary>
        /// 安防列表
        /// </summary>
        public List<SecurityAlarm> securities = new List<SecurityAlarm>();
 
        /// <summary>
        /// 加载功能列表
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="checkRepeat">
        /// <para>考虑到性能问题追加此变量,是否检测重复,只对function有效</para>
        /// <para>true:当重复存在时,替换掉</para>
        /// <para>false:直接无条件添加</para></param>
        public void IniFunctionList(string filePath, bool checkRepeat = false)
        {
            //MainPage.Log("12346890sdkxghjkl");
            // todo 增加设备,增加功能进功能列表
            if (filePath.StartsWith("FunctionData_"))
            {
                var functionDataBytes = FileUtlis.Files.ReadFile(filePath);
                var functionDataString = System.Text.Encoding.UTF8.GetString(functionDataBytes);
                var tempFunction = Newtonsoft.Json.JsonConvert.DeserializeObject<Function>(functionDataString);
                if (tempFunction == null)
                {
                    MainPage.Log("null");
                    FileUtlis.Files.DeleteFile(filePath);
                    return;
                }
                //音乐部分要修改
                //if(tempFunction.spk.ToString() == SPK.MusicStandard)
                //{
                //    UI.Music.A31MusicModel.A31MusicModelList.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<UI.Music.A31MusicModel>(functionDataString));
                //}
                if (checkRepeat == true)
                {
                    //检测重复
                    for (int i = 0; i < Functions.Count; i++)
                    {
                        if (Functions[i].deviceId == tempFunction.deviceId)
                        {
                            //先移除掉再加
                            Functions.RemoveAt(i);
                            Functions.Add(tempFunction);
                            return;
                        }
                    }
                }
                Functions.Add(tempFunction);
            }
            if (filePath.StartsWith("SceneData_"))
            {
                var sceneDataBytes = FileUtlis.Files.ReadFile(filePath);
                var sceneDataString = System.Text.Encoding.UTF8.GetString(sceneDataBytes);
                var tempScene = Newtonsoft.Json.JsonConvert.DeserializeObject<Scene>(sceneDataString);
                List.scenes.Add(tempScene);
            }
            else if (filePath.StartsWith("SecurityData_"))
            {
                var dataBytes = FileUtlis.Files.ReadFile(filePath);
                var dataString = System.Text.Encoding.UTF8.GetString(dataBytes);
                var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<SecurityAlarm>(dataString);
                if (temp == null)
                {
                    MainPage.Log("null");
                    FileUtlis.Files.DeleteFile(filePath);
                    return;
                }
                if (checkRepeat == true)
                {
                    //检测重复
                    for (int i = 0; i < securities.Count; i++)
                    {
                        if (securities[i].sid == temp.sid)
                        {
                            //先移除掉再加
                            securities.RemoveAt(i);
                            securities.Add(temp);
                            return;
                        }
                    }
                }
                securities.Add(temp);
            }
        }
 
        /// <summary>
        /// 获取设备功能列表
        /// </summary>
        public List<Function> GetDeviceFunctionList(SPK.BrandType brandType = SPK.BrandType.All)
        {
            List<Function> resultFunctions = new List<Function>();
            if (brandType == SPK.BrandType.All)
            {
            }
            else
            { 
                switch (brandType)
                {
                    case SPK.BrandType.Hdl:
                        foreach (var function in Functions)
                        {
                            if (!SPK.Get3tySpk(SPK.BrandType.All3tyBrand).Contains(function.spk))
                            {
                                resultFunctions.Add(function);
                            }
                        }
                        break;
 
                    case SPK.BrandType.Tuya:
                        foreach (var function in Functions)
                        {
                            if (SPK.Get3tySpk(SPK.BrandType.Tuya).Contains(function.spk))
                            {
                                resultFunctions.Add(function);
                            }
                        }
                        break;
                }
                return resultFunctions;
            }
 
            return Functions;
        }
 
 
 
 
        /// <summary>
        /// 清空设备功能列表
        /// </summary>
        public void Clear()
        {
            Functions = new List<Function>();
            _FunctionList = null;
        }
 
        /// <summary>
        /// 功能绑定房间
        /// </summary>
        public string FunctionsBindRooms(List<string> roomIds,List<string> deviceIds)
        {
            var pm = new HttpServerRequest();
            var pack = pm.BindDeviceToRoom(deviceIds, roomIds);
            if(pack.Code == StateCode.SUCCESS)
            {
 
            }
            else
            {
                IMessageCommon.Current.ShowErrorInfoAlter(pack.Code);
            }
            return pack.Code;
        }
 
        /// <summary>
        /// 删除场景
        /// todo
        /// </summary>
        public void DeleteScene(Scene scene,bool upSevser)
        {
            if (upSevser)
            {
                var pm = new HttpServerRequest();
                var packCode = pm.DeleteScene(scene.userSceneId);
                if (packCode == StateCode.SUCCESS)
                {
                    FileUtlis.Files.DeleteFile(scene.savePath);
                    List.scenes.Remove(scene);
                }
                else
                {
                    IMessageCommon.Current.ShowErrorInfoAlter(packCode);
                }
            }
            else
            {
                FileUtlis.Files.DeleteFile(scene.savePath);
                List.scenes.Remove(scene);
            }
        }
 
        /// <summary>
        /// 删除本地功能及文件
        /// </summary>
        public void DeleteFunction(Function delTemp)
        {
            Functions.Remove(Functions.Find((obj) => obj.deviceId == delTemp.deviceId));
            FileUtlis.Files.DeleteFile(delTemp.savePath);
        }
 
        bool lockReadFunctionStatus = false;
 
        /// <summary>
        /// 读取第三方设备状态
        /// </summary>
        public void Read3tyFunctionStatus()
        {
            //读取涂鸦设备状态
            if (GetDeviceFunctionList(SPK.BrandType.Tuya).Count > 0)
            {
                var page = new List<string>();
                int count = 0;
                foreach (var function in GetDeviceFunctionList(SPK.BrandType.Tuya))
                {
                    page.Add(function.deviceId);
                    count++;
                    if (count > 9)
                    {
                        DriverLayer.Control.Ins.ReadFunctionsInfo(page);
                        page.Clear();
                        count = 0;
                    }
                }
                DriverLayer.Control.Ins.ReadFunctionsInfo(page);
            }
 
        }
 
        /// <summary>
        /// 读取全部功能的状态
        /// </summary>
        public void ReadAllFunctionStatus()
        {
            if (lockReadFunctionStatus)
            {
                MainPage.Log($"多次进入--返回");
                return;
            }
            new Thread(() =>
            {
                if (lockReadFunctionStatus)
                {
                    MainPage.Log($"多次进入--返回");
                    return;
                }
                MainPage.Log($"进入--读取");
                lockReadFunctionStatus = true;
                try
                {
                    Read3tyFunctionStatus();
 
                    if(DriverLayer.Control.Ins.GatewayOnline_Local)
                    {
                        foreach (var function in GetDeviceFunctionList(SPK.BrandType.Hdl))
                        {
                            switch (function.Spk_Prefix)
                            {
                                case FunctionCategory.Music:
                                case FunctionCategory.DryContact:
                                    continue;
                            }
                            DriverLayer.Control.Ins.SendReadCommand(function);
                            //switch (function.Spk_Prefix)
                            //{
                            //    case FunctionCategory.Light:
                            //    case FunctionCategory.Electric:
                            //    case FunctionCategory.Curtain:
                            //    case FunctionCategory.AC:
                            //    case FunctionCategory.FloorHeat:
                            //    case FunctionCategory.Sensor:
                            //        DriverLayer.Control.Ins.SendReadCommand(function);
                            //        break;
                            //}
                        }
                    }
                    else
                    {
                        var page = new List<string>();
                        int count = 0;
 
                        foreach (var function in GetDeviceFunctionList(SPK.BrandType.Hdl))
                        {
                            page.Add(function.deviceId);
                            count++;
                            if (count > 9)
                            {
                                DriverLayer.Control.Ins.SendApiReadCommand(page);
                                page.Clear();
                                count = 0;
                            }
                        }
                        DriverLayer.Control.Ins.SendApiReadCommand(page);
                    }
                }
                catch (Exception ex)
                {
                    MainPage.Log($"Refresh Residence Function Status Error : {ex.Message}");
                }
                finally
                {
                    lockReadFunctionStatus = false;
                    MainPage.Log($"结束--读取");
                }
 
                
            })
            { IsBackground = true }.Start();
        }
 
        #region 场景相关
        /// <summary>
        /// 添加场景
        /// </summary>
        public string AddScene(Scene scene, out Scene result)
        {
            Scene tempScene = null;
            var pm = new HttpServerRequest();
            var revPack = pm.AddScene(scene);
            if (revPack.Code == StateCode.SUCCESS)
            {
                var sceneList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Scene>>(revPack.Data.ToString());
                tempScene = sceneList.Find((obj) => obj.sid == scene.sid);
                var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(tempScene));
                FileUtlis.Files.WriteFileByBytes(scene.savePath, ssd);
                result = tempScene;
            }
            else
            {
                result = tempScene;
            }
            return revPack.Code;
        }
        #endregion
 
        #region 收藏功能
     
        /// <summary>
        /// 收藏场景
        /// </summary>
        public string CollectScene(Scene scene)
        {
            var result = "";
            if (scene.collect)
            {
                result = ApiUtlis.Ins.HttpRequest.CollectDevice(scene.userSceneId).Code;
            }
            else
            {
                result = ApiUtlis.Ins.HttpRequest.CancelCollectDevice(scene.userSceneId).Code;
            }
 
            //提示错误
            if (result != StateCode.SUCCESS)
            {
                IMessageCommon.Current.ShowErrorInfoAlter(result);
            }
 
            return result;
        }
 
        #endregion 
 
 
        #region 
       
 
 
        #endregion
    }
 
}