wei
2021-03-31 19e762179bdc9efc23704bc471cf974ed282b43d
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
using System;
using System.Collections.Generic;
using HDL_ON;
using HDL_ON.DAL;
using HDL_ON.DAL.Server;
using HDL_ON.Entity;
using Shared;
 
namespace HDL_ON.Common
{
    public class ApiUtlis
    {
        public ApiUtlis()
        {
        }
 
        static ApiUtlis apiUtlis;
        public static ApiUtlis Ins
        {
            get
            {
                if (apiUtlis == null)
                {
                    apiUtlis = new ApiUtlis();
                }
                return apiUtlis;
            }
        }
 
        HttpServerRequest httpRequest;
 
        public HttpServerRequest HttpRequest
        {
            get
            {
                if (httpRequest == null)
                {
                    httpRequest = new HttpServerRequest();
                }
                return httpRequest;
            }
        }
        /// <summary>
        /// 校验网关是否在线
        /// </summary>
        public bool GatewayOnlineVerification()
        {
            bool result = DB_ResidenceData.Instance.HomeGateway.gatewayStatus;
            DriverLayer.Control.Ins.GatewayOnline_Cloud = result;
            if (!result)
            {
 
            }
 
            return result;
        }
 
        public bool DownloadDataComplete = true;
        /// <summary>
        /// 下载数据
        /// </summary>
        public void DownloadData()
        {
            if (MainPage.InternetStatus == 0)
                return;
            //线程开始时间
            var endTime = DateTime.Now.AddSeconds(5);
 
            MainPage.Log($"开始同步云端数据");
 
            DownloadDataComplete = false;
 
            var waitPage = new Loading();
            MainPage.BaseView.AddChidren(waitPage);
            waitPage.Start(Language.StringByID(StringId.PleaseWait));
 
            string code = StateCode.SUCCESS;
 
            var downloadDataThread = new System.Threading.Thread(() =>
            {
                MainPage.Log($"进入读取云端数据线程");
                //===================刷新Token=======================
                code = Ins.HttpRequest.RefreshToken();
                if (code != StateCode.SUCCESS)
                {
                    MainPage.Log($"刷新token失败");
                    return;
                }
                endTime = DateTime.Now.AddSeconds(5);
                MainPage.Log($"刷新token成功");
                //===================刷新住宅信息=======================
                code = Ins.HttpRequest.GetHomePager();
                if (code != StateCode.SUCCESS)
                {
                    MainPage.Log($"刷新住宅信息失败");
                    return;
                }
                endTime = DateTime.Now.AddSeconds(5);
                MainPage.Log($"刷新住宅信息成功");
                //===================刷新个人信息=======================
                code = Ins.HttpRequest.GetUserInfo();
                if (code != StateCode.SUCCESS)
                {
                    MainPage.Log($"刷新个人信息失败");
                    return;
                }
                endTime = DateTime.Now.AddSeconds(5);
                MainPage.Log($"刷新个人信息成功");
 
                //int count = 0;
                //while (count < 10)
                //{
                //    if (DB_ResidenceData.Instance.CheckWhetherGatewayIdIsNull())
                //    {
                //        System.Threading.Thread.Sleep(200);
                //    }
                //    else
                //    {
                //        break;
                //    }
                //    count++;
                //}
                //if (DB_ResidenceData.Instance.CheckWhetherGatewayIdIsNull())
                //{
                //    return;
                //}
 
 
                try
                {
                    //===================房间=======================
                    var roomResult = Ins.HttpRequest.GetRoomList();
                    if (roomResult.Code == StateCode.SUCCESS)
                    {
                        endTime = DateTime.Now.AddSeconds(100);
                        MainPage.Log($"读取房间信息成功");
                        var revData = Newtonsoft.Json.JsonConvert.DeserializeObject<SpatialApiPack>(roomResult.Data.ToString());
                        if (revData == null)
                        {
                            revData = new SpatialApiPack();
                        }
                        {
                            SpatialInfo.CurrentSpatial.UpdateSpatialList(revData.list);
                        }
                        endTime = DateTime.Now.AddSeconds(5);
                    }
                    else
                    {
                        MainPage.Log($"读取房间数据失败:Code:{roomResult.Code}; msg:{roomResult.message}");
                    }
                    //===================设备=======================
                    var deviceResult = Ins.HttpRequest.GetDeviceList();
                    if (deviceResult.Code == StateCode.SUCCESS)
                    {
                        endTime = DateTime.Now.AddSeconds(100);
                        MainPage.Log($"读取设备信息成功");
                        var deviceList = Newtonsoft.Json.JsonConvert.DeserializeObject<DevcieApiPack>(deviceResult.Data.ToString());
                        if (deviceList == null)
                        {
                            deviceList = new DevcieApiPack();
                        }
                        string delFile = "";
                        if (FunctionList.List.GetDeviceFunctionList().Count > 0)
                        {
 
                            for (int i = 0; i < FunctionList.List.GetDeviceFunctionList().Count;)
                            {
                                var localFunction = FunctionList.List.GetDeviceFunctionList()[i];
                                if (localFunction.Spk_Prefix == FunctionCategory.Music || string.IsNullOrEmpty(localFunction.Spk_Prefix))
                                {
                                    i++;
                                    continue;
                                }
                                var newFunction = deviceList.list.Find((obj) => obj.deviceId == localFunction.deviceId);
 
                                if (delFile == localFunction.savePath)
                                {
                                    i++;
                                    continue;
                                }
                                delFile = localFunction.savePath;
                                FunctionList.List.DeleteFunction(localFunction);
                            }
                        }
                        //处理剩下的新增功能
                        foreach (var newFunction in deviceList.list) 
                        {
                            MainPage.Log(newFunction.savePath);
                            newFunction.SaveFunctionFile();
                            FunctionList.List.IniFunctionList(newFunction.savePath);
                        }
                        endTime = DateTime.Now.AddSeconds(5);
                    }
                    else
                    {
                        MainPage.Log($"读取云端设备数据失败:Code:{deviceResult.Code};  Msg:{deviceResult.message}");
                    }
 
                    //===================场景==========================
                    var pack = Ins.HttpRequest.GetSceneList();
                    if (pack.Code == StateCode.SUCCESS)
                    {
                        endTime = DateTime.Now.AddSeconds(100);
                        MainPage.Log($"读取场景数据成功");
                        var sceneList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Scene>>(pack.Data.ToString());
                        if (sceneList == null)
                        {
                            sceneList = new List<Scene>();
                        }
                        for (int i = 0; i < FunctionList.List.scenes.Count;)
                        {
                            var localScene = FunctionList.List.scenes[i];
                            if (localScene == null)
                            {
                                FunctionList.List.scenes.Remove(localScene);
                                continue;
                            }
                            var newScene = sceneList.Find((obj) => obj.userSceneId == localScene.userSceneId);
                            if (newScene == null)//如果云端最新数据没有该条数据,则本地需要删掉该数据记录
                            {
                                FunctionList.List.DeleteScene(localScene, false);
                            }
                            else
                            {
                                i++;
                                if (localScene.modifyTime != newScene.modifyTime)
                                {
                                    localScene.name = newScene.name;
                                    localScene.collect = newScene.collect;
                                    localScene.modifyTime = newScene.modifyTime;
                                    localScene.roomIds = newScene.roomIds;
                                    localScene.image = newScene.image;
                                    localScene.SaveSceneFile();
                                }
                                sceneList.Remove(newScene);//操作完的数据清理掉,剩下的就是新增的功能
                            }
                        }
                        //处理剩下的新增功能
                        foreach (var tempScene in sceneList)
                        {
                            tempScene.SaveSceneFile();
                            FunctionList.List.scenes.Add(tempScene);
                        }
                        //如果是一端口需要提前下载场景数据,否则控制不了一端口场景
                        if (DB_ResidenceData.Instance.GatewayType == 0)
                        {
                            var sceneIds = new List<string>();
                            foreach (var tempScene in FunctionList.List.scenes)
                            {
                                sceneIds.Add(tempScene.userSceneId);
                            }
                            var packInfo = HttpRequest.GetSceneListInfo(sceneIds);
                            if (packInfo.Code == StateCode.SUCCESS)
                            {
                                var serverTempList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Scene>>(packInfo.Data.ToString());
                                foreach (var localScene in FunctionList.List.scenes)
                                {
                                    var serverTemp = serverTempList.Find((obj) => obj.userSceneId == localScene.userSceneId);
                                    if (serverTemp != null)
                                    {
                                        localScene.functions = serverTemp.functions;
                                        localScene.SaveSceneFile();
                                    }
                                }
                            }
                            else
                            {
                                IMessageCommon.Current.ShowErrorInfoAlter(packInfo.Code);
                            }
                        }
                        endTime = DateTime.Now.AddSeconds(5);
                    }
                    else
                    {
                        MainPage.Log($"读取云端场景数据失败:Code:{pack.Code}; Msg:{pack.message}");
                    }
 
                    //===================读取逻辑列表==========================
                    UI.UI2.Intelligence.Automation.MainView.GetLogicList();
                    //===================读取可视对讲数据==========================
                    var flVideoInfo = HttpRequest.CheckFlVideo();
                    if(flVideoInfo.Code == StateCode.SUCCESS)
                    {
                        FunctionList.List.videoIntercom = Newtonsoft.Json.JsonConvert.DeserializeObject<UI.UI2.FuntionControlView.Video.VideoMethod.VideoClouds>(flVideoInfo.Data.ToString());
                    }
 
                }
                catch (Exception ex)
                {
                    MainPage.Log($"数据初始化失败:{ex.Message}");
                }
                finally
                {
                    Application.RunOnMainThread(() =>
                    {
                        MainPage.Log($"结束读取云端数据线程");
                        endTime = DateTime.MinValue;
                        DownloadDataComplete = true;
                        waitPage.Hide();
                        waitPage.RemoveFromParent();
                    });
                    //不考虑网关情况,直接订阅
                    DAL.Mqtt.MqttClient.InitState();
 
                    FunctionList.List.Read3tyFunctionStatus();
                }
            });
            downloadDataThread.IsBackground = true;
            downloadDataThread.Priority = System.Threading.ThreadPriority.Highest;
            downloadDataThread.Start();
 
            //网络卡顿,终止下载线程
            new System.Threading.Thread(() =>
            {
                try
                {
                    while (endTime > DateTime.Now)
                    {
                        if (DownloadDataComplete)
                        {
                            break;
                        }
                        System.Threading.Thread.Sleep(100);
                    }
                    if (!DownloadDataComplete)
                    {
                        //downloadDataThread.Abort();
                    }
                    DownloadDataComplete = true;
                    Application.RunOnMainThread(() =>
                    {
                        waitPage.Hide();
                        waitPage.RemoveFromParent();
                    });
                    //初始化数据
                    //SpatialInfo.CurrentSpatial.Clear();
 
                }
                catch { }
                finally
                {
                    if (DB_ResidenceData.Instance.CurrentRegion != null)
                    {
                        #region 读取本地数据
                        //读取音乐信息
                        UI.Music.A31MusicModel.ReadMusicStates();
                        //搜索网关
                        DriverLayer.Control.Ins.SearchLoaclGateway();
                        #endregion
                    }
                }
            })
            { IsBackground = true }.Start();
        }
 
        /// <summary>
        /// 获取功能信息
        /// </summary>
        public void GetFunctionInfo(string functionId)
        {
            var deviceResult = Ins.HttpRequest.GetDeviceInfo(functionId);
            if (deviceResult.Code == StateCode.SUCCESS)
            {
                MainPage.Log($"读取设备信息成功");
                var packList = Newtonsoft.Json.JsonConvert.DeserializeObject<DevcieApiPack>(deviceResult.Data.ToString());
 
                foreach(var function in packList.list)
                {
                    function.SaveFunctionFile();
                    FunctionList.List.IniFunctionList(function.savePath,true);
                }
            }
        }
    }
}