1
wxr
2023-03-31 7e42cc13a14b7de31c9f5d5c61cdf24f3246335d
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
using System;
using System.Collections.Generic;
 
namespace Shared.SimpleControl.Phone
{
    public class DoorLockAlarmSceneTargets : FrameLayout
    {
        DoorLock doorLock;
        int sceneType;//当前场景类型  
        public string SceneName = Language.StringByID (R.MyInternationalizationString.NotConfigured);//场景名称
        Button btnEditedName;
 
        public DoorLockAlarmSceneTargets (DoorLock door_Lock, int currentTypeIndex)
        {
            BackgroundColor = SkinStyle.Current.MainColor;
            this.doorLock = door_Lock;
 
            if (currentTypeIndex == 0) {
                sceneType = 0x01;
            } else if (currentTypeIndex == 1) {
                sceneType = 0x02;
            } else if (currentTypeIndex == 2) {
                sceneType = 0x03;
            } else if (currentTypeIndex == 3) {
                sceneType = 0x04;
            } else if (currentTypeIndex == 4) {
                sceneType = 0x05;
            } else if (currentTypeIndex == 5) {
                sceneType = 0x06;
            }
 
 
        }
 
        public void DoorLockAlarmSceneShow (int currentTypeIndex)
        {
            #region 标题
            var topView = new FrameLayout () {
                Height = Application.GetRealHeight (130),
                BackgroundColor = 0xff1e1f20,
            };
            AddChidren (topView);
 
            var title = new Button () {
                TextAlignment = TextAlignment.Center,
                TextSize = 19,
                Y = Application.GetRealHeight (10),
            };
            topView.AddChidren (title);
            if (currentTypeIndex == 0) {
                title.TextID = R.MyInternationalizationString.HijackAlarm;
            } else if (currentTypeIndex == 1) {
                title.TextID = R.MyInternationalizationString.DoorUncloseAlarm;
            } else if (currentTypeIndex == 2) {
                title.TextID = R.MyInternationalizationString.LowVoltageAlarm;
            } else if (currentTypeIndex == 3) {
                title.TextID = R.MyInternationalizationString.ErrorAlarm;
            } else if (currentTypeIndex == 4) {
                title.TextID = R.MyInternationalizationString.KeyUnlockAlarm;
            } else if (currentTypeIndex == 5) {
                title.TextID = R.MyInternationalizationString.TamperResistantAlarm;
            }
 
            var back = new Button () {
                Height = Application.GetRealHeight (90),
                Width = Application.GetRealWidth (85),
                UnSelectedImagePath = "Item/Back.png",
                SelectedImagePath = "Item/BackSelected.png",
                Y = Application.GetRealHeight (30),
            };
            topView.AddChidren (back);
            back.MouseUpEventHandler += (sender, e) => {
                this.RemoveFromParent ();
            };
            #endregion
 
            Button btnEditScence = new Button () {
                Height = Application.GetRealHeight (80),
                TextAlignment = TextAlignment.CenterLeft,
                Y = topView.Bottom,
                BackgroundColor = SkinStyle.Current.TitileView,
                TextID = R.MyInternationalizationString.SceneTriggering
            };
            AddChidren (btnEditScence);
 
            var middleVerticalScrolViewLayoutEdited = new VerticalScrolViewLayout () {
                Height = Application.GetRealHeight (80),
                Y = btnEditScence.Bottom,
                BackgroundColor = SkinStyle.Current.ViewColor,
            };
            AddChidren (middleVerticalScrolViewLayoutEdited);
 
            string saveSceneFilePath = null;
            doorLock.DictionaryList.TryGetValue ("002_" + sceneType.ToString ("000"), out saveSceneFilePath);
            if (saveSceneFilePath == null) {
                var frameLayout1 = new FrameLayout () {
                    Height = Application.GetRealHeight (80),
                };
                middleVerticalScrolViewLayoutEdited.AddChidren (frameLayout1);
 
                btnEditedName = new Button () {
                    Height = LayoutParams.MatchParent,
                    TextAlignment = TextAlignment.Center,
                    Text = SceneName,
                };
                frameLayout1.AddChidren (btnEditedName);
            } else {
                var frameLayout = new RowLayout () {
                    Height = Application.GetRealHeight (80),
                    BackgroundColor = SkinStyle.Current.ViewColor,
                };
                middleVerticalScrolViewLayoutEdited.AddChidren (frameLayout);
 
                var scene = Scene.GetSceneByFilePath (saveSceneFilePath);
                btnEditedName = new Button () {
                    Height = LayoutParams.MatchParent,
                    TextAlignment = TextAlignment.Center,
                    Text = scene == null ? SceneName : scene.Name,
                };
                frameLayout.AddChidren (btnEditedName);
            }
 
            btnEditScence.MouseUpEventHandler += (sender, e) => {
                // if (btnEditScence.IsSelected) {
                middleVerticalScrolViewLayoutEdited.Height = Application.GetRealHeight (80);
                //btnEditScence.IsSelected = false;
                //} else {
                //    middleVerticalScrolViewLayoutEdited.Height = Application.GetRealHeight (0);
                //    btnEditScence.IsSelected = true;
                //}
            };
 
            Button btnUnEditedScene = new Button () {
                Height = Application.GetRealHeight (80),
                TextAlignment = TextAlignment.CenterLeft,
                BackgroundColor = SkinStyle.Current.TitileView,
                Text = Language.StringByID (R.MyInternationalizationString.AlternativeScenes),
                Y = middleVerticalScrolViewLayoutEdited.Bottom,
            };
            AddChidren (btnUnEditedScene);
 
            var middleVerticalScrolViewLayout = new VerticalScrolViewLayout () {
                Height = Application.GetRealHeight (1136 - 126 - 160) - middleVerticalScrolViewLayoutEdited.Height,
                Y = btnUnEditedScene.Bottom,
                BackgroundColor = SkinStyle.Current.ViewColor,
            };
            AddChidren (middleVerticalScrolViewLayout);
 
            btnUnEditedScene.MouseUpEventHandler += (sender, e) => {
                // if (btnUnEditedScene.IsSelected) {
                //     btnUnEditedScene.IsSelected = false;
                middleVerticalScrolViewLayout.Height = Application.GetRealHeight (1136 - 126 - 160) - middleVerticalScrolViewLayoutEdited.Height;
                //} else {
                //    btnUnEditedScene.IsSelected = true;
                //    middleVerticalScrolViewLayout.Height = Application.GetRealHeight (0);
            };
 
            #region sceneList
            //全部类型的场景列表
            var allSceneFilePaths = new List<string> ();
            //全局场景中所有的场景
            var sceneFileList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (Scene.GlobalSceneFilePath)));
            allSceneFilePaths.AddRange (sceneFileList);
            //房间的所有场景
            foreach (var room in Room.Lists) {
                if (string.IsNullOrEmpty(room.Name)) {
                    continue;
                }
                allSceneFilePaths.AddRange (room.SceneFilePathList);
            }
 
