JLChen
2020-06-04 6d55af8792cf8fbef0055e677b900fc352dba9a2
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
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Shared;
using Shared.SimpleControl;
using Shared.SimpleControl.R;
using SmartHome;
 
namespace SuperGateWay
{
    public class LogicListPage : FrameLayout
    {
        public LogicListPage ()
        {
            Tag = "Logic";
        }
        Button beforeClickButton = new Button ();
        GateWay gateWay;
        Logic logicIfon = null;
        public void Show (GateWay gateWay)
        {
            this.gateWay = gateWay;
            this.BackgroundColor = 0xFF1F1F1F;
            this.AddChidren (new Button {
                Height = Application.GetRealHeight (30),
            });
 
            var topFrameLayout = new FrameLayout {
                Height = Application.GetRealHeight (100),
                Y = Application.GetRealHeight (30),
            };
 
            AddChidren (topFrameLayout);
 
            var titleName = new Button {
                TextID = MyInternationalizationString.automation,
                TextSize = 17,
            };
            topFrameLayout.AddChidren (titleName);
 
            var back = new Button {
                Width = Application.GetRealWidth (82),
                Height = Application.GetRealHeight (89),
                X = Application.GetRealWidth (10),
                Gravity = Gravity.CenterVertical,
                UnSelectedImagePath = "MusicIcon/HomepageBack.png",
            };
            topFrameLayout.AddChidren (back);
            back.MouseDownEventHandler += (sender, e) => {
                RemoveFromParent ();
            };
 
            var searchdevice = new Button {
                Width = Application.GetMinRealAverage (60),
                Height = Application.GetMinRealAverage (80),
                X = Application.GetRealWidth (550),
                Gravity = Gravity.CenterVertical,
                UnSelectedImagePath = "MusicIcon/seekdevice.png",
            };
            topFrameLayout.AddChidren (searchdevice);
           
            var tetleframeLayout = new FrameLayout {
                Y = topFrameLayout.Bottom,
                Height = Application.GetRealHeight (100),
                BackgroundColor = 0xff0f0f0f,
 
            };
            AddChidren (tetleframeLayout);
 
            var tetlebtn = new Button {
                Width = Application.GetRealWidth (400),
                TextID = MyInternationalizationString.selectsavedautomation,
                //Text = "请选择已保存自动化",
                TextSize = 16,
                TextAlignment = TextAlignment.CenterLeft,
                X = Application.GetRealWidth (40),
            };
            tetleframeLayout.AddChidren (tetlebtn);
 
            var middle = new VerticalScrolViewLayout ();
            middle.Y = tetleframeLayout.Bottom;
            middle.Height = Application.GetRealHeight (Application.DesignHeight - 130 - 100 - 80);
            middle.BackgroundColor = 0xff2F2F2F;
            this.AddChidren (middle);
 
            var btncomplete = new Button {
                Y = middle.Bottom,
                Height = Application.GetRealHeight (80),
                TextID = MyInternationalizationString.complete,
                TextSize=16,
            };
            AddChidren (btncomplete);
                       
            ///加载逻辑界面的方法
            refreshlogicView (middle);
 
            ///逻辑刷新图标的点击事件
            searchdevice.MouseUpEventHandler += (sender, e) => {
                middle.RemoveAll ();
                logicIfon = null;
                refreshlogiclist (() => { 
                    ///加载逻辑界面的方法
                    refreshlogicView (middle);
                });
            };
 
            ///保存控件的点击事件
            btncomplete.MouseUpEventHandler += (sedder, e) => {
                if (gateWay.Logics.Count == 0||logicIfon==null) {
                    var alert = new Alert (Language.StringByID (MyInternationalizationString.Prompt),
                    Language.StringByID (MyInternationalizationString.selectlogic),
                    Language.StringByID (MyInternationalizationString.OK));
                    alert.Show ();
                    return;
                }
 
                var selectedLogicState = new SelectedLogicState ();
                MainPage.MainFrameLayout.AddChidren (selectedLogicState);
                selectedLogicState.Show (gateWay, logicIfon);
            };
 
        }
        /// <summary>
        /// 读取逻辑列表
        /// </summary>
        /// <returns>The refreshlogiclist.</returns>
        /// <param name="gateWay">Gate way.</param>
        async void refreshlogiclist (Action action)
        {
            gateWay.Logics.Clear ();
            MainPage.Loading.Start ();
            await System.Threading.Tasks.Task.Run (() => {
                try {
                    var logicuidlist = Control.getLogiclist ();
                    //logicuidlist.Add ("logicuidlist");
                    if (logicuidlist == null) {
                        return;
                    }
                    var array = logicuidlist.ToArray ();
                    Array.Sort (array);
                    foreach (var logicuid in array) {
                        try {
                            var result= Control.getSceneByUID (logicuid.Replace (".json", ""));
                            // string result = "{\"module\":\"logic\",\"name\":\"logic_name\",\"sid\":\"10010B0506010001\",\"objects\":[{\"sid\":\"45678912\",\"value\":\"1\"}],\"delay\":\"10\",\"trigger\":\"0\",\"operator\":\"&&\",\"conditions\":[{\"sid\":\"10010B0506010001\",\"value\":\"1\"}]}";
                            //string result = "{\"module\":\"logic\",\"name\":\"logic_name\",\"sid\":\"1234556778\",\"param\":{\"operator\":\"&&\",\"delay\":\"10\"},\"input\":[{\"sid\":\"1001560306010001\",\"value\":\"1\",\"param\":{\"continue\":\"5\",\"compare\":\"=\",\"chinese_calendar\":\"false\"}},{\"sid\":\"1001590405010001\",\"value\":\"1\",\"param\":{\"continue\":\"5\",\"compare\":\"=\",\"chinese_calendar\":\"false\"}}],\"output\":[{\"objects\":[{\"sid\":\"1001560305000002\",\"value\":\"0\",\"delay\":\"10\"},{\"sid\":\"1001780302000002\",\"value\":\"0\",\"delay\":\"10\"}]},{\"objects\":[{\"sid\":\"1001780302010002\",\"value\":\"1\",\"delay\":\"10\"},{\"sid\":\"1001780305010002\",\"value\":\"1\",\"delay\":\"10\"}]}]}";
                            if (result == null) {
                                continue;
                            }
                            var logicstring = Newtonsoft.Json.JsonConvert.DeserializeObject<Logic> (result);
 
                            if (logicstring != null) {
                                //logicstring.inputSidToDevice ();
                                //logicstring.outputSidToDevice ();
                                gateWay.Logics.Add (logicstring);
                            }
 
                        } catch (Exception e) {
                            var s = e.Message;
                        }
                    }
 
                } catch { }
            });
            MainPage.Loading.Hide ();
            action ();
 
        }
        /// <summary>
        /// 加载逻辑界面的方法
        /// </summary>
        /// <param name="middle">Middle.</param>
        /// <param name="logicIfon">Logic ifon.</param>
        void refreshlogicView (VerticalScrolViewLayout middle ) 
        {
            foreach (var logic in gateWay.Logics) {
                var deviceRowLayout = new RowLayout {
                    Height = Application.GetRealHeight (100),
                };
                middle.AddChidren (deviceRowLayout);
 
                var btn = new Button {
                    Height = Application.GetRealHeight (100),
                    Width = LayoutParams.MatchParent,
                    SelectedBackgroundColor = 0xfffe5e00,
                };
                deviceRowLayout.AddChidren (btn);
 
                var btnlogic = new Button {
                    Width = Application.GetRealWidth (200),
                    Text = logic.name,
                    TextAlignment = TextAlignment.CenterLeft,
                    X = Application.GetRealWidth (40),
                };
                deviceRowLayout.AddChidren (btnlogic);
 
                var btnlogicback = new Button {
                    Width = Application.GetRealWidth (87),
                    Height = Application.GetRealHeight (100),
                    UnSelectedImagePath = "MusicIcon/Next.png",
                    SelectedImagePath = "MusicIcon/NextSelecte.png",
                    X = Application.GetRealWidth (525),
                };
                deviceRowLayout.AddChidren (btnlogicback);
 
                EventHandler<MouseEventArgs> logicclick = (sender, e) => {
                    logicIfon = logic;
                    beforeClickButton.IsSelected = false;
                    beforeClickButton = btn;
                    btn.IsSelected = true;
                };
                btnlogic.MouseUpEventHandler += logicclick;
                deviceRowLayout.MouseUpEventHandler += logicclick;
                btnlogicback.MouseUpEventHandler += logicclick;
                btn.MouseUpEventHandler += logicclick;
            }
 
        }
    }
}