using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Shared.Common;
using Shared.Phone.UserCenter.SmartSound.Util;
using Shared.Phone.UserCenter.SmartSound.Widget;
using ZigBee.Device;
namespace Shared.Phone.UserCenter.SmartSound.Forms
{
public class SmartSoundControlContentForm : EditorCommonForm
{
///
/// 临时数据
///
private List TempDatas = new List();
private SmartSoundInfo.SoundInfo mSoundInfo;
///
/// 列表控件
///
private VerticalListControl listView = null;
private FrameLayout contentLayout = null;
///
/// 当前楼层的索引
///
public int CurrentIndex = 0;
private BottomClickButton bottomClickButton = null;
///
/// 当前楼层的名称
///
private static NormalViewControl pullLayoutText = null;
public SmartSoundControlContentForm(SmartSoundInfo.SoundInfo soundInfo)
{
this.mSoundInfo = soundInfo;
}
///
/// 画面显示(底层会固定调用此方法,借以完成画面创建)
///
public void ShowForm()
{
IniFloorData();//先加载数据
TempDatas.Clear();
this.ScrollEnabled = false;
base.SetTitleText("选择房间");//设置标题信息
contentLayout = new FrameLayout();
this.bodyFrameLayout.AddChidren(contentLayout);
IniAllDataView();
}
#region 加载本地所有数据,生成供用户选择房间的界面
private void IniAllDataView()
{
contentLayout.RemoveAll();
var pullLayout = new MyPullControl(this, SmartSound.LocaData);
contentLayout.AddChidren(pullLayout);
pullLayout.InitControl();
LoadAllRoomListView();
bottomClickButton = new BottomClickButton();
this.bodyFrameLayout.AddChidren(bottomClickButton);
bottomClickButton.Text = "确认";
bottomClickButton.ButtonClickEvent += (sender, e) =>
{
if (SmartSound.getInstantiate().LayerList != null && SmartSound.getInstantiate().LayerList.Count > 0)
{
//生成一个弹窗画面
var dialogForm = new TextDialog("确认删除现有房间列表的所有控制内容,添加全新的场景和功能?", "确认");
dialogForm.SetTitleText("提示");
//按下确认按钮
dialogForm.ComfirmClickEvent += () =>
{
//画面关闭
dialogForm.CloseDialog();
uploadData();
};
}
else
{
uploadData();
}
};
bottomClickButton.Visible = false;
}
///
/// 提交数据到服务器
///
private void uploadData()
{
HdlThreadLogic.Current.RunThread(async () =>
{
this.ShowProgressBar();
//保存数据
if (SmartSound.getInstantiate().LayerList == null)
SmartSound.getInstantiate().LayerList = new List();
SmartSound.getInstantiate().LayerList.Clear();
SmartSound.getInstantiate().LayerList = CollateData();
SmartSound.getInstantiate().TokenID = mSoundInfo.Id;
SmartSound.getInstantiate().UserID = mSoundInfo.UserID;
SmartSound.getInstantiate().HomeID = mSoundInfo.HomeID;
//上传数据到服务器
string str = await postBatchEdit();
CloseProgressBar();
HdlThreadLogic.Current.RunMainInThread(() =>
{
CloseForm();
});
});
}
private void LoadAllRoomListView()
{
try
{
if (SmartSound.LocaData == null || SmartSound.LocaData.Count < 1)
return;
// 加载当前楼层的所有房间 ListView
SmartSound.Layer layer = SmartSound.LocaData[CurrentIndex];
if (listView == null)
{
listView = new VerticalListControl();
contentLayout.AddChidren(listView);
listView.Y = Application.GetRealHeight(173);
listView.Height = bodyFrameLayout.Height- Application.GetRealHeight(173);
}
pullLayoutText.Text = layer.LayerName;
listView.RemoveAll();
foreach(SmartSound.Room room in layer.RoomList)
{
var roomRowLayout = new RoomRowLayout(this, room);
listView.AddChidren(roomRowLayout);
roomRowLayout.InitControl();
}
//多个的时候,确认按钮会遮挡一部分。追加三个空的行的高度,把它顶上来
if (listView.ChildrenCount > 5)
{
TextView textView = new TextView();
textView.Height = Application.GetRealHeight(127 * 3);
listView.AddChidren(textView);
}
}
catch { }
}
private async Task postBatchEdit()
{
// The remote server returned an error: (400) Bad Request.
string url = "https://developer.hdlcontrol.com/zigbeespeakerservice/BatchEdit";
string json = Newtonsoft.Json.JsonConvert.SerializeObject(SmartSound.getInstantiate());
string response_str = MyHttpWebResponse.PostWebRequest(url, json, Encoding.UTF8);
return response_str;
}
#endregion
///
/// 将数据转换为智能音箱使用的数据
///
private void IniFloorData()
{
if (SmartSound.LocaData == null)
SmartSound.LocaData = new List();
SmartSound.LocaData.Clear();
Dictionary floorDictionary = Config.Instance.Home.FloorDics;//楼层列表
#region 分楼层
if (floorDictionary.Count < 1)
{
//没有楼层,模拟一个
SmartSound.Layer layer = new SmartSound.Layer();
layer.LayerID = DateTime.Now.ToString("yyyyMMddHHmmssms");
layer.LayerName = "一楼";
layer.RoomList.Clear();
SmartSound.LocaData.Add(layer);
}
else
{
foreach (string layer_id in floorDictionary.Keys)
{
SmartSound.Layer layer = new SmartSound.Layer();
layer.LayerID = layer_id;
layer.LayerName = floorDictionary[layer_id];
layer.RoomList.Clear();
SmartSound.LocaData.Add(layer);
}
}
#endregion
#region 房间入住对应的楼层
var roomList = HdlRoomLogic.Current.GetAllListRooms();//获取所有房间
for (int n = 0; n < SmartSound.LocaData.Count; n++)
{
SmartSound.Layer layer = SmartSound.LocaData[n];
foreach (Room room in roomList)
{
try
{
if (layer.LayerID != room.FloorId && SmartSound.LocaData.Count > 1)
continue;
//添加房间
SmartSound.Room smartRoom = new SmartSound.Room();
layer.RoomList.Add(smartRoom);
smartRoom.RoomID = room.Id;
smartRoom.RoomName = room.Name;
smartRoom.DeviceList.Clear();
smartRoom.SceneList.Clear();
#region 添加设备 灯光、窗帘、场景、空调
for (int j = 0; j < room.ListDevice.Count; j++)
{
string key = room.ListDevice[j].ToString();
var device = HdlDeviceCommonLogic.Current.GetDevice(key);
if (device == null)
continue;
SmartSound.Device smartDevice = new SmartSound.Device();
smartDevice.DeviceAddress = device.DeviceAddr;//设备 MAC
smartDevice.Epoint = device.DeviceEpoint;//设备端口
smartDevice.DeviceName = HdlDeviceCommonLogic.Current.GetDeviceEpointName(device);//设备名称
smartDevice.NicksName = "";
smartDevice.DeviceType = GetDeviceType(device);//设备类型
if (smartDevice.DeviceType == 0)
continue;
if(ZbGateway.DicGatewayBaseInfo.ContainsKey(device.CurrentGateWayId) ==true)
{
smartDevice.GatewayID = ZbGateway.DicGatewayBaseInfo[device.CurrentGateWayId].Id;//网关 Id
}
else
{
smartDevice.GatewayID = device.CurrentGateWayId;//网关 Id
}
if (smartDevice.DeviceName == "" || smartDevice.DeviceName == string.Empty)
continue;
smartRoom.DeviceList.Add(smartDevice);
}
#endregion
var sceneList = HdlSceneLogic.Current.GetRoomSceneList(room);
#region //添加场景
for (int k = 0; k < sceneList.Count; k++)
{
try
{
SceneUI scene = sceneList[k];
if (scene == null) continue;
SmartSound.Scene smartScene = new SmartSound.Scene();
smartScene.SceneName = scene.Name;
smartScene.SceneID = scene.Id;
smartScene.DelayTime = scene.DelayTime;
smartScene.NicksName = "";
if (ZbGateway.DicGatewayBaseInfo.ContainsKey(ZbGateway.MainGateWay.GwId) == true)
{
smartScene.GatewayID = ZbGateway.DicGatewayBaseInfo[ZbGateway.MainGateWay.GwId].Id;//网关 Id
}
else
{
smartScene.GatewayID = ZbGateway.MainGateWay.GwId;//网关 Id
}
if (smartScene.SceneName == "" || smartScene.SceneName == string.Empty)
continue;
smartRoom.SceneList.Add(smartScene);
}
catch (Exception e) { }
}
#endregion
if (smartRoom.DeviceList.Count == 0 && sceneList.Count == 0)
layer.RoomList.Remove(smartRoom);
}
catch (Exception e)
{
string errpr = e.Message;
}
}
}
#endregion
}
///
/// Zigbee设备类型转换为智能音箱使用的类型
///
///
private int GetDeviceType(ZigBee.Device.CommonDevice device)
{
int type = 0;//1=开关灯,2=调光灯,3=RGB 灯,4=窗帘模块,5=开合帘,6=卷帘,7=空调,8=面板,9=新风
if (device.Type == DeviceType.OnOffOutput && device.DfunctionType == DeviceFunctionType.A灯光)
{
type = 1;//这里面的就是继电器灯光了
}
else if (device.Type == DeviceType.DimmableLight)
{
type = 2;//调光
}
else if (device.Type == DeviceType.ColorDimmableLight)
{
type = 3;
}
else if (device.Type == DeviceType.WindowCoveringDevice)
{
//窗帘
if (((Rollershade)device).WcdType == 4)//开合帘
{
type = 5;
}
else if (((Rollershade)device).WcdType == 0)//卷帘
type = 6;//卷帘
}
else if (device.Type == DeviceType.Thermostat)
{
type = 7;
}
else if (device.Type == DeviceType.FreshAir)
{
type = 9;
}
return type;
}
///
/// 将选中的数据添加到临时数据列表中,便于后期的"保存"
///
private void AddData(SmartSound.Room room)
{
if (TempDatas == null)
TempDatas = new List();
SmartSound.Layer tempLayer = SmartSound.LocaData[CurrentIndex];
if (tempLayer == null) return;
#region 添加楼层
bool floor_exists = false;
// 过滤相同的楼层
for (int i = 0; i < TempDatas.Count; i++)
{
if (tempLayer.LayerID == TempDatas[i].LayerID)
{
floor_exists = true;
break;
}
}
if (!floor_exists)
{
tempLayer.RoomList.Clear();
TempDatas.Add(tempLayer);
}
#endregion
#region 添加房间
for (int i = 0; i < TempDatas.Count; i++)
{
if (SmartSound.LocaData[CurrentIndex].LayerID != TempDatas[i].LayerID)
continue;
bool room_exists = false;
for (int n = 0; n < TempDatas[i].RoomList.Count; n++)
{
if (room.RoomID == TempDatas[i].RoomList[n].RoomID)
{
room_exists = true;
break;
}
}
if (!room_exists)
{
TempDatas[i].RoomList.Add(room);
CheckedAll(room);//添加房间成功,默认选中所有设备
break;
}
}
#endregion
}
private void RemoveData(SmartSound.Room room)
{
try
{
for (int i = 0; i < TempDatas.Count; i++)
{
for (int j = 0; j < TempDatas[i].RoomList.Count; j++)
{
if (TempDatas[i].RoomList[j].RoomID == room.RoomID)
{
TempDatas[i].RoomList.RemoveAt(j);
}
}
}
}
catch { }
}
///
/// 默认选择所有设备
///
///
private void CheckedAll(SmartSound.Room room)
{
// 标记所有场景
for (int n = 0; n < room.SceneList.Count; n++)
{
SmartSound.Scene _scene = room.SceneList[n];
_scene.Checked = true;
}
//标记所有设备
for (int k = 0; k < room.DeviceList.Count; k++)
{
SmartSound.Device _device = room.DeviceList[k];
_device.Checked = true;
}
}
///
/// 重新整理数据,把没有选中的数据都剔除掉
///
private List CollateData()
{
try
{
//移出未选中的设备、场景
for (int i = 0; i < TempDatas.Count; i++)
{
SmartSound.Layer _layer = TempDatas[i];
for (int j = 0; j < _layer.RoomList.Count; j++)
{
SmartSound.Room _room = _layer.RoomList[j];
if (_room.Checked)
{
#region 清除未选中的设备
for (int k = 0; k < _room.DeviceList.Count; k++)
{
SmartSound.Device _device = _room.DeviceList[k];
if (!_device.Checked)
{
_room.DeviceList.Remove(_device);
k--;
}
}
#endregion
#region 清除未选中的场景
for (int n = 0; n < _room.SceneList.Count; n++)
{
SmartSound.Scene _scene = _room.SceneList[n];
if (!_scene.Checked)
{
_room.SceneList.Remove(_scene);
n--;
}
}
#endregion
}
else
{
_layer.RoomList.Remove(_room);
j--;
}
}
}
//移出没有设备的房间
for (int j = 0; j < TempDatas.Count; j++)
{
SmartSound.Layer _layer = TempDatas[j];
if (_layer.RoomList.Count < 1)
{
TempDatas.Remove(_layer);
j--;
}
}
}
catch { }
return TempDatas;
}
#region ■ 自定义楼层选择控件_____________________
///
/// 房间列表行
///
private class MyPullControl : FrameRowControl
{
private SmartSoundControlContentForm smartSoundDataAdd = null;
private List Layers = null;
///
/// 房间列表
///
private NormalViewControl btnName = null;
///
/// 房间列表行
///
public MyPullControl(SmartSoundControlContentForm _SmartSoundDataAdd,List _layers)
{
this.smartSoundDataAdd = _SmartSoundDataAdd;
this.Layers = _layers;
this.UseClickStatu = false;
this.BackgroundColor = UserCenterColor.Current.White;
this.Height = Application.GetRealHeight(173);
}
///
/// 初始化控件
///
public void InitControl()
{
//显示文本
btnName = this.AddLeftCaption(string.Empty, 700);
btnName.Height = Application.GetRealHeight(60);
btnName.TextSize = 17;
btnName.Y = Application.GetRealHeight(57);
btnName.Text = "房间列表";
var right_icon=this.AddRightArrow();
right_icon.SelectedImagePath = "SmartSound/PullDown.png";
right_icon.UnSelectedImagePath = "SmartSound/PullDown.png";
right_icon.ButtonClickEvent += (sender, e) =>
{
ShowPullList();
};
pullLayoutText = this.AddMostRightView(Layers[smartSoundDataAdd.CurrentIndex].LayerName, 300, false);
pullLayoutText.Name = "pullLayout";
pullLayoutText.ButtonClickEvent += (sender, e) =>
{
ShowPullList();
};
pullLayoutText.Text = Layers[smartSoundDataAdd.CurrentIndex].LayerName;
if (Layers.Count == 1)
{
right_icon.Visible = false;
pullLayoutText.Visible = false;
}
}
private void ShowPullList()
{
//房间列表行{房间列表 textView,楼层下拉窗}
//显示房间{带选择框的;当选中了房间后需要在底部弹出确认按钮,点击确认跳转到新的界面}
var pull_frame = new TopRightMenuControl(Layers.Count, 2);
pull_frame.Y = this.Height;
for (int i = 0; i < Layers.Count; i++)
{
int index = i;
//创建楼层
pull_frame.AddRowMenu(Layers[i].LayerName, "Item/CreatFloor.png", "Item/CreatFloorSelected.png", () =>
{
smartSoundDataAdd.CurrentIndex = index;
smartSoundDataAdd.LoadAllRoomListView();
});
}
}
}
#endregion
#region ■ 自定义房间选择控件_____________________
///
/// 房间列表行
///
private class RoomRowLayout : FrameRowControl
{
private SmartSoundControlContentForm smartSoundControlContentForm = null;
private SmartSound.Room mRoom = null;
///
/// 房间名称
///
private NormalViewControl btnName = null;
private MostRightIconControl Right_icon = null;
///
/// 房间列表行
///
public RoomRowLayout(SmartSoundControlContentForm _smartSoundControlContentForm,SmartSound.Room _room)
{
this.smartSoundControlContentForm = _smartSoundControlContentForm;
this.mRoom = _room;
this.UseClickStatu = false;
this.BackgroundColor = UserCenterColor.Current.White;
this.Height = Application.GetRealHeight(170);
}
///
/// 初始化控件
///
public void InitControl()
{
//显示文本
btnName = this.AddLeftCaption(string.Empty, 2000);
btnName.Height = Application.GetRealHeight(60);
btnName.TextSize = 14;
btnName.Y = Application.GetRealHeight(57);
btnName.Text = mRoom.RoomName;
Right_icon = this.AddMostRightEmptyIcon(58, 58);
Right_icon.Gravity = Gravity.CenterVertical;
btnName.ButtonClickEvent += (sender, e) =>
{
mRoom.Checked = true;
RefreshRightIconState(mRoom.Checked);
SmartSoundContentForDevice deviceForm = new SmartSoundContentForDevice(mRoom);
deviceForm.AddForm();
};
this.Right_icon.ButtonClickEvent += (sender, e) =>
{
mRoom.Checked = !mRoom.Checked;
RefreshRightIconState(mRoom.Checked);
};
RefreshRightIconState(mRoom.Checked);
AddBottomLine();
}
private void RefreshRightIconState(bool bol = false)
{
try
{
if (bol)
{
Right_icon.SelectedImagePath = "Item/ItemSelected.png";
Right_icon.UnSelectedImagePath = "Item/ItemSelected.png";
smartSoundControlContentForm.AddData(mRoom);
}
else
{
Right_icon.SelectedImagePath = "Item/ItemUnSelected.png";
Right_icon.UnSelectedImagePath = "Item/ItemUnSelected.png";
smartSoundControlContentForm.RemoveData(mRoom);
}
bool isShow = false;
if(smartSoundControlContentForm==null) return;
foreach (SmartSound.Room _room in SmartSound.LocaData[smartSoundControlContentForm.CurrentIndex].RoomList)
{
if (_room.Checked)
{
isShow = true;
break;
}
}
smartSoundControlContentForm.bottomClickButton.Visible = isShow;
}
catch(Exception e)
{
string error = e.Message;
}
}
}
#endregion
}
}