using Shared; using Shared.SimpleControl; using Shared.SimpleControl.R; using System; using System.Net; using System.Threading; using System.Threading.Tasks; namespace Shared.SimpleControl.Phone { public class SystemRemote { static SystemRemote () { //如果在远程连接的情况下,就会有定时发送心跳包 System.Threading.Tasks.Task.Run (() => { while (true) { System.Threading.Thread.Sleep (10 * 1000); if (CommonPage.IsRemote) { Control.ControlBytesSend (Command.ReadRemark, 0, 0, new byte [] { }, SendCount.Zero); Console.WriteLine ("心跳包"); } } }); } /// /// 加密算法 /// /// /// /// /// static void encryption (int [] SessionKey, int SessionLen, int [] KeyCode, int KeyCodeLen) { int j, flag = 0; int session = 0; int temp1, temp2, temp = 0; int sk = 0; flag = 0; j = SessionLen; int k = 0; while (0 < j) { if (0 < flag) { session = 0xFF & (session + SessionKey [k++]); flag = 0; } else { session = 0xFF & (session - SessionKey [k++]); flag = 1; } j--; } sk = 0xFF & (session % 15); flag = 0; j = KeyCodeLen; k = 0; while (0 < j) { temp1 = 0xFF & (KeyCode [k] + (session % j)); temp1 = 0xFF & (~temp1 + 1); temp2 = 0xFF & (temp1 & 0xf0); temp1 = 0xFF & (temp1 & 0x0f); switch (sk) { case 0: temp = 0xFF & ((temp2 >> 4) | (temp1 << 4)); break; case 1: temp = 0xFF & ((temp2 + j * 2) | (temp1 + j % 4)); break; case 2: temp = 0xFF & ((temp2 + j % 5) | (temp1 + 1)); break; case 3: temp = 0xFF & ((temp2 >> 2) | (temp1 << 2)); break; case 4: temp = 0xFF & ((temp2 - 5) | (temp1 + 4)); break; case 5: temp = 0xFF & ((temp2 % 7) | (temp1 + 4)); break; case 6: temp = 0xFF & ((temp2 + j) | (temp1)); break; case 7: temp = 0xFF & ((temp2 - j) | (temp1 + j % 2)); break; case 8: temp = 0xFF & ((~temp2) | (temp1 + j % 6)); break; case 9: temp = 0xFF & ((temp2 + j % 3) | (temp1 + j)); break; case 10: temp = 0xFF & ((temp2) | (temp1 + 1)); break; case 11: temp = 0xFF & ((temp2) | (temp1)); break; case 12: temp = 0xFF & ((temp2 - j) | (temp1 + 4)); break; case 13: temp = 0xFF & ((temp2) | (temp1 + j)); break; case 14: temp = 0xFF & ((temp2 - 2) | (temp1 - 4)); break; default: temp = 0xFF & ((temp2 >> 4) | (temp1 << 4)); break; } KeyCode [k] = temp; k++; j--; } } static byte [] makeSurePassword (string Remote_ProjectName, string Remote_UserName, string Remote_Password) { byte [] tempBytes = new byte [66]; byte [] b1 = CommonPage.MyEncodingGB2312.GetBytes (Remote_ProjectName); //工程备注 System.Array.Copy (b1, 0, tempBytes, 0, 20 < b1.Length ? 20 : b1.Length); //用户名 byte [] b2 = CommonPage.MyEncodingGB2312.GetBytes (Remote_UserName); System.Array.Copy (b2, 0, tempBytes, 20, 8 < b2.Length ? 8 : b2.Length); byte [] b3 = CommonPage.MyEncodingGB2312.GetBytes (Remote_Password); int [] KeyCode = new int [8]; System.Array.Copy (b3, 0, KeyCode, 0, 8 < b3.Length ? 8 : b3.Length); int [] SessionKey = new int [] { (int)0x01, 0x01, 0x01, 0x01, (int)0x01, (int)0x01, 0x01, 0x01 }; //密匙 //0xF1 0x05 0x23 0x49 0xac 0xe6 0x5b 0x28 tempBytes [36] = (byte)SessionKey [0]; tempBytes [37] = (byte)SessionKey [1]; tempBytes [38] = (byte)SessionKey [2]; tempBytes [39] = (byte)SessionKey [3]; tempBytes [40] = (byte)SessionKey [4]; tempBytes [41] = (byte)SessionKey [5]; tempBytes [42] = (byte)SessionKey [6]; tempBytes [43] = (byte)SessionKey [7]; encryption (SessionKey, 8, KeyCode, (byte)8); // 远程访问的密码 //0x0e 0x0f 0x0e 0x12 0x13 0x12 0x13 0x04 tempBytes [28] = (byte)KeyCode [0]; tempBytes [29] = (byte)KeyCode [1]; tempBytes [30] = (byte)KeyCode [2]; tempBytes [31] = (byte)KeyCode [3]; tempBytes [32] = (byte)KeyCode [4]; tempBytes [33] = (byte)KeyCode [5]; tempBytes [34] = (byte)KeyCode [6]; tempBytes [35] = (byte)KeyCode [7]; int year = System.DateTime.Now.Year; //ca.get();//获取年份 int month = System.DateTime.Now.Month; //ca.get(Calendar.MONTH);//获取月份 int day = System.DateTime.Now.Day; //ca.get(Calendar.DAY_OF_MONTH);//获取日 int hour = System.DateTime.Now.Hour; //ca.get(Calendar.HOUR);//小时 int minute = System.DateTime.Now.Minute; //ca.get(Calendar.MINUTE);//分 int second = System.DateTime.Now.Second; //ca.get(Calendar.SECOND);//秒 var yearString = year.ToString (); tempBytes [44] = byte.Parse (yearString.Substring (yearString.Length - 2, 2)); tempBytes [45] = (byte)month; tempBytes [46] = (byte)day; tempBytes [47] = (byte)hour; tempBytes [48] = (byte)minute; tempBytes [49] = (byte)second; tempBytes [50] = (byte)('H'); tempBytes [51] = (byte)('D'); tempBytes [52] = (byte)('L'); tempBytes [53] = (byte)('-'); tempBytes [54] = (byte)('O'); tempBytes [55] = (byte)('N'); tempBytes [56] = (byte)(' '); tempBytes [57] = (byte)(' '); tempBytes [58] = (byte)(' '); tempBytes [59] = (byte)(' '); tempBytes [60] = (byte)('2'); tempBytes [61] = (byte)('.'); tempBytes [62] = (byte)('0'); tempBytes [63] = (byte)('.'); tempBytes [64] = (byte)('0'); tempBytes [65] = (byte)('0'); return tempBytes; } public static bool checeInternet () { //byte [] bytes = null; //var dateTime = DateTime.Now; //System.Threading.Tasks.Task.Run (() => { // bytes = new System.Net.WebClient { Proxy = null }.DownloadData ("http://www.baidu.com"); //}); //while ((DateTime.Now - dateTime).TotalMilliseconds < 8000) { // System.Threading.Thread.Sleep (100); // if (bytes != null) { // return true; // } //} return true; } static bool ShowTip = true; //static Alert sssAlert = new Alert ("", "Make sure your gateway is powered ON and connected to WIFI router", "Cancel", "Try again"); /// /// 连接远程一端口交换机 /// /// 用户名 /// 工程名 /// 密码 /// 连接为真,断开为假 public static void LinkRemote (string userName, string projectName, string password, bool isConnect) { bool result = false; bool notInternet = false; string linkMsg = UserConfig.Instance.CurrentRegion.RegionName + " : "; try { if (!checeInternet ()) { MainPage.AddTip (Language.StringByID (R.MyInternationalizationString.CheckInternet)); Application.RunOnMainThread (() => { MainPage.WiFiStatus = "CrabtreeAdd/WiFiUnlink.png"; MainPage.Loading.Hide (); notInternet = true; }); } else { notInternet = false; CommonPage.IsRemote = true; Application.RunOnMainThread (() => { MainPage.Loading.Start ("Connecting..."); MainPage.Loading.CurStatus = true; }); //System.Threading.Tasks.Task.Run (() => { byte [] userNameBytes = CommonPage.MyEncodingGB2312.GetBytes (userName); byte [] projectBytes = CommonPage.MyEncodingGB2312.GetBytes (projectName); byte [] firstBytes = new byte [28]; //用户名 System.Array.Copy (userNameBytes, 0, firstBytes, 0, 8 < userNameBytes.Length ? 8 : userNameBytes.Length); //工程备注 System.Array.Copy (projectBytes, 0, firstBytes, 8, 20 < projectBytes.Length ? 20 : projectBytes.Length); //服务器的子网号设备号固定为1,251 //1byte // 0不存在 1存在 //远程访问的用户名 8 bytes //工程备注 20 bytes //IP地址: 4bytes //端口: 2bytes //在线状态: 1byte 0不在线 1在线 byte [] firstReturnBytes = Control.ControlBytesSendHasReturn (Command.RemoteFirst, 251, 251, firstBytes); //连接不到远程服务器 if (firstReturnBytes == null) { linkMsg = Language.StringByID (R.MyInternationalizationString.LinkSeverLoser);//",远程连接与服务器通讯失败。"; } //从服务器获取用户远程连接的信息 else { if (firstReturnBytes [0] == 0) { linkMsg += Language.StringByID (R.MyInternationalizationString.LinkNotExist); return; } byte [] secoudBytes = new byte [7]; System.Array.Copy (firstReturnBytes, 29, secoudBytes, 0, 6); secoudBytes [6] = isConnect ? (byte)1 : (byte)0; byte [] secoudBytesReturnBytes = Control.ControlBytesSendHasReturn (Command.RemoteSecoud, 251, 251, secoudBytes); if (!isConnect) { //断开连接远程服务器 linkMsg += Language.StringByID (R.MyInternationalizationString.Remoteserverhasnnotdisconnected); return; } var tempBytes = makeSurePassword (projectName, userName, password); //密码验证 if (secoudBytesReturnBytes != null) { byte [] isSuccess = Control.ControlBytesSendHasReturn (Command.Remote3th, 0xFF, 0xFF, tempBytes); if (isSuccess == null) { //一端口交换机没有数据回复 linkMsg += Language.StringByID (R.MyInternationalizationString.RemoteDevicesHaveNoReply); return; } if (isSuccess [0] == 0xF8) { result = true; //远程连接成功 linkMsg += Language.StringByID (R.MyInternationalizationString.LinkSuccess); } else { //密码错误 linkMsg += Language.StringByID (R.MyInternationalizationString.LinkLoser); } } } } } catch (Exception ex) { Console.WriteLine (ex.Message); } finally { if (!result) { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); if (ShowTip) { ShowTip = false; Alert sssAlert = new Alert ("", "Make sure your gateway is powered ON and connected to WIFI router", "Cancel", "Try again"); sssAlert.Show (); sssAlert.ResultEventHandler += (sd, dd) => { ShowTip = true; if (dd) { new System.Threading.Thread (() => { LinkRemote (userName, projectName, password, isConnect); }) { IsBackground = true }.Start (); } }; } }); } else { CommonPage.IsRemote = result; MainPage.AddTip (linkMsg); Application.RunOnMainThread (() => { MainPage.Loading.Hide (); MainPage.Loading.CurStatus = false; if (notInternet) { MainPage.WiFiStatus = "CrabtreeAdd/WiFiUnlink.png"; } else { if (result) { //UserMiddle.LinkStatusTip.BackgroundColor = 0xAA69E64A; MainPage.WiFiStatus = "CrabtreeAdd/CloudLink.png"; } else { //UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.DelColor; MainPage.WiFiStatus = "CrabtreeAdd/CloudUnlink.png"; } } }); } } } } }