using System;
|
using System.Collections.Generic;
|
|
namespace Shared.SimpleControl.Phone
|
{
|
public class AlexaDeviceListPage : BasePage
|
{
|
///// <summary>
|
///// 暂时测试后面去掉
|
///// </summary>
|
//string AlexaTestURL = "https://developer.hdlcontrol.com";
|
|
/// <summary>
|
/// 当前音箱、用户、住宅等数据
|
/// </summary>
|
SpeakerInfo mAlexa = new SpeakerInfo ();
|
/// <summary>
|
/// 设备、场景列表
|
/// </summary>
|
List<TargetInfo> targetInfoList = new List<TargetInfo> ();
|
|
/////// <summary>
|
/////// 语音房间数据
|
/////// </summary>
|
////List<AlexaRoomsItem> alexaRoomsList = new List<AlexaRoomsItem> ();
|
///// <summary>
|
///// 设备列表
|
///// </summary>
|
//List<AlexaDeviceListItem> alexaDeviceList = new List<AlexaDeviceListItem> ();
|
///// <summary>
|
///// 场景列表
|
///// </summary>
|
//List<SceneListItem> alexaSceneList = new List<SceneListItem> ();
|
//AlexaRoomsItem mAlexaRoomsItem = new AlexaRoomsItem ();
|
|
VerticalScrolViewLayout bodyView;
|
|
bool IsDeviceList = true;
|
|
public AlexaDeviceListPage (SpeakerInfo speakerInfo)
|
{
|
BackgroundColor = SkinStyle.Current.MainColor;
|
mAlexa = speakerInfo;
|
}
|
|
/// <summary>
|
/// 显示页面
|
/// </summary>
|
public override void ShowPage ()
|
{
|
#region ---TopView---
|
base.ShowPage ();
|
this.topTitleBtn.Text = "Alexa";
|
//添加按钮点击事件
|
EventHandler<MouseEventArgs> addSubaccountHandler = (sender, e) => {
|
OpenAlexaSettingPage ();
|
};
|
this.topItemButton.MouseUpEventHandler += addSubaccountHandler;
|
#endregion
|
|
#region 设备-场景
|
var topView = new FrameLayout () {
|
Height = Application.GetRealHeight (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 接口请求方法
|
/// <summary>
|
/// 请求获取设备和场景列表
|
/// </summary>
|
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<List<TargetInfo>> (revertObj.Data.ToString ());
|
Application.RunOnMainThread (() => {
|
if (list != null && list.Count > 0) {
|
targetInfoList = list;
|
} else {
|
targetInfoList = new List<TargetInfo> ();
|
}
|
RefreshListView ();
|
});
|
} else {
|
//提示错误
|
IMessageCommon.Current.ShowErrorInfoAlter (revertObj.Code);
|
}
|
} catch {
|
|
} finally {
|
Application.RunOnMainThread (() => {
|
MainPage.Loading.Hide ();
|
});
|
}
|
});
|
}
|
|
/// <summary>
|
/// 添加或者更新设备和场景设备列表
|
/// </summary>
|
bool UpdateSpeakerDeviceList (List<TargetInfo> 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
|
/// <summary>
|
/// 刷新列表View
|
/// </summary>
|
void RefreshListView ()
|
{
|
//当前选择的是设备
|
if (IsDeviceList) {
|
InitDevcieRow (bodyView);
|
} else {
|
InitSceneRow (bodyView);
|
}
|
}
|
|
/// <summary>
|
/// 添加设备RowView
|
/// </summary>
|
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<TargetInfo> (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 ();
|
});
|
}
|
});
|
|
}
|
};
|
};
|
}
|
}
|
|
|
/// <summary>
|
/// 添加场景界面
|
/// </summary>
|
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<TargetInfo> (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 添加设备和场景处理方法
|
|
///// <summary>
|
///// 生成设备目标数据
|
///// </summary>
|
///// <param name="device"></param>
|
///// <param name="path"></param>
|
///// <returns></returns>
|
//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;
|
//}
|
|
///// <summary>
|
///// 添加或者编辑 设备和场景列表
|
///// 同时刷新列表
|
///// </summary>
|
//void AddAlexaDeviceAndSceneWithRefresh (List<AlexaDeviceListItem> mDeviceList, List<SceneListItem> mSceneList)
|
//{
|
// if (AddAlexaDeviceAndSceneWithOneRoom (mDeviceList, mSceneList)) {
|
// //编辑成功,重新刷新获取
|
// GetDeviceAndSceneList ();
|
// }
|
//}
|
|
///// <summary>
|
///// 添加或者编辑 设备和场景列表
|
///// </summary>
|
///// <param name="mDeviceList"></param>
|
///// <param name="mSceneList"></param>
|
///// <returns></returns>
|
//bool AddAlexaDeviceAndSceneWithOneRoom (List<TargetInfo> mList)
|
//{
|
// //提交更新设备数据
|
// var mAlexaRooms = new AlexaRoomsItem () {
|
// RoomName = UserConfig.Instance.CurrentRegion.homeName,
|
// LayerName = UserConfig.Instance.CurrentRegion.homeName,
|
// DeviceList = mDeviceList,
|
// SceneList = mSceneList
|
// };
|
// var AlexaRoomsList = new List<AlexaRoomsItem> ();
|
// AlexaRoomsList.Add (mAlexaRooms);
|
// return AddDevicesScene (AlexaRoomsList);
|
//}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="lsit"></param>
|
void AddDevicesSceneList (List<TargetInfo> 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 ();
|
});
|
}
|
});
|
}
|
|
/// <summary>
|
/// 打开场景或者设备添加页面
|
/// </summary>
|
void OpenAlexaSettingPage ()
|
{
|
Action<List<TargetInfo>> 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
|
|
}
|
}
|