黄学彪
2020-12-17 9f326f4000847e6167d8166fa2f6a66f53cb3734
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
using System;
using Foundation;
using Microsoft.AppCenter;
using Microsoft.AppCenter.Analytics;
using Microsoft.AppCenter.Crashes;
using Shared;
using Shared.Common;
using Shared.IOS.TBL;
using UIKit;
 
namespace Home.IOS
{
    [Register("AppDelegate")]
    public class AppDelegate : BaseApplicationDelegate
    {
        public static bool haveToSignOut = false;
        // class-level declarations
        public override UIWindow Window
        {
            get;
            set;
        }
 
        internal static UINavigationController rootViewController;
 
        public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
        {
            Shared.Application.IsGpsEnable = false;
            Shared.Application.IsMusicEnable = false;
            base.FinishedLaunching(application, launchOptions);
            Shared.Application.FontSize = 12;
            Shared.Application.IsUsePingFang = true;
            Window = new UIWindow(UIScreen.MainScreen.Bounds);
            rootViewController = new UINavigationController(new GateWay.Ios.MainViewController()) { NavigationBarHidden = true };
            Window.RootViewController = rootViewController;
            Window.MakeKeyAndVisible();
 
            Shared.IOS.TBL.WXApi.RegisterApp("wx2ec8f53f6fa36e82", "https://hdlcontrol.com/ZigbeeApp/");
 
            DeviceTokenAction += (deviceToken) =>
            {
                Shared.Common.Config.Instance.RegistrationID = deviceToken;
            };
            haveToSignOut = false;
            if (launchOptions != null)
            {
                // check for a remote notification
                if (launchOptions.ContainsKey(UIApplication.LaunchOptionsRemoteNotificationKey))
                {
                    NSDictionary userInfo = launchOptions[UIApplication.LaunchOptionsRemoteNotificationKey] as NSDictionary;
                    if (userInfo != null)
                    {
                        NSString key_hiddenJson = new NSString("HiddenJson");
                        if (userInfo.ContainsKey(key_hiddenJson))
                        {
                            var hiddenJson = userInfo["HiddenJson"].ToString();
                            if (!string.IsNullOrEmpty(hiddenJson))
                            {
                                if (hiddenJson.Contains("cmtID"))
                                {
 
                                    ///目前根据这个判断是否是可视对讲数据
                                    // VideoMethod(hiddenJson);
                                }
                                else
                                {
 
                                    NSDictionary hiddenJsonDic = userInfo["HiddenJson"] as NSDictionary;
                                    if (hiddenJsonDic != null)
                                    {
                                        NSString key_Offline = new NSString("Offline");
                                        if (hiddenJsonDic.ContainsKey(key_Offline))
                                        {
                                            string signOut = hiddenJsonDic["Offline"].ToString();
 
                                            if (signOut == "0")
                                            {
                                                //不下线
                                            }
                                            else
                                            {
                                                //标记下线
                                                haveToSignOut = true;
                                            }
                                        }
 
                                    }
                                }
                            }
 
 
                        }
 
                    }
                }
            }
            AppCenter.Start("0ca801a2-742b-42fb-ba10-7ba4391371aa", typeof(Analytics), typeof(Crashes));
            GDMapKit.setGDApiKey("3889cdfa4ef10c0d43348ffa2c1017c8");//高德地图key
 
            return true;
        }
 
