wxr
2019-12-25 3df772e4fdd553b2954e8f3f55ce7ff5905407c3
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
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.TipEquipmentNotOnline), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
        }
 
        static object lockObj = new object ();
 
        /// <summary>
        /// 检测/连接远程
        /// </summary>
        /// <returns>The link remote.</returns>
        /// <param name="status">Status.</param>
        public static void CheckLinkRemote (int status)
        {
            lock (lockObj) {
                Console.WriteLine ("CheckLinkRemote!!!");
                System.Threading.Tasks.Task.Run (() => {
                    try {
                        if (status == 0) {
                            Application.RunOnMainThread (() => {
                                MainPage.WiFiStatus = "CrabtreeAdd/WiFiUnlink.png";
                                MainPage.AddTip (Language.StringByID (R.MyInternationalizationString.InternetStatusTip));
                                //UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.DelColor;
                            });
                        } else {
                            MainPage.WiFiStatus = "CrabtreeAdd/WiFi.png";
                            if (MainPage.LoginUser == null || !MainPage.LoginUser.IsLogin) {
                                return;
                            }
 
                            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.OnePortWirelessFR.ToString ());
                            });
                            string rmf = "";
                            List<string> linkList = new List<string> ();
                            GatewayBase common = null;
                            string gateWayString = "";
                            if (gateWayList.Count > 1) {
                                foreach (var gatewayFileName in gateWayList) {
                                    var tempStrings = gatewayFileName.Split ('_');
                                    if (tempStrings [1].ToString () == DeviceType.OnePortBus.ToString () || tempStrings [1].ToString () == DeviceType.RCU.ToString () ||
                                        tempStrings [1].ToString () == DeviceType.OnePortWirelessFR.ToString ()) {
                                        gateWayString = CommonPage.MyEncodingUTF8.GetString (IO.FileUtils.ReadFile (gatewayFileName));
                                        common = Newtonsoft.Json.JsonConvert.DeserializeObject<GatewayBase> (gateWayString);
 
                                        if (common.MAC.Replace (".", "") == UserConfig.Instance.GatewayMAC.Replace (".", "")) {
                                            rmf = gatewayFileName;
                                            if (!string.IsNullOrEmpty (common.Remote_UserName) && !string.IsNullOrEmpty (common.Remote_Password) &&
                                                !string.IsNullOrEmpty (common.Remote_GroupName) && !string.IsNullOrEmpty (common.Remote_ProjectName)) {
                                                break;
                                            }
                                        }
                                        common = null;
                                    }
                                }
                            } else if (gateWayList.Count == 1) {
                                rmf = gateWayList [0];
                                gateWayString = CommonPage.MyEncodingUTF8.GetString (IO.FileUtils.ReadFile (rmf));
                                common = Newtonsoft.Json.JsonConvert.DeserializeObject<GatewayBase> (gateWayString);
                            } else {
                                return;
                            }
#if DEBUG
                            if (common == null) {
                                if (MainPage.LoginUser.AccountString == "464027401@qq.com") {
                                    gateWayString = CommonPage.MyEncodingUTF8.GetString (IO.FileUtils.ReadFile (UserConfig.Instance.RemoteModeFile));
                                    common = Newtonsoft.Json.JsonConvert.DeserializeObject<GatewayBase> (gateWayString);
                                }
                            }
#endif
                            if (common == null || common.Type == DeviceType.UnKown) {
                                return;
                            }
                            Application.RunOnMainThread (() => {
                                MainPage.Loading.Start ("Please wait...");
                            });
                            if (status == 1) {
                                //if (common.Type == DeviceType.OnePortWirelessFR) {
                                //    var gateWay = Newtonsoft.Json.JsonConvert.DeserializeObject<OnePortWirelessFR> (gateWayString);
                                //    var mac = gateWay.MAC.Replace (".", "");
                                //    SystemRemote.LinkRemote (gateWay.Remote_UserName, gateWay.Remote_ProjectName, gateWay.Remote_Password, true, mac);
                                //} else if (common.Type == DeviceType.OnePortBus) {
                                //    var gateWay = Newtonsoft.Json.JsonConvert.DeserializeObject<OnePortBus> (gateWayString);
                                //    var mac = gateWay.MAC.Replace (".", "");
                                //    SystemRemote.LinkRemote (gateWay.Remote_UserName, gateWay.Remote_ProjectName, gateWay.Remote_Password, true, mac);
                                //} else if (common.Type == DeviceType.RCU) {
                                //    var gateWay = Newtonsoft.Json.JsonConvert.DeserializeObject<RCU> (gateWayString);
                                //    var mac = gateWay.MAC.Replace (".", "");
                                //    SystemRemote.LinkRemote (gateWay.Remote_UserName, gateWay.Remote_ProjectName, gateWay.Remote_Password, true, mac);
                                //}
                                var gateWay = Newtonsoft.Json.JsonConvert.DeserializeObject<GatewayBase> (gateWayString);
                                SystemRemote.LinkRemote (gateWay.Remote_UserName, gateWay.Remote_ProjectName, gateWay.Remote_Password, true);
                            } else {
                                CommonPage.IsRemote = false;
                                CommonPage.FindGateway = true;
                                var result = Control.ControlBytesSendHasReturn (Command.ReadDeviceMac, common.SubnetID, common.DeviceID, new byte [] { }, false);
                                CommonPage.FindGateway = false;
                                if (result != null) {
                                    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.Type == DeviceType.OnePortBus) {
                                    if (common.MAC == mac0) {
                                        Application.RunOnMainThread (() => {
                                            MainPage.WiFiStatus = "CrabtreeAdd/WiFi.png";
                                            //UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.LinkStatusTipColor;
                                        });
                                        return;
                                    }
                                }
                                var gateWay = Newtonsoft.Json.JsonConvert.DeserializeObject<GatewayBase> (gateWayString);
                                SystemRemote.LinkRemote (gateWay.Remote_UserName, gateWay.Remote_ProjectName, gateWay.Remote_Password, true);
                            }
                        }
                    } catch (Exception ex) {
                        Console.WriteLine (ex.ToString ());
                    } finally {
                        Application.RunOnMainThread (() => {
                            if (status != 0) {
                                Shared.SimpleControl.Phone.UserMiddle.ReadAllDeviceStatus ();
                            }
                            UserMiddle.btnLinkStatus.UnSelectedImagePath = MainPage.WiFiStatus;
                            MainPage.Loading.Hide ();
                        });
                    }
                });
            }
        }
    }
 
}