JLChen
2020-02-20 965290476c027deb96891cde1095d9a100581a6b
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
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 bool isConnectiong = false;
 
        /// <summary>
        /// 检测/连接远程
        /// </summary>
        public static void CheckLinkRemote (int status)
        {
            //if (isConnectiong) {
            //    return;
            //}
            //isConnectiong = true;
            UserConfig.Instance.internetStatus = status;
#if wallon
                return;
#endif
            if (MainPage.LoginUser == null || !MainPage.LoginUser.IsLogin || isConnectiong) {
                return;
            }
 
            isConnectiong = true;
            new System.Threading.Thread (async () => {
                try {
                    if (status == 0) {
                        Application.RunOnMainThread (() => {
                            MainPage.AddTip (Language.StringByID (R.MyInternationalizationString.InternetStatusTip));
                            MainPage.WiFiStatus = "CrabtreeAdd/WiFiUnlink.png";
                            UserMiddle.btnLinkStatus.UnSelectedImagePath = MainPage.WiFiStatus;
 
                        });
 
                        await DisConnectRemoteAsync ();
 
                    } else {
 
                        //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 () ||
                                obj.Split ('_') [1].ToString () == DeviceType.OnePortMqttFR.ToString ());
                        });
                        bool canRemote = false;
                        if (CommonPage.IsRemote) {
                            //await SmartHome.MqttCommon.StartCloudMqtt ();
                            SmartHome.MqttCommon.MqttRemoteSend (new byte [] { }, 3);
                        }
                        if (gateWayList == null || gateWayList.Count == 0) {
                            Utlis.ShowAppLinkStatus (AppLinkStatus.WiFiOffline);
                            await DisConnectRemoteAsync ();
                        }
                        //本地是否存在与当前住宅绑定网关的数据
                        bool HomeGatewaysExistsLocally = 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 (".", "")) {
                                    HomeGatewaysExistsLocally = true;
                                    canRemote = true;
                                    if (status == 2) {
                                        //var result = Control.ControlBytesSendHasReturn (Command.ReadGateway, common.SubnetID, common.DeviceID, new byte [] { (byte)new Random ().Next (255), (byte)new Random ().Next (255) });
                                        #region
                                        var control = new Control ();
                                        control.Send (new Target () {
                                            IPEndPoint = new System.Net.IPEndPoint (System.Net.IPAddress.Parse (new Net.NetWiFi ().BroadcastIpAddress.ToString ()), 6000),
                                            Command = Command.ReadGateway,
                                            SubnetID = common.SubnetID,
                                            DeviceID = common.DeviceID,
                                            AddData = new byte [] { (byte)new Random ().Next (255), (byte)new Random ().Next (255) },
                                        }, SendCount.Three, true, true);
                                        var result = control.UsefulBytes;
 
                                        #endregion
                                        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]);
                                            if (common.MAC == mac0) {
                                                CommonPage.IsRemote = false;
                                                Application.RunOnMainThread (() => {
                                                    MainPage.WiFiStatus = "CrabtreeAdd/WiFi.png";
                                                    UserMiddle.btnLinkStatus.UnSelectedImagePath = MainPage.WiFiStatus;
                                                    MainPage.Loading.Hide ();
                                                });
                                                Shared.SimpleControl.Phone.UserMiddle.ReadAllDeviceStatus ();
                                                await SmartHome.MqttCommon.DisConnectRemoteMqttClient ();
                                                return;
                                            }
                                        }
                                    } else {
                                        break;
                                    }
                                }
                            }
                        }
 
 
 
                        //能远程
                        if (canRemote) {
                            if (!CommonPage.IsRemote) {
                                if (UserConfig.Instance.CheckHomeGateways ()) {
                                    if (!string.IsNullOrEmpty (UserConfig.Instance.CurrentRegion.HomeGateways [0].GatewayUniqueId)) {
                                        Utlis.ShowAppLinkStatus (AppLinkStatus.CloudUnlink);
                                        CommonPage.IsRemote = true;
                                        SmartHome.MqttCommon.IsGatewayOnline = false;
                                        //await SmartHome.MqttCommon.StartCloudMqtt ();
                                    }
                                }
                            }
 
                        } else {
 
                            //不能远程,如果之前远程模式则断开MQTT连接,显示为未连接
                            if (CommonPage.IsRemote) {
                                Utlis.ShowAppLinkStatus (AppLinkStatus.WiFiOffline);
                                CommonPage.IsRemote = false;
                                await SmartHome.MqttCommon.DisConnectRemoteMqttClient ();
 
                            }
 
                            if (!HomeGatewaysExistsLocally) {
                                Utlis.ShowAppLinkStatus (AppLinkStatus.WiFiOffline);
                            }
 
                        }
 
 
                        //if (canRemote && !CommonPage.IsRemote) {
                        //    if (UserConfig.Instance.CheckHomeGateways ()) {
                        //        if (!string.IsNullOrEmpty (UserConfig.Instance.CurrentRegion.HomeGateways [0].GatewayUniqueId)) {
                        //            Utlis.ShowAppLinkStatus (AppLinkStatus.CloudUnlink);
                        //            Shared.SimpleControl.CommonPage.IsRemote = true;
                        //            //await SmartHome.MqttCommon.StartCloudMqtt ();
                        //        }
                        //    }
                        //}
 
 
 
                    }
                } catch (Exception ex) {
                    Console.WriteLine ("CheckLinkRemote : " + ex.ToString ());
                } finally {
                    isConnectiong = false;
                    Application.RunOnMainThread (() => {
                        MainPage.Loading.Hide ();
                    });
                }
            }) { IsBackground = true }.Start ();
        }
 
 
 
        /// <summary>
        /// 关闭远程连接模式,并断开MQTT
        /// </summary>
        /// <returns></returns>
        static async System.Threading.Tasks.Task DisConnectRemoteAsync (){
 
            if (CommonPage.IsRemote) {
                CommonPage.IsRemote = false;
                await SmartHome.MqttCommon.DisConnectRemoteMqttClient ();
            }
        }
    }
}