using System; using System.Collections.Generic; namespace Shared.SimpleControl.Phone { public class HomeListView : FrameLayout { public HomeListView () { BackgroundColor = SkinStyle.Current.MainColor; } public void ShowHomeList () { Application.RunOnMainThread (() => { MainPage.Loading.Start (Language.StringByID (R.MyInternationalizationString.load)); }); System.Threading.Tasks.Task.Run (() => { loadHomeLists (); Application.RunOnMainThread (() => { MainPage.Loading.Hide (); ShowHomeListView (); }); }); } public void ShowHomeListView () { RemoveAll (); #region 标题 var topView = new FrameLayout () { Y = Application.GetRealHeight (36), Height = Application.GetRealHeight (90), }; AddChidren (topView); var title = new Button () { TextAlignment = TextAlignment.Center, TextID = R.MyInternationalizationString.HouseList, TextSize = 19, TextColor = SkinStyle.Current.TextColor1 }; topView.AddChidren (title); var logo = new Button () { Width = Application.GetRealWidth (154), Height = Application.GetRealHeight (90), X = Application.GetRealWidth (486), UnSelectedImagePath = MainPage.LogoString, Gravity = Gravity.CenterVertical, }; topView.AddChidren (logo); var back = new Button () { Height = Application.GetRealHeight (90), Width = Application.GetRealWidth (85), UnSelectedImagePath = "Item/Back.png", Gravity = Gravity.CenterVertical, }; topView.AddChidren (back); back.MouseUpEventHandler += (sender, e) => { this.RemoveFromParent (); }; #endregion var BodyView = new FrameLayout () { Width = LayoutParams.MatchParent, Height = Application.GetRealHeight (Application.DesignHeight - 126 - 90), Y = topView.Bottom, BackgroundColor = SkinStyle.Current.ViewColor, }; AddChidren (BodyView); VerticalScrolViewLayout VerticalScrolViewMiddle = new VerticalScrolViewLayout (); BodyView.AddChidren (VerticalScrolViewMiddle); new System.Threading.Thread (() => { try { Application.RunOnMainThread (() => { MainPage.Loading.Start (Language.StringByID (R.MyInternationalizationString.load)); }); foreach (var home in UserConfig.Instance.HomeLists) { List gatewayList = null; var requestObj3 = new GatewayByRegionListObj () { RegionID = home.RegionID }; var requestJson3 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj3); var revertObj3 = MainPage.RequestHttps ("GatewayByRegionList", requestJson3, true, false); if (revertObj3.StateCode == "SUCCESS") { var responseDataObj = Newtonsoft.Json.JsonConvert.DeserializeObject> (revertObj3.ResponseData.ToString ()); gatewayList = responseDataObj; if (gatewayList != null) { Application.RunOnMainThread (() => { addRow (home, VerticalScrolViewMiddle, gatewayList.Count == 0 ? null : gatewayList [0]); }); } } } } catch { Application.RunOnMainThread (() => { new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.PasswordIsError), Language.StringByID (R.MyInternationalizationString.Close)).Show (); }); } finally { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); }); } }) { IsBackground = true}.Start(); Button btnAddArea = new Button () { Y = Application.GetRealHeight (Application.DesignHeight - 90), Height = Application.GetRealHeight (90), TextColor = SkinStyle.Current.TextColor1, BackgroundColor = SkinStyle.Current.MainColor, TextID = R.MyInternationalizationString.IncreaseTheArea, TextAlignment = TextAlignment.Center }; AddChidren (btnAddArea); btnAddArea.MouseUpEventHandler += (sender1, e1) => { AddHomeDialog (); }; } void addRow (RegionInfoRes home, VerticalScrolViewLayout VerticalScrolViewMiddle, GatewayRes gatewayObj) { string gatewayMAC = ""; if (gatewayObj != null) { gatewayMAC = gatewayObj.MAC; } var rowView = new RowLayout () { Height = Application.GetRealHeight (110), }; VerticalScrolViewMiddle.AddChidren (rowView); Button btnRowLine = new Button () { Y = Application.GetRealHeight (107), Height = Application.GetRealHeight (3), BackgroundColor = SkinStyle.Current.Black50Transparent }; rowView.AddChidren (btnRowLine); var btnPoint = new Button () { Width = Application.GetRealWidth (10), Height = Application.GetRealHeight (10), X = Application.GetRealWidth (50), Gravity = Gravity.CenterVertical, UnSelectedImagePath = "Item/Point.png", SelectedImagePath = "Item/Point.png", Visible = home.RegionID != UserConfig.Instance.CurrentRegion.RegionID }; rowView.AddChidren (btnPoint); var btnName = new Button () { Width = Application.GetRealWidth (350), Text = home.RegionName, TextAlignment = TextAlignment.CenterLeft, X = Application.GetRealWidth (90), TextColor = SkinStyle.Current.TextColor1, }; rowView.AddChidren (btnName); btnName.MouseUpEventHandler += (ss, ee) => { AddHomeDialog (home, gatewayObj); }; Button 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", }; rowView.AddChidren (rightButton1); EventHandler eHandler = (sender, e) => { if (home.RegionID == UserConfig.Instance.CurrentRegion.RegionID) { //UserConfig.Instance.GatewayMAC = gatewayMAC; goSearchView (); } else { Alert alert = new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.SwitchResidentialToGoInDeviceList), Language.StringByID (R.MyInternationalizationString.Cancel), Language.StringByID (R.MyInternationalizationString.Confrim)); alert.ResultEventHandler += ( sender2, e2) => { if (e2) { ChangeRegion (home, gatewayMAC, true); } }; alert.Show (); } }; rowView.MouseUpEventHandler += eHandler; rightButton1.MouseUpEventHandler += eHandler; if (home.RegionID == UserConfig.Instance.CurrentRegion.RegionID) { Button btnCheck = new Button () { X = Application.GetRealWidth (20), Y = Application.GetRealHeight (25), Width = Application.GetRealWidth (72), Height = Application.GetRealHeight (61), UnSelectedImagePath = "Skin/Check.png", }; rowView.AddChidren (btnCheck); } if (UserConfig.Instance.CurrentRegion.RegionID != home.RegionID) { Button btnDel = new Button () { TextID = R.MyInternationalizationString.Del, BackgroundColor = SkinStyle.Current.DelColor, }; rowView.AddRightView (btnDel); btnDel.MouseUpEventHandler += (sd, ee) => { Dialog dialog = new Dialog (); FrameLayout dialogBody = new FrameLayout () { Width = Application.GetRealWidth (500), Height = Application.GetRealHeight (400), Radius = 5, BorderColor = SkinStyle.Current.Transparent, BorderWidth = 0, BackgroundColor = SkinStyle.Current.DialogColor, Gravity = Gravity.Center, }; dialog.AddChidren (dialogBody); Button btnTitle = new Button () { Height = Application.GetRealHeight (80), BackgroundColor = SkinStyle.Current.DialogTitle, TextAlignment = TextAlignment.Center, TextID = R.MyInternationalizationString.DeleteTheArea, TextColor = SkinStyle.Current.DialogTextColor, }; dialogBody.AddChidren (btnTitle); TextView btnTip = new TextView () { X = Application.GetRealWidth (50), Y = btnTitle.Bottom, Width = Application.GetRealWidth (400), Height = Application.GetRealHeight (120), TextColor = SkinStyle.Current.TextColor, TextID = R.MyInternationalizationString.TipDelArea, Enable = false, TextSize = 14, IsMoreLines = true, }; dialogBody.AddChidren (btnTip); btnTip.IsMoreLines = true; EditText etPw = new EditText () { X = Application.GetRealWidth (50), Y = btnTip.Bottom + Application.GetRealHeight (10), Width = Application.GetRealWidth (400), Height = Application.GetRealHeight (70), TextColor = SkinStyle.Current.TextColor, Radius = 5, BorderColor = SkinStyle.Current.BorderColor, BorderWidth = 1, SecureTextEntry = true, }; dialogBody.AddChidren (etPw); FrameLayout bottomView = new FrameLayout () { Y = Application.GetRealHeight (400 - 88), Height = Application.GetRealHeight (90), BackgroundColor = SkinStyle.Current.Black50Transparent, }; dialogBody.AddChidren (bottomView); Button btnClose = new Button () { Width = Application.GetRealWidth (250), TextAlignment = TextAlignment.Center, TextColor = SkinStyle.Current.DialogTextColor, TextID = R.MyInternationalizationString.Close, BackgroundColor = SkinStyle.Current.DialogTitle, }; bottomView.AddChidren (btnClose); btnClose.MouseUpEventHandler += (asdf, eell) => { dialog.Close (); }; Button btnConfirm = new Button () { X = Application.GetRealWidth (252), Width = Application.GetRealWidth (250), TextColor = SkinStyle.Current.DialogTextColor, TextID = R.MyInternationalizationString.confirm, BackgroundColor = SkinStyle.Current.DialogTitle, TextAlignment = TextAlignment.Center, }; bottomView.AddChidren (btnConfirm); btnConfirm.MouseUpEventHandler += (dou, ni) => { string password = etPw.Text.Trim (); if (string.IsNullOrEmpty (password)) { return; } System.Threading.Tasks.Task.Run (() => { try { Application.RunOnMainThread (() => { MainPage.Loading.Start (Language.StringByID (R.MyInternationalizationString.load)); }); if (password == MainPage.LoginUser.Password) { var requestObj2 = new DeleteHomeObj () { Id = home.RegionID }; var requestJson2 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj2); var revertObj2 = MainPage.RequestHttps ("DeleteHome", requestJson2); if (revertObj2.StateCode == "SUCCESS") { Application.RunOnMainThread (() => { ShowHomeList (); dialog.Close (); }); } } else { Application.RunOnMainThread (() => { new Alert ("", Language.StringByID (R.MyInternationalizationString.PasswordIsError), Language.StringByID (R.MyInternationalizationString.Close)).Show (); }); } } catch (Exception ex) { //Console.WriteLine (ex.Message); //MainPage.FailureToServer (); } finally { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); }); } }); }; dialog.Show (); }; } } /// /// 进入后台搜索设备界面 /// void goSearchView () { SystemHomePage systemHomePage = new SystemHomePage (); //SysHomePage systemHomePage = new SysHomePage (); UserMiddle.SettingPageView.AddChidren (systemHomePage); systemHomePage.Init (); UserMiddle.SettingPageView.PageIndex = UserMiddle.SettingPageView.ChildrenCount - 1; } /// /// 更换本地区域 /// /// Home. /// Gateway mac. void ChangeRegion (RegionInfoRes home, string gatewayMAC, bool goSystem = false) { MainPage.Loading.Start (""); System.Threading.Tasks.Task.Run (() => { try { var backuplist = IO.FileUtils.ReadFiles (); int index = 0; if (string.IsNullOrEmpty (UserConfig.Instance.CurrentRegion.RegionName)) { UserConfig.Instance.CurrentRegion = UserConfig.Instance.HomeLists [0]; } string oldRegionRootPath = IO.FileUtils.CreateRegionBackup (UserConfig.Instance.CurrentRegion.RegionID.ToString ()); IO.FileUtils.DeleteRegionFiles (oldRegionRootPath); foreach (var fileName in backuplist) { if (fileName == "linphonerc" || fileName == "AccountListDB") continue; System.IO.FileInfo fileInfo = new System.IO.FileInfo (IO.FileUtils.RootPath + fileName); if (fileInfo.Exists) { fileInfo.MoveTo (oldRegionRootPath + fileName); Console.WriteLine ("move file : " + fileName); } index++; Application.RunOnMainThread (() => { int pro = (int)(index * 1.0 / backuplist.Count * 50); MainPage.Loading.Text = pro.ToString () + "%"; }); } IO.FileUtils.DeleteAllFile (); string newRegionRootPath = IO.FileUtils.CreateRegionBackup (home.RegionID.ToString ()); IO.FileUtils.RestoreRegionFiles (newRegionRootPath); UserConfig.Instance.CurrentRegion = home; //UserConfig.Instance.GatewayMAC = gatewayMAC; UserConfig.Instance.SaveUserConfig (); Room.InitAllRoom (); Application.RunOnMainThread (() => { UserConfig.Instance.RefreshUserConfig (); new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.SwitchRegionSuccessfully), Language.StringByID (R.MyInternationalizationString.Close)).Show (); //this.RemoveFromParent (); ShowHomeList (); EquipmentPublicClass.CheckLinkRemote (2); }); MainPage.LoginUser.SaveUserInfo (); } catch (Exception ex) { Application.RunOnMainThread (() => { new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.CheckInternet), Language.StringByID (R.MyInternationalizationString.Close)).Show (); }); Console.WriteLine (ex.Message); } finally { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); if (goSystem) goSearchView (); }); } }); } void AddHomeDialog (RegionInfoRes home = null, GatewayRes gatewayObj = null) { string gatewayMAC = ""; if (gatewayObj != null) { gatewayMAC = gatewayObj.MAC; } // 是否是修改已经存在的住宅 bool isModify = home == null ? false : true; if (!isModify) { home = new RegionInfoRes (); } new System.Threading.Thread (() => { Application.RunOnMainThread (() => { MainPage.Loading.Start (Language.StringByID (R.MyInternationalizationString.load)); }); Application.RunOnMainThread (() => { MainPage.Loading.Hide (); Dialog dialog = new Dialog (); FrameLayout dialogBodyView = new FrameLayout () { Gravity = Gravity.Center, Width = Application.GetRealWidth (500), Height = Application.GetRealHeight (600), Radius = 5, BorderColor = SkinStyle.Current.Transparent, BorderWidth = 0, BackgroundColor = SkinStyle.Current.DialogColor, }; dialog.AddChidren (dialogBodyView); Button btnTitle = new Button () { Height = Application.GetRealHeight (80), TextAlignment = TextAlignment.Center, TextID = R.MyInternationalizationString.HonseManagement, TextColor = SkinStyle.Current.DialogTextColor, BackgroundColor = SkinStyle.Current.DialogTitle, }; dialogBodyView.AddChidren (btnTitle); Button 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, TextID = R.MyInternationalizationString.ResidentialName }; dialogBodyView.AddChidren (btnZoneName); EditText etZoneName = 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 = home.RegionName, }; dialogBodyView.AddChidren (etZoneName); if (MainPage.LoginUser.AccountType == 1) { etZoneName.Enable = false; etZoneName.BorderWidth = 0; } #region ---绑定mac--- Button btnZoneMAC = new Button () { X = Application.GetRealWidth (50), Y = etZoneName.Bottom + Application.GetRealHeight (10), Width = Application.GetRealWidth (310), Height = Application.GetRealHeight (80), TextAlignment = TextAlignment.CenterLeft, TextColor = SkinStyle.Current.TextColor, TextID = R.MyInternationalizationString.GatewayMAC }; dialogBodyView.AddChidren (btnZoneMAC); Button btnScan = new Button () { X = btnZoneMAC.Right, Y = etZoneName.Bottom + Application.GetRealHeight (5), Width = Application.GetRealWidth (90), Height = Application.GetRealHeight (90), UnSelectedImagePath = "Item/Scan.png" }; dialogBodyView.AddChidren (btnScan); EditText etZoneMAC = new EditText () { X = Application.GetRealWidth (50), Y = btnZoneMAC.Bottom + Application.GetRealHeight (10), Width = Application.GetRealWidth (400), Height = Application.GetRealHeight (80), TextAlignment = TextAlignment.Center, TextColor = SkinStyle.Current.TextColor, Radius = 5, BorderColor = SkinStyle.Current.BorderColor, BorderWidth = 2, Text = gatewayMAC, }; dialogBodyView.AddChidren (etZoneMAC); btnScan.MouseUpEventHandler += (sdf, fds) => { com.hdl.on.Scan.OpenScan ((scanMAC) => { Application.RunOnMainThread (() => { ulong scan16bit1 = 0; var tryresult = ulong.TryParse (scanMAC, out scan16bit1); if (tryresult) { var scan16bit = scan16bit1.ToString ("X"); var count = 16 - scan16bit.Length; for (int i = 0; i < count; i++) { scan16bit = "0" + scan16bit; } etZoneMAC.Text = scan16bit; } else { new Tip () { MaxWidth = 150, Text = Language.StringByID (R.MyInternationalizationString.ScanFail), Direction = AMPopTipDirection.Up, CloseTime = 2 }.Show (etZoneMAC); } }); }); }; #endregion FrameLayout BottomView = new FrameLayout () { Y = Application.GetRealHeight (600 - 88), Height = Application.GetRealHeight (90), BackgroundColor = SkinStyle.Current.Black50Transparent, }; dialogBodyView.AddChidren (BottomView); Button btnClose = new Button () { Width = Application.GetRealWidth (250), TextID = R.MyInternationalizationString.Close, TextColor = SkinStyle.Current.DialogTextColor, BackgroundColor = SkinStyle.Current.DialogTitle, }; BottomView.AddChidren (btnClose); btnClose.MouseUpEventHandler += (sdf, fds) => { dialog.Close (); }; Button btnOption = new Button () { X = btnClose.Right + Application.GetRealWidth (2), Width = Application.GetRealWidth (250), TextID = home.RegionID != UserConfig.Instance.CurrentRegion.RegionID ? R.MyInternationalizationString.Change : R.MyInternationalizationString.SAVE, TextColor = SkinStyle.Current.DialogTextColor, BackgroundColor = SkinStyle.Current.DialogTitle, }; BottomView.AddChidren (btnOption); if (!isModify) { btnOption.TextID = R.MyInternationalizationString.SAVE; } btnOption.MouseUpEventHandler += (sdf, fds) => { if (btnOption.TextID == R.MyInternationalizationString.Change) { ChangeRegion (home, etZoneMAC.Text.Trim ()); } else { var macString = etZoneMAC.Text.Trim ().Replace (".", ""); if (!isModify) { if (etZoneName.Text.Trim () == "") { new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.PleaseEnterZoneName), Language.StringByID (R.MyInternationalizationString.Close)).Show (); return; } if (macString == "")// { try { Convert.ToUInt64 (macString, 16); } catch { new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.TipEntryMACLenght), Language.StringByID (R.MyInternationalizationString.Close)).Show (); return; } if (macString.Length != 16) { new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.TipEntryMACLenght), Language.StringByID (R.MyInternationalizationString.Close)).Show (); return; } } home.RegionName = etZoneName.Text.Trim (); AddHome (home.RegionName, macString); } else { //修改住宅名称 if (home.RegionName != etZoneName.Text.Trim ()) { home.RegionName = etZoneName.Text.Trim (); ChangeHomeName (home.RegionID, home.RegionName); } if (macString.Length != 16) { new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.TipEntryMACLenght), Language.StringByID (R.MyInternationalizationString.Close)).Show (); return; } else { //修改住宅绑定的mac if (macString != gatewayMAC) { ChangeHomeMACByHomeId (home.RegionID, macString); } } } } dialog.Close (); }; etZoneName.TextChangeEventHandler += (adsf, lkadf) => { if (etZoneName.Text.Trim () != home.RegionName) { btnOption.TextID = R.MyInternationalizationString.SAVE; } else { btnOption.TextID = R.MyInternationalizationString.Change; } }; dialog.Show (); }); }) { IsBackground = true}.Start(); } /// /// 添加住宅,需要传入住宅名称跟住宅绑定的网关mac /// /// Home name. /// Mac string. void AddHome (string homeName, string macString) { var requestObj21 = new AddHomeAndGatewayByONObj () { Region = homeName, MAC = macString }; var requestJson21 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj21); var revertObj21 = MainPage.RequestHttps ("AddHomeAndGatewayByON", requestJson21); if (revertObj21.StateCode == "SUCCESS") { new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.addsuccess), Language.StringByID (R.MyInternationalizationString.Close)).Show (); ShowHomeList (); } } void loadHomeLists () { var requestJson2 = Newtonsoft.Json.JsonConvert.SerializeObject (new HomeListObj ()); var revertObj2 = MainPage.RequestHttps ("HomeList", requestJson2); if (revertObj2.StateCode == "SUCCESS") { var responseDataObj = Newtonsoft.Json.JsonConvert.DeserializeObject> (revertObj2.ResponseData.ToString ()); UserConfig.Instance.HomeLists = responseDataObj; UserConfig.Instance.SaveUserConfig (); } } /// /// 修改住宅名称 /// void ChangeHomeName (int homeID, string homeName) { if (UserConfig.Instance.CurrentRegion.RegionName == homeName) return; var requestObj2 = new EditHomeObj () { Id = homeID, Name = homeName }; var requestJson2 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj2); var revertObj2 = MainPage.RequestHttps ("EditHome", requestJson2); if (revertObj2.StateCode == "SUCCESS") { new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.AmendTheSuccess), Language.StringByID (R.MyInternationalizationString.Close)).Show (); if (homeID == UserConfig.Instance.CurrentRegion.RegionID) { IO.FileUtils.ReNameFoler (UserConfig.Instance.CurrentRegion.RegionName, homeName); UserConfig.Instance.CurrentRegion.RegionName = homeName; } UserConfig.Instance.SaveUserConfig (); this.ShowHomeList (); } } void ChangeHomeMACByHomeId (int homeId, string macString) { var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (new EditMACByHomeId { RegionID = homeId, MAC = macString }); var respone = MainPage.RequestHttps ("EditMACByHomeId", requestJson); if (respone.StateCode == "SUCCESS") { new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.AmendTheSuccess), Language.StringByID (R.MyInternationalizationString.Close)).Show (); UserConfig.Instance.CurrentRegion.MAC = macString; UserConfig.Instance.SaveUserConfig (); this.ShowHomeList (); } } } }