using Shared.Common;
using Shared.Phone.UserCenter;
using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
namespace Shared.Phone.Category
{
///
/// 场景执行目标添加设备的界面
///
public class AdjustTargetAddDeviceForm : EditorCommonForm
{
#region ■ 变量声明___________________________
///
/// 完成选择的事件(Key:设备主键)
///
public Action>> FinishSelectEvent = null;
///
/// 当前已经添加的执行目标
///
private List listAdjustTarget = null;
///
/// 当前已经添加的执行目标(存在检测用)
///
private Dictionary> dicOldListTask = null;
///
/// 当前界面上显示的执行目标(存在检测用)
///
private Dictionary> dicNewListTask = null;
///
/// 完成按钮
///
private BottomClickButton btnFinishControl = null;
///
/// 设备功能的菜单控件
///
private HorizontalScrolViewLayout deviceFunctionMenuContr = null;
///
/// 设备列表控件
///
private VerticalListControl listDeviceView = null;
#endregion
#region ■ 初始化_____________________________
///
/// 画面显示(底层会固定调用此方法,借以完成画面创建)
///
/// 当前执行目标列表
public void ShowForm(List i_listAdjustTarget)
{
this.listAdjustTarget = i_listAdjustTarget;
//设置头部信息
base.SetTitleText(Language.StringByID(R.MyInternationalizationString.AddFunction));
//初始化中部信息
this.InitMiddleFrame();
}
///
/// 初始化中部信息
///
private void InitMiddleFrame()
{
//初始化房间菜单控件
this.InitRoomMenuControl();
}
#endregion
#region ■ 房间菜单控件_______________________
///
/// 初始化房间菜单控件
///
private void InitRoomMenuControl()
{
//获取能够显示的房间列表
var listRoom = this.GetCanShowRoomList();
if (listRoom.Count == 0)
{
//没有可以添加的目标
this.ShowNotDataImage(bodyFrameLayout, Language.StringByID(R.MyInternationalizationString.uNotHadAddTarget));
return;
}
//这个控件的高度为:房间菜单的底部到屏幕底部
var functionBodyView = new FrameLayout();
//房间菜单控件
var roomSwitchContr = new RoomDeviceGroupMenuControl(listRoom);
this.bodyFrameLayout.AddChidren(roomSwitchContr);
//选择事件
roomSwitchContr.SelectRoomEvent += (selectRoom) =>
{
//在外面清空(特效的问题)
this.deviceFunctionMenuContr?.RemoveAll();
this.listDeviceView?.RemoveAll();
HdlThreadLogic.Current.RunMainInThread(() =>
{
//刷新设备分支控件
this.RefreshFunctionView(selectRoom, functionBodyView);
});
};
functionBodyView.Y = roomSwitchContr.Bottom;
functionBodyView.Height = bodyFrameLayout.Height - roomSwitchContr.Bottom;
bodyFrameLayout.AddChidren(functionBodyView);
//完成按钮
this.btnFinishControl = new BottomClickButton();
btnFinishControl.TextID = R.MyInternationalizationString.uFinish;
bodyFrameLayout.AddChidren(btnFinishControl);
btnFinishControl.Visible = false;
btnFinishControl.ButtonClickEvent += (sender, e) =>
{
//回调函数
this.FinishSelectEvent?.Invoke(this.dicNewListTask);
this.CloseForm();
};
//执行初始化(会自动触发SelectRoomEvent事件)
roomSwitchContr.InitControl();
}
///
/// 获取能够显示的房间列表
///
///
///
private List GetCanShowRoomList()
{
//先将列表Dictionary化
this.dicNewListTask = new Dictionary>();
this.dicOldListTask = new Dictionary>();
foreach (var data in this.listAdjustTarget)
{
if (data.Type == 0)
{
//只要设备
string mainkey = LocalDevice.Current.GetDeviceMainKeys(data.DeviceAddr, data.Epoint);
dicOldListTask[mainkey] = data.TaskList;
}
}
//当前楼层的全部房间
var lisrRoom = HdlRoomLogic.Current.GetRoomsByCurrentFloorIdAppendLoveRoom();
var listShowRoom = new List();
foreach (var room in lisrRoom)
{
foreach (var mainkey in room.ListDevice)
{
var device = LocalDevice.Current.GetDevice(mainkey);
if (this.CheckDeviceCanShow(device) == true)
{
//这个设备本地存在,并且它还没有加入到执行目标列表,则这个房间可以显示
listShowRoom.Add(room);
break;
}
}
}
return listShowRoom;
}
#endregion
#region ■ 功能分支___________________________
///
/// 刷新功能分支控件
///
///
private void RefreshFunctionView(Common.Room room, FrameLayout functionBodyView)
{
//获取分组后的设备
var dicGroupDevice = this.GetAllGroupDevice(room);
//只初始化一次
if (this.deviceFunctionMenuContr == null)
{
//设备菜单的白色背景
var functionBack1 = new FrameLayout();
functionBack1.X = ControlCommonResourse.XXLeft;
functionBack1.Height = Application.GetRealHeight(160);
functionBack1.Width = Application.GetRealWidth(1028);
functionBack1.BackgroundColor = ZigbeeColor.Current.GXCBackgroundColor;
functionBodyView.AddChidren(functionBack1);
functionBack1.SetCornerWithSameRadius(Application.GetRealHeight(17), HDLUtils.RectCornerTopLeft);
var functionBack2 = new FrameLayout();
functionBack2.X = ControlCommonResourse.XXLeft;
functionBack2.Y = functionBack1.Bottom - Application.GetRealHeight(50);
functionBack2.Height = Application.GetRealHeight(279 - 160 + 50);
functionBack2.Width = Application.GetRealWidth(1028);
functionBack2.BackgroundColor = ZigbeeColor.Current.GXCBackgroundColor;
functionBodyView.AddChidren(functionBack2);
functionBack2.SetCornerWithSameRadius(Application.GetRealHeight(58), HDLUtils.RectCornerBottomLeft);
//设备菜单的左右滑动的控件
this.deviceFunctionMenuContr = new HorizontalScrolViewLayout();
deviceFunctionMenuContr.X = ControlCommonResourse.XXLeft;
deviceFunctionMenuContr.Height = Application.GetRealHeight(279);
deviceFunctionMenuContr.Width = Application.GetRealWidth(1028);
functionBodyView.AddChidren(deviceFunctionMenuContr);
//设备的背景容器
var frameDeviceBack = new FrameLayout();
frameDeviceBack.X = ControlCommonResourse.XXLeft;
frameDeviceBack.Y = deviceFunctionMenuContr.Bottom + Application.GetRealHeight(35);
frameDeviceBack.BackgroundColor = UserCenterColor.Current.White;
frameDeviceBack.Width = bodyFrameLayout.Width;
frameDeviceBack.Height = functionBodyView.Height - deviceFunctionMenuContr.Bottom - Application.GetRealHeight(35);
frameDeviceBack.SetCornerWithSameRadius(Application.GetRealHeight(58), HDLUtils.RectCornerTopLeft);
functionBodyView.AddChidren(frameDeviceBack);
//设备列表控件
this.listDeviceView = new VerticalListControl(35);
listDeviceView.Y = Application.GetRealHeight(11);
listDeviceView.Width = Application.GetRealWidth(1022);
listDeviceView.Height = frameDeviceBack.Height - Application.GetRealHeight(11);
listDeviceView.SetCornerWithSameRadius(Application.GetRealHeight(58), HDLUtils.RectCornerTopLeft);
frameDeviceBack.AddChidren(listDeviceView);
}
//上一次选择的菜单和数据
DeviceRowInfo nowSelectDeviceInfo = null;
MainPage.Controls.DeviceFunctionUnallocatedControl oldSelectContr = null;
foreach (int Textid in dicGroupDevice.Keys)
{
var rowInfo = dicGroupDevice[Textid];
//设备类型的容器
var devieFrame = new FrameLayout();
devieFrame.Width = Application.GetRealWidth(220);
deviceFunctionMenuContr.AddChidren(devieFrame);
//菜单图片控件
var deviceObjContr = new MainPage.Controls.DeviceFunctionUnallocatedControl();
devieFrame.AddChidren(deviceObjContr);
deviceObjContr.InitControl(Language.StringByID(Textid), rowInfo.IconPath, rowInfo.IconPathSelected, rowInfo.listDeviceKeys);
deviceObjContr.ButtonClickEvent += (sender, e) =>
{
//选择的是同一个东西的话,不处理
if (nowSelectDeviceInfo.TextId != rowInfo.TextId)
{
//上一次的菜单取消,本次菜单选择
oldSelectContr.SetSelectStatu(false);
deviceObjContr.SetSelectStatu(true);
oldSelectContr = deviceObjContr;
nowSelectDeviceInfo = rowInfo;
HdlThreadLogic.Current.RunMainInThread(() =>
{
//初始化设备列表控件
this.InitListDeviceControls(listDeviceView, rowInfo, room);
});
}
};
if (nowSelectDeviceInfo == null)
{
//设置初始选择
nowSelectDeviceInfo = rowInfo;
//记录初始选择的菜单控件
oldSelectContr = deviceObjContr;
deviceObjContr.SetSelectStatu(true);
}
}
if (nowSelectDeviceInfo != null)
{
//初始化默认的设备列表控件
this.InitListDeviceControls(listDeviceView, nowSelectDeviceInfo, room);
}
}
#endregion
#region ■ 初始化设备列表控件_________________
///
/// 初始化设备列表控件
///
///
///
private void InitListDeviceControls(VerticalListControl listView, DeviceRowInfo rowInfo, Room i_room)
{
//先清空
listView.RemoveAll();
var listDevice = new List();
for (int i = 0; i < rowInfo.listDeviceKeys.Count; i++)
{
var device = LocalDevice.Current.GetDevice(rowInfo.listDeviceKeys[i]);
if (device != null)
{
listDevice.Add(device);
}
else
{
rowInfo.listDeviceKeys.RemoveAt(i);
i--;
}
}
HdlThreadLogic.Current.RunMain(() =>
{
foreach (var device in listDevice)
{
//添加设备行
this.AddDeviceRow(listView, device);
}
//调整桌布,促使它能够滑动超过完成按钮
listView.AdjustRealHeightByBottomButton(Application.GetRealHeight(23), Application.GetRealHeight(529));
});
}
///
/// 添加设备行
///
///
///
private void AddDeviceRow(VerticalListControl listView, CommonDevice device)
{
string mainkey = LocalDevice.Current.GetDeviceMainKeys(device);
//设备控件
var rowDevice = new FrameRowControl(listView.rowSpace / 2);
rowDevice.RightOffset = ControlCommonResourse.XXLeft - Application.GetRealWidth(109);
listView.AddChidren(rowDevice);
//设备图标背景控件
var frameIconBackGroud = new FrameLayout();
frameIconBackGroud.Height = this.GetPictrueRealSize(112);
frameIconBackGroud.Width = this.GetPictrueRealSize(112);
frameIconBackGroud.Gravity = Gravity.CenterVertical;
frameIconBackGroud.X = Application.GetRealWidth(46);
frameIconBackGroud.Radius = (uint)this.GetPictrueRealSize(112 / 2);
frameIconBackGroud.BackgroundColor = Common.ZigbeeColor.Current.GXCGrayBackgroundColor;
rowDevice.AddChidren(frameIconBackGroud, ChidrenBindMode.NotBind);
frameIconBackGroud.Y += rowDevice.chidrenYaxis;
//设备图标控件
var btnDeviceIcon = new IconViewControl(78);
btnDeviceIcon.Gravity = Gravity.Center;
Common.LocalDevice.Current.SetDeviceIconToControl(btnDeviceIcon, device);
frameIconBackGroud.AddChidren(btnDeviceIcon);
//重新绑定事件
rowDevice.ChangedChidrenBindMode(frameIconBackGroud, ChidrenBindMode.BindEvent);
//设备名字
var btnDeviceName = new NormalViewControl(600, 60, true);
btnDeviceName.X = Application.GetRealWidth(181);
btnDeviceName.Gravity = Gravity.CenterVertical;
btnDeviceName.Text = Common.LocalDevice.Current.GetDeviceEpointName(device);
rowDevice.AddChidren(btnDeviceName, ChidrenBindMode.BindEvent);
btnDeviceName.Y += rowDevice.chidrenYaxis;
//底线
var btnBottomLine = new NormalViewControl(Application.GetRealWidth(841), ControlCommonResourse.BottomLineHeight, false);
btnBottomLine.X = Application.GetRealWidth(181);
btnBottomLine.Y = rowDevice.Height - ControlCommonResourse.BottomLineHeight;
btnBottomLine.BackgroundColor = UserCenterColor.Current.ButtomLine;
rowDevice.AddChidren(btnBottomLine, ChidrenBindMode.NotBind);
//右箭头
rowDevice.AddRightArrow();
//状态
var btnStatu = rowDevice.AddMostRightView("", 400);
if (this.dicNewListTask.ContainsKey(mainkey) == true)
{
//显示配置状态
btnStatu.Text = HdlSafeguardLogic.Current.GetAdjustTargetStatuText(dicNewListTask[mainkey]);
}
rowDevice.ButtonClickEvent += (sender, e) =>
{
//功能的详细配置
this.ShowDeviceDetailSettion(device, btnStatu);
};
}
#endregion
#region ■ 功能的详细配置_____________________
///
/// 功能的详细配置
///
///
///
private void ShowDeviceDetailSettion(CommonDevice device, NormalViewControl btnStatu)
{
string mainKeys = LocalDevice.Current.GetDeviceMainKeys(device);
List listTaskinfo = null;
if (dicNewListTask.ContainsKey(mainKeys) == true)
{
//取缓存中还未保存的数据
listTaskinfo = dicNewListTask[mainKeys];
}
if (device.Type == DeviceType.DimmableLight//调光器
|| device.Type == DeviceType.ColorDimmableLight)//彩灯
{
var form = new UserCenter.Safety.AlarmTargetStatuSelectLightForm();
form.AddForm(device, listTaskinfo);
form.FinishSelectEvent += (statuText, listInfo) =>
{
if (listInfo.Count == 0) { statuText = string.Empty; }
btnStatu.Text = statuText;
//将新的执行目标添加入缓存
this.AddSettionDataToMemory(device, listInfo);
};
}
else if (device.Type == DeviceType.Thermostat)//空调
{
var form = new UserCenter.Safety.AlarmTargetStatuSelectAcForm();
form.AddForm(device, listTaskinfo);
form.FinishSelectEvent += (statuText, listInfo) =>
{
if (listInfo.Count == 0) { statuText = string.Empty; }
btnStatu.Text = statuText;
//将新的执行目标添加入缓存
this.AddSettionDataToMemory(device, listInfo);
};
}
else if (device.Type == DeviceType.WindowCoveringDevice)//窗帘
{
var form = new UserCenter.Safety.AlarmTargetStatuSelectCurtainForm();
form.AddForm(device, listTaskinfo);
form.FinishSelectEvent += (statuText, listInfo) =>
{
if (listInfo.Count == 0) { statuText = string.Empty; }
btnStatu.Text = statuText;
//将新的执行目标添加入缓存
this.AddSettionDataToMemory(device, listInfo);
};
}
else
{
//其他直接归为开关类
var form = new UserCenter.Safety.AlarmTargetStatuSelectSwitchForm();
form.AddForm(device, listTaskinfo);
form.FinishSelectEvent += (statuText, listInfo) =>
{
if (listInfo.Count == 0) { statuText = string.Empty; }
btnStatu.Text = statuText;
//将新的执行目标添加入缓存
this.AddSettionDataToMemory(device, listInfo);
};
}
}
///
/// 将新的执行目标添加入缓存
///
///
///
private void AddSettionDataToMemory(CommonDevice device, List listInfo)
{
string mainKeys = LocalDevice.Current.GetDeviceMainKeys(device);
if (listInfo == null || listInfo.Count == 0)
{
//指定为无动作模式
if (this.dicNewListTask.ContainsKey(mainKeys) == true)
{
this.dicNewListTask.Remove(mainKeys);
if (dicNewListTask.Count == 0)
{
this.btnFinishControl.Visible = false;
}
}
}
else
{
//确认添加动作
this.dicNewListTask[mainKeys] = listInfo;
if (this.btnFinishControl.Visible == false)
{
this.btnFinishControl.Visible = true;
}
}
}
#endregion
#region ■ 整合设备___________________________
///
/// 获取分组后的设备
///
///
private Dictionary GetAllGroupDevice(Common.Room room)
{
//全部的设备
var listDeviceTemp = HdlRoomLogic.Current.GetRoomListDevice(room);
var listDevice = new List();
foreach (var device in listDeviceTemp)
{
//判断该设备能否显示
if (this.CheckDeviceCanShow(device) == true)
{
listDevice.Add(device);
}
}
//根据设备所属类型排序
listDevice = LocalDevice.Current.SortDeviceByBelongType(listDevice);
var dic = new Dictionary();
foreach (var device in listDevice)
{
var typeInfo = LocalDevice.Current.GetDeviceBelongEnumInfo(device);
//按所属ID分组
if (dic.ContainsKey(typeInfo.BeloneTextId) == false)
{
dic[typeInfo.BeloneTextId] = new DeviceRowInfo();
string path1 = string.Empty;
string path2 = string.Empty;
//获取图片
LocalDevice.Current.GetDeviceFunctionTypeMenuIcon(typeInfo.ConcreteType, ref path1, ref path2);
dic[typeInfo.BeloneTextId].IconPath = path1;
dic[typeInfo.BeloneTextId].IconPathSelected = path2;
dic[typeInfo.BeloneTextId].TextId = typeInfo.BeloneTextId;
}
dic[typeInfo.BeloneTextId].listDeviceKeys.Add(LocalDevice.Current.GetDeviceMainKeys(device));
}
return dic;
}
#endregion
#region ■ 界面关闭___________________________
///
/// 界面关闭
///
public override void CloseFormBefore()
{
this.FinishSelectEvent = null;
base.CloseFormBefore();
}
#endregion
#region ■ 一般方法___________________________
///
/// 检测设备能否显示
///
///
private bool CheckDeviceCanShow(CommonDevice device)
{
if (device == null) { return false; }
if (device.Type == DeviceType.AirSwitch ||//空气开关
device.Type == DeviceType.ColorDimmableLight ||//调光灯
device.Type == DeviceType.DimmableLight ||//彩灯
device.Type == DeviceType.OnOffOutput ||//继电器
device.Type == DeviceType.Thermostat ||//空调
device.Type == DeviceType.WindowCoveringDevice)//窗帘
{
string mainkey = LocalDevice.Current.GetDeviceMainKeys(device);
return this.dicOldListTask.ContainsKey(mainkey) == false;
}
return false;
}
#endregion
#region ■ 结构体_____________________________
///
/// 设备行信息
///
private class DeviceRowInfo
{
///
/// 文本ID,目前用来做主键
///
public int TextId = 0;
///
/// 图标
///
public string IconPath = string.Empty;
///
/// 图标
///
public string IconPathSelected = string.Empty;
///
/// 设备回路主键
///
public List listDeviceKeys = new List();
}
#endregion
}
}