wxr
2023-06-06 592974441a4df95fffd9167c90192da1a390b1c2
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
using System;
using System.Collections.Generic;
using HDL_ON.DAL.Server;
using Shared;
 
namespace HDL_ON.Entity
{
    /// <summary>
    /// 读取服务器空间信息返回到数据包
    /// </summary>
    public class SpatialApiPack
    {
        public List<SpatialInfo> list = new List<SpatialInfo>();
 
        public string totalCount = "0";
 
        public string totalPage = "0";
 
        public string pageNo = "0";
 
        public string pageSize = "0";
 
    }
    /// <summary>
    /// 空间信息基础类
    /// 包含楼层/房间
    /// </summary>
    public class SpatialInfo
    {
        static SpatialInfo _currentSpatial;
        public static SpatialInfo CurrentSpatial
        {
            get
            {
                if (_currentSpatial == null)
                {
                    _currentSpatial = new SpatialInfo();
                }
                return _currentSpatial;
            }
        }
        public SpatialInfo()
        {
        }
 
        public SpatialInfo(string spatialType)
        {
            roomType = "FLOOR";
            parentId = DB_ResidenceData.Instance.CurrentRegion.id;
        }
 
        public string homeId = "";
        public string roomId = "";
        public string roomName = "";
        //public string roomImage = "";
        public string roomImage = "Classification/Room/Roombg.png";
        public string roomType = "";
        public string parentId = "";
        public string uid = Guid.NewGuid().ToString();
        public string floorRoomName = "";
        public string createTime = "";
        public string modifyTime = "";
        ///// <summary>
        ///// 删除标记 20201208:可欣:APP的在线和离线问题,刚刚确定了,离线无法编辑,只能控制
        ///// 需要删除数据时,标记为:true
        ///// 由云端删除成功之后,返回数据再清除本地数据
        ///// </summary>
        //public bool DeleteSign = false;
 
