wei
2020-12-11 488efb508eb0648773fe7b68e810e04bcd7ca075
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
using System;
using System.Collections.Generic;
using HDL_ON.DAL.Server;
using Shared;
 
namespace HDL_ON.Entity
{
    public class SceneApiPack
    {
        static SceneApiPack _ScenePack;
        public static SceneApiPack ScenePack
        {
            get
            {
                if(_ScenePack == null)
                {
                    _ScenePack = new SceneApiPack();
                }
                return _ScenePack;
            }
        }
 
    }
 
    public class Scene 
    {
        public Scene()
        {
            sid = NewSid();
            if (!string.IsNullOrEmpty(DB_ResidenceData.residenceData.HomeGateway.gatewayId))
            {
                gatewayId = DB_ResidenceData.residenceData.HomeGateway.gatewayId;
            }
        }
        /// <summary>
        /// 云端唯一ID
        /// </summary>
        public string userSceneId = "";
        /// <summary>
        /// 场景sid
        /// </summary>
        public string sid = "";
        /// <summary>
        /// 场景名称
        /// </summary>
        public string name = "";
        /// <summary>
        /// 网关ID
        /// </summary>
        public string gatewayId = "";
        /// <summary>
        /// 云端oss存储图片的路径
        /// </summary>
        public string image = "";
        /// <summary>
        /// 所属房间列表
        /// </summary>
        public List<string> roomIds = new List<string>();
        /// <summary>
        /// 所属房间列表
        /// </summary>
        public List<string> uids = new List<string>();
        /// <summary>
        /// 延时
        /// </summary>
        public int delay = 0;
        /// <summary>
        /// 延时显示的文本
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public string delayText
        {
            get
            {
                string text = "";
                switch (delay)
                {
                    case 0:
                        text = Language.StringByID(StringId.NoDelay);
                        break;
                    case 30:
                        text = "30s";
                        break;
                    case 60:
                        text = "1min";
                        break;
                    case 120:
                        text = "2min";
                        break;
                    case 300:
                        text = "5min";
                        break;
                }
                return text;
            }
        }
 
        /// <summary>
        /// 场景分组
        /// </summary>
        public string group = "1";
        /// <summary>
        /// 场景类型
        /// </summary>
        public SceneType sceneType = SceneType.OrdinaryScenes;
        /// <summary>
        /// 收藏标记
        /// </summary>
        public bool collect = false;
 
        ///// <summary>
        ///// 场景背景
        ///// </summary>
        //public string ImagePath = "Intelligence/Gallery/scenebg1.png";//"FunctionIcon/Scene/s1.png";
 
        ///// <summary>
        ///// 场景背景
        ///// </summary>
        public string ImagePath
        {
            get
            {
                if (string.IsNullOrEmpty(image))
                {
                    return "Intelligence/Gallery/scenebg1.png";
                }
                else
                {
                    return image;
                }
            }
            set
            {
                image = value;
            }
        }
        
 
        /// <summary>
        /// 场景功能列表
        /// </summary>
        public List<SceneFunction> functions = new List<SceneFunction>();
        /// <summary>
        /// 场景推送配置列表
        /// </summary>
        public List<ScenePushConfig> pushConfigs = new List<ScenePushConfig>();
 
        /// <summary>
        /// 生成逻辑sid方法
        /// </summary>
        public string NewSid()
        {
            string sceneId = "";
            try
            {
                string sOidBeginsWith = "000101";//厂商 + 通讯方式
                DateTime dt = DateTime.Now;
                DateTime startTime = TimeZoneInfo.ConvertTimeToUtc(new DateTime(2020, 1, 1));
                long m = (long)((dt - startTime).TotalMilliseconds / 10);
                string sTimeSpan = "00000000";
 
                byte[] arry = new byte[4];
                arry[0] = (byte)(m & 0xFF);
                arry[1] = (byte)((m & 0xFF00) >> 8);
                arry[2] = (byte)((m & 0xFF0000) >> 16);
                arry[3] = (byte)((m >> 24) & 0xFF);
                sTimeSpan = arry[0].ToString("X2") + arry[1].ToString("X2") + arry[2].ToString("X2") + arry[3].ToString("X2");
 
 
                if (sTimeSpan.Length > 8)
                {
                    sTimeSpan = sTimeSpan.Substring(0, 8);
                }
                else
                {
                    sTimeSpan = "00000000";
                }
 
                sceneId = sOidBeginsWith + sTimeSpan;
 
                sceneId += "0A";
                sceneId += "0A01";
                int maxId = 1;
 
 
                for (int i = 0; i < FunctionList.List.scenes.Count; i++)
                {
                    string s = FunctionList.List.scenes[i].sid.Substring(20, 4);
                    int iThisSceneId = Convert.ToInt16(s, 16);
                    if (iThisSceneId > maxId)
                        maxId = iThisSceneId;
                }
 
                sceneId += (maxId + 1).ToString("X4");
                sceneId += "0000";
            }
            catch
            {
                return sceneId;
            }
            return sceneId;
        }
 
