JLChen
2020-05-28 04bd96cbf5d12a4e4b116d2b05fd0d05ea81ee23
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
using System;
using System.Collections.Generic;
using System.Net;
using System.IO;
using System.Text;
 
namespace Shared.SimpleControl.Phone
{
    public class DoorLockAlarmScene : FrameLayout
    {
        DoorLock doorLock;
        DoorLockSceneTargets sceneTargetsInfo;
        Button btn;
 
        public DoorLockAlarmScene (DoorLock doorLock)
        {
            this.doorLock = doorLock;
            BackgroundColor = SkinStyle.Current.MainColor;
        }
 
        public int SceneID;//场景类型
        public string SceneName = Language.StringByID (R.MyInternationalizationString.NotConfigured);//场景名称
        Button btnEditedName;
 
        /// <summary>
        ///  读门锁所有报警场景信息
        /// </summary>
        /// <param name="door_lock">Door lock.</param>
 
        public void DoorLockAlarmSceneShow ()
        {
            #region 标题
            var topView = new FrameLayout () {
                Height = Application.GetRealHeight (130),
                BackgroundColor = SkinStyle.Current.MainColor,
            };
            AddChidren (topView);
 
            var title = new Button () {
                TextAlignment = TextAlignment.Center,
                TextID = R.MyInternationalizationString.SceneAlarmSetting,
                TextSize = 19,
                Y = Application.GetRealHeight (10),
                TextColor = SkinStyle.Current.TextColor1,
            };
            topView.AddChidren (title);
 
            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
 
            var middleVerticalScrolViewLayout = new VerticalScrolViewLayout () {
                Height = Application.GetRealHeight (Application.DesignHeight - 126),
                Y = topView.Bottom,
                BackgroundColor = SkinStyle.Current.ViewColor,
            };
            AddChidren (middleVerticalScrolViewLayout);
 
            for (int i = 0; i < 5; i++) {
                var RowView = new FrameLayout () {
                    Height = Application.GetRealHeight (110),
                };
                middleVerticalScrolViewLayout.AddChidren (RowView);
 
                var btnIcon = new Button () {
                    Width = Application.GetRealHeight (13),
                    Height = Application.GetRealHeight (13),
                    X = Application.GetRealWidth (40),
                    UnSelectedImagePath = "Item/Point.png",
                    SelectedImagePath = "Item/PointSelected.png",
                    Gravity = Gravity.CenterVertical,
                };
                RowView.AddChidren (btnIcon);
 
                var sceneAlarmType = new Button () {
                    X = btnIcon.Right + Application.GetRealWidth (20),
                    Width = Application.GetRealWidth (300),
                    Height = Application.GetRealHeight (110),
                    TextAlignment = TextAlignment.CenterLeft,
                    TextColor = SkinStyle.Current.TextColor1,
                };
                RowView.AddChidren (sceneAlarmType);
 
                var btnSwitch = new Button () {
                    X = sceneAlarmType.Right + Application.GetRealWidth (145),
                    Gravity = Gravity.CenterVertical,
                    Width = Application.GetMinRealAverage (90),
                    Height = Application.GetMinRealAverage (53),
                    UnSelectedImagePath = "Item/SwitchClose.png",
                    SelectedImagePath = "Item/SwitchOpen.png",
                };
                RowView.AddChidren (btnSwitch);
 
                if (i == 0) {
                    sceneAlarmType.TextID = R.MyInternationalizationString.DoorUncloseAlarm;
                } else if (i == 1) {
                    sceneAlarmType.TextID = R.MyInternationalizationString.LowVoltageAlarm;
                } else if (i == 2) {
                    sceneAlarmType.TextID = R.MyInternationalizationString.ErrorAlarm;
                } else if (i == 3) {
                    sceneAlarmType.TextID = R.MyInternationalizationString.KeyUnlockAlarm;
                } else if (i == 4) {
                    sceneAlarmType.TextID = R.MyInternationalizationString.TamperResistantAlarm;
                }
 
                 btn = new Button () {
                    Y = RowView.Height - 1,
                    Height = 1,
                    BackgroundColor = SkinStyle.Current.LineColor,
                };
                RowView.AddChidren (btn);
 
                int sceneType = 0;//当前场景类型
                string alarmMessage = null;
                int currentTypeIndex = i;
                if (currentTypeIndex == 0) {
                    sceneType = 0x02;
                    alarmMessage = Language.StringByID (R.MyInternationalizationString.DoorUncloseAlarm);
                 } else if (currentTypeIndex == 1) {
                    sceneType = 0x03;
                    alarmMessage = Language.StringByID (R.MyInternationalizationString.LowVoltageAlarm);
                 } else if (currentTypeIndex == 2) {
                    sceneType = 0x04;
                    alarmMessage = Language.StringByID (R.MyInternationalizationString.ErrorAlarm);
                 } else if (currentTypeIndex == 3) {
                    sceneType = 0x05;
                    alarmMessage = Language.StringByID (R.MyInternationalizationString.KeyUnlockAlarm);
                 } else if (currentTypeIndex == 4) {
                    sceneType = 0x06;
                    alarmMessage = Language.StringByID (R.MyInternationalizationString.TamperResistantAlarm);
                 }
  
                int alarmID = currentTypeIndex;
 
   
                readStatus ((obj) => {
                    Application.RunOnMainThread (() => {
                        if (obj) {
                            btnSwitch.IsSelected = true;
 
                        } else {
                            btnSwitch.IsSelected = false;
                        }
                    });
                },sceneType);
 
                EventHandler<MouseEventArgs> eHandler = (sender, e) => {
                    btnSwitch.IsSelected = !btnSwitch.IsSelected;
                    if (btnSwitch.IsSelected) {
                        var alert = new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.SureToOpenAlarmNotice), Language.StringByID (R.MyInternationalizationString.Cancel), Language.StringByID (R.MyInternationalizationString.Confrim));
                        alert.ResultEventHandler += (sender1, e1) => {
                            if (e1) {
                                System.Threading.Tasks.Task.Run (() => {
                                var sendBytes = new byte [12];
                                sendBytes [0] = doorLock.PhysicsLoopID;
                                sendBytes [1] = 2;//报警
                                sendBytes [2] = (byte)sceneType;
                                sendBytes [3] = 0;//用户ID
                                sendBytes [4] = 0;
                                sendBytes [5] = 0x58;
                                sendBytes [6] = doorLock.SubnetID;
                                sendBytes [7] = 0;
                                sendBytes [8] = (byte)(alarmID + 210);//密码10以下,指纹ID+10,感应卡id+100
                                sendBytes [9] = 0;
                                sendBytes [10] = 0;
                                sendBytes [11] = 0;
                                var result = Control.ControlBytesSendHasReturn (Command.SetDoorLockSceneObj, doorLock.SubnetID, doorLock.DeviceID, sendBytes);
                                if (result == null) {
                                    Application.RunOnMainThread (() => {
                                        new Tip () { MaxWidth = 150, Text = Language.StringByID (R.MyInternationalizationString.TipEquipmentNotOnline), Direction = AMPopTipDirection.Down, CloseTime = 2 }.Show (btnSwitch);
                                    });
                                    return;
                                } else {
                                    var result1 = result [0];
                                    if (result1 == 0xF8) {
                                        Application.RunOnMainThread (() => {
                                                new Tip () { MaxWidth = 150, Text = Language.StringByID (R.MyInternationalizationString.OpenDoorLockNotificationsSuccessfully), Direction = AMPopTipDirection.Down, CloseTime = 1 }.Show (btnSwitch);
                                        });
                                            NoticePushServer (alarmMessage, alarmID, btnSwitch );
                                    } else {
                                        Application.RunOnMainThread (() => {
                                                new Tip () { MaxWidth = 150, Text = Language.StringByID (R.MyInternationalizationString.OpenDoorLockNotificationsUnSuccessfully), Direction = AMPopTipDirection.Down, CloseTime = 2 }.Show (btnSwitch);
                                        });
                                            btnSwitch.IsSelected = false;
                                    }
                                }
                                });
                              }
                         };
                        alert.Show ();
                    } else {
                        var alert = new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.SureToCloseAlarmNotice), Language.StringByID (R.MyInternationalizationString.Cancel), Language.StringByID (R.MyInternationalizationString.Confrim));
                        alert.ResultEventHandler += (sender1, e1) => {
                            if (e1) {
                                System.Threading.Tasks.Task.Run (() => {
                                    var sendBytes = new byte [12];
                                    sendBytes [0] = doorLock.PhysicsLoopID;
                                    sendBytes [1] = 2;//报警
                                    sendBytes [2] = (byte)sceneType;
                                    sendBytes [3] = 0;//用户ID
                                    sendBytes [4] = 0;
                                    sendBytes [5] = 0;
                                    sendBytes [6] = doorLock.SubnetID;
                                    sendBytes [7] = 0;
                                    sendBytes [8] = (byte)(alarmID + 210);//密码10以下,指纹ID+10,感应卡id+100
                                    sendBytes [9] = 0;
                                    sendBytes [10] = 0;
                                    sendBytes [11] = 0;
                                    var result = Control.ControlBytesSendHasReturn (Command.SetDoorLockSceneObj, doorLock.SubnetID, doorLock.DeviceID, sendBytes);
                                    if (result == null) {
                                        Application.RunOnMainThread (() => {
                                            new Tip () { MaxWidth = 150, Text = Language.StringByID (R.MyInternationalizationString.TipEquipmentNotOnline), Direction = AMPopTipDirection.Down, CloseTime = 2 }.Show (btnSwitch);
                                        });
                                        return;
                                    } else {
                                        var result1 = result [0];
                                        if (result1 == 0xF8) {
                                            Application.RunOnMainThread (() => {
                                                new Tip () { MaxWidth = 150, Text = Language.StringByID (R.MyInternationalizationString.CloseDoorLockNotificationsSuccessfully), Direction = AMPopTipDirection.Down, CloseTime = 2 }.Show (btnSwitch);
                                            });
                                         } else {
                                            Application.RunOnMainThread (() => {
                                                new Tip () { MaxWidth = 150, Text = Language.StringByID (R.MyInternationalizationString.CloseDoorLockNotificationsUnSuccessfully), Direction = AMPopTipDirection.Down, CloseTime = 2 }.Show (btnSwitch);
                                            });
                                            btnSwitch.IsSelected = true;
 
                                        }
                                    }
                                });
                            }
                        };
                        alert.Show ();
                    }
                };
                btnSwitch.MouseUpEventHandler += eHandler;
 
            }
        }
 
        public void readStatus (Action<bool> action,int type)
        {
            System.Threading.Tasks.Task.Run (() => {
                var sceneTargetInfoBytes = Control.ControlBytesSendHasReturn (Command.ReadDoorLockSceneObj, doorLock.SubnetID, doorLock.DeviceID, new byte [] { doorLock.PhysicsLoopID, 2, (byte)type, 0, 0 });
                if (sceneTargetInfoBytes == null) {
                    action (false);
                } else {
                    var sceneTargetsInfo = new DoorLockSceneTargets ();
                    sceneTargetsInfo.TargetSerialNum = sceneTargetInfoBytes [4];
                    sceneTargetsInfo.ControlType = sceneTargetInfoBytes [5];//通用开关0x58,无效0
                    sceneTargetsInfo.TargetSunbetID = sceneTargetInfoBytes [6];
                    sceneTargetsInfo.TargetDeviceID = sceneTargetInfoBytes [7];
                    sceneTargetsInfo.TargetLoopID = sceneTargetInfoBytes [8];
   
                    var command = sceneTargetInfoBytes [5];
                    if (sceneTargetsInfo.TargetSunbetID == doorLock.SubnetID &&
                       sceneTargetsInfo.TargetDeviceID == 0 &&
                        command == 0x58) {
                        action (true);
                    } else {
                        action (false);
                    }
                }
            });
        }
 
        /// <summary>
        /// 服务器请求
        /// </summary>
        /// <returns>The web.</returns>
        /// <param name="url">URL.</param>
        /// <param name="data">Data.</param>
        string requestWeb (string data)
        {
            var webClient = new WebClient { };
            webClient.Headers.Remove ("ContentType");
            webClient.Headers.Add ("ContentType", "application/json");
            var bytes = webClient.UploadData ("https://developer.hdlcontrol.com/api/AddPushMessage", "POST", Encoding.UTF8.GetBytes (data));
            if (bytes == null) {
                return null;
            } else {
                return Encoding.UTF8.GetString (bytes);
            }
        }
 
        public static string messageJson (PushMessageEntity pushMessage)
        {
            if (pushMessage == null) {
                return null;
            }
            return Newtonsoft.Json.JsonConvert.SerializeObject (pushMessage);
        }
 
        /// <summary>
        /// 消息推送到服务器
        /// </summary>
        /// <param name="message">Message.</param>
        public async void NoticePushServer (string message,int alarmID,Button btnSwitch)
        {
            string result = null;
            await System.Threading.Tasks.Task.Run (() => {
                //获取一端口交换机的mac
                var gateWayString = CommonPage.MyEncodingUTF8.GetString (IO.FileUtils.ReadFile (UserConfig.Instance.RemoteModeFile));
                if (gateWayString == null || UserConfig.Instance.RemoteModeFile.Split ('_').Length < 2) {
                    result = Language.StringByID (R.MyInternationalizationString.NoGateway);
                    return;
                }
                var common = Newtonsoft.Json.JsonConvert.DeserializeObject<Common> (gateWayString);
                if (common == null) {
                    result = Language.StringByID (R.MyInternationalizationString.NoGateway);
                    return;
                }
                string mac = null;
                if (common.Type == DeviceType.OnePortWirelessFR) {
                    var gateWay = Newtonsoft.Json.JsonConvert.DeserializeObject<OnePortWirelessFR> (gateWayString);
                    mac = gateWay.MAC.Replace (".", "");
                } else if (common.Type == DeviceType.OnePortBus) {
                    var gateWay = Newtonsoft.Json.JsonConvert.DeserializeObject<OnePortBus> (gateWayString);
                    mac = gateWay.MAC.Replace (".", "");
                } else {
                    result = Language.StringByID (R.MyInternationalizationString.NoGateway);
                    return;
                }
 
                var pushMessage = new PushMessageEntity ();
                pushMessage.Mac = mac;
                pushMessage.SubnetID = doorLock.SubnetID;
                pushMessage.DeviceID = doorLock.DeviceID;
                pushMessage.SwitchNum = alarmID + 210;
                pushMessage.MessageMode = 1;
                pushMessage.AlarmEmail = new byte [] { 2 };
                pushMessage.DetailMessage = message;
 
                var mess = messageJson (pushMessage);
                try {
                    var recevieResult = requestWeb (mess);
                    var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<PushMessageResult> (recevieResult);
                    if (obj.StateCode == "SUCCESS") {
                        Application.RunOnMainThread (() => {
                            new Tip () { MaxWidth = 150, Text = Language.StringByID (R.MyInternationalizationString.SynchronousNotificationsSuccessfully), Direction = AMPopTipDirection.Down, CloseTime = 2 }.Show (btn);
                        });
                    } else {
                        Application.RunOnMainThread (() => {
                            new Tip () { MaxWidth = 150, Text = Language.StringByID (R.MyInternationalizationString.SynchronousNotificationsUnsuccessfully), Direction = AMPopTipDirection.Down, CloseTime = 2 }.Show (btn);
                            //btnSwitch.IsSelected = false;跟服务器状态没有关系,这里状态是有发到门锁的数据确定
                        });
                    }
                } catch (Exception ex) {
                    Application.RunOnMainThread (() => {
                        new Tip () { MaxWidth = 150, Text = Language.StringByID (R.MyInternationalizationString.SynchronousNotificationsUnsuccessfully), Direction = AMPopTipDirection.Down, CloseTime = 2 }.Show (btn);
                    });
                }
            });
            if (result != null) {
                Application.RunOnMainThread (() => {
                    new Tip () { MaxWidth = 150, Text = result, Direction = AMPopTipDirection.Down, CloseTime = 2 }.Show (btn);
                });
            }
        }
        
     }
 }