using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
namespace Shared.Phone.UserCenter.Safety
{
///
/// 添加报警目标指定设备类型的设备列表
///
public class AddDeviceAlarmTargetListForm : UserCenterCommonForm
{
///
/// 列表控件
///
private VerticalScrolViewLayout listView = null;
///
/// 防区ID(这个东西似乎是唯一的)
///
private int zoonID = 0;
///
/// 设备列表
///
private List listAllDevice = null;
///
/// 现存的报警目标
///
private Dictionary dicAlarmDevice = null;
///
/// 需要添加的报警设备
///
private Dictionary dicSaveDevice = new Dictionary();
///
/// 画面显示(底层会固定调用此方法,借以完成画面创建)
///
/// 防区ID
/// 设备类型的名称
/// 设备信息
public void ShowForm(int i_zoonID, string i_deviceText, List i_listDevice)
{
this.zoonID = i_zoonID;
this.listAllDevice = i_listDevice;
//设置头部信息
base.SetTitleText(i_deviceText);
//完成
var btnfinish = new TopLayoutFinshView();
topFrameLayout.AddChidren(btnfinish);
btnfinish.MouseUpEventHandler += (sender, e) =>
{
//保存选择的设备
this.DoSaveSelectDeviceAsync();
};
//初始化中部信息
this.InitMiddleFrame(i_deviceText);
}
///
/// 初始化中部信息
///
/// 列表头部名称
private void InitMiddleFrame(string titleText)
{
//XXX列表
var btnTitle = new TitleViewControl();
btnTitle.TextColor = UserCenterColor.Current.TextGrayColor;
btnTitle.Y = Application.GetRealHeight(60);
btnTitle.Text = titleText + Language.StringByID(R.MyInternationalizationString.uList);
bodyFrameLayout.AddChidren(btnTitle);
this.listView = new VerticalScrolViewLayout();
this.listView.Y = btnTitle.Bottom;
this.listView.Height = bodyFrameLayout.Height - btnTitle.Bottom;
bodyFrameLayout.AddChidren(this.listView);
//设置中部信息
this.SetMiddleFrameInfo();
}
///
/// 设置中部信息
///
private void SetMiddleFrameInfo()
{
new System.Threading.Thread(() =>
{
//获取现存的报警设备
var listData = Common.LocalSafeguard.Current.GetLocalAlarmTargetInfoByZoneId(this.zoonID);
dicAlarmDevice = new Dictionary();
foreach (var data in listData)
{
if (data.Type == 1)
{
//不需要场景
continue;
}
string mainkeys = Common.LocalDevice.Current.GetDeviceMainKeys(data.DeviceAddr, data.Epoint);
dicAlarmDevice[mainkeys] = data;
}
foreach (var deviceInfo in listAllDevice)
{
//如果那个设备已经添加了,则不再显示
if (Common.LocalSafeguard.Current.IsAlarmDeviceExist(this.zoonID, deviceInfo.Device) == true)
{
continue;
}
Application.RunOnMainThread(() =>
{
//添加行目标
this.AddRowLayout(deviceInfo);
});
}
})
{ IsBackground = true }.Start();
}
///
/// 添加行目标
///
///
private void AddRowLayout(uDeviceInfo deviceInfo)
{
var row = new DeviceRoomViewRow(this.listView, deviceInfo.Device, deviceInfo.listRoomName);
//添加向右的图标
row.AddRightIconControl();
//状态显示
var btnStatu = new RowSecondRightTextView();
row.AddChidren(btnStatu);
string mainKeys = Common.LocalDevice.Current.GetDeviceMainKeys(deviceInfo.Device);
List listTaskinfo = null;
if (this.dicAlarmDevice.ContainsKey(mainKeys) == true)
{
//显示现存的状态文本
listTaskinfo = this.dicAlarmDevice[mainKeys].TaskList;
btnStatu.Text = SafeguardLogic.GetLightAlarmStatuText(listTaskinfo);
btnStatu.TextColor = UserCenterColor.Current.Green;
}
else
{
//无动作
btnStatu.TextID = R.MyInternationalizationString.uNotAction;
}
row.MouseUpEvent += (sender, e) =>
{
var form = new LightAlarmSettionForm();
this.AddForm(form, deviceInfo.Device, listTaskinfo);
form.formCloseEvent += (statuText, listInfo) =>
{
btnStatu.Text = statuText;
if (listInfo == null)
{
//指定为无动作模式
if (this.dicSaveDevice.ContainsKey(mainKeys) == true)
{
this.dicSaveDevice.Remove(mainKeys);
}
btnStatu.TextColor = UserCenterColor.Current.TextColor;
}
else
{
//确认添加动作
var data = new TaskInfoData();
this.dicSaveDevice[mainKeys] = data;
data.MacAddress = deviceInfo.Device.DeviceAddr;
data.Epoint = deviceInfo.Device.DeviceEpoint;
data.listInfos = listInfo;
btnStatu.TextColor = UserCenterColor.Current.Green;
}
};
};
}
///
/// 保存选择的设备
///
private async void DoSaveSelectDeviceAsync()
{
if (this.dicSaveDevice.Count == 0)
{
Application.RunOnMainThread(() =>
{
this.CloseForm();
});
return;
}
var listAction = new List();
foreach (var data in this.dicSaveDevice.Values)
{
var actionObj = new Safeguard.AlarmActionObj();
actionObj.DeviceAddr = data.MacAddress;
actionObj.Epoint = data.Epoint;
actionObj.Type = 0;
actionObj.TaskList = data.listInfos;
listAction.Add(actionObj);
}
//打开进度条
this.ShowProgressBar();
//添加报警目标到安防
bool success = await Common.LocalSafeguard.Current.AddAlarmTagetToSafety(this.zoonID, listAction);
//关闭进度条
this.CloseProgressBar();
if (success == true)
{
Application.RunOnMainThread(() =>
{
//刷新一览画面
this.LoadFormMethodByName("AlarmTargetSettionForm", "SetMiddleInfo");
//关闭自身
this.CloseForm();
});
}
}
///
/// 报警目标数据
///
private class TaskInfoData
{
///
/// MAC地址
///
public string MacAddress = string.Empty;
///
/// 端口号
///
public int Epoint = 0;
///
/// 报警动作
///
public List listInfos = new List();
}
}
}