using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
namespace Shared.Phone.UserCenter.DevicePir
{
///
/// PIR传感器绑定界面★
///
public class PirSensorBindTargetForm : UserCenterCommonForm
{
///
/// 列表控件
///
private VerticalScrolViewLayout listView = null;
///
/// 编辑按钮
///
private BottomClickButton btnEditor = null;
///
/// 传感器设备
///
private IASZone deviceIASZone = null;
///
/// 已经存在的绑定设备
///
private Dictionary dicEsixtDevice = new Dictionary();
///
/// 画面显示(底层会固定调用此方法,借以完成画面创建)
///
/// 传感器设备
public void ShowForm(IASZone i_iasZone)
{
deviceIASZone = i_iasZone;
//设置头部信息
base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uBindTargetsSettion));
//右上添加按钮
var btnAddDeviceIcon = new TopLayoutMostRightView();
btnAddDeviceIcon.UnSelectedImagePath = "Item/Add.png";
btnAddDeviceIcon.SelectedImagePath = "Item/AddSelected.png";
topFrameLayout.AddChidren(btnAddDeviceIcon);
btnAddDeviceIcon.MouseUpEventHandler += (sender, e) =>
{
//显示设备选择的界面
this.ShowDeviceSelectForm();
};
//初始化中部信息
this.InitMiddleFrame();
}
///
/// 初始化中部信息
///
private void InitMiddleFrame()
{
this.listView = new VerticalScrolViewLayout();
this.listView.Height = bodyFrameLayout.Height;
bodyFrameLayout.AddChidren(this.listView);
this.dicEsixtDevice.Clear();
//开启进度条
this.ShowProgressBar();
new System.Threading.Thread(async () =>
{
List listDevice = await Common.LocalDevice.Current.GetBindTargetDevice(this.deviceIASZone);
if (listDevice == null)
{
//关闭进度条
this.CloseProgressBar(ShowReLoadMode.YES);
return;
}
foreach(CommonDevice device in listDevice)
{
Application.RunOnMainThread(() =>
{
this.AddRowlayout(device);
});
}
//关闭进度条
this.CloseProgressBar();
})
{ IsBackground = true }.Start();
}
///
/// 添加行
///
///
private void AddRowlayout(CommonDevice device)
{
//初始化编辑按钮
this.InitEditorButton();
string mainKeys = Common.LocalDevice.Current.GetDeviceMainKeys(device);
this.dicEsixtDevice[mainKeys] = device;
var row = new DeviceRoomViewRow(this.listView, device);
row.ChangedChidrenBindMode(row.btnIcon, ChidrenBindMode.BindEventOnly);
row.ChangedChidrenBindMode(row.btnRoom, ChidrenBindMode.BindEventOnly);
row.ChangedChidrenBindMode(row.btnDevie, ChidrenBindMode.BindEventOnly);
//删除
var btnDelete = new RowDeleteButton();
row.AddRightView(btnDelete);
btnDelete.MouseUpEventHandler += (sender, e) =>
{
//确认要删除吗?
string msg = Language.StringByID(R.MyInternationalizationString.uShowDoDeleteMsg);
this.ShowConfirmMsg(msg, "DeleteTargetDevice", device, row);
};
}
///
/// 删除绑定目标
///
///
///
public async void DeleteTargetDevice(CommonDevice device, DeviceRoomViewRow row)
{
//开启进度条
this.ShowProgressBar();
bool result = await Common.LocalDevice.Current.DeleteDeviceTarget(this.deviceIASZone, device);
//关闭进度条
this.CloseProgressBar();
if (result == false)
{
return;
}
Application.RunOnMainThread(() =>
{
string mainKeys = Common.LocalDevice.Current.GetDeviceMainKeys(device);
this.dicEsixtDevice.Remove(mainKeys);
row.RemoveFromParent();
if (listView.ChildrenCount == 0)
{
this.btnEditor.RemoveFromParent();
this.btnEditor = null;
}
});
}
///
/// 显示设备选择的界面
///
private void ShowDeviceSelectForm()
{
var listShowDevice = new List();
var listTempDevice = Common.LocalDevice.Current.listAllDevice;
foreach (var device in listTempDevice)
{
//如果是传感器,或者是没有开关簇的话(这里判断的是输入簇)
if ((device is IASZone) || Common.LocalDevice.Current.InDeviceIsCanOnOff(device) == false)
{
continue;
}
string mainKeys = Common.LocalDevice.Current.GetDeviceMainKeys(device);
if (this.dicEsixtDevice.ContainsKey(mainKeys) == true)
{
//如果已经添加了,则不再显示
continue;
}
listShowDevice.Add(device);
}
var listSelect = new List();
foreach (string mainkeys in this.dicEsixtDevice.Keys)
{
listSelect.Add(mainkeys);
}
var form = new SelectDeviceForm();
this.AddForm(form, listShowDevice, listSelect, false);
//添加绑定目标
form.SetTitleText(Language.StringByID(R.MyInternationalizationString.AddBindTargets));
//设备选择确定
form.ActionSelectDevice += (async (listDevice) =>
{
if (listDevice.Count == 0)
{
return;
}
//开启进度条
this.ShowProgressBar();
var listNewDevice = await Common.LocalDevice.Current.BindDeviceTarget(this.deviceIASZone, listDevice);
//关闭进度条
this.CloseProgressBar();
if (listNewDevice == null || listNewDevice.Count == 0)
{
return;
}
foreach (CommonDevice device in listNewDevice)
{
string mainKeys = Common.LocalDevice.Current.GetDeviceMainKeys(device);
this.dicEsixtDevice[mainKeys] = device;
Application.RunOnMainThread(() =>
{
this.AddRowlayout(device);
});
}
Application.RunOnMainThread(() =>
{
//打开PIR传感器的灯光特效配置界面
var newform = new PirSensorLightSettionForm();
this.AddForm(newform, this.deviceIASZone);
});
});
}
///
/// 初始化编辑按钮
///
private void InitEditorButton()
{
if (this.btnEditor != null)
{
return;
}
Application.RunOnMainThread(() =>
{
//编辑
this.btnEditor = new BottomClickButton();
this.btnEditor.MouseUpTime = 0;
this.btnEditor.TextID = R.MyInternationalizationString.uEditor;
bodyFrameLayout.AddChidren(this.btnEditor);
this.btnEditor.MouseUpEvent += (sender, e) =>
{
var form = new PirSensorLightSettionForm();
this.AddForm(form, deviceIASZone);
};
this.listView.Height = this.btnEditor.Y - ControlCommonResourse.BottomButtonAndListViewSpace;
});
}
}
}