            int currentSceneIndex = 0;
            foreach (var sceneFilePath in allSceneFilePaths) {
                var scene = Scene.GetSceneByFilePath (sceneFilePath);
                if (scene == null) {
                    continue;
                }
 
                var scemeAlarmFrameLayout = new RowLayout () {
                    Height = Application.GetRealHeight (115),
                    BackgroundImagePath = "DoorLockPic/door_lock_ scene.png"
                };
                middleVerticalScrolViewLayout.AddChidren (scemeAlarmFrameLayout);
                if (saveSceneFilePath == sceneFilePath) {
                    scemeAlarmFrameLayout.BackgroundImagePath = "DoorLockPic/door_lock_ scene_on.png";
                }
 
                var btnRemarkName = new Button () {
                    Width = Application.GetRealWidth (420),
                    Height = LayoutParams.MatchParent,
                    Text = scene.Name,
                    TextAlignment = TextAlignment.CenterLeft,
                    X = Application.GetRealWidth (80),
                };
                scemeAlarmFrameLayout.AddChidren (btnRemarkName);
 
                int currentSceneClickIndex = currentSceneIndex;
                EventHandler<MouseEventArgs> hander = (sender, e) => {
                    Scene tempScene = Newtonsoft.Json.JsonConvert.DeserializeObject<Scene> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (sceneFilePath)));
                    if (tempScene.DeviceFilePathList.Count == 0) {
                        new Tip () { MaxWidth = 150, Text = Language.StringByID (R.MyInternationalizationString.EmptyTargetsLeadToControlFailure), Direction = AMPopTipDirection.Down, CloseTime = 4 }.Show ((View)sender);
                        return;
                    }
 
                    int logicModuleNum = 0;
                    foreach (var deviceFilePath in tempScene.DeviceFilePathList) {
                        if (deviceFilePath.Split ('_') [1] == DeviceType.LogicModule.ToString ()) {
                            logicModuleNum++;
                        }
                    }
 
