using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
namespace Shared.Phone.UserCenter.Safety
{
///
/// 添加报警目标(设备)的列表界面
///
public class AlarmTargetAddDeviceForm : EditorCommonForm
{
#region ■ 变量声明___________________________
///
/// 列表控件
///
private VerticalListControl listView = null;
///
/// 防区ID(这个东西似乎是唯一的)
///
private int zoonID = 0;
///
/// 设备列表
///
private List listAllDevice = null;
///
/// 需要添加的报警设备
///
private Dictionary dicSaveDevice = new Dictionary();
#endregion
#region ■ 初始化_____________________________
///
/// 画面显示(底层会固定调用此方法,借以完成画面创建)
///
/// 防区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);
//初始化中部信息
this.InitMiddleFrame(i_deviceText);
}
///
/// 初始化中部信息
///
/// 列表头部名称
private void InitMiddleFrame(string titleText)
{
//清空bodyFrame
this.ClearBodyFrame();
var frameBack = new FrameLayout();
frameBack.BackgroundColor = UserCenterColor.Current.White;
frameBack.Height = Application.GetRealHeight(8);
bodyFrameLayout.AddChidren(frameBack);
this.listView = new VerticalListControl(12);
listView.Y = frameBack.Bottom;
listView.BackgroundColor = UserCenterColor.Current.White;
listView.Height = bodyFrameLayout.Height - frameBack.Bottom;
bodyFrameLayout.AddChidren(listView);
//完成
var btnfinish = new BottomClickButton();
btnfinish.TextID = R.MyInternationalizationString.uFinish;
bodyFrameLayout.AddChidren(btnfinish);
btnfinish.ButtonClickEvent += (sender, e) =>
{
//保存选择的设备
this.DoSaveSelectDeviceAsync();
};
HdlThreadLogic.Current.RunMainInThread(() =>
{
int count = listAllDevice.Count - 1;
for (int i = 0; i < listAllDevice.Count; i++)
{
//如果那个设备已经添加了,则不再显示
if (HdlSafeguardLogic.Current.IsAlarmDeviceExist(this.zoonID, listAllDevice[i]) == true)
{
continue;
}
//添加行目标
this.AddRowLayout(listAllDevice[i], i != count);
}
//调整真实高度
listView.AdjustRealHeightByBottomButton(Application.GetRealHeight(23));
});
}
#endregion
#region ■ 添加行目标_________________________
///
/// 添加行目标
///
///
///
private void AddRowLayout(CommonDevice device, bool addLine)
{
string mainKeys = Common.LocalDevice.Current.GetDeviceMainKeys(device);
var rowContr = new FrameRowControl(listView.rowSpace / 2);
listView.AddChidren(rowContr);
//图标
var btnIcon = rowContr.AddLeftIcon(81);
Common.LocalDevice.Current.SetDeviceIconToControl(btnIcon, device);
//设备名
var btnName = rowContr.AddLeftCaption(Common.LocalDevice.Current.GetDeviceEpointName(device), 700);
btnName.TextSize = 15;
//右箭头
rowContr.AddRightArrow();
if (addLine == true)
{
//底线
rowContr.AddBottomLine();
}
//状态显示
var btnStatu = rowContr.AddMostRightView("", 500);
rowContr.ButtonClickEvent += (sender, e) =>
{
List listTaskinfo = null;
if (dicSaveDevice.ContainsKey(mainKeys) == true)
{
//取缓存中还未保存的数据
listTaskinfo = dicSaveDevice[mainKeys].listInfos;
}
if (device.Type == DeviceType.DimmableLight//调光器
|| device.Type == DeviceType.ColorDimmableLight)//彩灯
{
var form = new AlarmTargetStatuSelectLightForm();
form.AddForm(device, listTaskinfo);
form.FinishSelectEvent += (statuText, listInfo) =>
{
btnStatu.Text = statuText;
//将新的报警目标添加入缓存
this.AddAlarmSettionDataToMemory(device, listInfo);
};
}
else if (device.Type == DeviceType.WindowCoveringDevice)//窗帘
{
var form = new AlarmTargetStatuSelectCurtainForm();
form.AddForm(device, listTaskinfo);
form.FinishSelectEvent += (statuText, listInfo) =>
{
btnStatu.Text = statuText;
//将新的报警目标添加入缓存
this.AddAlarmSettionDataToMemory(device, listInfo);
};
}
else
{
//其他直接归为开关类
var form = new AlarmTargetStatuSelectSwitchForm();
form.AddForm(device, listTaskinfo);
form.FinishSelectEvent += (statuText, listInfo) =>
{
btnStatu.Text = statuText;
//将新的报警目标添加入缓存
this.AddAlarmSettionDataToMemory(device, listInfo);
};
}
};
}
///
/// 将新的报警目标添加入缓存
///
///
///
private void AddAlarmSettionDataToMemory(CommonDevice device, List listInfo)
{
string mainKeys = Common.LocalDevice.Current.GetDeviceMainKeys(device);
if (listInfo == null || listInfo.Count == 0)
{
//指定为无动作模式
if (this.dicSaveDevice.ContainsKey(mainKeys) == true)
{
this.dicSaveDevice.Remove(mainKeys);
}
}
else
{
//确认添加动作
var data = new TaskInfoData();
this.dicSaveDevice[mainKeys] = data;
data.MacAddress = device.DeviceAddr;
data.Epoint = device.DeviceEpoint;
data.listInfos.AddRange(listInfo);
}
}
#endregion
#region ■ 保存选择的设备_____________________
///
/// 保存选择的设备
///
private async void DoSaveSelectDeviceAsync()
{
if (this.dicSaveDevice.Count == 0)
{
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);
}
//添加报警目标到安防
bool success = await HdlSafeguardLogic.Current.AddAlarmTagetToSafety(this.zoonID, listAction);
if (success == true)
{
//关闭自身
this.CloseForm();
}
}
#endregion
#region ■ 结构体_____________________________
///
/// 报警目标数据
///
private class TaskInfoData
{
///
/// MAC地址
///
public string MacAddress = string.Empty;
///
/// 端口号
///
public int Epoint = 0;
///
/// 报警动作
///
public List listInfos = new List();
}
#endregion
}
}