        public override void ReceivedLocalNotification(UIApplication application, UILocalNotification notification)
        {
 
        }
        public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
        {
            if (userInfo == null)
            {
                return;
            }
            Console.WriteLine(userInfo);
 
            if (application.ApplicationState == UIApplicationState.Active || application.ApplicationState == UIApplicationState.Background)
            {
                NSString key_hiddenJson = new NSString("HiddenJson");
                if (userInfo.ContainsKey(key_hiddenJson))
                {
                    var hiddenJson = userInfo["HiddenJson"].ToString();
                    if (string.IsNullOrEmpty(hiddenJson))
                    {
                        return;
                    }
 
                    if (hiddenJson.Contains("cmtID"))
                    {
 
                        ///目前根据这个判断是否是可视对讲数据
                        VideoMethod(hiddenJson);
                    }
                    else
                    {
 
                        NSDictionary hiddenJsonDic = userInfo["HiddenJson"] as NSDictionary;
                        if (hiddenJsonDic == null)
                        {
                            return;
                        }
                        NSString key_Offline = new NSString("Offline");
                        if (hiddenJsonDic.ContainsKey(key_Offline))
                        {
                            string signOut = hiddenJsonDic["Offline"].ToString();
 
                            if (signOut == "0")
                            {
                                //不下线
                            }
                            else
                            {
                                //强制下线
                                CommonPage.Instance.SingOut();
                                CommonPage.Instance.ShowErrorInfoAlert(Shared.R.MyInternationalizationString.uHadBeenLoginAndOffLine);
                            }
                        }
                    }
                }
            }
            else
            {
                NSString key_hiddenJson = new NSString("HiddenJson");
                if (userInfo.ContainsKey(key_hiddenJson))
                {
 
                    var hiddenJson = userInfo["HiddenJson"].ToString();
                    if (string.IsNullOrEmpty(hiddenJson)) {
                        return;
                    }
 
                    if (hiddenJson.Contains("cmtID"))
                    {
                        ///目前根据这个判断是否是可视对讲数据
                        VideoMethod(hiddenJson);
                    }
                    else
                    {
 
                        NSDictionary hiddenJsonDic = userInfo["HiddenJson"] as NSDictionary;
                        if (hiddenJsonDic == null)
                        {
                            return;
                        }
                        NSString key_Offline = new NSString("Offline");
                        if (hiddenJsonDic.ContainsKey(key_Offline))
                        {
                            string signOut = hiddenJsonDic["Offline"].ToString();
 
                            if (signOut == "0")
                            {
                                //不下线
                            }
                            else
                            {
                                //标记下线
                                haveToSignOut = true;
                            }
                        }
                    }
                }
 
            }
            completionHandler(UIBackgroundFetchResult.NewData);
        }
        /// <summary>
        /// ios通知栏点击打开可视对讲的方法
        /// </summary>
        /// <param name="hiddenJson">云端推过来的数据</param>
        private  void VideoMethod(string hiddenJson)
        {
 
            var json = Newtonsoft.Json.Linq.JObject.Parse(hiddenJson);
            if (json == null)
            {
                return;
            }
            GateWay.Ios.ESVideoInfo eSVideoInfo = new GateWay.Ios.ESVideoInfo();
            if (json["uuid"].ToString().Contains(","))
            {
                var uuid = json["uuid"].ToString().Split(',');
                eSVideoInfo.ESVideoUUID = uuid[0];
            }
            else
            {
                eSVideoInfo.ESVideoUUID = json["uuid"].ToString();
            }
 
            eSVideoInfo.DeviceName = Language.StringByID(Shared.R.MyInternationalizationString.theOutdoorUnit);
            eSVideoInfo.ESRoomID = int.Parse(json["roomno"].ToString());
            if (!string.IsNullOrEmpty(json["unitno"].ToString()))
            {
                string str = json["unitno"].ToString();
                string str1 = str.Substring(0, 2).TrimStart('0');
                string str2 = str.Substring(2, 2).TrimStart('0');
                eSVideoInfo.RoomName = str1 + Language.StringByID(Shared.R.MyInternationalizationString.dong) + str2 + Language.StringByID(Shared.R.MyInternationalizationString.unit);
            }
            GateWay.Ios.ESVideo.ShowESvideoVideoIntercom(eSVideoInfo);
 
            //var url = "https://global.hdlcontrol.com/HangZhouHdlCloudApi/VideoIntercom/Answer";
            //var dd=await Shared.Phone.Device.Logic.Send.HttpWebRequest(url, json.ToString(), "POST", 3, true);
        }
 
        public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
        {
            Console.WriteLine(userInfo);
            if (application.ApplicationState == UIApplicationState.Active)
            {
                System.Console.WriteLine("ReceivedRemoteNotification1");
            }
            else if (application.ApplicationState == UIApplicationState.Background)
            {
                System.Console.WriteLine("ReceivedRemoteNotification2");
            }
            else if (application.ApplicationState == UIApplicationState.Inactive)
            {
                System.Console.WriteLine("ReceivedRemoteNotification3");
            }
        }
 
        public override void OnResignActivation(UIApplication application)
        {
            // Invoked when the application is about to move from active to inactive state.
            // This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) 
            // or when the user quits the application and it begins the transition to the background state.
            // Games should use this method to pause the game.
            base.OnResignActivation(application);
        }
 
        public override void DidEnterBackground(UIApplication application)
        {
            // Use this method to release shared resources, save user data, invalidate timers and store the application state.
            // If your application supports background exection this method is called instead of WillTerminate when the user quits.
            //Shared.BusSocket.Stop ();On在这里停止BusSocket
            ZigBee.Common.Application.FindGateWaySocket.Stop();
            base.DidEnterBackground(application);
        }
 
