using System; using System.Collections.Generic; using System.Text; namespace Shared.SimpleControl.Phone { public class GuideAddGateway : FrameLayout { static GuideAddGateway inView; static VerticalScrolViewLayout verticalScrolView; static List addedList = new List (); public GuideAddGateway () { BackgroundColor = SkinStyle.Current.MainColor; inThisView = true; } public override void RemoveFromParent () { inView = null; base.RemoveFromParent (); inThisView = false; } /// /// 修改网关备注 /// static void ResetGatewayName (GatewayBase gatewayBase,Button nameButton1) { Dialog dialog = new Dialog (); var dialogView = new FrameLayout (); dialogView.MouseUpEventHandler += (sn, dd) => { dialog.Close (); }; dialog.AddChidren (dialogView); var bodyView = new FrameLayout () { Gravity = Gravity.Center, Width = Application.GetRealWidth (500), Height = Application.GetRealHeight (400), Radius = 5, BorderColor = SkinStyle.Current.Transparent, BorderWidth = 0, BackgroundColor = SkinStyle.Current.DialogColor, }; dialogView.AddChidren (bodyView); var btnTitle = new Button () { Height = Application.GetRealHeight (80), TextAlignment = TextAlignment.Center, Text = "Setting", TextColor = SkinStyle.Current.DialogTextColor, BackgroundColor = SkinStyle.Current.DialogTitle, }; bodyView.AddChidren (btnTitle); var btnZoneName = new Button () { X = Application.GetRealWidth (50), Y = btnTitle.Bottom + Application.GetRealHeight (10), Width = Application.GetRealWidth (400), Height = Application.GetRealHeight (80), TextAlignment = TextAlignment.CenterLeft, TextColor = SkinStyle.Current.TextColor, Text = "Gateway Name : " }; bodyView.AddChidren (btnZoneName); var etRemark = new EditText () { X = Application.GetRealWidth (50), Y = btnZoneName.Bottom + Application.GetRealHeight (10), Width = Application.GetRealWidth (400), Height = Application.GetRealHeight (80), TextColor = SkinStyle.Current.TextColor, TextAlignment = TextAlignment.Center, Radius = 5, BorderColor = SkinStyle.Current.BorderColor, BorderWidth = 2, Text = gatewayBase.Name, }; bodyView.AddChidren (etRemark); etRemark.EditorEnterAction += (obj) => { Application.HideSoftInput (); }; if (MainPage.LoginUser.AccountType == 1) { etRemark.Enable = false; etRemark.BorderWidth = 0; } var BottomView = new FrameLayout () { Y = Application.GetRealHeight (400 - 88), Height = Application.GetRealHeight (90), BackgroundColor = SkinStyle.Current.Black50Transparent, }; bodyView.AddChidren (BottomView); var btnClose = new Button () { Width = Application.GetRealWidth (250), TextID = R.MyInternationalizationString.cancel, TextColor = SkinStyle.Current.DialogTextColor, BackgroundColor = SkinStyle.Current.DialogTitle, TextAlignment = TextAlignment.Center, }; BottomView.AddChidren (btnClose); btnClose.MouseUpEventHandler += (bb, cc) => { dialog.Close (); }; var btnOption = new Button () { X = btnClose.Right + 1, Width = Application.GetRealWidth (250), TextID = R.MyInternationalizationString.OK, TextColor = SkinStyle.Current.DialogTextColor, BackgroundColor = SkinStyle.Current.DialogTitle, TextAlignment = TextAlignment.Center, }; btnOption.MouseUpEventHandler += (sdf, fds) => { dialog.Close (); if (etRemark.Text.Trim () == "") { new Alert ("", "Please enter gateway name", Language.StringByID (R.MyInternationalizationString.Close)).Show (); return; } gatewayBase.Name = etRemark.Text.Trim (); CommonPage.UpdateRemark (gatewayBase.SubnetID, gatewayBase.DeviceID, etRemark.Text.Trim ()); nameButton1.Text = etRemark.Text.Trim (); IO.FileUtils.SaveEquipmentMessage (gatewayBase); }; BottomView.AddChidren (btnOption); dialog.Show (); } public void ShowPage (RegionInfoRes home) { addedList = new List (); inView = this; #region 标题 var topView = new FrameLayout () { Y = Application.GetRealHeight (36), Height = Application.GetRealHeight (90), Width = Application.GetRealWidth (640), }; AddChidren (topView); var title = new Button () { TextAlignment = TextAlignment.Center, Text = home.RegionName, TextColor = SkinStyle.Current.TextColor1, TextSize = 19, }; topView.AddChidren (title); var logo = new Button () { Width = Application.GetRealWidth (154), Height = Application.GetRealHeight (90), X = Application.GetRealWidth (486), UnSelectedImagePath = MainPage.LogoString, }; topView.AddChidren (logo); logo.MouseUpEventHandler += (dd, fff) => { #if DEBUG verticalScrolView.RemoveAll (); addedList.Clear (); SearchGatewayList (); #endif }; var back = new Button () { Height = Application.GetRealHeight (90), Width = Application.GetRealWidth (85), UnSelectedImagePath = "Item/Back.png", SelectedImagePath = "Item/BackSelected.png", }; topView.AddChidren (back); back.MouseUpEventHandler += (sender, e) => { (Parent as PageLayout).PageIndex -= 1; }; #endregion verticalScrolView = new VerticalScrolViewLayout () { Y = Application.GetRealHeight (126), //Y = waitView.Bottom, Height = Application.GetRealHeight (1136 - 126 - 0), BackgroundColor = SkinStyle.Current.ViewColor }; AddChidren (verticalScrolView); SearchGatewayList (); } //static DateTime waitingTime = DateTime.Now; //static int sleepTime = 0; public static void InitGatewayRowView (GatewayBase common) { if (inView == null) return; lock (addedList) { if (addedList.Find ((obj) => obj.SubnetID == common.SubnetID && obj.DeviceID == common.DeviceID) != null) return; addedList.Add (common); var gatewayView = new RowLayout () { Height = Application.GetRealHeight (120), BackgroundColor = SkinStyle.Current.ViewColor, }; verticalScrolView.AddChidren (gatewayView); var pointButton1 = new Button () { Width = Application.GetRealHeight (13), Height = Application.GetRealHeight (13), X = Application.GetRealWidth (60), UnSelectedImagePath = "Item/Point.png", SelectedImagePath = "Item/PointSelected.png", Gravity = Gravity.CenterVertical, }; gatewayView.AddChidren (pointButton1); var nameButton1 = new Button () { Width = Application.GetRealWidth (342), Height = LayoutParams.MatchParent, Text = common.Name, TextAlignment = TextAlignment.CenterLeft, X = pointButton1.Right + Application.GetRealWidth (20), TextColor = SkinStyle.Current.TextColor1 }; gatewayView.AddChidren (nameButton1); var rightButton1 = new Button () { X = Application.GetRealWidth (550), Width = Application.GetRealWidth (28), Height = Application.GetRealHeight (40), Gravity = Gravity.CenterVertical, UnSelectedImagePath = "Item/Right.png", SelectedImagePath = "Item/RightSelected.png", }; gatewayView.AddChidren (rightButton1); var goNextRight = new Button () { X = Application.GetRealWidth (550), Width = Application.GetRealWidth (80), }; gatewayView.AddChidren (goNextRight); var btnDel = new Button () { Text = "Delete", BackgroundColor = SkinStyle.Current.DelColor, }; gatewayView.AddRightView (btnDel); btnDel.MouseUpEventHandler += (sender, e) => { var delAlert = new Alert ("", "If you delete gateway, you will not able to control the devices. Still you want to delete the gateway?", "No", "Yes"); delAlert.Show (); delAlert.ResultEventHandler += ( sender1, e1) => { if (e1) { var delFile = IO.FileUtils.ReadFiles ().FindAll ((w) => { return (w.Split ('_') [0] == "Equipment") && (w.Split ('_') [2] == common.SubnetID.ToString ()); }); for (int k = 0; k < delFile.Count; k++) { if (delFile [k].Contains (DeviceType.OnePortWirelessFR.ToString ()) || delFile [k].Contains (DeviceType.OnePortBus.ToString ()) || delFile [k].Contains (DeviceType.RCU.ToString ()) || delFile [k].Contains (DeviceType.SuperWireless.ToString ())) { continue; } IO.FileUtils.DeleteFile (delFile [k]); } addedList.Remove (common); gatewayView.RemoveFromParent (); } }; }; EventHandler eHandler = (sender, e) => { MainPage.Loading.Start ("Please wait..."); bool needHide = true; new System.Threading.Thread (() => { System.Threading.Thread.Sleep (10000); if (needHide) { MainPage.Loading.MouseUpEventHandler += (sd, dd) => { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); needHide = false; }); }; } }) { IsBackground = true }.Start (); bool goNext = false; System.Threading.Tasks.Task.Run (() => { try { if (common.Type == DeviceType.OnePortWirelessFR) { var bingResidenceIdBytes = Control.ControlBytesSendHasReturn (Command.Read_APP_Data_STORE_1D5C_CMD, common.SubnetID, common.DeviceID, new byte [] { }); if (bingResidenceIdBytes == null) { Application.RunOnMainThread (() => { try { if (inThisView) { new Alert ("", "No response from gateway. Please make sure the gateway is online and its firmware is up to date.", Language.StringByID (R.MyInternationalizationString.Close)).Show (); } } catch { } }); } int bingResideceId = 0; for (int i = 0; i < bingResidenceIdBytes.Length; i++) { bingResideceId += (int)(bingResidenceIdBytes [i] * Math.Pow (256, 3 - i)); } if (bingResideceId != UserConfig.Instance.CurrentRegion.RegionID && bingResideceId != 0) { Application.RunOnMainThread (() => { if (inThisView) { new Alert ("", "This gateway has been bound to a residence, please reset it before binding to another residence.", Language.StringByID (R.MyInternationalizationString.Close)).Show (); } }); } else { goNext = true; } } else { goNext = true; } } catch (Exception ex) { Console.WriteLine (ex.Message); } finally { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); needHide = false; if (goNext) { CommonPage.FindGatewayChilrenIPAddress = common.IPAddress; inThisView = false; if (inView != null) { Console.WriteLine ("inView != null"); var deviceListView = new GuideSettingGateway (common); (inView.Parent as PageLayout).AddChidren (deviceListView); deviceListView.ShowPage (); (inView.Parent as PageLayout).PageIndex = (inView.Parent as PageLayout).ChildrenCount - 1; } else { Console.WriteLine ("inView == null"); UserHomePage.FrameLayoutMain.AddChidren (UserMiddle.guidePageView); var guide = new GuideAddResidence (); UserMiddle.guidePageView.AddChidren (guide); guide.ShowHomeList (); UserMiddle.guidePageView.PageIndex = 0; } } }); } }); }; goNextRight.MouseUpEventHandler += eHandler; rightButton1.MouseUpEventHandler += eHandler; nameButton1.MouseUpEventHandler += (sender, e) => { ResetGatewayName (common, nameButton1); }; } } static bool inThisView = false; Alert checkInternetAlert = new Alert ("", "Your phone in not connected to WIFI network, please connet", "Close", "Search again"); Alert confirmAlert = new Alert ("", "Please make sure gateway is powered up and comect to WiFi router.", "Close", "Search again"); Alert confirmAlert1 = new Alert ("", "If not all the gateways are in the list,you can search again.", "Close", "Search again"); void SearchGatewayList () { if (UserConfig.Instance.internetStatus == 0 || UserConfig.Instance.internetStatus == 1) { checkInternetAlert.Show (); checkInternetAlert.ResultEventHandler += (dsss, eee) => { if (eee) { SearchGatewayList (); } }; return; } CommonPage.FindGateway = true; new System.Threading.Thread (() => { try { Application.RunOnMainThread (() => { MainPage.Loading.Start ("Searching for gateway..."); }); #region 搜索设备 CommonPage.LocalPhoneFindDevice = true; CommonPage.RandomHigh = (byte)new Random ().Next (255); CommonPage.RandomLow = (byte)new Random ().Next (255); CommonPage.GateWayList.Clear (); CommonPage.FindGateway = true; //如果两次都没有数据反馈,就不读取 int readCount = 2; while (0 < readCount) { readCount--; System.IO.MemoryStream ms = new System.IO.MemoryStream (); ms.WriteByte (CommonPage.RandomHigh); ms.WriteByte (CommonPage.RandomLow); List list = CommonPage.GateWayList; int tempCount = list.Count; string s = ";"; for (int i = 0; i < list.Count; i++) { Common common = list [i]; if (s.Contains (";" + common.SubnetID.ToString () + ":" + common.DeviceID.ToString ())) continue; s += common.SubnetID.ToString () + ":" + common.DeviceID.ToString () + ";"; ms.WriteByte (common.SubnetID); ms.WriteByte (common.DeviceID); } Control control = new Control (); Console.WriteLine ("组播搜索网关,搜索地址是:" + CommonPage.EndPoint.ToString () + " " + CommonPage.GateWayList.Count); control.Send (new Target () { IPEndPoint = CommonPage.EndPoint, Command = Command.ReadGateway, SubnetID = 0xFF, DeviceID = 0xFF, AddData = ms.ToArray () }, SendCount.Zero, false); Console.WriteLine ("广播搜索网关,搜索地址是:" + new Net.NetWiFi ().BroadcastIpAddress.ToString () + " " + CommonPage.GateWayList.Count); control.Send (new Target () { IPEndPoint = new System.Net.IPEndPoint (new Net.NetWiFi ().BroadcastIpAddress, 6000), Command = Command.ReadGateway, SubnetID = 0xFF, DeviceID = 0xFF, AddData = ms.ToArray () }, SendCount.Zero, false); System.Threading.Thread.Sleep (800); //如果数量不相等,就重置次数 if (tempCount != CommonPage.GateWayList.Count) { readCount = 2; } } CommonPage.FindGateway = false; CommonPage.LocalPhoneFindDevice = false; #endregion } catch (Exception ex) { Console.WriteLine (ex.ToString ()); } finally { Application.RunOnMainThread (() => { if (CommonPage.GateWayList.Count == 0) { MainPage.Loading.Hide (); if (this != null && this.Parent != null) { confirmAlert.Show (); confirmAlert.ResultEventHandler += (ddf, ddd) => { MainPage.Loading.Hide (); if (ddd) { SearchGatewayList (); } }; } } else { if (this != null && this.Parent != null) { if (inThisView){// this.Parent.GetChildren (this.Parent.ChildrenCount - 1) == this) { confirmAlert1.Show (); confirmAlert1.ResultEventHandler += (ddf, ddd) => { MainPage.Loading.Hide (); if (ddd) { SearchGatewayList (); } }; } } } }); } }) { IsBackground=true}.Start(); } } }