        /// <summary>
        /// 更新空间信息
        /// </summary>
        /// <param name="spatials">空间信息</param>
        /// <param name="optionType">操作类型:ADD/UPDATE/DELETE</param>
        public void UpdateSpatialList(List<SpatialInfo> spatials)
        {
            var roomUpdateList = spatials.FindAll((obj) => obj.roomType == "ROOM");
            var floorUpdateList = spatials.FindAll((obj) => obj.roomType == "FLOOR");
            CurrentSpatial.DeleteAllRoom();
            FloorList.Clear();
            //处理房间列表
            if (roomUpdateList != null && roomUpdateList.Count > 0)
            {
                foreach (var newRoom in roomUpdateList)
                {
                    newRoom.SaveRoomFile();
                    RoomList.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<Room>(
                        Newtonsoft.Json.JsonConvert.SerializeObject(newRoom)));
                }
            }
            //楼层房间列表
            if (floorUpdateList != null && floorUpdateList.Count > 0)
            {
                foreach (var updateTemp in floorUpdateList)
                {
                    FloorList.Add(updateTemp);
                }
                var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(FloorList));
                Common.FileUtlis.Files.WriteFileByBytes(dataSavePath, ssd);
            }
        }
 
        #region 楼层列表
        /// <summary>
        /// 楼层列表
        /// </summary>
        List<SpatialInfo> floors;
        /// <summary>
        /// 楼层列表
        /// </summary>
        public List<SpatialInfo> FloorList
        {
            get
            {
                if (MainPage.NoLoginMode)
                {
                    if (floors == null)
                    {
                        floors = new List<SpatialInfo>();
                    }
                    return floors;
                }
 
                if (floors == null)
                {
                    try
                    {
                        var floorsDataBytes = Common.FileUtlis.Files.ReadFile("FloorsData");
                        if (floorsDataBytes != null)
                        {
                            var floorsDataString = System.Text.Encoding.UTF8.GetString(floorsDataBytes);
                            MainPage.Log(floorsDataString);
                            floors = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SpatialInfo>>(floorsDataString);
                        }
                        if(floors == null)
                        {
                            floors = new List<SpatialInfo>();
                        }
                    }
                    catch (Exception ex)
                    {
                            floors = new List<SpatialInfo>();
                        MainPage.Log($"楼层数据初始化失败:{ex.Message}");
                    }
                }
                return floors;
            }
        }
        /// <summary>
        /// 数据保存路径
        /// </summary>
        string dataSavePath
        {
            get
            {
                if(roomType == "ROOM")
                {
                    return "RoomPath_" + uid;
                }
                if(roomType == "FLOOR")
                {
                    return "FloorsData"; 
                }
                else
                {
                    return "SpatialInfo_"+uid;
                }
            }
        }
        /// <summary>
        /// 增加楼层
        /// </summary>
        /// <param name="floor"></param>
        public string AddFloor(SpatialInfo floor, out SpatialInfo newFloor)
        {
            var pm = new HttpServerRequest();
            var revPack = new ResponsePackNew();
            revPack = pm.AddRoom(new List<SpatialInfo>() { floor });
            if (revPack.Code == StateCode.SUCCESS)
            {
                var revData = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SpatialInfo>>(revPack.Data.ToString());
                if (revData.Count > 0)
                {
                    var addFloor = revData.Find((obj) => obj.uid == floor.uid);
                    if (addFloor != null)
                    {
                        CurrentSpatial.FloorList.Add(addFloor);
                        floor = addFloor;
                    }
                }
                var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(CurrentSpatial.FloorList));
                Common.FileUtlis.Files.WriteFileByBytes(dataSavePath, ssd);
            }
            else
            {
                Utlis.ShowTip(Language.StringByID(StringId.AddFloorFail) + "\r\nCode:" + revPack.Code);
            }
            newFloor = floor;
            return revPack.Code;
        }
        /// <summary>
        /// 更新楼层信息
        /// </summary>
        /// <param name="floor"></param>
        public string UpdateFloor(SpatialInfo floor)
        {
            var pm = new HttpServerRequest();
            var revPack = pm.UpdateRoom(new List<SpatialInfo>() { floor });
            if (revPack.Code == StateCode.SUCCESS)
            {
                var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(FloorList));
                Common.FileUtlis.Files.WriteFileByBytes(dataSavePath, ssd);
            }
            return revPack.Code;
        }
        /// <summary>
        /// 删除楼层
        /// </summary>
        /// <param name="floor"></param>
        public string DelFloor(string floorId)
        {
            if (string.IsNullOrEmpty( floorId))
                return "";
            var pm = new HttpServerRequest();
            var code = pm.DeleteRoom(new List<string>() { floorId }).Code;
            if (code == StateCode.SUCCESS)
            {
                var tRoom = CurrentSpatial.FloorList.Find((obj) => obj.roomId == floorId);
                if (tRoom != null)
                {
                    CurrentSpatial.FloorList.Remove(tRoom);
                }
                var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(FloorList));
                Common.FileUtlis.Files.WriteFileByBytes(dataSavePath, ssd);
            }
            else
            {
                Utlis.ShowTip(Language.StringByID(StringId.DeleteFloorFail) + "\r\nCode:" + code);
            }
            return code;
        }
 
        #endregion
 
        #region 房间列表
        /// <summary>
        /// 房间列表
        /// </summary>
        List<Room> rooms;
        /// <summary>
        /// 房间列表
        /// </summary>
        public List<Room> RoomList
        {
            get
            {
                if(MainPage.NoLoginMode )
                {
                    if (rooms == null)
                    {
                        rooms = new List<Room>();
                    }
                    return rooms;
                }
                if (rooms == null)
                {
                    try
                    {
                        rooms = new List<Room> { };
                        var filePathList = Common.FileUtlis.Files.ReadFiles();
 
                        foreach (var filePath in filePathList)
                        {
                            if (filePath.StartsWith("RoomData_"))
                            {
                                var roomDataBytes = Common.FileUtlis.Files.ReadFile(filePath);
                                var roomDataString = System.Text.Encoding.UTF8.GetString(roomDataBytes);
                                var room = Newtonsoft.Json.JsonConvert.DeserializeObject<Room>(roomDataString);
                                if (room != null)
                                {
                                    rooms.Add(room);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MainPage.Log($"功能数据初始化失败:{ex.Message}");
                    }
                }
                return rooms;
            }
        }
        /// <summary>
        /// 增加房间
        /// </summary>
        /// <param name="room"></param>
        public string AddRoom(Room room,out Room newRoom)
        {
            var pm = new HttpServerRequest();
            var pack = pm.AddRoom(new List<SpatialInfo>() { room });
 
            if (pack.Code == StateCode.SUCCESS)
            {
                var revData = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SpatialInfo>>(pack.Data.ToString());
                if (revData != null)
                {
                    if (revData.Count > 0)
                    {
                        var tempRoom = revData.Find((obj) => obj.uid == room.uid);
                        if (tempRoom != null)
                        {
                            room.roomId = tempRoom.roomId;
                            CurrentSpatial.RoomList.Add(room);
                            var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(room));
                            Common.FileUtlis.Files.WriteFileByBytes(dataSavePath, ssd);
                        }
                    }
                }
            }
            newRoom = room;
            return pack.Code;
        }
        /// <summary>
        /// 删除房间
        /// </summary>
        /// <param name="room"></param>
        public string DelRoom(Room room)
        {
            var pm = new HttpServerRequest();
            var pack = pm.DeleteRoom(new List<string>() { room.roomId });
            if (pack.Code == StateCode.SUCCESS)
            {
                var tRoom = RoomList.Find((obj) => obj.roomId == room.roomId);
                if (tRoom != null)
                {
                    CurrentSpatial.RoomList.Remove(room);
                }
                Common.FileUtlis.Files.DeleteFile(room.dataSavePath);
            }
            return pack.Code;
        }
        /// <summary>
        /// 删除所有房间
        /// </summary>
        public void DeleteAllRoom()
        {
            for(int i=0;i< RoomList.Count;)
            {
                Common.FileUtlis.Files.DeleteFile(RoomList[i].dataSavePath);
                RoomList.RemoveAt(i);
            }
            rooms = null;
        }
 
        public void Clear()
        {
            if (_currentSpatial != null)
            {
                if (_currentSpatial.floors != null)
                {
                    _currentSpatial.floors.Clear();
                }
                if (_currentSpatial.rooms != null)
                {
                    _currentSpatial.rooms.Clear();
                }
                _currentSpatial = null;
            }
        }
 
        /// <summary>
        /// 初始化每个房间的功能数据
        /// </summary>
        public void InitRoomListFunctions()
        {
            new System.Threading.Thread(() =>
            {
                try
                {
                    //初始化住宅所有房间功能数据
                    foreach (var r in RoomList)
                    {
                        InitRoomFuntion(r);
                    }
                }
                catch (Exception ex)
                {
                    MainPage.Log($"Init room function error : {ex.Message}");
                }
            })
            { IsBackground = true }.Start();
        }
        /// <summary>
        /// 初始化房间功能
        /// </summary>
        /// <param name="tempRoom"></param>
        public void InitRoomFuntion(Room tempRoom)
        {
            tempRoom.GetRoomFunctions(true);
            tempRoom.GetRoomScenes(true);
        }
 
        /// <summary>
        /// 保存房间数据文件
        /// </summary>
        public void SaveRoomFile()
        {
            var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
            Common.FileUtlis.Files.WriteFileByBytes(dataSavePath, ssd);
        }
        /// <summary>
        /// 更新房间数据
        /// </summary>
        public void UpdataRoomInfo()
        {
            new System.Threading.Thread(() =>
            {
                var pm = new HttpServerRequest();
                var pack = pm.UpdateRoom(new List<SpatialInfo>() { this });
                this.SaveRoomFile();
                if (pack.Code != StateCode.SUCCESS)
                {
                    IMessageCommon.Current.ShowErrorInfoAlter(pack.Code);
                }
            })
            { IsBackground = true }.Start();
        }
      
        #endregion
 
    }
 
 
 
    [Serializable]
    public class Room : SpatialInfo
    {
        public Room()
        {
            roomType = "ROOM";
            roomName = "Room";
        }
        //public string backgroundImage = "Classification/Room/Roombg.png";
        public string backgroundImage
        {
            get
            {
                if (string.IsNullOrEmpty(roomImage))
                {
                    return "Classification/Room/Roombg.png";
                }
                else
                {
                    return roomImage;
                }
            }
            set
            {
                roomImage = value;
            }
        }
 
 
        /// <summary>
        /// 楼层
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public string floorName
        {
            get
            {
                if (CurrentSpatial.FloorList.Count > 0)
                {
                    var f = CurrentSpatial.FloorList.Find((obj) => obj.uid == parentId);
                    if (f != null)
                    {
                        return f.roomName + " ";
                    }
                }
                return "";
            }
        }
 
        ///// <summary>
        ///// 房间功能列表
        ///// </summary>
        //List<Function> functions = new List<Function>();
        /// <summary>
        /// 获取房间功能列表
        /// </summary>
        public List<Function> GetRoomFunctions(bool needRefresh)
        {
            if (needRefresh)
            {
            }
            var functions = new List<Function>();
            if (functions.Count == 0)
            {
                foreach (var f in FunctionList.List.GetDeviceFunctionList())
                {
                    if (f.roomIds.Contains(roomId) && roomId != null)
                    {
                        functions.Add(f);
                    }
                }
                foreach(var g in FunctionList.List.groupControls)
                {
                    if (g.roomIds.Contains(roomId) && roomId != null)
                    {
                        functions.Add(g);
                    }
                }
            }
            return functions;
        }
        ///// <summary>
        ///// 增加房间功能
        ///// 操作的是缓存数据,不用保存
        ///// </summary>
        //public void AddRoomFunction(Function function)
        //{
        //    functions.Add(function);
        //}
        ///// <summary>
        ///// 删除房间功能
        ///// 操作的是缓存数据,不用保存
        ///// </summary>
        //public void RemoveRoomFunction(Function function)
        //{
        //    functions.Remove(functions.Find((obj) => obj.sid == function.sid));
        //}
        /// <summary>
        /// 房间场景列表
        /// </summary>
        List<Scene> scenes = new List<Scene>();
        /// <summary>
        /// 获取场景列表
        /// </summary>
        /// <returns></returns>
        public List<Scene> GetRoomScenes(bool needRefresh)
        {
            if (needRefresh)
            {
                scenes = new List<Scene>();
            }
            if (scenes.Count == 0)
            {
                foreach (var s in FunctionList.List.scenes)
                {
                    if (s.roomIds.Contains(roomId) && roomId != null)
                    {
                        scenes.Add(s);
                    }
                }
            }
            return scenes;
        }
        /// <summary>
        /// 增加房间场景
        /// 操作的是缓存数据,不用保存
        /// </summary>
        /// <param name="scene"></param>
        public void AddRoomScene(Scene scene)
        {
            scenes.Add(scene);
        }
        /// <summary>
        /// 移除房间场景
        /// 操作的是缓存数据,不用保存
        /// </summary>
        /// <param name="scene"></param>
        public void RemoveRoomScene(Scene scene)
        {
            scenes.Remove(scenes.Find((obj) => obj.sid == scene.sid));
        }
    }
 
 
}