using System; using System.Collections.Generic; namespace Shared.SimpleControl.Phone { public class AlexaDeviceListPage : BasePage { ///// ///// 暂时测试后面去掉 ///// //string AlexaTestURL = "https://developer.hdlcontrol.com"; /// /// 当前音箱、用户、住宅等数据 /// SpeakerInfo mAlexa = new SpeakerInfo (); /// /// 设备、场景列表 /// List targetInfoList = new List (); /////// /////// 语音房间数据 /////// ////List alexaRoomsList = new List (); ///// ///// 设备列表 ///// //List alexaDeviceList = new List (); ///// ///// 场景列表 ///// //List alexaSceneList = new List (); //AlexaRoomsItem mAlexaRoomsItem = new AlexaRoomsItem (); VerticalScrolViewLayout bodyView; bool IsDeviceList = true; public AlexaDeviceListPage (SpeakerInfo speakerInfo) { BackgroundColor = SkinStyle.Current.MainColor; mAlexa = speakerInfo; } /// /// 显示页面 /// public override void ShowPage () { #region ---TopView--- base.ShowPage (); //this.topTitleBtn.Text = "Alexa"; //设置标题为音箱备注 if (mAlexa != null) { if (string.IsNullOrEmpty (mAlexa.remark)) { this.topTitleBtn.Text = mAlexa.platformName; } else { this.topTitleBtn.Text = mAlexa.remark; } } //添加按钮点击事件 EventHandler addSubaccountHandler = (sender, e) => { OpenAlexaSettingPage (); }; this.topItemButton.MouseUpEventHandler += addSubaccountHandler; #endregion #region 设备-场景 var topView = new FrameLayout () { Height = Application.GetRealWidth (110), BackgroundColor = SkinStyle.Current.TitileView, }; BaseContentView.AddChidren (topView); var btnDevice = new Button () { Width = Application.GetRealWidth (320), TextSize = 16, TextID = R.MyInternationalizationString.Device, TextColor = SkinStyle.Current.SelectedColor }; topView.AddChidren (btnDevice); var btnScene = new Button () { Width = Application.GetRealWidth (320), X = btnDevice.Right, TextSize = 16, TextID = R.MyInternationalizationString.Scenes, TextColor = SkinStyle.Current.TextColor1 }; var btnLine = new Button () { Width = 1, BackgroundColor = SkinStyle.Current.White20Transparent, X = btnDevice.Right, }; topView.AddChidren (btnLine); btnDevice.MouseUpEventHandler += (sender, e) => { btnDevice.TextColor = SkinStyle.Current.SelectedColor; btnScene.TextColor = SkinStyle.Current.TextColor1; if (!IsDeviceList) { IsDeviceList = true; InitDevcieRow (bodyView); } }; topView.AddChidren (btnScene); btnScene.MouseUpEventHandler += (sender, e) => { btnDevice.TextColor = SkinStyle.Current.TextColor1; btnScene.TextColor = SkinStyle.Current.SelectedColor; if (IsDeviceList) { IsDeviceList = false; InitSceneRow (bodyView); } }; #endregion bodyView = new VerticalScrolViewLayout () { Y = topView.Bottom, Height = BaseContentView.Height - topView.Bottom, BackgroundColor = SkinStyle.Current.ViewColor, }; BaseContentView.AddChidren (bodyView); ///请求获取设备和场景列表 GetDeviceAndSceneList (); } #region 接口请求方法 /// /// 请求获取设备和场景列表 /// void GetDeviceAndSceneList () { System.Threading.Tasks.Task.Run (() => { try { Application.RunOnMainThread (() => { MainPage.Loading.Start (Language.StringByID (R.MyInternationalizationString.load)); }); var revertObj = HttpServerRequest.Current.GetSpeakerDeviceList (0, mAlexa.tokenId); if (revertObj.Code == StateCode.SUCCESS) { var list = Newtonsoft.Json.JsonConvert.DeserializeObject> (revertObj.Data.ToString ()); Application.RunOnMainThread (() => { if (list != null && list.Count > 0) { targetInfoList = list; } else { targetInfoList = new List (); } RefreshListView (); }); } else { //提示错误 IMessageCommon.Current.ShowErrorInfoAlter (revertObj.Code); } } catch { } finally { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); }); } }); } /// /// 添加或者更新设备和场景设备列表 /// bool UpdateSpeakerDeviceList (List mList) { var updateSpeakerDeviceListObj = new UpdateSpeakerDeviceListObj () { homeId = mAlexa.homeId, tokenId = mAlexa.tokenId, targetInfos = mList }; var revertObj = HttpServerRequest.Current.UpdateSpeakerDeviceList (updateSpeakerDeviceListObj); if (revertObj.Code == StateCode.SUCCESS) { return true; } else { //登录失败 IMessageCommon.Current.ShowErrorInfoAlter (revertObj.Code); } return false; } #endregion #region 加载View /// /// 刷新列表View /// void RefreshListView () { //当前选择的是设备 if (IsDeviceList) { InitDevcieRow (bodyView); } else { InitSceneRow (bodyView); } } /// /// 添加设备RowView /// public void InitDevcieRow (VerticalScrolViewLayout bodyView) { bodyView.RemoveAll (); if (targetInfoList == null) return; foreach (var device in targetInfoList) { //过滤场景数据 if (device.isDevice == false) continue; var rowView = new RowLayout () { Height = Application.GetRealHeight (110), }; bodyView.AddChidren (rowView); var btn = new Button () { X = Application.GetRealWidth (60), Width = Application.GetRealWidth (640 - 60), Text = device.targetName, TextAlignment = TextAlignment.CenterLeft, TextColor = SkinStyle.Current.TextColor1, }; rowView.AddChidren (btn); var btnDel = new Button () { TextID = R.MyInternationalizationString.Delete, BackgroundColor = SkinStyle.Current.DelColor, }; rowView.AddRightView (btnDel); btnDel.MouseUpEventHandler += (sender, e) => { Alert alert = new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.TipDeleteEquipmentMessage), Language.StringByID (R.MyInternationalizationString.Cancel), Language.StringByID (R.MyInternationalizationString.Confrim)); alert.Show (); alert.ResultEventHandler += (s, dd) => { if (dd) { System.Threading.Tasks.Task.Run (() => { Application.RunOnMainThread (() => { MainPage.Loading.Start (Language.StringByID (R.MyInternationalizationString.load)); }); try { var newList = new List (targetInfoList); newList.Remove (device); bool result = UpdateSpeakerDeviceList (newList); Application.RunOnMainThread (() => { if (result) { rowView.RemoveFromParent (); targetInfoList.Remove (device); } else { new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.DeleteFailed), Language.StringByID (R.MyInternationalizationString.Close)).Show (); } }); } catch { } finally { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); }); } }); } }; }; } } /// /// 添加场景界面 /// public void InitSceneRow (VerticalScrolViewLayout bodyView) { bodyView.RemoveAll (); if (targetInfoList == null) return; foreach (var scene in targetInfoList) { if (scene.isDevice) continue; var rowView = new RowLayout () { Height = Application.GetRealHeight (110), }; bodyView.AddChidren (rowView); var btn = new Button () { X = Application.GetRealWidth (60), Width = Application.GetRealWidth (640 - 60), Text = scene.targetName, TextAlignment = TextAlignment.CenterLeft, TextColor = SkinStyle.Current.TextColor1, }; rowView.AddChidren (btn); btn.MouseUpEventHandler += (dd, ff) => { //var alexaSceneSettingPage = new AlecaSceneEditPage (); //UserMiddle.SettingPageView.AddChidren (alexaSceneSettingPage); //alexaSceneSettingPage.ShowScene (scene); //UserMiddle.SettingPageView.PageIndex = 2; }; var btnDel = new Button () { //Text = "Delete", TextID = R.MyInternationalizationString.Delete, BackgroundColor = SkinStyle.Current.DelColor, }; rowView.AddRightView (btnDel); btnDel.MouseUpEventHandler += (sender, e) => { Alert alert = new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.SureDelete), Language.StringByID (R.MyInternationalizationString.Cancel), Language.StringByID (R.MyInternationalizationString.Confrim)); alert.Show (); alert.ResultEventHandler += (s, dd) => { if (dd) { System.Threading.Tasks.Task.Run (() => { Application.RunOnMainThread (() => { MainPage.Loading.Start (Language.StringByID (R.MyInternationalizationString.load)); }); try { var newList = new List (targetInfoList); newList.Remove (scene); bool result = UpdateSpeakerDeviceList (newList); //var result = true; Application.RunOnMainThread (() => { if (result) { rowView.RemoveFromParent (); targetInfoList.Remove (scene); } else { new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.DeleteFailed), Language.StringByID (R.MyInternationalizationString.Close)).Show (); } }); } catch { } finally { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); }); } }); } }; }; } } #endregion #region 添加设备和场景处理方法 ///// ///// 生成设备目标数据 ///// ///// ///// ///// //AlexaDeviceListItem GetAlexaDeviceListItem (Common device, string path) //{ // var mAlexaDeviceTyp = HDLCommon.Current.GetAlexaDeviceType (device.Type); // var deviceControl = new Obj_Buspro_DeviceControl () { // SubnetID = device.SubnetID, // DeviceID = device.DeviceID, // LoopID = device.LoopID, // GatewayID = UserConfig.Instance.MacMark, // DevicePath = device.SavePath, // }; // var mAlexaDeviceItem = new AlexaDeviceListItem () { // DeviceName = device.Name, // NicksName = device.Name, // DeviceType = (int)mAlexaDeviceTyp, // ControlObj = deviceControl, // }; // return mAlexaDeviceItem; //} ///// ///// 添加或者编辑 设备和场景列表 ///// 同时刷新列表 ///// //void AddAlexaDeviceAndSceneWithRefresh (List mDeviceList, List mSceneList) //{ // if (AddAlexaDeviceAndSceneWithOneRoom (mDeviceList, mSceneList)) { // //编辑成功,重新刷新获取 // GetDeviceAndSceneList (); // } //} ///// ///// 添加或者编辑 设备和场景列表 ///// ///// ///// ///// //bool AddAlexaDeviceAndSceneWithOneRoom (List mList) //{ // //提交更新设备数据 // var mAlexaRooms = new AlexaRoomsItem () { // RoomName = UserConfig.Instance.CurrentRegion.homeName, // LayerName = UserConfig.Instance.CurrentRegion.homeName, // DeviceList = mDeviceList, // SceneList = mSceneList // }; // var AlexaRoomsList = new List (); // AlexaRoomsList.Add (mAlexaRooms); // return AddDevicesScene (AlexaRoomsList); //} /// /// /// /// void AddDevicesSceneList (List lsit) { System.Threading.Tasks.Task.Run (() => { Application.RunOnMainThread (() => { MainPage.Loading.Start (Language.StringByID (R.MyInternationalizationString.load)); }); try { bool result = UpdateSpeakerDeviceList (lsit); //var result = true; Application.RunOnMainThread (() => { if (result) { GetDeviceAndSceneList (); } }); } catch { } finally { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); }); } }); } /// /// 打开场景或者设备添加页面 /// void OpenAlexaSettingPage () { Action> saveAction = (lsit) => { AddDevicesSceneList (lsit); }; System.Threading.Tasks.Task.Factory.StartNew (() => { try { Application.RunOnMainThread (() => { MainPage.Loading.Start (Language.StringByID (R.MyInternationalizationString.load)); }); var tempRoom = new Room (); //if (IsDeviceList) { // foreach (var device in alexaDeviceList) { // tempRoom.DeviceFilePathList.Add (device.ControlObj.DevicePath); // } //} else { // foreach (var scene in alexaSceneList) { // tempRoom.SceneFilePathList.Add (scene.SceneName); // } //} Application.RunOnMainThread (() => { if (IsDeviceList) { var userDeviceListPage = new AlexaSettingDevicePage (targetInfoList); userDeviceListPage.Show (); userDeviceListPage.ShowPage (saveAction); } else { var mAlexaSettingScenePage = new AlexaSettingScenePage (targetInfoList); mAlexaSettingScenePage.Show (); mAlexaSettingScenePage.ShowPage (saveAction); } }); } catch (Exception ex) { Shared.Utlis.WriteLine (ex.Message); } finally { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); }); } }); } #endregion } }