        public override void WillEnterForeground(UIApplication application)
        {
            var internetStatus = GateWay.Ios.Reachability.InternetConnectionStatus();
            Shared.Phone.UserCenter.HdlWifiLogic.Current.NetworkStateChanged((int)internetStatus);
 
            //ZigBee.Common.Application.FindGateWaySocket.Start();
            // Called as part of the transiton from background to active state.
            // Here you can undo many of the changes made on entering the background.
            System.Console.WriteLine("WillEnterForeground");
            UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
            base.WillEnterForeground(application);
        }
 
        public override void OnActivated(UIApplication application)
        {
            // Restart any tasks that were paused (or not yet started) while the application was inactive. 
            // If the application was previously in the background, optionally refresh the user interface.
            base.OnActivated(application);
 
            if (haveToSignOut == true)
            {
                //强制下线
                CommonPage.Instance.SingOut();
                CommonPage.Instance.ShowErrorInfoAlert(Shared.R.MyInternationalizationString.uHadBeenLoginAndOffLine);
            }
            //检查连接远程
            //代码
        }
 
        public override void WillTerminate(UIApplication application)
        {
            // Called when the application is about to terminate. Save data, if needed. See also DidEnterBackground.
            base.WillTerminate(application);
        }
 
        public static Action<string> RespAction;
        public static void WXLogin()
        {
 
 
            var req = new SendAuthReq { Scope = "snsapi_userinfo", State = "ZigbeeApp", OpenID = "0c806938e2413ce73eef92cc3" };
            WXApi.SendAuthReq(req, rootViewController, null, null);
 
        }
 
        MyWXApiDelegate myWXApiDelegate = new MyWXApiDelegate { };
 
        class MyWXApiDelegate : WXApiDelegate
        {
            public override void OnReq(BaseReq req)
            {
 
            }
            public override void OnResp(BaseResp resp)
            {
                if (resp is SendAuthResp)
                {
                    var sendAuthResp = resp as SendAuthResp;
                    switch (sendAuthResp.ErrCode)
                    {
                        case 0:
                            try
                            {
                                var result = new System.Net.WebClient { }.DownloadString($"https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx2ec8f53f6fa36e82&secret=a08585cd6ff2ce64570b9e7e6525dd8e&code={((SendAuthResp)resp).Code}&grant_type=authorization_code");
                                RespAction?.Invoke(result);
                            }
                            catch
                            {
                                RespAction?.Invoke(null);
                            }
                            break;
                        default:
                            RespAction?.Invoke(null);
                            break;
                    }
 
                }
            }
        }
        public override bool ContinueUserActivity(UIApplication application, NSUserActivity userActivity, UIApplicationRestorationHandler completionHandler)
        {
            return WXApi.HandleOpenUniversalLink(userActivity, myWXApiDelegate);
        }
    }
}
 
namespace Shared
{
    public static class QRCode
    {
        static ZXing.Mobile.MobileBarcodeScanner scanner;
        public static async System.Threading.Tasks.Task ScanQRcode(Action<string> action, string cancel = "取消", string flashText = "闪光灯", string titleText = "二维码扫描")
        {
            if (scanner == null)
            {
                var mZXingOverlayView = new Home.IOS.ZXingOverlayView(cancel, flashText, titleText);
                scanner = new ZXing.Mobile.MobileBarcodeScanner(Home.IOS.AppDelegate.rootViewController) { FlashButtonText = flashText, TopText = titleText, BottomText = "", CancelButtonText = cancel };
                scanner.UseCustomOverlay = true;
 
                scanner.CustomOverlay = mZXingOverlayView;
                var bOn = false;
                mZXingOverlayView.OnCancel += () => {
 
                    scanner?.Cancel();
                };
 
                mZXingOverlayView.OnTorch += () =>
                {
                    bOn = !bOn;
                    scanner?.Torch(bOn);
                };
 
            }
 
            var result = await scanner.Scan();
 
            if (result != null)
                action?.Invoke(result.Text);
            else
                action?.Invoke(null);
        }
 
        public static byte[] BytesFromText(string text, int width = 300, int height = 300)
        {
            var barcodeWriter = new ZXing.Mobile.BarcodeWriter
            {
                Format = ZXing.BarcodeFormat.QR_CODE,
                Options = new ZXing.Common.EncodingOptions
                {
                    Width = width,
                    Height = height,
                    Margin = 0
                }
            };
 
            barcodeWriter.Renderer = new ZXing.Mobile.BitmapRenderer();
            var uiImage = barcodeWriter.Write(text);
            var data = uiImage.AsJPEG();
 
            var resultBytes = new byte[data.Length];
            System.Runtime.InteropServices.Marshal.Copy(data.Bytes, resultBytes, 0, resultBytes.Length);
 
            return resultBytes;
        }
    }
}