using Shared.SimpleControl.Phone;
using System;
using System.Collections.Generic;
namespace Shared.SimpleControl.Pad
{
///
/// 通过灯光设备类型,显示出所有房间的灯光
///
public class UserDeviceToLight : Shared.Dialog
{
///
/// 当前设备界面
///
static UserDeviceToLight curView;
VerticalScrolViewLayout RoomListScrolView;
UserLightPage ControlBodyView;
VerticalScrolViewLayout AllLightsView;
Button btnShowAll;
Button btnShowAllIcon;
///
/// 构造函数
///
public UserDeviceToLight ()
{
curView = this;
showAllRoomLights ();
readAllStatus ();
}
///
/// 更新当前灯光
///
public static void UpdateStatus ()
{
#region 更新用户主界面灯光点亮总数
int brighingCount = 0;
foreach (var room in Room.Lists) {
if (room.Name == "") {
continue;
}
foreach (var light in room.DeviceList) {
if (light.Type == DeviceType.LightDimming) {
if ((light as LightDimming).CurrentBrightness > 0)
brighingCount++;
} else if (light.Type == DeviceType.LightSwitch)
if ((light as LightSwitch).CurrentBrightness == 100)
brighingCount++;
if (light.Type == DeviceType.LightRGB)
if ((light as LightLogic).CurrentBrightness > 0)
brighingCount++;
}
}
UserMiddle.UpdataLightLightingCount (brighingCount, Language.StringByID (R.MyInternationalizationString.Lights));
#endregion
if (curView != null)
curView.updateLigthOnCount ();
}
///
/// 显示当前多少个灯是亮的
///
void updateLigthOnCount ()
{
Application.RunOnMainThread (() => {
if (curView == null)
return;
for (int i = 0; i < curView.RoomListScrolView.ChildrenCount; i++) {
if (curView.RoomListScrolView.GetChildren (i).GetType () == typeof (FrameLayout)) {
var roomRow = (FrameLayout)curView.RoomListScrolView.GetChildren (i);
if (roomRow.Tag != null) {
foreach (var room in Room.Lists) {
if (room.Name == "") {
continue;
}
if (roomRow.Tag.ToString () == room.Name) {
int oCount = 0;
foreach (var light in room.DeviceList) {
if (light.Type == DeviceType.LightDimming) {
if ((light as LightDimming).CurrentBrightness > 0)
oCount++;
} else if (light.Type == DeviceType.LightRGB) {
if ((light as LightLogic).CurrentBrightness > 0)
oCount++;
} else if (light.Type == DeviceType.LightSwitch) {
if ((light as LightSwitch).CurrentBrightness == 100)
oCount++;
}
}
for (int j = 0; j < roomRow.ChildrenCount; j++) {
if (roomRow.GetChildren (j).GetType () == typeof (EditText)) {
var et = (EditText)roomRow.GetChildren (j);
et.Text = oCount.ToString ();
if (oCount > 0) {
et.TextColor = SkinStyle.Current.SelectedColor;
} else {
et.TextColor = SkinStyle.Current.TextColor1;
et.Text = room.DeviceList.FindAll ((obj) => {
return obj.Type == DeviceType.LightRGB || obj.Type == DeviceType.LightDimming
|| obj.Type == DeviceType.LightSwitch;
}).Count.ToString ();
}
}
}
}
}
}
}
}
});
}
bool showTip = true;
///
/// 控制所有的灯
///
/// The blue component.
void controlAllLight (byte b)
{
showTip = true;
System.Threading.Tasks.Task.Run (() => {
Application.RunOnMainThread (() => {
MainPage.Loading.Start ("Sending...");
});
foreach (var room in Room.Lists) {
if (room == null)
continue;
if (room.Name == "") {
continue;
}
foreach (var common in room.DeviceList) {
byte [] returnBytes = null;
if (common.Type == DeviceType.LightDimming || common.Type == DeviceType.LightSwitch || common.Type == DeviceType.LightRGB) {
if (common.Type == DeviceType.LightDimming) {
var light = (LightDimming)common;
if (b == 100) {
if (light.LastOpenBrightness == 0) {
returnBytes = Control.ControlBytesSendHasReturn (Command.SetSingleLight, common.SubnetID, common.DeviceID, new byte [] { common.LoopID, b, light.DelayTimeHeight, light.DelayTimeLow });
} else
returnBytes = Control.ControlBytesSendHasReturn (Command.SetSingleLight, common.SubnetID, common.DeviceID, new byte [] { common.LoopID, ((LightDimming)common).LastOpenBrightness, light.DelayTimeHeight, light.DelayTimeLow });
} else
returnBytes = Control.ControlBytesSendHasReturn (Command.SetSingleLight, common.SubnetID, common.DeviceID, new byte [] { common.LoopID, b, light.DelayTimeHeight, light.DelayTimeLow });
} else if (common.Type == DeviceType.LightSwitch) {
returnBytes = Control.ControlBytesSendHasReturn (Command.SetSingleLight, common.SubnetID, common.DeviceID, new byte [] { common.LoopID, b, ((LightSwitch)common).DelayTimeHeight, ((LightSwitch)common).DelayTimeLow });
} else if (common.Type == DeviceType.LightRGB) {
var light = (LightLogic)common;
byte b2 = b;
if (b2 == 100) {
if (light.LastOpenBrightness != 0) {
b2 = light.LastOpenBrightness;
}
}
returnBytes = Control.ControlBytesSendHasReturn (Command.SetLogicLoopColor, common.SubnetID, common.DeviceID, new byte [] {
common.LoopID, b2, 254, light.CustomDelayTimeOpen,light.CustomDelayTimeClose,
3,light.RStatus,light.GStatus,light.BStatus,0,0});
}
if (showTip) {
Application.RunOnMainThread (() => {
if (returnBytes == null) {
string tipString = Language.StringByID (R.MyInternationalizationString.ControlFailure);
var btnTip = new Button () {
Height = Application.GetRealHeight (140),
Text = common.Name + tipString,// + "..." + controlDeviceIndex.ToString () + @"/" + sceneDeviceCount.ToString (),
BackgroundColor = 0xFF000000,
};
MainPage.MainFrameLayout.AddChidren (btnTip);
btnTip.MouseUpEventHandler += (sender, e) => {
MainPage.MainFrameLayout.Remove (btnTip);
showTip = false;
};
System.Threading.Tasks.Task.Run (() => {
System.Threading.Thread.Sleep (3000);
Application.RunOnMainThread (() => {
MainPage.MainFrameLayout.Remove (btnTip);
});
});
}
});
}
}
}
}
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
});
});
}
///
/// 通过设备类型方式选择设备
///
void showAllRoomLights ()
{
#region 标题
FrameLayout topBackView = new FrameLayout () {
Height = Application.GetRealHeight (150),
BackgroundColor = 0xFF2f2f2f
};
AddChidren (topBackView);
var topView = new FrameLayout () {
Y = Application.GetRealHeight (36),
Height = Application.GetRealHeight (90),
};
AddChidren (topView);
var title = new Button () {
TextAlignment = TextAlignment.Center,
Text = Language.StringByID (R.MyInternationalizationString.Lights),
TextSize = 21,
};
topView.AddChidren (title);
var logo = new Button () {
Width = Application.GetRealWidth (154),
Height = Application.GetRealHeight (90),
X = Application.GetRealWidth (486),
UnSelectedImagePath = MainPage.LogoString,
};
topView.AddChidren (logo);
var back = new Button () {
Height = Application.GetRealHeight (120),
Width = Application.GetRealWidth (95),
UnSelectedImagePath = "Item/Back.png",
SelectedImagePath = "Item/BackSelected.png",
Gravity = Gravity.CenterVertical,
};
topView.AddChidren (back);
back.MouseUpEventHandler += (sender, e) => {
Close ();
curView = null;
};
var btnl = new Button () {
Height = 1,
BackgroundColor = 0xFF2f2f2f,
Y = topView.Height - 1
};
topView.AddChidren (btnl);
#endregion
FrameLayout bordorView = new FrameLayout () {
Width = Application.GetRealWidth (480),
Height = Application.GetRealHeight (Application.DesignHeight - 150),
Y = Application.GetRealHeight (150),
BackgroundColor = 0xFF2f2f2f
};
AddChidren (bordorView);
#region 全开-全关
FrameLayout powerView = new FrameLayout () {
Height = Application.GetRealHeight (140),
Width = Application.GetRealWidth (480),
BackgroundColor = SkinStyle.Current.MainColor,
};
bordorView.AddChidren (powerView);
var btnAllON = new Button () {
Width = Application.GetRealWidth (240),
TextSize = 16,
TextID = R.MyInternationalizationString.ALLON,
SelectedTextColor = SkinStyle.Current.SelectedColor,
TextColor = SkinStyle.Current.TextColor1,
};
powerView.AddChidren (btnAllON);
var btnAllOff = new Button () {
Width = Application.GetRealWidth (240),
X = btnAllON.Right,
TextSize = 16,
TextID = R.MyInternationalizationString.ALLOFF,
SelectedTextColor = SkinStyle.Current.SelectedColor,
TextColor = SkinStyle.Current.TextColor1,
};
var btnLine = new Button () {
Width = 1,
BackgroundColor = 0xFF2f2f2f,
X = btnAllON.Right,
};
powerView.AddChidren (btnLine);
var btnLine1 = new Button () {
Height = 1,
Y = powerView.Height - 1,
BackgroundColor = 0xFF2f2f2f,
};
powerView.AddChidren (btnLine1);
btnAllON.MouseUpEventHandler += (sender, e) => {
btnAllON.TextColor = SkinStyle.Current.SelectedColor;
btnAllOff.TextColor = SkinStyle.Current.TextColor1;
Alert alert = new Alert (Language.StringByID (R.MyInternationalizationString.Tip),
Language.StringByID (R.MyInternationalizationString.SureOpenAllLight),
Language.StringByID (R.MyInternationalizationString.Cancel),
Language.StringByID (R.MyInternationalizationString.Confrim));
alert.Show ();
alert.ResultEventHandler += (sender2, e2) => {
if (e2) {
controlAllLight (100);
}
};
};
powerView.AddChidren (btnAllOff);
btnAllOff.MouseUpEventHandler += (sender, e) => {
btnAllON.TextColor = SkinStyle.Current.TextColor1;
btnAllOff.TextColor = SkinStyle.Current.SelectedColor;
Alert alert = new Alert (Language.StringByID (R.MyInternationalizationString.Tip),
Language.StringByID (R.MyInternationalizationString.SureCloseAllLight),
Language.StringByID (R.MyInternationalizationString.Cancel),
Language.StringByID (R.MyInternationalizationString.Confrim));
alert.Show ();
alert.ResultEventHandler += (sender2, e2) => {
if (e2) {
controlAllLight (0);
}
};
};
#endregion
RoomListScrolView = new VerticalScrolViewLayout () {
Width = LayoutParams.MatchParent,
Height = Application.GetRealHeight (Application.DesignHeight - 300 - 140),
Y = powerView.Bottom,
};
bordorView.AddChidren (RoomListScrolView);
//显示有多少个房间,每个房间有多少个灯开
int devcieCount = 0;
foreach (var room in Room.Lists) {
if (room.Name == "") {
continue;
}
if (null == room.DeviceList.Find ((obj) => { return obj.Type == DeviceType.LightDimming || obj.Type == DeviceType.LightSwitch || obj.Type == DeviceType.LightRGB; })) {
continue;
}
EventHandler ShowLightRoom = (button, mouseEventArgs) => {
btnShowAll.IsSelected = btnShowAllIcon.IsSelected = false;
for (int i = 0; i < RoomListScrolView.ChildrenCount; i++) {
var view = RoomListScrolView.GetChildren (i);
if (view.Tag.ToString () == room.Name) {
for (int j = 0; j < (view as FrameLayout).ChildrenCount; j++) {
var btn = (view as FrameLayout).GetChildren (j);
if (btn.GetType () == typeof (Button))
(btn as Button).IsSelected = true;
}
} else {
for (int j = 0; j < (view as FrameLayout).ChildrenCount; j++) {
var btn = (view as FrameLayout).GetChildren (j);
if (btn.GetType () == typeof (Button))
(btn as Button).IsSelected = false;
}
}
}
if (ControlBodyView != null) {
ControlBodyView.RemoveFromParent ();
}
if (AllLightsView != null)
AllLightsView.RemoveFromParent ();
ControlBodyView = new UserLightPage (room) {
Width = Application.GetRealWidth (Application.DesignWidth) - bordorView.Width,
Height = Application.GetRealHeight (Application.DesignHeight - 150),
X = bordorView.Right,
Y = bordorView.Y,
BackgroundColor = SkinStyle.Current.MainColor
};
AddChidren (ControlBodyView);
ControlBodyView.showLightsRoom (room);
};
FrameLayout roomView = new FrameLayout () {
Height = Application.GetRealHeight (140),
Tag = room.Name
};
RoomListScrolView.AddChidren (roomView);
Button tempDeviceName = new Button () {
Height = Application.GetRealHeight (140),
TextAlignment = TextAlignment.CenterLeft,
SelectedTextColor = SkinStyle.Current.SelectedColor,
TextColor = SkinStyle.Current.TextColor1,
BackgroundColor = SkinStyle.Current.Transparent,
SelectedBackgroundColor = SkinStyle.Current.MainColor,
Padding = new Padding (0, 35, 0, 0),
Text = room.Name,
};
roomView.AddChidren (tempDeviceName);
tempDeviceName.MouseUpEventHandler += ShowLightRoom;
Button btnPoint = new Button () {
Width = Application.GetRealWidth (15),
Height = Application.GetRealHeight (15),
X = Application.GetRealWidth (45),
Gravity = Gravity.CenterVertical,
UnSelectedImagePath = "Item/Point.png",
SelectedImagePath = "Item/PointSelected.png",
};
roomView.AddChidren (btnPoint);
EditText btnNumber = new EditText () {
Width = Application.GetMinRealAverage (50),
Height = Application.GetMinRealAverage (35),
X = bordorView.Width - Application.GetRealWidth (70),
Gravity = Gravity.CenterVertical,
UnSelectedImagePath = "Item/LightingCount.png",
SelectedImagePath = "Item/LightingCount.png",
Enable = false,
Text = "0",
TextColor = SkinStyle.Current.TextColor1,
//SelectedTextColor = SkinStyle.Current.SelectedBackgroundColor,
TextAlignment = TextAlignment.Center,
Tag = room.Name
};
roomView.AddChidren (btnNumber);
Button btnLightIcon = new Button () {
Width = Application.GetRealWidth (100),
Height = Application.GetRealHeight (80),
Gravity = Gravity.CenterVertical,
UnSelectedImagePath = "Item/RH_Lights.png",
SelectedImagePath = "Item/RH_LightsSelected.png",
X = btnNumber.Right
};
roomView.AddChidren (btnLightIcon);
Button btnNull = new Button () {
Y = Application.GetRealHeight (138),
Height = Application.GetRealHeight (5),
BackgroundColor = SkinStyle.Current.MainColor,
};
roomView.AddChidren (btnNull);
Button btnNull2 = new Button () {
Width = 1,
X = roomView.Width - 1,
BackgroundColor = 0xFF2f2f2f,
};
roomView.AddChidren (btnNull2);
Button btnSelected = new Button () {
X = roomView.Width - 5,
Width = 5,
SelectedBackgroundColor = SkinStyle.Current.SelectedColor,
BackgroundColor = SkinStyle.Current.Transparent,
};
roomView.AddChidren (btnSelected);
if (devcieCount == 0) {
tempDeviceName.IsSelected = true;
btnPoint.IsSelected = true;
ControlBodyView = new UserLightPage (room) {
Width = Application.GetRealWidth (Application.DesignWidth) - bordorView.Width,
Height = Application.GetRealHeight (Application.DesignHeight - 150),
X = bordorView.Right,
Y = bordorView.Y,
};
AddChidren (ControlBodyView);
ControlBodyView.showLightsRoom (room);
btnSelected.IsSelected = true;
tempDeviceName.IsSelected = true;
btnSelected.IsSelected = true;
}
devcieCount++;
}
updateLigthOnCount ();
#region 显示全部的灯光
FrameLayout ShowAllView = new FrameLayout () {
Y = bordorView.Height - Application.GetRealHeight (150),
Height = Application.GetRealHeight (150),
BackgroundColor = SkinStyle.Current.MainColor,
};
bordorView.AddChidren (ShowAllView);
btnShowAllIcon = new Button () {
Width = Application.GetRealWidth (130),
UnSelectedImagePath = "Light/AllLight.png",
SelectedImagePath = "Light/AllLightOn.png",
};
ShowAllView.AddChidren (btnShowAllIcon);
btnShowAll = new Button () {
TextAlignment = TextAlignment.Center,
TextColor = SkinStyle.Current.TextColor1,
SelectedTextColor = SkinStyle.Current.SelectedColor,
TextSize = 18,
Text = Language.StringByID (R.MyInternationalizationString.AllLight),
};
ShowAllView.AddChidren (btnShowAll);
btnShowAll.MouseUpEventHandler += (sender, e) => {
for (int i = 0; i < RoomListScrolView.ChildrenCount; i++) {
var view = RoomListScrolView.GetChildren (i);
for (int j = 0; j < (view as FrameLayout).ChildrenCount; j++) {
var btn = (view as FrameLayout).GetChildren (j);
if (btn.GetType () == typeof (Button))
(btn as Button).IsSelected = false;
}
}
btnShowAll.IsSelected = btnShowAllIcon.IsSelected = true;
if (ControlBodyView != null)
ControlBodyView.RemoveFromParent ();
AllLightsView = new VerticalScrolViewLayout () {
Width = Application.GetRealWidth (Application.DesignWidth) - bordorView.Width,
Height = Application.GetRealHeight (Application.DesignHeight - 150),
X = bordorView.Right,
Y = bordorView.Y,
BackgroundColor = SkinStyle.Current.MainColor
};
AddChidren (AllLightsView);
int lightCount = 0;
foreach (var room in Room.Lists)
UserLightPage.addLights (room, lightCount, AllLightsView);
};
#endregion
}
///
/// 读取设备状态
///
public static void readAllStatus ()
{
System.Threading.Tasks.Task.Run (() => {
System.Collections.Generic.List readList = new System.Collections.Generic.List ();
foreach (Room room in Room.Lists) {
var list = room.DeviceList.FindAll ((obj) => {
return obj.LastUpdateTime.AddMinutes (Common.Time) <= DateTime.Now && (obj.Type == DeviceType.LightDimming || obj.Type == DeviceType.LightSwitch || obj.Type == DeviceType.LightLogic
|| obj.Type == DeviceType.FanModule);
});
foreach (Common common in list) {
var s = common.Type + "_" + common.SubnetID + "_" + common.DeviceID;
//已经读取过当前设备就不再读取
if (readList.Contains (s)) {
continue;
}
readList.Add (s);
Control.ControlBytesSendHasReturn (Command.ReadLightEquipmentAllLoopBrightness, common.SubnetID, common.DeviceID, new byte [] { });
}
var listGRB = room.DeviceList.FindAll ((obj) => {
return obj.LastUpdateTime.AddMinutes (Common.Time) <= DateTime.Now && (obj.Type == DeviceType.LightRGB);
});
foreach (Common common in listGRB) {
var s = common.Type + "_" + common.SubnetID + "_" + common.DeviceID;
//已经读取过当前设备就不再读取
if (readList.Contains (s)) {
continue;
}
readList.Add (s);
Control.ControlBytesSendHasReturn (Command.ReadLogicLoopColor, common.SubnetID, common.DeviceID, new byte [] { common.LoopID });
}
}
});
}
public static void UpdataEnergy (string updataFlag, int energy)
{
Application.RunOnMainThread (() => {
if (curView == null || curView.AllLightsView == null) {
return;
}
for (int i = 0; i < curView.AllLightsView.ChildrenCount; i++) {
if (curView.AllLightsView.GetChildren (i).GetType () == typeof (HorizontalScrolViewLayout)) {
var rowView = (HorizontalScrolViewLayout)curView.AllLightsView.GetChildren (i);
for (int k = 0; k < rowView.ChildrenCount; k++) {
if (rowView.GetChildren (k).GetType () == typeof (RowLayout)) {
var frameLayout = (RowLayout)rowView.GetChildren (k);
var o = frameLayout.GetTagByKey ("UpdataKey");
if (o != null && o.ToString () == updataFlag) {
for (int j = 0; j < frameLayout.ChildrenCount; j++) {
var childView = frameLayout.GetChildren (j);
if (childView.GetType () == typeof (Button)) {
Button btn = (Button)childView;
btn.Text = energy.ToString () + " W";
}
}
}
}
}
}
}
});
}
///
/// 接受到灯光发出的数据之后更新UI
///
public static void UpdateAllLightsStatus (string updataFlag, byte brightness)
{
Application.RunOnMainThread (() => {
if (curView == null || curView.AllLightsView == null) {
return;
}
for (int i = 0; i < curView.AllLightsView.ChildrenCount; i++) {
if (curView.AllLightsView.GetChildren (i).GetType () == typeof (HorizontalScrolViewLayout)) {
var rowView = (HorizontalScrolViewLayout)curView.AllLightsView.GetChildren (i);
for (int k = 0; k < rowView.ChildrenCount; k++) {
if (rowView.GetChildren (k).GetType () == typeof (RowLayout)) {
var frameLayout = (RowLayout)rowView.GetChildren (k);
var o = frameLayout.GetTagByKey ("UpdataKey");
if (o != null && o.ToString () == updataFlag) {
for (int j = 0; j < frameLayout.ChildrenCount; j++) {
var childView = frameLayout.GetChildren (j);
if (childView.GetType () == typeof (Button)) {
Button btn = (Button)childView;
if (brightness == 0) {
btn.IsSelected = false;
} else {
btn.IsSelected = true;
}
} else if (childView.GetType () == typeof (HorizontalSeekBar) && 3 < (DateTime.Now - (DateTime)childView.Tag).TotalSeconds) {
(childView as HorizontalSeekBar).Progress = brightness;
}
}
}
}
}
}
}
});
}
}
}