        /// <summary>
        /// 获取设备添加到房间的房间名称
        /// </summary>
        /// <returns></returns>
        public string GetRoomListName()
        {
            string roomNameList = "";
            foreach (var roomId in roomIds)
            {
                var findRoom = DB_ResidenceData.residenceData.Rooms.Find(obj => obj.roomId == roomId);
                if (findRoom == null)
                {
                    continue;
                }
                if (roomNameList != "")
                {
                    roomNameList += ",";
                }
                roomNameList += findRoom.floorName + findRoom.roomName;
            }
            if (roomNameList == "" )
            {
                roomNameList = Shared.Language.StringByID(StringId.WholeHouseScene);
            }
            return roomNameList;
        }
 
        /// <summary>
        /// 数据存储文件名
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public string savePath
        {
            get
            {
                return "SceneData_" + sid;
            }
        }
 
        /// <summary>
        /// 保存功能数据
        /// </summary>
        public void SaveSceneData(bool upServer)
        {
            if (upServer)
            {
                UploadScene();
            }
            else
            {
                var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
                Common.FileUtlis.Files.WriteFileByBytes(savePath, ssd);
                MainPage.Log($"Save Scene Data : {savePath}");
            }
        }
 
        /// <summary>
        /// 上传数据到云端
        /// </summary>
        void UploadScene()
        {
            var pm = new HttpServerRequest();
            ResponsePackNew revPack;
            if (string.IsNullOrEmpty(createTime) && string.IsNullOrEmpty(modifyTime))
            {
                revPack = pm.AddScene(this);
                if (revPack.Code == StateCode.SUCCESS)
                {
                    var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
                    Common.FileUtlis.Files.WriteFileByBytes(savePath, ssd);
                }
            }
            else if (!string.IsNullOrEmpty(createTime) && !string.IsNullOrEmpty(modifyTime))
            {
                revPack = pm.EditScene(this);
                if (revPack.Code == StateCode.SUCCESS)
                {
                    var scenes = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Scene>>(revPack.Data.ToString());
                    var tempScene = scenes.Find((obj) => obj.sid == sid);
                    if (tempScene != null)
                    {
                        var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(tempScene));
                        Common.FileUtlis.Files.WriteFileByBytes(savePath, ssd);
                    }
                }
            }
        }
 
        /// <summary>
        /// 云端数据创建的时间
        /// </summary>
        public string createTime = "";
        /// <summary>
        /// 云端数据修改的最后时间
        /// </summary>
        public string modifyTime = "";
    }
    /// <summary>
    /// 场景功能对象
    /// </summary>
    public class SceneFunction
    {
        public string sid = "";
 
        public List<SceneFunctionStatus> status = new List<SceneFunctionStatus>();
        /// <summary>
        /// 功能延时
        /// </summary>
        public string delay = "0";
 
        Function _localFunction;
        /// <summary>
        /// 本地对应的功能
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public Function localFunction {
            get
            {
                if(_localFunction == null)
                {
                    _localFunction = ConvertFunctionObject();
                }
                return _localFunction;
            }
        }
 
        /// <summary>
        /// 转换成功能对象
        /// </summary>
        /// <returns></returns>
        Function ConvertFunctionObject()
        {
            var localFunction = DB_ResidenceData.functionList.GetAllDeviceFunctionList().Find((obj) => obj.sid == sid);
            foreach (var s in status)
            {
                localFunction.attributes.Add(new FunctionAttributes() { key = s.key, curValue = s.value, value = new List<string>() { s.value } });
            }
            return localFunction;
        }
      
    }
 
    /// <summary>
    /// 场景功能属性
    /// </summary>
    public class SceneFunctionStatus
    {
        public string key = "";
        public string value = "";
    }
    /// <summary>
    /// 场景推送配置
    /// </summary>
    public class ScenePushConfig
    {
        /// <summary>
        /// 推送方式
        /// </summary>
        public string pushMethod = "";
        /// <summary>
        /// 推送内容
        /// </summary>
        public string pushContent = "";
        /// <summary>
        /// 推送目标集合
        /// </summary>
        public List<string> pushTarget = new List<string>();
    }
 
}