using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
namespace Shared.Phone.UserCenter.Safety
{
///
/// 已经设置了的传感器的一览画面
///
public class SensorDeviceSettionListForm : UserCenterCommonForm
{
#region ■ 变量声明___________________________
///
/// 列表控件
///
private VerticalScrolViewLayout listView = null;
///
/// 防区ID
///
private int zoonID = 0;
#endregion
#region ■ 初始化_____________________________
///
/// 画面显示(底层会固定调用此方法,借以完成画面创建)
///
/// 防区名字
/// 防区ID
public void ShowForm(string SectorsName, int i_zoonID)
{
this.zoonID = i_zoonID;
//设置头部信息
base.SetTitleText(SectorsName + Language.StringByID(R.MyInternationalizationString.uSettion));
//右上角的“+”图标
var btnTopIcon = new TopLayoutMostRightView();
btnTopIcon.UnSelectedImagePath = "Item/Add.png";
btnTopIcon.SelectedImagePath = "Item/AddSelected.png";
topFrameLayout.AddChidren(btnTopIcon);
btnTopIcon.MouseUpEventHandler += (sender, e) =>
{
//添加新的传感器
this.AddNewSensorDevice();
};
listView = new VerticalScrolViewLayout();
listView.Height = bodyFrameLayout.Height;
bodyFrameLayout.AddChidren(listView);
//初始化传感器列表信息
this.InitSensorDevicesListInfo();
}
///
/// 初始化传感器列表信息
///
private void InitSensorDevicesListInfo()
{
this.listView.RemoveAll();
//获取指定防区全部的传感器设备的信息
var listInfo = Common.LocalSafeguard.Current.GetSensorDevicesInfoByZoonID(this.zoonID);
new System.Threading.Thread(() =>
{
foreach (var info in listInfo)
{
Application.RunOnMainThread(() =>
{
this.AddRowLayout(info);
});
}
})
{ IsBackground = true }.Start();
}
#endregion
#region ■ 添加传感器行_______________________
///
/// 添加行
///
/// 传感器信息
private void AddRowLayout(Safeguard.ZoneDeviceListData sensorInfo)
{
CommonDevice device = Common.LocalDevice.Current.GetDevice(sensorInfo.MacAddr, sensorInfo.Epoint);
if (device == null)
{
return;
}
var rowLayout = new RowLayout();
rowLayout.Height = ControlCommonResourse.ListViewRowHeight;
listView.AddChidren(rowLayout);
//图标
var btnIcon = new RowLeftIconView();
Common.LocalDevice.Current.SetDeviceIconToControl(btnIcon, device);
rowLayout.AddChidren(btnIcon);
//设备名
var btnDevice = new RowTopBlackView();
btnDevice.Text = Common.LocalDevice.Current.GetDeviceEpointName(device);
rowLayout.AddChidren(btnDevice);
//房间
var btnRoom = new RowBottomGrayView();
btnRoom.Text = Common.Room.CurrentRoom.GetRoomNameByDevice(device);
rowLayout.AddChidren(btnRoom);
//旁路状态
var txtStatu = new RowMostRightTextView();
txtStatu.TextColor = UserCenterColor.Current.Red;
rowLayout.AddChidren(txtStatu);
//旁路设置
var btnBypass = new RowBypassButton();
if (sensorInfo.IsBypass == 1)
{
//已设置旁路
txtStatu.TextID = R.MyInternationalizationString.uHadSetBypass;
//取消旁路
btnBypass.TextID = R.MyInternationalizationString.uCancelBypass;
}
rowLayout.AddLeftView(btnBypass);
btnBypass.MouseUpEventHandler += (sender, e) =>
{
//设置旁路状态
this.SetBypassStatu(sensorInfo, device);
};
//删除
var btnDelete = new RowDeleteButton();
rowLayout.AddRightView(btnDelete);
btnDelete.MouseUpEventHandler += (sender, e) =>
{
string msg = Language.StringByID(R.MyInternationalizationString.uShowDoDeleteMsg);
this.ShowConfirmMsg(msg, "DeleteRow", rowLayout, device);
};
}
#endregion
#region ■ 删除传感器行_______________________
///
/// 删除行
///
///
///
public async void DeleteRow(RowLayout rowLayout, CommonDevice device)
{
//开启进度条
this.ShowProgressBar();
bool result = await Common.LocalSafeguard.Current.DeleteSensorDevice(this.zoonID, new List() { device });
//关闭进度条
this.CloseProgressBar();
if (result == false)
{
return;
}
Application.RunOnMainThread(() =>
{
//从画面中移除
rowLayout.RemoveFromParent();
});
}
#endregion
#region ■ 旁路设置___________________________
///
/// 设置旁路状态
///
/// 传感器信息
/// 传感器设备
private async void SetBypassStatu(Safeguard.ZoneDeviceListData sensorInfo, CommonDevice device)
{
//变更状态
int statu = sensorInfo.IsBypass == 1 ? 0 : 1;
bool result = await Common.LocalSafeguard.Current.SetByPassStatuToSafety(this.zoonID, device, statu);
if (result == false)
{
return;
}
//变更状态
sensorInfo.IsBypass = statu;
this.InitSensorDevicesListInfo();
}
#endregion
#region ■ 添加新的传感器_____________________
///
/// 添加新的传感器
///
private void AddNewSensorDevice()
{
//获取能够添加的传感器
var list = this.GetCanAddSensorDevice();
//显示选择界面
var form = new SelectDeviceForm();
this.AddForm(form, list, new List(), true);
form.SetTitleText(Language.StringByID(R.MyInternationalizationString.uAddSensor));
//设备选择确定
form.ActionSelectDeviceEx += (async (listDevice) =>
{
if (listDevice.Count == 0)
{
Application.RunOnMainThread(() =>
{
//关闭界面
form.CloseForm();
});
return;
}
//开启进度条
this.ShowProgressBar();
//添加设备到安防
bool success = await Common.LocalSafeguard.Current.AddSensorDevice(this.zoonID, listDevice);
//关闭进度条
this.CloseProgressBar();
if (success == true)
{
Application.RunOnMainThread(() =>
{
//关闭界面
form.CloseForm();
});
//刷新列表
this.InitSensorDevicesListInfo();
}
});
}
///
/// 获取能够添加的传感器
///
///
private List GetCanAddSensorDevice()
{
List listNew = new List();
List listDevices = Common.LocalDevice.Current.listAllDevice;
foreach (CommonDevice device in listDevices)
{
//只要传感器
if (device.Type != DeviceType.IASZone)
{
continue;
}
//如果那个设备已经添加了,则不再显示
if (Common.LocalSafeguard.Current.IsSensorDeviceExist(device) == true)
{
continue;
}
listNew.Add(device);
}
return listNew;
}
#endregion
}
}