using Shared.Common;
using Shared.Phone.Device.CommonForm;
using Shared.Phone.UserCenter;
using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
namespace Shared.Phone.MainPage
{
///
/// 主页(注意,BodyFrameLyout是场景和功能的桌布)
///
public class UserHomeView2 : EditorCommonForm
{
#region ■ 变量声明___________________________
///
/// 设备卡片控件(主键为:设备主键)
///
private Dictionary dicDeviceCardControl = new Dictionary();
///
/// 场景卡片控件(主键为:场景ID)
///
private Dictionary dicSceneCardControl = new Dictionary();
///
/// 当前选择的分支 1:场景 2:功能
///
private int NowSelectIndex = 1;
#endregion
#region ■ 初始化_____________________________
///
/// 界面显示
///
public void ShowForm()
{
this.BackgroundColor = ZigbeeColor.Current.GXCGrayBackgroundColor;
//清除全部
this.RemoveAll();
//初始化头部控件
this.InitTopFrameLayoutControl();
//初始化中间控件
this.InitMidFrameLayoutControl();
//添加设备状态上报事件
this.AddNormalDeviceReportEvent();
//添加传感器状态上报事件
this.AddSensorDeviceReportEvent();
}
///
/// 初始化头部控件
///
private void InitTopFrameLayoutControl()
{
//头部容器
base.topFrameLayout = new FrameLayout();
topFrameLayout.Y = Application.GetRealHeight(60);
topFrameLayout.Height = Application.GetRealHeight(127);
topFrameLayout.BackgroundColor = ZigbeeColor.Current.GXCGrayBackgroundColor;
this.AddChidren(topFrameLayout);
//拥有楼层
if (Config.Instance.Home.FloorDics.Count > 0)
{
//楼层名
var btnFloor = new NormalViewControl(500, 100, true);
//楼层图标
var btnFloorIcon = new IconViewControl(69);
btnFloorIcon.X = ControlCommonResourse.XXLeft;
btnFloorIcon.Gravity = Gravity.CenterVertical;
btnFloorIcon.UnSelectedImagePath = "Item/Floor.png";
topFrameLayout.AddChidren(btnFloorIcon);
btnFloorIcon.ButtonClickEvent += (sender, e) =>
{
//显示选择楼层的界面
this.ShowSelectFloorForm(btnFloor);
};
//楼层名
btnFloor = new NormalViewControl(500, 100, true);
btnFloor.X = btnFloorIcon.Right;
btnFloor.Gravity = Gravity.CenterVertical;
btnFloor.TextColor = ZigbeeColor.Current.GXCTextColor;
btnFloor.Text = Config.Instance.Home.GetCurrentFloorName;
btnFloor.IsBold = true;
topFrameLayout.AddChidren(btnFloor);
btnFloor.ButtonClickEvent += (sender, e) =>
{
//显示选择楼层的界面
this.ShowSelectFloorForm(btnFloor);
};
}
//安防快捷方式
if (UserCenterResourse.ResidenceOption.SafetyShortcut)
{
var btnSafety = new Controls.SafetyShortcutControl();
btnSafety.X = Application.GetRealWidth(860);
btnSafety.Gravity = Gravity.CenterVertical;
topFrameLayout.AddChidren(btnSafety);
}
//消息图标
var btnMessage = new MessageManagementControl();
btnMessage.X = Application.GetRealWidth(953);
btnMessage.Gravity = Gravity.CenterVertical;
btnMessage.UnSelectedImagePath = "Item/Message.png";
btnMessage.SelectedImagePath = "Item/MessageSelected.png";
topFrameLayout.AddChidren(btnMessage);
//住宅名字
var btnHouseName = new NormalViewControl(700, 95, true);
btnHouseName.X = ControlCommonResourse.XXLeft;
btnHouseName.Y = Application.GetRealHeight(161);
btnHouseName.TextSize = 24;
btnHouseName.TextColor = ZigbeeColor.Current.GXCTextDeepBlackColor;
btnHouseName.IsBold = true;
btnHouseName.Text = Config.Instance.Home.Name;
this.AddChidren(btnHouseName);
//切换住宅
btnHouseName.MouseLongEventHandler += (sender, e) =>
{
if (Config.Instance.HomeFilePathList.Count == 0)
{
//当前住宅为空,请先建立住宅
this.ShowMassage(ShowMsgType.Remind, Language.StringByID(R.MyInternationalizationString.CurrentlyTheUserIshHouseIsEmptyPleaseBuildANewHouseFirst));
}
else
{
var selectHouse = new Device.Category.SelectHouse();
selectHouse.Init();
selectHouse.HouseAction = (houseId) =>
{
ChangeResidence(House.GetHouseByHouseId(houseId));
};
}
};
}
///
/// 初始化中间控件
///
private void InitMidFrameLayoutControl()
{
//检测是否绑定有网关
if (this.CheckHadBindGateway() == false)
{
//显示没有绑定过网关的界面
this.ShowNoGatewayTip();
}
else
{
var roomPageView = new HorizontalPages();
roomPageView.Y = Application.GetRealHeight(302);
roomPageView.Width = Application.GetRealWidth(CommonPage.AppRealWidth);
roomPageView.Height = Application.GetRealHeight(478);
this.AddChidren(roomPageView);
//突出边距
roomPageView.TCBJ = Application.GetRealWidth(112);
//两个page之间的间距
roomPageView.JMBJ = Application.GetRealWidth(69);
//当前房间
var rList = HdlRoomLogic.Current.GetRoomsByCurrentFloorIdAppendLoveRoom();
foreach (var room in rList)
{
var roomView = new RoomView(0, 0);
roomPageView.AddChidren(roomView);
roomView.Init(room);
roomView.HideName(true);
roomView.action += () =>
{
this.ShowForm();
};
}
//设置房间的初始选择
var curIndex = rList.FindIndex((obj) => obj.Id == HdlRoomLogic.Current.CurrentRoom.Id);
roomPageView.PageIndex = curIndex;
if (roomPageView.GetChildren(roomPageView.PageIndex) != null)
{
//当前房间,则不显示那些数据出来
(roomPageView.GetChildren(roomPageView.PageIndex) as RoomView).HideName(false);
}
//控件开始滑动的事件
//roomPageView.StartScrollAction += () =>
//{
// //开始滑动时,隐藏当前的数据显示
// (roomPageView.GetChildren(roomPageView.PageIndex) as RoomView).HideName(true);
//};
roomPageView.PageChange += (sender, e) =>
{
if (roomPageView.GetChildren(roomPageView.PageIndex - 1) != null)
{
(roomPageView.GetChildren(roomPageView.PageIndex - 1) as RoomView).HideName(true);
}
if (roomPageView.GetChildren(roomPageView.PageIndex + 1) != null)
{
(roomPageView.GetChildren(roomPageView.PageIndex + 1) as RoomView).HideName(true);
}
if (roomPageView.GetChildren(roomPageView.PageIndex) != null)
{
(roomPageView.GetChildren(roomPageView.PageIndex) as RoomView).HideName(false);
}
//切换当前房间
HdlRoomLogic.Current.CurrentRoom = rList[roomPageView.PageIndex];
//刷新设备桌布控件
this.RefreshBodyView();
};
//功能和场景的背景图
var functionSceneView = new FrameLayout();
functionSceneView.Y = Application.GetRealHeight(861);
functionSceneView.Width = Application.GetRealWidth(832);
functionSceneView.Height = Application.GetRealHeight(167);
functionSceneView.Gravity = Gravity.CenterHorizontal;
functionSceneView.BackgroundImagePath = "Item/SceneFunctionBG.png";
this.AddChidren(functionSceneView);
//场景
var btnScene = new NormalViewControl(350, 100, true);
btnScene.X = Application.GetRealWidth(30);
btnScene.Y = Application.GetRealHeight(12);
btnScene.TextColor = ZigbeeColor.Current.GXCTextGrayColor3;
btnScene.SelectedTextColor = ZigbeeColor.Current.GXCTextWhiteColor;
btnScene.TextID = R.MyInternationalizationString.Scence;
btnScene.IsSelected = true;
btnScene.TextSize = 16;
btnScene.IsBold = true;
btnScene.TextAlignment = TextAlignment.Center;
functionSceneView.AddChidren(btnScene);
//功能
var btnFunction = new NormalViewControl(360, 100, true);
btnFunction.X = Application.GetRealWidth(430);
btnFunction.Y = Application.GetRealHeight(12);
btnFunction.TextColor = ZigbeeColor.Current.GXCTextGrayColor3;
btnFunction.SelectedTextColor = ZigbeeColor.Current.GXCTextWhiteColor;
btnFunction.TextID = R.MyInternationalizationString.Function;
btnFunction.IsSelected = false;
btnFunction.TextAlignment = TextAlignment.Center;
functionSceneView.AddChidren(btnFunction);
//功能和场景bodyView
this.bodyFrameLayout = new FrameLayout()
{
Y = functionSceneView.Bottom,
Height = Application.GetRealHeight(750),
Gravity = Gravity.CenterHorizontal,
BackgroundColor = ZigbeeColor.Current.GXCGrayBackgroundColor
};
this.AddChidren(bodyFrameLayout);
//选择功能分栏
btnFunction.ButtonClickEvent += (sender, e) =>
{
//功能分支选择
this.NowSelectIndex = 2;
btnScene.IsSelected = false;
btnFunction.IsSelected = true;
btnScene.TextSize = 14;
btnScene.IsBold = false;
btnFunction.TextSize = 16;
btnFunction.IsBold = true;
//刷新设备桌布控件
this.RefreshBodyView();
};
//选择场景分栏
btnScene.ButtonClickEvent += (sender, e) =>
{
//场景分支选择
this.NowSelectIndex = 1;
btnFunction.IsSelected = false;
btnScene.IsSelected = true;
btnScene.TextSize = 16;
btnScene.IsBold = true;
btnFunction.TextSize = 14;
btnFunction.IsBold = false;
this.RefreshBodyView();
};
//刷新设备桌布控件
this.RefreshBodyView();
}
}
///
/// 提示用户没有网关,并快速跳转到添加网关界面
///
private void ShowNoGatewayTip()
{
var bg = new PicViewControl(717, 478);
bg.Y = Application.GetRealHeight(302);
bg.Gravity = Gravity.CenterHorizontal;
bg.UnSelectedImagePath = "Item/NoBindGW.png";
this.AddChidren(bg);
var tip = new NormalViewControl(Application.GetMinRealAverage(717), Application.GetMinRealAverage(58), false);
tip.Y = Application.GetRealHeight(815);
tip.Gravity = Gravity.CenterHorizontal;
tip.TextID = R.MyInternationalizationString.NewAccountNeedBingGW;
tip.TextColor = ZigbeeColor.Current.GXCTextGrayColor;
tip.TextAlignment = TextAlignment.Center;
this.AddChidren(tip);
var addBG = new PicViewControl(971, 366);
addBG.Y = Application.GetRealHeight(1048);
addBG.UnSelectedImagePath = "Item/VirtualFrame.png";
addBG.Gravity = Gravity.CenterHorizontal;
this.AddChidren(addBG);
var addGatewayBtn = new IconViewControl(89);
addGatewayBtn.Y = Application.GetRealHeight(1143);
addGatewayBtn.UnSelectedImagePath = "Item/Add_GW.png";
addGatewayBtn.Gravity = Gravity.CenterHorizontal;
this.AddChidren(addGatewayBtn);
addGatewayBtn.ButtonClickEvent += (sender, e) =>
{
var form = new UserCenter.GatewayAdd.NewGateWayMenuSelectForm();
form.AddForm();
};
var addTip = new PicViewControl(717, 58);
addTip.Y = Application.GetRealHeight(1256);
addTip.Gravity = Gravity.CenterHorizontal;
addTip.TextID = R.MyInternationalizationString.AddSmartGW;
addTip.TextColor = ZigbeeColor.Current.GXCTextBlackColor;
addTip.TextAlignment = TextAlignment.Center;
AddChidren(addTip);
addTip.ButtonClickEvent += (sender, e) =>
{
var form = new UserCenter.GatewayAdd.NewGateWayMenuSelectForm();
form.AddForm();
};
}
#endregion
#region ■ 功能分支___________________________
///
/// 显示功能
///
private void ShowFunction()
{
//先清空缓存
bodyFrameLayout.RemoveAll();
this.dicDeviceCardControl.Clear();
//当前房间的设备数
if (HdlRoomLogic.Current.CurrentRoom.ListDevice.Count == 0)
{
ShowNoFunctionTip();
return;
}
//列表控件
var listView = new VerticalFrameControl();
listView.Height = bodyFrameLayout.Height;
bodyFrameLayout.AddChidren(listView);
HdlThreadLogic.Current.RunThread(() =>
{
//初始化设备卡片列表控件
this.InitDeviceListCardControl(listView);
});
}
///
/// 初始化设备卡片列表控件
///
///
private void InitDeviceListCardControl(VerticalFrameControl listView)
{
var listDevice = new List();
foreach (var mainkeys in HdlRoomLogic.Current.CurrentRoom.ListDevice)
{
var device = LocalDevice.Current.GetDevice(mainkeys);
if (device == null)
{
//这个设备不见了
continue;
}
listDevice.Add(device);
}
//设备计数
int contrCount = 0;
//X轴坐标(图片左边有余白)
int XX = Application.GetMinRealAverage(44);
//Y轴坐标
int YY = 0;
foreach (var device in listDevice)
{
if (listView.Parent == null)
{
return;
}
System.Threading.Thread.Sleep(20);
HdlThreadLogic.Current.RunMain(() =>
{
Controls.DeviceCardCommon cardContr = null;
//窗帘
if (device.Type == DeviceType.WindowCoveringDevice)
{
cardContr = new Controls.DeviceCurtainCardControl();
}
//继电器
else if (device.Type == DeviceType.OnOffOutput)
{
cardContr = new Controls.DeviceRelayCardControl();
}
//空气开关
else if (device.Type == DeviceType.AirSwitch)
{
cardContr = new Controls.DeviceAirSwitchCardControl();
}
//空调
else if (device.Type == DeviceType.Thermostat)
{
cardContr = new Controls.DeviceAcCardControl();
}
//彩灯(调光器)
else if (device.Type == DeviceType.DimmableLight)
{
cardContr = new Controls.DeviceColorLightCardControl();
}
//传感器
else if (device.Type == DeviceType.IASZone)
{
cardContr = new Controls.DeviceSensorCardControl();
}
//温湿度
else if (device.Type == DeviceType.TemperatureSensor)
{
cardContr = new Controls.DeviceTemperatureCardControl();
}
//门锁
else if (device.Type == DeviceType.DoorLock)
{
cardContr = new Controls.DeviceDoorLockCardControl();
}
//无法识别
else
{
cardContr = new Controls.DeviceCardCommon();
}
//初始化卡片
cardContr.X = XX;
cardContr.Y = YY;
listView.frameTable.AddChidren(cardContr);
cardContr.InitControl(device);
//卡片需要被移除的事件
cardContr.CardNeedRemoveEvent += () =>
{
//重置设备卡片控件坐标(卡片需要被删除专用)
this.ResetDeviceCardControlLocation(LocalDevice.Current.GetDeviceMainKeys(device));
};
//设备计数
contrCount++;
//卡片左右两边有14的余白,每两个后,X轴重置
XX = contrCount % 2 == 0 ? Application.GetMinRealAverage(44) : cardContr.Right + Application.GetMinRealAverage(20);
if (contrCount % 2 == 0)
{
//没两个之后,Y轴递增
YY = cardContr.Bottom + Application.GetMinRealAverage(15);
}
//控件记录到缓存中
this.dicDeviceCardControl[LocalDevice.Current.GetDeviceMainKeys(device)] = cardContr;
//发送获取状态的命令
cardContr.SendStatuComand(device);
if (contrCount == listDevice.Count)
{
//调整桌布大小
listView.AdjustTableHeight();
}
});
}
}
///
/// 显示没有功能
///
private void ShowNoFunctionTip()
{
var noFunction = new Button
{
Y = Application.GetRealHeight(69),
Width = Application.GetMinRealAverage(683),
Height = Application.GetMinRealAverage(392),
Gravity = Gravity.CenterHorizontal,
UnSelectedImagePath = "Item/NoFunction.png"
};
bodyFrameLayout.AddChidren(noFunction);
var noFunctionTip = new Button()
{
Y = noFunction.Bottom,
Height = Application.GetRealHeight(200),
Text = Language.StringByID(R.MyInternationalizationString.NoFunction_Tip).Replace("{\\r\\n}", "\r\n"),
TextColor = ZigbeeColor.Current.GXCPlaceHolderTextColor,
TextAlignment = TextAlignment.Center,
IsMoreLines = true
};
bodyFrameLayout.AddChidren(noFunctionTip);
}
#endregion
#region ■ 重置设备卡片控件坐标_______________
///
/// 重置设备卡片控件坐标(卡片需要被删除专用)
///
/// 需要被删除的卡片主键
private void ResetDeviceCardControlLocation(string deleteKey)
{
if (this.dicDeviceCardControl.ContainsKey(deleteKey) == false)
{
//如果列表里面没有这个控件的话
return;
}
//设备计数
int contrCount = 0;
bool canMove = false;
foreach (var mainkey in this.dicDeviceCardControl.Keys)
{
if (deleteKey == mainkey)
{
//如果已经到了要删除的卡片,删除掉它,然后它之后的卡片全部向上移动
this.dicDeviceCardControl[mainkey].RemoveFromParent();
canMove = true;
continue;
}
//设备计数
contrCount++;
//卡片左右两边有14的余白,每两个后,X轴重置
int XX = 0;
if (contrCount % 2 == 1)
{
//第一个固定44
XX = Application.GetMinRealAverage(44);
}
else
{
//第二个是44+控件宽度+20余白
XX = Application.GetMinRealAverage(44) + this.dicDeviceCardControl[mainkey].Width + Application.GetMinRealAverage(20);
}
//没两个之后,Y轴递增
int YY = 0;
if (contrCount > 2)
{
//求商
int value = contrCount / 2;
//控件的底部有15的间隔
YY = value * (this.dicDeviceCardControl[mainkey].Height + Application.GetMinRealAverage(15));
}
if (canMove == true)
{
//移动控件
this.dicDeviceCardControl[mainkey].X = XX;
this.dicDeviceCardControl[mainkey].Y = YY;
}
}
//删除掉那张卡片的主键
this.dicDeviceCardControl.Remove(deleteKey);
}
#endregion
#region ■ 场景分支___________________________
///
/// 显示场景
///
private void ShowScene()
{
//先清空缓存
bodyFrameLayout.RemoveAll();
this.dicSceneCardControl.Clear();
if (HdlRoomLogic.Current.CurrentRoom.ListSceneId.Count == 0)
{
//显示没场景
this.ShowNoSceneTip();
return;
}
//列表控件
var listView = new VerticalFrameControl();
listView.Height = bodyFrameLayout.Height;
bodyFrameLayout.AddChidren(listView);
HdlThreadLogic.Current.RunThread(() =>
{
//初始化设备卡片列表控件
this.InitSceneListCardControl(listView);
});
}
///
/// 初始化场景卡片列表控件
///
///
private void InitSceneListCardControl(VerticalFrameControl listView)
{
var listScene = new List();
foreach (int sceneId in HdlRoomLogic.Current.CurrentRoom.ListSceneId)
{
var sceneUi = HdlSceneLogic.Current.GetSceneUIBySceneId(sceneId);
if (sceneUi == null)
{
//这个场景不见了
continue;
}
listScene.Add(sceneUi);
}
//场景计数
int contrCount = 0;
//X轴坐标(图片左边有余白)
int XX = Application.GetMinRealAverage(44);
//Y轴坐标
int YY = 0;
foreach (var sceneUi in listScene)
{
if (listView.Parent == null)
{
return;
}
System.Threading.Thread.Sleep(20);
HdlThreadLogic.Current.RunMain(() =>
{
//初始化卡片
var cardContr = new Controls.SceneCardControl();
cardContr.X = XX;
cardContr.Y = YY;
listView.frameTable.AddChidren(cardContr);
cardContr.InitControl(sceneUi);
//卡片需要被移除的事件
cardContr.CardNeedRemoveEvent += () =>
{
//重置设备卡片控件坐标(卡片需要被删除专用)
this.ResetSceneCardControlLocation(sceneUi.Id);
};
//设备计数
contrCount++;
//每两个后,X轴重置
XX = contrCount % 2 == 0 ? Application.GetMinRealAverage(44) : cardContr.Right + Application.GetMinRealAverage(20);
if (contrCount % 2 == 0)
{
//没两个之后,Y轴递增
YY = cardContr.Bottom + Application.GetMinRealAverage(15);
}
//控件记录到缓存中
this.dicSceneCardControl[sceneUi.Id] = cardContr;
if (contrCount == listScene.Count)
{
//调整桌布高度
listView.AdjustTableHeight();
//设置目前场景的延迟状态
this.SetSceneDelayTime(listView);
}
});
}
}
///
/// 显示没场景
///
private void ShowNoSceneTip()
{
var noScene = new Button
{
Y = Application.GetRealHeight(69),
Width = Application.GetMinRealAverage(683),
Height = Application.GetMinRealAverage(392),
Gravity = Gravity.CenterHorizontal,
UnSelectedImagePath = "Item/NoFunction.png"
};
bodyFrameLayout.AddChidren(noScene);
var noScenceTip = new Button()
{
Y = noScene.Bottom,
Height = Application.GetRealHeight(200),
Text = Language.StringByID(R.MyInternationalizationString.NoScene_Tip).Replace("{\\r\\n}", "\r\n"),
TextColor = ZigbeeColor.Current.GXCPlaceHolderTextColor,
TextAlignment = TextAlignment.Center,
IsMoreLines = true
};
bodyFrameLayout.AddChidren(noScenceTip);
}
///
/// 设置场景的延迟状态
///
///
private async void SetSceneDelayTime(VerticalFrameControl listView)
{
await System.Threading.Tasks.Task.Delay(1500);
//等待1.5秒之后,获取延迟状态
var result = await Scene.CatDelaySceneAsync();
if (result == null || result.catDelaySceneResponseData == null)
{
return;
}
if (listView.Parent == null)
{
//控件已经被移除
return;
}
foreach (var data in result.catDelaySceneResponseData.DelayScenesList)
{
var sceneUi = HdlSceneLogic.Current.GetSceneUIBySceneId(data.ScenesId);
if (sceneUi == null)
{
continue;
}
if (this.dicSceneCardControl.ContainsKey(data.ScenesId) == false)
{
//目前主页上还没有这个场景
continue;
}
//修改剩余延迟时间
sceneUi.RemainTime = data.RemainTime;
HdlThreadLogic.Current.RunMain(() =>
{
//刷新卡片信息
this.dicSceneCardControl[data.ScenesId].RefreshControlInfo(sceneUi);
});
}
}
#endregion
#region ■ 重置场景卡片控件坐标_______________
///
/// 重置场景卡片控件坐标(卡片需要被删除专用)
///
/// 需要被删除的卡片主键
private void ResetSceneCardControlLocation(int deleteId)
{
if (this.dicSceneCardControl.ContainsKey(deleteId) == false)
{
//如果列表里面没有这个控件的话
return;
}
//设备计数
int contrCount = 0;
bool canMove = false;
foreach (var sceneId in this.dicSceneCardControl.Keys)
{
if (deleteId == sceneId)
{
//如果已经到了要删除的卡片,删除掉它,然后它之后的卡片全部向上移动
this.dicSceneCardControl[sceneId].RemoveFromParent();
canMove = true;
continue;
}
//设备计数
contrCount++;
//每两个后,X轴重置
int XX = 0;
if (contrCount % 2 == 1)
{
//第一个固定44
XX = Application.GetMinRealAverage(44);
}
else
{
//第二个是44+控件宽度+20余白
XX = Application.GetMinRealAverage(44) + this.dicSceneCardControl[sceneId].Width + Application.GetMinRealAverage(20);
}
//没两个之后,Y轴递增
int YY = 0;
if (contrCount > 2)
{
//求商
int value = contrCount / 2;
//控件的底部有15的间隔
YY = value * (this.dicSceneCardControl[sceneId].Height + Application.GetMinRealAverage(15));
}
if (canMove == true)
{
//移动控件
this.dicSceneCardControl[sceneId].X = XX;
this.dicSceneCardControl[sceneId].Y = YY;
}
}
//删除掉那张卡片的主键
this.dicSceneCardControl.Remove(deleteId);
}
#endregion
#region ■ 刷新_______________________________
///
/// 刷新设备桌布控件
///
public void RefreshBodyView()
{
bodyFrameLayout.RemoveAll();
if (this.NowSelectIndex == 2)
{
ShowFunction();
}
else
{
ShowScene();
}
}
#endregion
#region ■ 切换住宅___________________________
///
/// 切换住宅
///
/// Home.
private void ChangeResidence(House home)
{
try
{
CommonPage.Loading.Start();
new System.Threading.Thread(async () =>
{
Config.Instance.HomeId = home.Id;
Config.Instance.Home = House.GetHouseByFilePath(home.FileName);
Global.CreateHomeDirectory(home.Id);
Config.Instance.Save();
//刷新个人中心的内存及线程
await UserCenter.UserCenterLogic.InitUserCenterMenmoryAndThread();
//初始化全部房间
HdlRoomLogic.Current.InitAllRoom();
Application.RunOnMainThread(() =>
{
ShowForm();
CommonPage.Loading.Hide();
});
})
{ IsBackground = true }.Start();
}
catch (Exception ex)
{
Application.RunOnMainThread(() =>
{
CommonPage.Loading.Hide();
Console.WriteLine(ex.Message);
});
}
}
#endregion
#region ■ 一般设备状态上报___________________
///
/// 添加一般设备状态上报事件
///
private void AddNormalDeviceReportEvent()
{
//设备属性上报
HdlGatewayReceiveLogic.Current.AddAttributeEvent("UserHomeViewDeviceStatus", ReceiveComandDiv.A设备属性上报, (report) =>
{
HdlThreadLogic.Current.RunMain(() =>
{
//处理一般设备的上报数据
this.AdjustNormalDeviceReportData(report);
}, ShowErrorMode.NO);
});
//设备在线上报
HdlGatewayReceiveLogic.Current.AddAttributeEvent("UserHomeViewDeviceOnline", ReceiveComandDiv.A设备在线上报, (report) =>
{
HdlThreadLogic.Current.RunMain(() =>
{
string mainKeys = LocalDevice.Current.GetDeviceMainKeys(report);
if (this.dicDeviceCardControl.ContainsKey(mainKeys) == false)
{
//当前主页没有这个东西
return;
}
//设备卡片
var deviceCardContr = this.dicDeviceCardControl[mainKeys];
var localDevice = LocalDevice.Current.GetDevice(mainKeys);
localDevice.IsOnline = report.IsOnline;
//刷新卡片状态
deviceCardContr.SetCardStatu(localDevice.IsOnline == 1);
}, ShowErrorMode.NO);
});
//设备控制反馈上报
HdlGatewayReceiveLogic.Current.AddAttributeEvent("UserHomeViewDeviceRespone", ReceiveComandDiv.A节点控制反馈, (report) =>
{
HdlThreadLogic.Current.RunMain(() =>
{
string mainKeys = LocalDevice.Current.GetDeviceMainKeys(report);
if (this.dicDeviceCardControl.ContainsKey(mainKeys) == false)
{
//当前主页没有这个东西
return;
}
//设备卡片
var deviceCardContr = this.dicDeviceCardControl[mainKeys];
//已经接收到网关的反馈
deviceCardContr.SetHadGetResponeResultStatu();
}, ShowErrorMode.NO);
});
}
///
/// 处理一般设备的上报数据
///
///
private void AdjustNormalDeviceReportData(CommonDevice report)
{
string mainKeys = LocalDevice.Current.GetDeviceMainKeys(report);
if (this.dicDeviceCardControl.ContainsKey(mainKeys) == false)
{
//当前主页没有这个东西
return;
}
//设备卡片
var deviceCardContr = this.dicDeviceCardControl[mainKeys];
//本地设备对象
var locadevice = LocalDevice.Current.GetDevice(mainKeys);
//有反馈,这个设备肯定是在线的
locadevice.IsOnline = 1;
locadevice.LastDateTime = DateTime.Now;
#region ■ 开关功能
//开关功能
if (report.DeviceStatusReport.CluterID == 6)
{
locadevice.DeviceStatusReport = report.DeviceStatusReport;
((LightBase)locadevice).OnOffStatus = report.DeviceStatusReport.AttriBute[0].AttriButeData;
//刷新卡片信息
deviceCardContr.RefreshControlInfo(locadevice);
}
#endregion
#region ■ 窗帘数据
//窗帘数据
else if (report.DeviceStatusReport.CluterID == 258)
{
//窗帘类型
if (report.DeviceStatusReport.AttriBute[0].AttributeId == 0)
{
locadevice.DeviceStatusReport = report.DeviceStatusReport;
((Rollershade)report).WcdType = report.DeviceStatusReport.AttriBute[0].AttriButeData;
//刷新卡片信息
deviceCardContr.RefreshControlInfo(locadevice);
}
//窗帘百分比
else if (report.DeviceStatusReport.AttriBute[0].AttributeId == 8)
{
locadevice.DeviceStatusReport = report.DeviceStatusReport;
((Rollershade)report).WcdCurrentPositionLiftPercentage = report.DeviceStatusReport.AttriBute[0].AttriButeData;
//刷新卡片信息
deviceCardContr.RefreshControlInfo(locadevice);
}
}
#endregion
#region ■ 空调数据
//空调数据
else if (report.DeviceStatusReport.CluterID == 513)
{
locadevice.DeviceStatusReport = report.DeviceStatusReport;
foreach (var attData in report.DeviceStatusReport.AttriBute)
{
var curTemp = attData.AttriButeData / 100;
if (attData.AttributeId == 0)
{
//此属性表明室内当前的温度 * 100,实际温度为“LocalTemperature / 100”,单位:℃
((AC)locadevice).currentLocalTemperature = curTemp;
}
else if (attData.AttributeId == 17)
{
//此属性表明室内当前的温度 * 100,实际温度为“LocalTemperature / 100”,单位:℃
((AC)locadevice).currentCoolingSetpoint = curTemp;
}
else if (attData.AttributeId == 18)
{
//此属性表明此设备当前的制热温度,实际温度为“HeatingSetpoint / 100”,单位:℃。
((AC)locadevice).currentHeatingSetpoint = curTemp;
}
else if (attData.AttributeId == 28)
{
//此属性描述恒温设备正处于哪种模式
((AC)locadevice).currentSystemMode = attData.AttriButeData;
}
else if (attData.AttributeId == 4096)
{
//此属性表明此设备当前的自动温度,实际温度为“AutoSetpoint / 100”,单位:℃。
((AC)locadevice).currentAutoSetpoint = curTemp;
}
}
//刷新卡片信息
deviceCardContr.RefreshControlInfo(locadevice);
}
//空调数据
else if (report.DeviceStatusReport.CluterID == 514)
{
locadevice.DeviceStatusReport = report.DeviceStatusReport;
foreach (var attData in report.DeviceStatusReport.AttriBute)
{
if (attData.AttributeId == 0)
{
//风扇模式
((AC)locadevice).currentFanMode = attData.AttriButeData;
}
else if (attData.AttributeId == 4096)
{
//风扇扫风
((AC)locadevice).currentFanSwingMode = attData.AttriButeData;
}
}
//刷新卡片信息
deviceCardContr.RefreshControlInfo(locadevice);
}
#endregion
#region ■ 亮度数据
//亮度数据
else if (report.DeviceStatusReport.CluterID == 8)
{
locadevice.DeviceStatusReport = report.DeviceStatusReport;
if (report.DeviceStatusReport.AttriBute[0].AttributeId == 0)
{
//此属性表明当前亮度程度
((DimmableLight)locadevice).Level = report.DeviceStatusReport.AttriBute[0].AttriButeData;
//刷新卡片信息
deviceCardContr.RefreshControlInfo(locadevice);
}
}
#endregion
#region ■ 温度数据
//温度数据
else if (report.DeviceStatusReport.CluterID == 1026)
{
foreach (var attData in report.DeviceStatusReport.AttriBute)
{
//温度
if (attData.AttributeId == (int)AttriButeId.MeasuredValue)
{
if (attData.AttriButeData == 0)
{
((TemperatureSensor)locadevice).Temperatrue = 0;
}
else if (attData.AttriButeData > 32767)
{
//负数(特殊处理)
string strValue = (attData.AttriButeData - 65536).ToString();
//小数点需要一位
strValue = strValue.Substring(0, strValue.Length - 1);
((TemperatureSensor)locadevice).Temperatrue = Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
}
else
{
//小数点需要一位
string strValue = attData.AttriButeData.ToString();
strValue = strValue.Substring(0, strValue.Length - 1);
((TemperatureSensor)locadevice).Temperatrue = Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
}
//刷新卡片信息
deviceCardContr.RefreshControlInfo(locadevice);
}
}
}
#endregion
#region ■ 湿度数据
//湿度数据
else if (report.DeviceStatusReport.CluterID == 1029)
{
foreach (var attData in report.DeviceStatusReport.AttriBute)
{
//湿度
if (attData.AttributeId == (int)AttriButeId.MeasuredValue)
{
if (attData.AttriButeData == 0)
{
((TemperatureSensor)locadevice).Humidity = 0;
}
else
{
//小数点需要一位(湿度没有负数)
string strValue = attData.AttriButeData.ToString();
strValue = strValue.Substring(0, strValue.Length - 1);
((TemperatureSensor)locadevice).Humidity = Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
}
//刷新卡片信息
deviceCardContr.RefreshControlInfo(locadevice);
}
}
}
#endregion
}
#endregion
#region ■ 传感器状态上报_____________________
///
/// 添加传感器状态上报事件
///
private void AddSensorDeviceReportEvent()
{
//传感器上报
HdlGatewayReceiveLogic.Current.AddAttributeEvent("UserHomeViewSensor", ReceiveComandDiv.A传感器上报, (report) =>
{
HdlThreadLogic.Current.RunMain(() =>
{
//处理传感器上报数据
this.AdjustSensorDeviceReportData(report);
}, ShowErrorMode.NO);
});
}
///
/// 处理传感器上报数据
///
///
private void AdjustSensorDeviceReportData(CommonDevice report)
{
string mainKeys = LocalDevice.Current.GetDeviceMainKeys(report);
if (this.dicDeviceCardControl.ContainsKey(mainKeys) == false)
{
//当前主页没有这个东西
return;
}
//设备卡片
var deviceCardContr = this.dicDeviceCardControl[mainKeys];
//本地设备对象
var locadevice = LocalDevice.Current.GetDevice(mainKeys);
((IASZone)locadevice).iASInfo = ((IASZone)report).iASInfo;
//有反馈,这个设备肯定是在线的
locadevice.IsOnline = 1;
locadevice.LastDateTime = DateTime.Now;
//刷新卡片信息
deviceCardContr.RefreshControlInfo(locadevice);
}
#endregion
#region ■ 切换楼层___________________________
///
/// 显示选择楼层的界面
///
private void ShowSelectFloorForm(NormalViewControl btnFloor)
{
var floorFL = new Device.Category.SelectFloor();
this.AddChidren(floorFL);
floorFL.Init(35, 153);
floorFL.changeFloor = true;
floorFL.FloorAction = (floorId) =>
{
btnFloor.Text = Config.Instance.Home.GetFloorNameById(floorId);
HdlRoomLogic.Current.CurrentRoom = HdlRoomLogic.Current.GetLoveRoom();
this.ShowForm();
};
}
#endregion
#region ■ 界面关闭___________________________
///
/// 界面关闭
///
public override void CloseFormBefore()
{
HdlGatewayReceiveLogic.Current.RemoveEvent("UserHomeViewDeviceOnline");
HdlGatewayReceiveLogic.Current.RemoveEvent("UserHomeViewDeviceRespone");
HdlGatewayReceiveLogic.Current.RemoveEvent("UserHomeViewDeviceStatus");
HdlGatewayReceiveLogic.Current.RemoveEvent("UserHomeViewSensor");
base.CloseFormBefore();
}
#endregion
#region ■ 一般方法___________________________
///
/// 检测是否绑定有网关
///
private bool CheckHadBindGateway()
{
//获取本地已经绑定的网关
var gatewayList = HdlGatewayLogic.Current.GetAllLocalGateway();
if (gatewayList.Count == 0)
{
return false;
}
//主人或者管理员才行
if (UserCenterResourse.UserInfo.AuthorityNo == 1 || UserCenterResourse.UserInfo.AuthorityNo == 2)
{
return true;
}
return false;
}
#endregion
}
}