using Shared.SimpleControl.Phone;
using System;
using System.Collections.Generic;
using Shared.SimpleControl;
namespace Shared.SimpleControl.Pad
{
public class UserCurtainPage : FrameLayout
{
///
/// 当前打开的窗帘界面
///
public static UserCurtainPage curView;
FrameLayout ProgressControlView;
FrameLayout roomBackgroundImageView;
Common curtain;
Button btnCloseCurtain;
Button btnOpenCurtain;
Button btnStopCurtain;
Button btnCurrentStatus;
HorizontalSeekBar horizontalSeekBar;
///
/// 显示当前房间窗帘设备
///
/// Room.
public UserCurtainPage (Common curtain, Room room)
{
this.curtain = curtain;
curView = this;
BackgroundColor = 0xFF2f2f2f;
statrtReadProgress ();
showCurtainsRoom (curtain, room);
}
//窗帘停止回复的命令没有必要调用UpdateStatus
///
/// 接受到窗帘发出的数据之后更新UI
///
/// The user lights.
public static void UpdateStatus (Common curtain)
{
Application.RunOnMainThread (() => {
if (curView == null || curView.curtain != curtain) {
return;
}
if (curView.curtain.Type == DeviceType.CurtainTrietex) {
var curtainTrietex = curView.curtain as CurtainTrietex;
//不在模拟进度时
if (curView.th == null) {
curView.btnCurrentStatus.Text = Language.StringByID(R.MyInternationalizationString.CurrentStatus) +" : "+ curtainTrietex.CurtainProress.ToString () + "%";
curView.horizontalSeekBar.Progress = curtainTrietex.CurtainProress;
}
} else if (curView.curtain.Type == DeviceType.CurtainRoller) {
var curtainRoller = curView.curtain as CurtainRoller;
//不在模拟进度时
if (curView.th == null) {
curView.btnCurrentStatus.Text = Language.StringByID(R.MyInternationalizationString.CurrentStatus) +" : "+ curtainRoller.CurtainProress.ToString () + "%";
curView.horizontalSeekBar.Progress = curtainRoller.CurtainProress;
}
}
});
}
///
/// 房间模式进入的窗帘界面
///
/// The lights room.
public void showCurtainsRoom (Common curtain, Room room)
{
roomBackgroundImageView = new FrameLayout () {
X = Application.GetRealWidth (10),
Y = Application.GetRealHeight (10),
Width = Application.GetRealWidth (920),
Height = Application.GetRealHeight (520),
};
AddChidren (roomBackgroundImageView);
roomBackgroundImageView.BackgroundImagePath = room.BackGroundImage;
roomBackgroundImageView.MouseLongEventHandler += (sender, e) => {
Camera.SelectPicture ((obj) => {
if (obj != null) {
room.BackGroundImage = obj;
roomBackgroundImageView.BackgroundImagePath = obj;
room.Save (typeof (Room).Name + "_" + room.Name);
}
}, room.Name);
};
#region 开关停
FrameLayout ControlView = new FrameLayout () {
X = roomBackgroundImageView.Right + Application.GetRealWidth (10),
Y = roomBackgroundImageView.Y,
Width = Application.GetRealWidth (600),
Height = Application.GetRealHeight (680),
};
AddChidren (ControlView);
btnOpenCurtain = new Button () {
Gravity = Gravity.CenterHorizontal,
Y = Application.GetRealHeight (10),
Width = Application.GetRealWidth (270),
Height = Application.GetRealHeight (150),
UnSelectedImagePath = "PadAdding/Black.png",
SelectedImagePath = "PadAdding/ModeBlackOn.png",
SelectedTextColor = SkinStyle.Current.SelectedColor,
TextColor = SkinStyle.Current.TextColor1,
TextID = R.MyInternationalizationString.Open,
};
ControlView.AddChidren (btnOpenCurtain);
btnStopCurtain = new Button () {
X = btnOpenCurtain.X,
Y = btnOpenCurtain.Bottom + Application.GetRealHeight (20),
Width = Application.GetRealWidth (270),
Height = Application.GetRealHeight (150),
UnSelectedImagePath = "PadAdding/Black.png",
SelectedImagePath = "PadAdding/ModeBlackOn.png",
SelectedTextColor = SkinStyle.Current.SelectedColor,
TextColor = SkinStyle.Current.TextColor1,
TextID = R.MyInternationalizationString.Stop,
};
ControlView.AddChidren (btnStopCurtain);
btnCloseCurtain = new Button () {
X = btnOpenCurtain.X,
Y = btnStopCurtain.Bottom + Application.GetRealHeight (20),
Width = Application.GetRealWidth (270),
Height = Application.GetRealHeight (150),
UnSelectedImagePath = "PadAdding/Black.png",
SelectedImagePath = "PadAdding/ModeBlackOn.png",
SelectedTextColor = SkinStyle.Current.SelectedColor,
TextColor = SkinStyle.Current.TextColor1,
TextID = R.MyInternationalizationString.Close2,
};
ControlView.AddChidren (btnCloseCurtain);
#endregion
ProgressControlView = new FrameLayout () {
Y = roomBackgroundImageView.Bottom + Application.GetRealHeight(100),
Width = LayoutParams.MatchParent,
Height = Application.GetRealHeight (230),
};
AddChidren (ProgressControlView);
if (curtain.Type == DeviceType.CurtainModel) {
AddCurtainModeul (curtain);
} else if (curtain.Type == DeviceType.CurtainTrietex) {
AddCurtainsTrietex (curtain as CurtainTrietex);
} else if (curtain.Type == DeviceType.CurtainRoller) {
AddCurtainsRoller (curtain as CurtainRoller);
}
UpdateStatus (curtain);
}
///
/// 加载窗帘模块
///
void AddCurtainModeul (Common device)
{
btnCloseCurtain.MouseDownEventHandler += (sender3, e3) => {
Control.ControlBytesSend (Command.UpdataCurtainModelStutas, device.SubnetID, device.DeviceID, new byte [] { device.LoopID, 2 });
(sender3 as Button).IsSelected = true;
};
btnCloseCurtain.MouseUpEventHandler += (sender3, e3) => {
(sender3 as Button).IsSelected = false;
};
btnOpenCurtain.MouseDownEventHandler += (sender3, e3) => {
Control.ControlBytesSend (Command.UpdataCurtainModelStutas, device.SubnetID, device.DeviceID, new byte [] { device.LoopID, 1 });
(sender3 as Button).IsSelected = true;
};
btnOpenCurtain.MouseUpEventHandler += (sender3, e3) => {
(sender3 as Button).IsSelected = false;
};
btnStopCurtain.MouseDownEventHandler += (sender3, e3) => {
Control.ControlBytesSend (Command.UpdataCurtainModelStutas, device.SubnetID, device.DeviceID, new byte [] { device.LoopID, 0 });
(sender3 as Button).IsSelected = true;
};
btnStopCurtain.MouseUpEventHandler += (sender3, e3) => {
(sender3 as Button).IsSelected = false;
};
}
///
/// 加载开合帘
///
void AddCurtainsTrietex (CurtainTrietex device)
{
#region 进度条
btnCurrentStatus = new Button () {
Width = Application.GetRealWidth (400),
Height = Application.GetRealHeight (50),
X = Application.GetRealWidth (40),
Y = Application.GetRealHeight (20),
TextAlignment = TextAlignment.CenterLeft,
TextID = R.MyInternationalizationString.CurrentStatus
};
ProgressControlView.AddChidren (btnCurrentStatus);
var btnMinBrightness = new Button () {
Width = Application.GetRealWidth (90),
Height = Application.GetRealHeight (80),
X = Application.GetRealWidth (40),
Y = btnCurrentStatus.Bottom + Application.GetRealHeight (30),
Text = "0%",
TextColor = 0xFF333333,
TextAlignment = TextAlignment.Center,
};
ProgressControlView.AddChidren (btnMinBrightness);
var f = new FrameLayout () {
Width = Application.GetRealWidth (1200),
Height = Application.GetRealHeight (80),
X = btnMinBrightness.Right,
Y = btnMinBrightness.Y,
};
ProgressControlView.AddChidren (f);
horizontalSeekBar = new HorizontalSeekBar () {
Height = LayoutParams.MatchParent,
Gravity = Gravity.CenterVertical,
Progress = device.CurtainProress,
ProgressColor =SkinStyle.Current.SelectedColor,
ThumbColor = 0xFFFFFFFF,
};
f.AddChidren (horizontalSeekBar);
var btnMaxBrightness = new Button () {
X = f.Right + Application.GetRealHeight (10),
Y = btnMinBrightness.Y,
Width = Application.GetRealWidth (120),
Height = Application.GetRealHeight (80),
TextAlignment = TextAlignment.CenterLeft,
Text = "100%",
TextColor = 0xFF333333,
};
ProgressControlView.AddChidren (btnMaxBrightness);
horizontalSeekBar.ProgressChanged += (sender, e) => {
btnCurrentStatus.Text = e.ToString () + "%";
device.CurtainProress = (byte)e;
};
horizontalSeekBar.MouseDownEventHandler += (sender, e) => {
stop ();
};
horizontalSeekBar.MouseUpEventHandler += (sender, e) => {
Control.ControlBytesSend (Command.UpdataCurtainModelStutas, device.SubnetID, device.DeviceID, new byte [] { 17, Convert.ToByte (horizontalSeekBar.Progress) });
btnCurrentStatus.Text = Language.StringByID(R.MyInternationalizationString.CurrentStatus) +" : "+ device.CurtainProress.ToString () + "%";
th = new System.Threading.Thread ((obj) => {
//如果后面没有什么操作,到时自动还原接收更新状态
System.Threading.Thread.Sleep (20 * 1000);
th = null;
});
th.Start ();
};
#endregion
btnCloseCurtain.MouseDownEventHandler += (sender3, e3) => {
(sender3 as Button).IsSelected = true;
Control.ControlBytesSend (Command.UpdataCurtainModelStutas, device.SubnetID, device.DeviceID, new byte [] { 1, 2 });
stop ();
int startProgress = horizontalSeekBar.Progress;
if (0 < device.Speed) {
th = new System.Threading.Thread ((obj) => {
var startDateTime = DateTime.Now;
while (curView != null) {
System.Threading.Thread.Sleep (200);
var v = startProgress - (int)((DateTime.Now - startDateTime).TotalMilliseconds * device.Speed);
v = v < 0 ? 0 : v;
Application.RunOnMainThread (() => {
horizontalSeekBar.Progress = v;
btnCurrentStatus.Text = Language.StringByID(R.MyInternationalizationString.CurrentStatus) +" : "+ v + "%";
});
if (v <= 0) {
break;
}
}
//防止跳动
System.Threading.Thread.Sleep (2000);
th = null;
});
th.Start ();
} else {
readProgressDateTime = DateTime.Now;
//接近全开状态,这样初始化的值会是比较准确的
if (90 <= startProgress) {
intSpped (0);
}
}
};
btnCloseCurtain.MouseUpEventHandler += (sender3, e3) => {
(sender3 as Button).IsSelected = false;
};
btnOpenCurtain.MouseDownEventHandler += (sender3, e3) => {
(sender3 as Button).IsSelected = true;
Control.ControlBytesSend (Command.UpdataCurtainModelStutas, device.SubnetID, device.DeviceID, new byte [] { 1, 1 });
stop ();
int startProgress = horizontalSeekBar.Progress;
if (0 < device.Speed) {
th = new System.Threading.Thread ((obj) => {
var startDateTime = DateTime.Now;
while (curView != null) {
System.Threading.Thread.Sleep (200);
var v = startProgress + (int)((DateTime.Now - startDateTime).TotalMilliseconds * device.Speed);
v = 100 < v ? 100 : v;
Application.RunOnMainThread (() => {
horizontalSeekBar.Progress = v;
btnCurrentStatus.Text = Language.StringByID(R.MyInternationalizationString.CurrentStatus) +" : "+ v + "%";
});
if (100 <= v) {
break;
}
}
//防止跳动
System.Threading.Thread.Sleep (2000);
th = null;
});
th.Start ();
} else {
readProgressDateTime = DateTime.Now;
//接近全关状态,这样初始化的值会是比较准确的
if (startProgress <= 10) {
intSpped (100);
}
}
};
btnOpenCurtain.MouseUpEventHandler += (sender3, e3) => {
(sender3 as Button).IsSelected = false;
};
btnStopCurtain.MouseDownEventHandler += (sender3, e3) => {
(sender3 as Button).IsSelected = true;
Control.ControlBytesSend (Command.UpdataCurtainModelStutas, device.SubnetID, device.DeviceID, new byte [] { 1, 0 });
stop ();
};
btnStopCurtain.MouseUpEventHandler += (sender3, e3) => {
(sender3 as Button).IsSelected = false;
};
}
///
/// 加载开合帘
///
void AddCurtainsRoller (CurtainRoller device)
{
#region 进度条
btnCurrentStatus = new Button () {
Width = Application.GetRealWidth (400),
Height = Application.GetRealHeight (50),
X = Application.GetRealWidth (40),
Y = Application.GetRealHeight (20),
TextAlignment = TextAlignment.CenterLeft,
TextID = R.MyInternationalizationString.CurrentStatus
};
ProgressControlView.AddChidren (btnCurrentStatus);
var btnMinBrightness = new Button () {
Width = Application.GetRealWidth (90),
Height = Application.GetRealHeight (80),
X = Application.GetRealWidth (40),
Y = btnCurrentStatus.Bottom + Application.GetRealHeight (30),
Text = "0%",
TextColor = 0xFF333333,
TextAlignment = TextAlignment.Center,
};
ProgressControlView.AddChidren (btnMinBrightness);
var f = new FrameLayout () {
Width = Application.GetRealWidth (1200),
Height = Application.GetRealHeight (80),
X = btnMinBrightness.Right,
Y = btnMinBrightness.Y,
};
ProgressControlView.AddChidren (f);
horizontalSeekBar = new HorizontalSeekBar () {
Height = LayoutParams.MatchParent,
Gravity = Gravity.CenterVertical,
Progress = device.CurtainProress,
ProgressColor = SkinStyle.Current.SelectedColor,
ThumbColor = 0xFFFFFFFF,
};
f.AddChidren (horizontalSeekBar);
var btnMaxBrightness = new Button () {
X = f.Right + Application.GetRealHeight (10),
Y = btnMinBrightness.Y,
Width = Application.GetRealWidth (120),
Height = Application.GetRealHeight (80),
TextAlignment = TextAlignment.CenterLeft,
Text = "100%",
TextColor = 0xFF333333,
};
ProgressControlView.AddChidren (btnMaxBrightness);
horizontalSeekBar.ProgressChanged += (sender, e) => {
btnCurrentStatus.Text = Language.StringByID(R.MyInternationalizationString.CurrentStatus) +" : "+ e.ToString () + "%";
device.CurtainProress = (byte)e;
};
horizontalSeekBar.MouseDownEventHandler += (sender, e) => {
stop ();
};
horizontalSeekBar.MouseUpEventHandler += (sender, e) => {
Control.ControlBytesSend (Command.UpdataCurtainModelStutas, device.SubnetID, device.DeviceID, new byte [] { 17, Convert.ToByte (horizontalSeekBar.Progress) });
btnCurrentStatus.Text = Language.StringByID(R.MyInternationalizationString.CurrentStatus) +" : "+ device.CurtainProress.ToString () + "%";
th = new System.Threading.Thread ((obj) => {
//如果后面没有什么操作,到时自动还原接收更新状态
System.Threading.Thread.Sleep (20 * 1000);
th = null;
});
th.Start ();
};
#endregion
btnCloseCurtain.MouseDownEventHandler += (sender3, e3) => {
(sender3 as Button).IsSelected = true;
Control.ControlBytesSend (Command.UpdataCurtainModelStutas, device.SubnetID, device.DeviceID, new byte [] { 1, 2 });
stop ();
int startProgress = horizontalSeekBar.Progress;
if (0 < device.Speed) {
th = new System.Threading.Thread ((obj) => {
var startDateTime = DateTime.Now;
while (curView != null) {
System.Threading.Thread.Sleep (200);
var v = startProgress - (int)((DateTime.Now - startDateTime).TotalMilliseconds * device.Speed);
v = v < 0 ? 0 : v;
Application.RunOnMainThread (() => {
horizontalSeekBar.Progress = v;
btnCurrentStatus.Text = Language.StringByID(R.MyInternationalizationString.CurrentStatus) +" : "+ v + "%";
});
if (v <= 0) {
break;
}
}
//防止跳动
System.Threading.Thread.Sleep (2000);
th = null;
});
th.Start ();
} else {
readProgressDateTime = DateTime.Now;
//接近全开状态,这样初始化的值会是比较准确的
if (90 <= startProgress) {
intSpped (0);
}
}
};
btnCloseCurtain.MouseUpEventHandler += (sender3, e3) => {
(sender3 as Button).IsSelected = false;
};
btnOpenCurtain.MouseDownEventHandler += (sender3, e3) => {
(sender3 as Button).IsSelected = true;
Control.ControlBytesSend (Command.UpdataCurtainModelStutas, device.SubnetID, device.DeviceID, new byte [] { 1, 1 });
stop ();
int startProgress = horizontalSeekBar.Progress;
if (0 < device.Speed) {
th = new System.Threading.Thread ((obj) => {
var startDateTime = DateTime.Now;
while (curView != null) {
System.Threading.Thread.Sleep (200);
var v = startProgress + (int)((DateTime.Now - startDateTime).TotalMilliseconds * device.Speed);
v = 100 < v ? 100 : v;
Application.RunOnMainThread (() => {
horizontalSeekBar.Progress = v;
btnCurrentStatus.Text = Language.StringByID(R.MyInternationalizationString.CurrentStatus) +" : "+ v + "%";
});
if (100 <= v) {
break;
}
}
//防止跳动
System.Threading.Thread.Sleep (2000);
th = null;
});
th.Start ();
} else {
readProgressDateTime = DateTime.Now;
//接近全关状态,这样初始化的值会是比较准确的
if (startProgress <= 10) {
intSpped (100);
}
}
};
btnOpenCurtain.MouseUpEventHandler += (sender3, e3) => {
(sender3 as Button).IsSelected = false;
};
btnStopCurtain.MouseDownEventHandler += (sender3, e3) => {
(sender3 as Button).IsSelected = true;
Control.ControlBytesSend (Command.UpdataCurtainModelStutas, device.SubnetID, device.DeviceID, new byte [] { 1, 0 });
stop ();
};
btnStopCurtain.MouseUpEventHandler += (sender3, e3) => {
(sender3 as Button).IsSelected = false;
};
}
DateTime readProgressDateTime = System.DateTime.MaxValue;
System.Threading.Thread th;
void statrtReadProgress ()
{
int endProgress = 0;
System.Threading.Tasks.Task.Run (() => {
if (curtain.Type == DeviceType.CurtainRoller) {
var curtainroller = curtain as CurtainRoller;
Control.ControlBytesSend (Command.ReadCurtainStatus, curtainroller.SubnetID, curtainroller.DeviceID, new byte [] { 17 });
} else if (curtain.Type == DeviceType.CurtainTrietex) {
var curtaintrietex = curtain as CurtainTrietex;
Control.ControlBytesSend (Command.ReadCurtainStatus, curtaintrietex.SubnetID, curtaintrietex.DeviceID, new byte [] { 17 });
}
while (curView != null) {
System.Threading.Thread.Sleep (300);
if (readProgressDateTime == DateTime.MaxValue) {
continue;
}
if (curtain.Type == DeviceType.CurtainRoller) {
var curtainroller = curtain as CurtainRoller;
if (curtainroller.CurtainProress != endProgress) {
endProgress = curtainroller.CurtainProress;
readProgressDateTime = DateTime.Now;
}
if ((DateTime.Now - readProgressDateTime).TotalMilliseconds < 1000) {
Control.ControlBytesSend (Command.ReadCurtainStatus, curtainroller.SubnetID, curtainroller.DeviceID, new byte [] { 17 }, SendCount.Zero);
} else {
readProgressDateTime = DateTime.MaxValue;
}
} else if (curtain.Type == DeviceType.CurtainTrietex) {
var curtaintrietex = curtain as CurtainTrietex;
if (curtaintrietex.CurtainProress != endProgress) {
endProgress = curtaintrietex.CurtainProress;
readProgressDateTime = DateTime.Now;
}
if ((DateTime.Now - readProgressDateTime).TotalMilliseconds < 1000) {
Control.ControlBytesSend (Command.ReadCurtainStatus, curtaintrietex.SubnetID, curtaintrietex.DeviceID, new byte [] { 17 }, SendCount.Zero);
} else {
readProgressDateTime = DateTime.MaxValue;
}
}
}
});
}
///
/// 结束值
///
void intSpped (int v)
{
MainPage.Loading.Start (Language.StringByID (R.MyInternationalizationString.Initiating));
DateTime dateTime = DateTime.Now;
System.Threading.Tasks.Task.Run (() => {
while (curView != null) {
System.Threading.Thread.Sleep (500);
var bytes = Control.ControlBytesSendHasReturn (Command.ReadCurtainStatus, curtain.SubnetID, curtain.DeviceID, new byte [] { 17 });
if (bytes == null) {
break;
} else {
if (2 <= bytes.Length && v == bytes [1]) {
if (curtain.Type == DeviceType.CurtainRoller) {
(curtain as CurtainRoller).Speed = 100 / ((DateTime.Now - dateTime).TotalMilliseconds - 300);
} else if (curtain.Type == DeviceType.CurtainTrietex) {
(curtain as CurtainTrietex).Speed = 100 / ((DateTime.Now - dateTime).TotalMilliseconds - 300);
}
break;
}
}
}
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
});
});
}
void stop ()
{
try {
if (th != null) {
th.Abort ();
th = null;
//Control.ControlBytesSend (Command.ReadCurtainStatus, curtain.SubnetID, curtain.DeviceID, new byte [] { 17 });
}
} catch { }
}
}
}