                    if (logicModuleNum == 0) {
                        Application.RunOnMainThread (() => {
                            new Tip () { MaxWidth = 150, Text = Language.StringByID (R.MyInternationalizationString.PleaseAddLogicModule), Direction = AMPopTipDirection.Down, CloseTime = 4 }.Show ((View)sender);
                        });
                        return;
                    } else {
                        for (int j = 0; j < middleVerticalScrolViewLayout.ChildrenCount; j++) {
                            var rowView = middleVerticalScrolViewLayout.GetChildren (j);
                            if (rowView.GetType () == typeof (RowLayout)) {
                                if (currentSceneClickIndex == j) {
                                    btnEditedName.Text = btnRemarkName.Text;
                                    ((RowLayout)rowView).BackgroundImagePath = "DoorLockPic/door_lock_ scene_on.png";
                                } else {
                                    ((RowLayout)rowView).BackgroundImagePath = "DoorLockPic/door_lock_ scene.png";
                                }
                            }
                        }
                    }
 
                    bool showTip = true;
                    System.Threading.Tasks.Task.Run (() => {
                        int controlDeviceIndex = 0;
                        var sendBytes = new byte [12];
 
                        foreach (var deviceFilePath in tempScene.DeviceFilePathList) {
                            if (controlDeviceIndex > 19) {
                                controlDeviceIndex = 19;
                            }
 
                            sendBytes [0] = doorLock.LoopID;//开锁
                            sendBytes [1] = 2;//报警
                            sendBytes [2] = (byte)sceneType;
                            sendBytes [3] = 0;//用户ID
                            sendBytes [4] = (byte)controlDeviceIndex;
 
                            var common = Newtonsoft.Json.JsonConvert.DeserializeObject<Common> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
 
                            if (deviceFilePath.Split ('_') [1] == DeviceType.LightDimming.ToString ()) {
                                var device = Newtonsoft.Json.JsonConvert.DeserializeObject<LightDimming> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                if (device == null) {
                                    continue;
                                }
                                sendBytes [5] = 0x59;
                                sendBytes [6] = device.SubnetID;
                                sendBytes [7] = device.DeviceID;
                                sendBytes [8] = device.LoopID;
                                sendBytes [9] = device.CurrentBrightness;
                                sendBytes [10] = device.DelayTimeHeight;
                                sendBytes [11] = device.DelayTimeLow;
                                // sceneControl (doorLock, sendBytes, btnRemarkName, sender, sceneFilePath);
 
                            } else if (deviceFilePath.Split ('_') [1] == DeviceType.LightRGB.ToString ()) {
                                var device = Newtonsoft.Json.JsonConvert.DeserializeObject<LightLogic> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                if (device != null) {
                                }
 
                            } else if (deviceFilePath.Split ('_') [1] == DeviceType.LightSwitch.ToString ()) {
                                var device = Newtonsoft.Json.JsonConvert.DeserializeObject<LightSwitch> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                if (device == null) {
                                    continue;
                                }
                                sendBytes [5] = 0x59;
                                sendBytes [6] = device.SubnetID;
                                sendBytes [7] = device.DeviceID;
                                sendBytes [8] = device.LoopID;
                                sendBytes [9] = device.CurrentBrightness;
                                sendBytes [10] = device.DelayTimeHeight;
                                sendBytes [11] = device.DelayTimeLow;
                                //sceneControl (doorLock, sendBytes, btnRemarkName, sender, sceneFilePath);
                            } else if (deviceFilePath.Split ('_') [1] == DeviceType.CurtainModel.ToString ()) {
                                var device = Newtonsoft.Json.JsonConvert.DeserializeObject<CurtainModel> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                if (device == null) {
                                    continue;
                                }
                                sendBytes [5] = 0x5C;
                                sendBytes [6] = device.SubnetID;
                                sendBytes [7] = device.DeviceID;
                                sendBytes [8] = device.LoopID;
                                sendBytes [9] = (byte)device.Status;
                                // sceneControl (doorLock, sendBytes, btnRemarkName, sender, sceneFilePath);
                            } else if (deviceFilePath.Split ('_') [1] == DeviceType.CurtainRoller.ToString ()) {
                                var device = Newtonsoft.Json.JsonConvert.DeserializeObject<CurtainRoller> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                if (device == null) {
                                    continue;
                                }
                                sendBytes [5] = 0x5C;
                                sendBytes [6] = device.SubnetID;
                                sendBytes [7] = device.DeviceID;
                                sendBytes [8] = device.LoopID;
                                sendBytes [9] = (byte)device.Status;
                                //sceneControl (doorLock, sendBytes, btnRemarkName, sender, sceneFilePath);
                            } else if (deviceFilePath.Split ('_') [1] == DeviceType.CurtainTrietex.ToString ()) {
                                var device = Newtonsoft.Json.JsonConvert.DeserializeObject<CurtainTrietex> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                if (device == null) {
                                    continue;
                                }
                                sendBytes [5] = 0x5C;
                                sendBytes [6] = device.SubnetID;
                                sendBytes [7] = device.DeviceID;
                                sendBytes [8] = 1;
                                sendBytes [9] = (byte)device.Status;
                                // sceneControl (doorLock, sendBytes, btnRemarkName, sender, sceneFilePath);
                            } else if (deviceFilePath.Split ('_') [1] == DeviceType.HVAC.ToString ()
                                      || deviceFilePath.Split ('_') [1] == DeviceType.ACInfrared.ToString ()) {
                                var device = Newtonsoft.Json.JsonConvert.DeserializeObject<HVAC> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                if (device == null) {
                                    continue;
                                }
 
                            } else if (deviceFilePath.Split ('_') [1] == DeviceType.FoolHeat.ToString ()) {
 
                            } else if (deviceFilePath.Split ('_') [1] == DeviceType.FanModule.ToString ()) {
                                var device = Newtonsoft.Json.JsonConvert.DeserializeObject<FanModule> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                if (device == null) {
                                    continue;
                                }
                            } else if (deviceFilePath.Split ('_') [1] == DeviceType.LogicModule.ToString ()) {
                                var device = Newtonsoft.Json.JsonConvert.DeserializeObject<LogicModule> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                if (device == null) {
                                    continue;
                                }
                                sendBytes [5] = 0x55;
                                sendBytes [6] = device.SubnetID;
                                sendBytes [7] = device.DeviceID;
                                sendBytes [8] = device.AreaID;
                                sendBytes [9] = device.AreaSceneID;
                                sceneControl (doorLock, sendBytes, btnRemarkName, sender, sceneFilePath);
                            }
                            controlDeviceIndex++;
                        }
                    });
                };
 
                scemeAlarmFrameLayout.MouseUpEventHandler += hander;
                btnRemarkName.MouseUpEventHandler += hander;
                currentSceneIndex++;
            }
            #endregion
        }
 
        //报警场景控制,只对逻辑模块有用,所以其他地方都注释掉这个方法
        void sceneControl (DoorLock door_lock, byte [] sendBytes, Button btnRemarkName, object sender, string sceneFilePath)
        {
            Application.RunOnMainThread (() => {
                MainPage.Loading.Start (Language.StringByID (R.MyInternationalizationString.TargetsConfiguring) + "...");
            });
            var result = Control.ControlBytesSendHasReturn (Command.SetDoorLockSceneObj, door_lock.SubnetID, door_lock.DeviceID, sendBytes);
            if (result == null) {
                Application.RunOnMainThread (() => {
                    new Alert ("", Language.StringByID (R.MyInternationalizationString.TipEquipmentNotOnline), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                });
                return;
            } else {
                var result1 = result [0];
                if (result1 == 0xF8) {
                    Application.RunOnMainThread (() => {
                        new Tip () { MaxWidth = 150, Text = Language.StringByID (R.MyInternationalizationString.TargetsControlSuccessfully), Direction = AMPopTipDirection.Down, CloseTime = 2 }.Show ((View)sender);
                    });
                    string key = "002_" + sceneType.ToString ("000");
                    door_lock.DictionaryList.Remove (key);
                    door_lock.DictionaryList.Add (key, sceneFilePath);
                    IO.FileUtils.SaveEquipmentMessage (door_lock, door_lock.LoopID.ToString ());
                } else {
                    Application.RunOnMainThread (() => {
                        new Tip () { MaxWidth = 150, Text = Language.StringByID (R.MyInternationalizationString.FuquencyLyVertify), Direction = AMPopTipDirection.Down, CloseTime = 4 }.Show ((View)sender);
                    });
                }
            }
            Application.RunOnMainThread (() => {
                MainPage.Loading.Hide ();
            });
        }
 
    }
}