wei
2021-05-10 24068547ed1396034f56c7bd34ecbd2891f00653
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
using System;
using HDL_ON.UI.UI2.Intelligence.Automation.LogicView;
using Shared;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
 
namespace HDL_ON.UI.UI2.Intelligence.Automation
{
    public class MainView
    {
        /// <summary>
        /// VerticalRefreshLayout父控件
        /// </summary>
        public static FrameLayout automationPage;
        /// <summary>
        /// 加载逻辑列表显示界面
        /// </summary>
        public static void MainShow()
        {
            automationPage.RemoveAll();
            VerticalRefreshLayout vv = new VerticalRefreshLayout();
            vv.Height = Application.GetRealHeight(667 - 64 - 49);//模拟高度
            automationPage.AddChidren(vv);
            vv.BeginHeaderRefreshingAction += () =>
            {
                //清除之前自动化列表;
                Logic.LogicList.Clear();
                //获取自动列表
                GetLogicList();
                //刷新自动化界面
                MainShow();
                //关闭刷新View;
                vv.EndHeaderRefreshing();
            };
            //获取自动化列表
            GetLogicList();
            for (int i = Logic.LogicList.Count - 1; i >= 0; i--)//降序排列
            //for (int i = 0; i < Logic.LogicList.Count; i++)
            {
                var currLogic = Logic.LogicList[i];
                ///上下间隔12像素
                vv.AddChidren(new FrameLayout { Height = Application.GetRealHeight(12) });
                LogicView.SingleLogicView logicView = new LogicView.SingleLogicView();
                vv.AddChidren(logicView.FLayoutView());
                logicView.btnLogicName.Text = currLogic.name;
                logicView.btnWeekText.Text = GetCyclicText(currLogic);
                logicView.btnclick.MouseUpEventHandler += (sen, e) =>
                {
                    //Logic.currlogic = currLogic;
                    //var addLogic = new AddLogic();
                    //MainPage.BasePageView.AddChidren(addLogic);
                    //addLogic.Show();
                    //MainPage.BasePageView.PageIndex = MainPage.BasePageView.ChildrenCount - 1;
                    new MainView { }.SkipAddLogicPage(currLogic);
                };
                //开关图标的点击事件
                logicView.btnSwitchIcon.MouseUpEventHandler += (sender1, e1) =>
                {
                    logicView.btnSwitchIcon.IsSelected = !logicView.btnSwitchIcon.IsSelected;
                    if (logicView.btnSwitchIcon.IsSelected)
                    {
                        logicView.btnSwitchIcon.IsSelected = true;
                        currLogic.enable = "true";
                    }
                    else
                    {
                        logicView.btnSwitchIcon.IsSelected = false;
                        currLogic.enable = "false";
                    }
                    //Send.switchLogic(currLogic);
                    new System.Threading.Thread(() =>
                    {
                        try
                        {
                            //逻辑使能的命令
                            Send.SwitchLogic(currLogic);
 
                        }
                        catch { }
 
                    })
                    { IsBackground = true }.Start();
                };
                GetSelectIcon(logicView.frameLayout, currLogic);
                if (i == 0)//降序排列
                //if (Logic.LogicList.Count - 1 == i)
                {
                    ///最后一个距离最底部12,界面显示效果作用;
                    vv.AddChidren(new FrameLayout { Height = Application.GetRealHeight(12) });
                }
                if (currLogic.enable == "true")
                {
                    logicView.btnSwitchIcon.IsSelected = true;
                }
                else
                {
                    logicView.btnSwitchIcon.IsSelected = false;
                }
            }
 
            if (Logic.LogicList.Count == 0)
            {
                Button btnTipNot = new Button()
                {
                    Gravity = Gravity.CenterHorizontal,
                    Y = Application.GetRealHeight(120),
                    Width = Application.GetRealWidth(180),
                    Height = Application.GetRealWidth(180),
                    UnSelectedImagePath = "TipNot.png",
                };
                automationPage.AddChidren(btnTipNot);
                Button btnTipNotText = new Button()
                {
                    Y = btnTipNot.Bottom + Application.GetRealHeight(16),
                    Height = Application.GetRealHeight(20),
                    TextID = StringId.logicnull,
                    TextAlignment = TextAlignment.Center,
                    TextSize = LogicView.TextSize.text14,
                    TextColor = CSS.CSS_Color.textConfirmColor,
                };
                automationPage.AddChidren(btnTipNotText);
            }
        }
        /// <summary>
        ///  按+跳转到逻辑界面的方法
        /// </summary>
        public void SkipAddLogicPage(Logic logic=null)
        {
            if (logic == null)
            {
                Logic.currlogic = new Logic();
            }
            else {
                Logic.currlogic = logic;
            }
            if (IsGatewayType)
            {
                var addLogic = new AddLogic();
                MainPage.BasePageView.AddChidren(addLogic);
                addLogic.Show();
                MainPage.BasePageView.PageIndex = MainPage.BasePageView.ChildrenCount - 1;
            }
            else
            {
 
                var onePortAutomation = new OnePortAutomation();
                MainPage.BasePageView.AddChidren(onePortAutomation);
                onePortAutomation.Show();
                MainPage.BasePageView.PageIndex = MainPage.BasePageView.ChildrenCount - 1;
            }
        }
        /// <summary>
        ///返回循环描述文本
        /// </summary>
        /// <param name="logic"></param>
        public static string GetCyclicText(Logic logic)
        {
 
            string weekName = "";
            /// (执行一次:once,每天:day,每月:mon,星期:week,日期段:date_to_date)
            switch (logic.cycle.type)
            {
                case "once":
                    {
                        weekName = Language.StringByID(StringId.performA);
                    }
                    break;
                case "day":
                    {
                        weekName = Language.StringByID(StringId.days);
                    }
                    break;
                case "week":
                    {
 
                        weekName = GetWeekString(logic.cycle.value);
 
                    }
                    break;
                case "mon":
                    {
                        weekName = GetMonString(logic.cycle.value);
                    }
                    break;
            }
            return weekName;
        }
        /// <summary>
        /// 返回星期描述文本
        /// </summary>
        /// <param name="weekList"></param>
        /// <returns></returns>
        public static string GetWeekString(List<string> weekList)
        {
            string weekTextName = "";
            if (weekList.Contains("1"))
            {
                weekTextName += Language.StringByID(StringId.monday) + ",";
            }
            if (weekList.Contains("2"))
            {
                weekTextName += Language.StringByID(StringId.tuesday) + ",";
            }
            if (weekList.Contains("3"))
            {
                weekTextName += Language.StringByID(StringId.wednesday) + ",";
            }
            if (weekList.Contains("4"))
            {
                weekTextName += Language.StringByID(StringId.thursday) + ",";
            }
            if (weekList.Contains("5"))
            {
                weekTextName += Language.StringByID(StringId.friday) + ",";
            }
            if (weekList.Contains("6"))
            {
                weekTextName += Language.StringByID(StringId.saturday) + ",";
            }
            if (weekList.Contains("0"))
            {
                weekTextName += Language.StringByID(StringId.sunday) + ",";
            }
            if (weekTextName == "")
            {
                return "";
            }
            return weekTextName.TrimEnd(',');
        }
        /// <summary>
        /// 获取每月的字符串
        /// </summary>
        /// <param name="monList"></param>
        /// <returns></returns>
        public static string GetMonString(List<string> monList)
        {
            string monTextName = Language.StringByID(StringId.monthly);
 
            for (int i = 1; i < 32; i++)
            {
                if (monList.Contains(i.ToString()))
                {
                    monTextName += i.ToString() + ",";
                }
            }
            return monTextName.TrimEnd(',');
        }
        /// <summary>
        /// 获取逻辑列表
        /// </summary>
        public static void GetLogicList()
        {
            if (Logic.LogicList.Count != 0)
            {
                //自动化列表为0才去获取自动化列表;
                return;
            }
            List<string> logicIdList = new List<string>();
            //获取逻辑ID列表
            var idStr = Send.GetLogicIdList();
            if (idStr.Code == "0" && idStr.Data != null && idStr.Data.ToString() != "")
            {
                var date = Newtonsoft.Json.JsonConvert.SerializeObject(idStr.Data);
                var dateList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<LogicData>>(date);
                for (int i = 0; i < dateList.Count; i++)
                {
                    //添加逻辑ID
                    logicIdList.Add(dateList[i].userLogicId);
                }
            }
            //获取自动化列表
            var logicStr = Send.GetLogic(logicIdList);
            if (logicStr.Code == "0" && logicStr.Data != null && logicStr.Data.ToString() != "")
            {
                //获取逻辑详细返回是一个数组(object类型转换为数组);
                var jArray = JArray.Parse(logicStr.Data.ToString());
                for (int a = 0; a < jArray.Count; a++)
                {
                    var jay = jArray[a];
                    //数据返序列化为Logic对象
                    var str = Newtonsoft.Json.JsonConvert.SerializeObject(jay);
                    Console.WriteLine("获取逻辑==="+str);
                    var logic = Newtonsoft.Json.JsonConvert.DeserializeObject<Logic>(str);
                    if (logic != null)
                    {
                        //查找是否已经存在该逻辑
                        var if_logic = Logic.LogicList.Find((c) => c.userLogicId == logic.userLogicId);
                        if (if_logic == null)
                        {
 
                            //Logic newlogic = new Logic();
                            //newlogic.name = logic.name;
                            //newlogic.userLogicId = logic.userLogicId;
                            //newlogic.sid = logic.sid;
                            //newlogic.relation = logic.relation;
                            //newlogic.enable = logic.enable;
                            //newlogic.cycle = logic.cycle;
                            //for (int i = 0; i < logic.input.Count; i++)
                            //{
                            //    if (logic.input[i] == null || string.IsNullOrEmpty(logic.input[i].condition_type))
                            //    {
                            //        //防止调试软件乱传东西上来;
                            //        //防止有空对象;
                            //        //正常情况下不会出现的现象;
                            //        continue;
                            //    }
                            //    newlogic.input.Add(logic.input[i]);
                            //}
                            //for (int i = 0; i < logic.output.Count; i++)
                            //{
                            //    if (logic.output[i] == null || string.IsNullOrEmpty(logic.output[i].target_type))
                            //    {
                            //        //防止调试软件乱传东西上来;
                            //        //防止有空对象;
                            //        //正常情况下不会该现象;
                            //        continue;
                            //    }
                            //    newlogic.output.Add(logic.output[i]);
                            //}
                        
                            //添加逻辑
                            Logic.LogicList.Add(logic);
                        }
                    }
 
                }
            }
 
        }
        /// <summary>
        /// 判断JObject对象是存在健值
        /// </summary>
        /// <param name="jObject"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        private bool Exist(JObject jObject, string key)
        {
 
            if (jObject.Property(key) != null)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 显示条件或者目标类型图标
        /// </summary>
        /// <param name="layouFrame">父控件</param>
        /// <param name="logic">当前自动化</param>
        private static void GetSelectIcon(FrameLayout layouFrame, Logic logic)
        {
 
            //重新排列图标序号
            List<int> iconIntValue = new List<int>();
            iconIntValue.Clear();
 
            //1-10(表示条件图标)自己局部定义为了显示选中条件类型图标
            //1-时间图标
            //2-功能图标
            for (int i = 0; i < logic.input.Count; i++)
            {
                if (logic.input[i] == null)
                    continue;
                var int1 = int.Parse(logic.input[i].condition_type);
                int iconInt = 0;
                switch (int1)
                {
                    case 1:
                    case 2:
                        {
                            iconInt = 1;
 
                        }
                        break;
                    case 3:
                        {
                            iconInt = 2;
 
                        }
                        break;
                    case 4:
                    case 6:
                    case 9:
                        {
                            iconInt = 3;
 
                        }
                        break;
                }
 
                if (!iconIntValue.Contains(iconInt))
                {
                    iconIntValue.Add(iconInt);
                }
 
            }
 
            //分割条件和目标的图标
            iconIntValue.Add(11);
 
            //12-20(表示目标图标)自己局部定义为了显示选中目标类型图标
            //12-功能图标
            //13-场景图标
            //14-延时图标
            for (int i = 0; i < logic.output.Count; i++)
            {
                var int1 = int.Parse(logic.output[i].target_type);
                int iconInt = 0;
                switch (int1)
                {
                    case 1:
                        {
                            iconInt = 12;
 
                        }
                        break;
                    case 2:
                        {
                            iconInt = 13;
 
                        }
                        break;
                    case 3:
                        {
                            iconInt = 14;
 
                        }
                        break;
                }
 
                if (!iconIntValue.Contains(iconInt))
                {
                    iconIntValue.Add(iconInt);
                }
            }
            //图标控件的父控件
            FrameLayout frame = new FrameLayout
            {
                Height = Application.GetRealHeight(40),
                Width = Application.GetRealWidth(275),
                Y = Application.GetRealHeight(56),
            };
            layouFrame.AddChidren(frame);
 
 
            for (int i = 0; i < iconIntValue.Count; i++)
            {
                //图标控件
                Button btnIcon = new Button
                {
                    Width = Application.GetRealWidth(28),
                    Height = Application.GetRealWidth(28),
                    Gravity = Gravity.CenterVertical,
                };
                frame.AddChidren(btnIcon);
 
                if (i == 0)
                {
                    btnIcon.X = Application.GetRealWidth(12);
                }
                else
                {
                    btnIcon.X = Application.GetRealWidth(12 + (28 + 4) * i);
                }
                string strIcon = "";
                switch (iconIntValue[i])
                {
 
                    case 1:
                        {
                            strIcon = "LogicIcon/selectTheTime.png";
                        }
                        break;
                    case 2:
                        {
 
                            strIcon = "LogicIcon/selectTheFun.png";
                        }
                        break;
                    case 3:
                        {
 
                            strIcon = "LogicIcon/shiwaitianqi.png";
                        }
                        break;
                    case 11:
                        {
                            //分割条件和目标的图标
                            strIcon = "LogicIcon/link.png";
                        }
                        break;
                    case 12:
                        {
 
                            strIcon = "LogicIcon/selectTheFun.png";
                        }
                        break;
                    case 13:
                        {
                            strIcon = "LogicIcon/selectTheScene.png";
                        }
                        break;
                    case 14:
                        {
                            //strIcon = "LogicIcon/timeicon.png";
                        }
                        break;
 
                }
                btnIcon.UnSelectedImagePath = strIcon;
            }
        }
        /// <summary>
        /// 判断网关类型
        /// </summary>
        /// <returns></returns>
        public static bool IsGatewayType
        {
            get
            {
                if (Entity.DB_ResidenceData.Instance.GatewayType == 1)
                {
                    return true;
                }
                else
                {
                    return false;
 
                }
            }
        }
    }
    class LogicData
    {
        /// <summary>
        /// 云端唯一id
        /// </summary>
        public string userLogicId = ""; 
        /// <summary>
        /// 网关id
        /// </summary>
        public string gatewayId = "";
        /// <summary>
        /// 逻辑唯一标识
        /// </summary>
        public string sid = "";
        /// <summary>
        /// 逻辑名称
        /// </summary>
        public string name = "";
        /// <summary>
        /// 逻辑使能(开,关)
        /// </summary>
        public string enable = "";
        //public CycleA cycle;
        //public string modifyTime = "";
    }
 
    class CycleA
    {
        public string type = "";
        public string[] value;
    }
}