wxr
2020-01-10 55e72277f0b0b0f4159ef91f72153f8316d89a5b
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
using System;
using Shared.SimpleControl.Phone;
using System.Collections.Generic;
using System.Text;
using System.Net.NetworkInformation;
 
namespace Shared.SimpleControl
{
    public class EquipmentPublicClass
    {
 
        public void AlertNotOnline ()
        {
            new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.TipEquipmentNotOnline), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
        }
 
        static object lockFlag = "";
 
        public static bool RomoteLinkState = false;
 
        /// <summary>
        /// 检测/连接远程
        /// </summary>
        public static void CheckLinkRemote (int status)
        {
//#if DEBUG
//            Shared.SimpleControl.CommonPage.IsRemote = true;
//            SmartHome.MqttCommon.StartCloudMqtt ();
//            return;
//#endif
            lock (lockFlag){
                MainPage.tipTime += " Check remote step.1 : " + DateTime.Now.ToString ("yyyy-MM-dd hh:mm:ss:ffff") + ";\n";
                UserConfig.Instance.internetStatus = status;
                Console.WriteLine (MainPage.tipTime);
#if wallon
                return;
#endif
                if (MainPage.LoginUser == null || !MainPage.LoginUser.IsLogin) {
                    return;
                }
 
                new System.Threading.Thread (async () => {
                    try {
                        if (status == 0) {
                            Application.RunOnMainThread (() => {
                                MainPage.AddTip (Language.StringByID (R.MyInternationalizationString.InternetStatusTip));
                                UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.DelColor;
                            });
                        } else {
                            if (MainPage.LoginUser == null || !MainPage.LoginUser.IsLogin) {
                                return;
                            }
                            Application.RunOnMainThread (() => {
                                if (UserMiddle.LinkStatusTip.BackgroundColor == SkinStyle.Current.DelColor) {
                                    UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.MainColor;
                                }
                            });
                            var localFileList = IO.FileUtils.ReadFiles ();
                            var gateWayList = localFileList.FindAll ((obj) => {
                                return (obj.StartsWith ("Equipment_")) && (
                                 obj.Split ('_') [1].ToString () == DeviceType.OnePortBus.ToString () ||
                                    obj.Split ('_') [1].ToString () == DeviceType.RCU.ToString () ||
                                    obj.Split ('_') [1].ToString () == DeviceType.SuperWireless.ToString () ||
                                    obj.Split ('_') [1].ToString () == DeviceType.OnePortWirelessFR.ToString ());
                            });
                            bool canRemote = false;
                            MainPage.tipTime += " Check remote step.2 : " + DateTime.Now.ToString ("yyyy-MM-dd hh:mm:ss:ffff") + ";\n";
                            RomoteLinkState = CommonPage.IsRemote;
                            CommonPage.IsRemote = false;
                            foreach (var gatewayFileName in gateWayList) {
                                var tempStrings = gatewayFileName.Split ('_');
                                var gateWayString = CommonPage.MyEncodingUTF8.GetString (IO.FileUtils.ReadFile (gatewayFileName));
                                var common = Newtonsoft.Json.JsonConvert.DeserializeObject<GatewayBase> (gateWayString);
                                if (common != null) {
                                    if (common.MAC.Replace (".", "") == UserConfig.Instance.GatewayMAC.Replace (".", "")) {
                                        canRemote = true;
                                        if (status == 2) {
                                            CommonPage.FindGateway = true;
                                            //var result = Control.ControlBytesSendHasReturn (Command.ReadDeviceMac, common.SubnetID, common.DeviceID, new byte [] { }, false);
                                            var result = Control.ControlBytesSendHasReturn (Command.ReadGateway, common.SubnetID, common.DeviceID, new byte [] { (byte)new Random ().Next (255), (byte)new Random ().Next (255) });
                                            CommonPage.FindGateway = false;
                                            if (result != null) {
                                                canRemote = false;
                                                var mac0 = CommonPage.byteToHex16 (result [5]) + "." + CommonPage.byteToHex16 (result [6]) + "." + CommonPage.byteToHex16 (result [7]) + "." + CommonPage.byteToHex16 (result [8]) + "." + CommonPage.byteToHex16 (result [9]) + "." + CommonPage.byteToHex16 (result [10]) + "." + CommonPage.byteToHex16 (result [11]) + "." + CommonPage.byteToHex16 (result [12]);
                                                //var mac0 = CommonPage.byteToHex16 (result [0]) + "." + CommonPage.byteToHex16 (result [1]) + "." + CommonPage.byteToHex16 (result [2]) + "." + CommonPage.byteToHex16 (result [3]) + "." + CommonPage.byteToHex16 (result [4]) + "." + CommonPage.byteToHex16 (result [5]) + "." + CommonPage.byteToHex16 (result [6]) + "." + CommonPage.byteToHex16 (result [7]);
                                                if (common.MAC == mac0) {
                                                    var gatewayBytes = Control.ControlBytesSendHasReturn (Command.ReadGateWayModelInfo, common.SubnetID, common.DeviceID, new byte [] { });
                                                    if (gatewayBytes != null) {
                                                        common.Remote_GroupName = CommonPage.MyEncodingGB2312.GetString (gatewayBytes, 1, 20).Trim ('\0');
                                                        common.Remote_ProjectName = CommonPage.MyEncodingGB2312.GetString (gatewayBytes, 21, 20).Trim ('\0');
                                                        common.Remote_UserName = CommonPage.MyEncodingGB2312.GetString (gatewayBytes, 41, 8).Trim ('\0');
                                                        common.Remote_Password = CommonPage.MyEncodingGB2312.GetString (gatewayBytes, 49, 8).Trim ('\0');
                                                        //IO.FileUtils.SaveEquipmentMessage (gatewayDevice);
                                                        var requestJson2 = @"{'Token':'" + MainPage.LoginUser.LoginTokenString + "','MAC':'" + common.MAC + "','Password':'" + common.Remote_Password + "'}";
                                                        var revertObj2 = MainPage.RequestHttps ("UpdateRemoteSwitchPassword", requestJson2, true, false);
                                                        return;
                                                    } else {
                                                        if (Shared.SimpleControl.CommonPage.IsRemote) {
                                                            Shared.SimpleControl.CommonPage.IsRemote = false;
                                                            gatewayBytes = Control.ControlBytesSendHasReturn (Command.ReadGateWayModelInfo, common.SubnetID, common.DeviceID, new byte [] { });
                                                            if (gatewayBytes != null) {
                                                                common.Remote_GroupName = CommonPage.MyEncodingGB2312.GetString (gatewayBytes, 1, 20).Trim ('\0');
                                                                common.Remote_ProjectName = CommonPage.MyEncodingGB2312.GetString (gatewayBytes, 21, 20).Trim ('\0');
                                                                common.Remote_UserName = CommonPage.MyEncodingGB2312.GetString (gatewayBytes, 41, 8).Trim ('\0');
                                                                common.Remote_Password = CommonPage.MyEncodingGB2312.GetString (gatewayBytes, 49, 8).Trim ('\0');
                                                                //IO.FileUtils.SaveEquipmentMessage (gatewayDevice);
                                                                var requestJson2 = @"{'Token':'" + MainPage.LoginUser.LoginTokenString + "','MAC':'" + common.MAC + "','Password':'" + common.Remote_Password + "'}";
                                                                var revertObj2 = MainPage.RequestHttps ("UpdateRemoteSwitchPassword", requestJson2, true, false);
                                                                return;
                                                            }
                                                        }
                                                    }
                                                    Application.RunOnMainThread (() => {
                                                        UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.LinkStatusTipColor;
                                                        MainPage.Loading.Hide ();
                                                    });
                                                    Shared.SimpleControl.Phone.UserMiddle.ReadAllDeviceStatus ();
                                                    await SmartHome.MqttCommon.Close ();
                                                    return;
                                                }
                                            }
                                        } else {
                                            break;
                                        }
                                    }
                                }
                            }
                            MainPage.tipTime += " Check remote step.3 : " + DateTime.Now.ToString ("yyyy-MM-dd hh:mm:ss:ffff") + ";\n";
                            if (canRemote) {
                                if (!string.IsNullOrEmpty (UserConfig.Instance.CurrentRegion.MAC)) {
                                    Shared.SimpleControl.CommonPage.IsRemote = true;
                                    await SmartHome.MqttCommon.StartCloudMqtt ();
                                }
                            }
                        }
                    } catch (Exception ex) {
                        Console.WriteLine ("CheckLinkRemote : " + ex.ToString ());
                    } finally {
                        Application.RunOnMainThread (() => {
                            MainPage.Loading.Hide ();
                        });
                    }
                }) { IsBackground = true }.Start ();
            }
        }
    }
}