using Shared.Common;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using ZigBee.Device;
namespace Shared.Phone.UserCenter.DeviceCurtain
{
///
/// 开合帘的方向与限位的配置界面
///
public class AutoOpenDirectionAndLimitSettionForm : EditorCommonForm
{
#region ■ 变量声明___________________________
///
///窗帘的回路
///
private Rollershade curtainDevice = null;
///
/// 窗帘数据
///
private CurtainData curtainData = null;
///
/// 允许接收上限位(0:不允许 1:允许)
///
private string receiveUplimit = "0";
///
/// 允许接收下限位(0:不允许 1:允许)
///
private string receiveDownLimit = "0";
///
/// 是否备份窗帘数据
///
private bool backupCurtainData = false;
///
/// 列表控件
///
private VerticalListControl listView = null;
///
/// 开限位的进度条
///
private HorizontalSeekBar openSeekBar = null;
///
/// 合限位的进度条
///
private HorizontalSeekBar closeSeekBar = null;
#endregion
#region ■ 初始化_____________________________
///
/// 画面显示(底层会固定调用此方法,借以完成画面创建)
///
/// 窗帘的回路
public void ShowForm(Rollershade i_CurtainDevice)
{
UserView.HomePage.Instance.ScrollEnabled = false;
this.curtainDevice = i_CurtainDevice;
//设置标题信息
base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uDirectionAndLimit));
HdlThreadLogic.Current.RunThread(() =>
{
//初始化中部控件
this.InitMiddleFrame();
});
}
///
/// 初始化中部信息
///
private void InitMiddleFrame()
{
//获取设备初始数据
var result = this.InitCurtainDefultData();
if (result == false)
{
//显示重新加载的界面
this.ShowReLoadView();
return;
}
HdlThreadLogic.Current.RunMain(() =>
{
//清空bodyFrame
bodyFrameLayout.BackgroundColor = UserCenterColor.Current.White;
this.ClearBodyFrame();
this.listView = new VerticalListControl(29);
listView.Y = Application.GetRealHeight(-6);
listView.Height = bodyFrameLayout.Height;
bodyFrameLayout.AddChidren(listView);
//添加方向行
this.AddDirectionRow();
//添加限位行
this.AddLimitRow();
//添加重置电机行
this.AddElectricalMachineryRow();
//保存
var btnSave = new BottomClickButton();
btnSave.TextID = R.MyInternationalizationString.uSave;
bodyFrameLayout.AddChidren(btnSave);
btnSave.ButtonClickEvent += (sender, e) =>
{
//这个保存没啥意义
this.CloseForm();
};
//限位数据接收
this.ReceiveLimitData();
});
}
#endregion
#region ■ 方向行_____________________________
///
/// 添加方向行
///
private void AddDirectionRow()
{
var rowDirection = new FrameRowControl(listView.rowSpace / 2);
listView.AddChidren(rowDirection);
//方向
rowDirection.AddLeftCaption(Language.StringByID(R.MyInternationalizationString.uDirection), 600);
//右箭头
rowDirection.AddRightArrow();
//状态
var btnStatu = rowDirection.AddMostRightView("", 300);
btnStatu.TextID = curtainData.Direction == false ? R.MyInternationalizationString.uForwardDirection : R.MyInternationalizationString.uReverseDirection;
//底线
rowDirection.AddBottomLine();
int nowSelectNo = curtainData.Direction == false ? 0 : 1;
rowDirection.ButtonClickEvent += (sender, e) =>
{
var listText = new List();
listText.Add(Language.StringByID(R.MyInternationalizationString.uForwardDirection));//正向
listText.Add(Language.StringByID(R.MyInternationalizationString.uReverseDirection));//反向
var form = new BottomItemSelectForm();
form.AddForm(Language.StringByID(R.MyInternationalizationString.uDirectionSelect), listText, nowSelectNo);
form.FinishSelectEvent += async (index) =>
{
//变更方向
var result = await HdlDeviceCurtainLogic.Current.SetCurtainDirection(curtainDevice, index == 0 ? false : true);
if (result == false)
{
return;
}
nowSelectNo = index;
btnStatu.Text = listText[index];
curtainData.Direction = index == 0 ? false : true;
};
};
}
#endregion
#region ■ 限位行_____________________________
///
/// 添加限位行
///
private void AddLimitRow()
{
var frameBack = new FrameLayout();
frameBack.Height = Application.GetRealHeight(679);
var rowLimit = new FrameRowControl(listView.rowSpace / 2);
rowLimit.UseClickStatu = false;
listView.AddChidren(rowLimit);
//限位
rowLimit.AddLeftCaption(Language.StringByID(R.MyInternationalizationString.uLimit), 600);
//右箭头
var btnRinght = rowLimit.AddMostRightEmptyIcon(58, 58);
rowLimit.ChangedChidrenBindMode(btnRinght, ChidrenBindMode.NotBind);
btnRinght.UseClickStatu = false;
btnRinght.UnSelectedImagePath = "Item/Next.png";
btnRinght.SelectedImagePath = "Item/Down.png";
btnRinght.IsSelected = true;
btnRinght.ButtonClickEvent += (sender, e) =>
{
btnRinght.IsSelected = !btnRinght.IsSelected;
//展开折叠
frameBack.Height = frameBack.Height > 10 ? 0 : Application.GetRealHeight(679);
};
//底线
rowLimit.AddBottomLine();
listView.AddChidren(frameBack);
//添加开限位进度条
this.AddOpenLimitProgress(frameBack);
//添加合限位进度条
this.AddCloseLimitProgress(frameBack);
}
///
/// 添加开限位进度条
///
///
private void AddOpenLimitProgress(FrameLayout frameBack)
{
//开限位
var btnProgressView = new NormalViewControl(200, 50, true);
btnProgressView.X = ControlCommonResourse.XXLeft;
btnProgressView.Y = Application.GetRealHeight(60);
btnProgressView.TextSize = 12;
btnProgressView.TextColor = UserCenterColor.Current.TextGrayColor1;
btnProgressView.TextID = R.MyInternationalizationString.uOpenLimit;
frameBack.AddChidren(btnProgressView);
int progressValue = (int)(curtainData.OpenLimitValue / (curtainData.CurtainLength * 1.0) * 100);
var btnProgress1 = new NormalViewControl(200, 50, true);
btnProgress1.X = Application.GetRealWidth(200);
btnProgress1.Y = btnProgressView.Y;
btnProgress1.TextSize = 12;
btnProgress1.TextColor = UserCenterColor.Current.TextGrayColor1;
btnProgress1.Text = progressValue + "%";
frameBack.AddChidren(btnProgress1);
//进度条
this.openSeekBar = new HorizontalSeekBar();
openSeekBar.Y = btnProgressView.Bottom + Application.GetRealHeight(46);
openSeekBar.Gravity = Gravity.CenterHorizontal;
openSeekBar.Width = Application.GetRealWidth(962);
openSeekBar.Height = Application.GetRealHeight(84);
openSeekBar.Max = 100;
openSeekBar.BackgroundColor = 0xfff5f5f5;
openSeekBar.ThumbColor = Common.ZigbeeColor.Current.GXCButtonBlueColor;
openSeekBar.ProgressColor = 0xff288bfd;
openSeekBar.Progress = progressValue;
frameBack.AddChidren(openSeekBar);
var btnTemp1 = new NormalViewControl(200, 50, true);
btnTemp1.X = ControlCommonResourse.XXLeft;
btnTemp1.Y = btnProgressView.Bottom + Application.GetRealHeight(115);
btnTemp1.TextSize = 12;
btnTemp1.TextColor = UserCenterColor.Current.TextGrayColor3;
btnTemp1.Text = "0%";
frameBack.AddChidren(btnTemp1);
var btnTemp2 = new NormalViewControl(200, 50, true);
btnTemp2.X = frameBack.Width - Application.GetRealWidth(200 + 50);
btnTemp2.Y = btnTemp1.Y;
btnTemp2.TextAlignment = TextAlignment.CenterRight;
btnTemp2.TextSize = 12;
btnTemp2.TextColor = UserCenterColor.Current.TextGrayColor3;
btnTemp2.Text = "100%";
frameBack.AddChidren(btnTemp2);
//线
var btnLine = new NormalViewControl(frameBack.Width - ControlCommonResourse.XXLeft * 2, ControlCommonResourse.BottomLineHeight, false);
btnLine.X = ControlCommonResourse.XXLeft;
btnLine.Y = btnTemp2.Bottom + Application.GetRealHeight(69);
btnLine.BackgroundColor = UserCenterColor.Current.ButtomLine;
frameBack.AddChidren(btnLine);
int oldValue = progressValue;
int waitTime = 4;//500毫秒为单位
bool startWait = false;
bool checking = false;
openSeekBar.ProgressChanged += (sender, value) =>
{
//值继续改变,则等待时间还原
waitTime = 4;
//开限位不能大于合限位
if (value > closeSeekBar.Progress)
{
openSeekBar.Progress = closeSeekBar.Progress;
return;
}
if (checking == false)
{
startWait = true;
}
btnProgress1.Text = value + "%";
//数据变更,百分比
curtainData.OpenLimitPersent = value;
};
HdlThreadLogic.Current.RunThread(async () =>
{
while (openSeekBar.Parent != null)
{
if (startWait == false)
{
await Task.Delay(200);
continue;
}
//开启值改变等待
await Task.Delay(500);
waitTime--;
if (waitTime <= 0)
{
//如果2秒内值不再改变,则进行判断
startWait = false;
checking = true;
var value = curtainData.OpenLimitPersent;
if (oldValue == value)
{
//相同数值
checking = false;
continue;
}
oldValue = value;
if (curtainData.DeleteOpenLimit == true)
{
//重置开限位点
var result = await HdlDeviceCurtainLogic.Current.DeleteCurtainLimitPoint(curtainDevice, Rollershade.LimiType.UpLimit);
if (result == false)
{
checking = false;
continue;
}
curtainData.DeleteOpenLimit = false;
}
receiveUplimit = "1";
//将窗帘调整到指定百分比
curtainDevice.WcdGoToTiltValue(value);
checking = false;
}
}
});
}
///
/// 添加合限位进度条
///
///
private void AddCloseLimitProgress(FrameLayout frameBack)
{
//合限位
var btnProgressView = new NormalViewControl(200, 50, true);
btnProgressView.X = ControlCommonResourse.XXLeft;
btnProgressView.Y = Application.GetRealHeight(397);
btnProgressView.TextSize = 12;
btnProgressView.TextColor = UserCenterColor.Current.TextGrayColor1;
btnProgressView.TextID = R.MyInternationalizationString.uCloseLimit;
frameBack.AddChidren(btnProgressView);
int progressValue = (int)(curtainData.CloseLimitValue / (curtainData.CurtainLength * 1.0) * 100);
var btnProgress1 = new NormalViewControl(200, 50, true);
btnProgress1.X = Application.GetRealWidth(200);
btnProgress1.Y = btnProgressView.Y;
btnProgress1.TextSize = 12;
btnProgress1.TextColor = UserCenterColor.Current.TextGrayColor1;
btnProgress1.Text = progressValue + "%";
frameBack.AddChidren(btnProgress1);
//进度条
this.closeSeekBar = new HorizontalSeekBar();
closeSeekBar.Y = btnProgressView.Bottom + Application.GetRealHeight(46);
closeSeekBar.Gravity = Gravity.CenterHorizontal;
closeSeekBar.Width = Application.GetRealWidth(962);
closeSeekBar.Height = Application.GetRealHeight(84);
closeSeekBar.Max = 100;
closeSeekBar.BackgroundColor = 0xfff5f5f5;
closeSeekBar.ThumbColor = Common.ZigbeeColor.Current.GXCButtonBlueColor;
closeSeekBar.ProgressColor = 0xff288bfd;
closeSeekBar.Progress = progressValue;
frameBack.AddChidren(closeSeekBar);
var btnTemp1 = new NormalViewControl(200, 50, true);
btnTemp1.X = ControlCommonResourse.XXLeft;
btnTemp1.Y = btnProgressView.Bottom + Application.GetRealHeight(115);
btnTemp1.TextSize = 12;
btnTemp1.TextColor = UserCenterColor.Current.TextGrayColor3;
btnTemp1.Text = "0%";
frameBack.AddChidren(btnTemp1);
var btnTemp2 = new NormalViewControl(200, 50, true);
btnTemp2.X = frameBack.Width - Application.GetRealWidth(200 + 50);
btnTemp2.Y = btnTemp1.Y;
btnTemp2.TextAlignment = TextAlignment.CenterRight;
btnTemp2.TextSize = 12;
btnTemp2.TextColor = UserCenterColor.Current.TextGrayColor3;
btnTemp2.Text = "100%";
frameBack.AddChidren(btnTemp2);
//线
var btnLine = new NormalViewControl(frameBack.Width - ControlCommonResourse.XXLeft * 2, ControlCommonResourse.BottomLineHeight, false);
btnLine.X = ControlCommonResourse.XXLeft;
btnLine.Y = btnTemp2.Bottom + Application.GetRealHeight(69);
btnLine.BackgroundColor = UserCenterColor.Current.ButtomLine;
frameBack.AddChidren(btnLine);
int oldValue = progressValue;
int waitTime = 4;//500毫秒为单位
bool startWait = false;
bool checking = false;
closeSeekBar.ProgressChanged += (sender, value) =>
{
//值继续改变,则等待时间还原
waitTime = 4;
//合限位不能小于合限位
if (value < openSeekBar.Progress)
{
closeSeekBar.Progress = openSeekBar.Progress;
return;
}
if (checking == false)
{
startWait = true;
}
btnProgress1.Text = value + "%";
//数据变更,百分比
curtainData.CloseLimitPersent = value;
};
HdlThreadLogic.Current.RunThread(async () =>
{
while (closeSeekBar.Parent != null)
{
if (startWait == false)
{
await Task.Delay(200);
continue;
}
//开启值改变等待
await Task.Delay(500);
waitTime--;
if (waitTime <= 0)
{
//如果2秒内值不再改变,则进行判断
startWait = false;
checking = true;
var value = curtainData.CloseLimitPersent;
if (oldValue == value)
{
//相同数值
checking = false;
continue;
}
oldValue = value;
if (curtainData.DeleteCloseLimit == true)
{
//重置合限位点
var result = await HdlDeviceCurtainLogic.Current.DeleteCurtainLimitPoint(curtainDevice, Rollershade.LimiType.DownLimit);
if (result == false)
{
checking = false;
continue;
}
curtainData.DeleteCloseLimit = false;
}
receiveDownLimit = "1";
//将窗帘调整到指定百分比
curtainDevice.WcdGoToTiltValue(value);
checking = false;
}
}
});
}
#endregion
#region ■ 重置电机___________________________
///
/// 添加重置电机行
///
private void AddElectricalMachineryRow()
{
var rowReset = new FrameRowControl(listView.rowSpace / 2);
listView.AddChidren(rowReset);
//重置电机
rowReset.AddLeftCaption(Language.StringByID(R.MyInternationalizationString.uResetElectricalMachinery), 600);
//底线
rowReset.AddBottomLine();
rowReset.ButtonClickEvent += (sender, e) =>
{
//重置电机将初始化{0}方向与限位设置,确认继续?
string msg = Language.StringByID(R.MyInternationalizationString.uResetElectricalMachineryMsg);
msg = msg.Replace("{0}", "\r\n");
this.ShowMassage(ShowMsgType.Confirm, msg, () =>
{
HdlThreadLogic.Current.RunThread(async () =>
{
//重置窗帘
var result = await HdlDeviceCurtainLogic.Current.RestoreCurtain(curtainDevice);
if (result == false)
{
return;
}
//获取数据后,备份窗帘数据
this.backupCurtainData = true;
//重新初始化界面
this.InitMiddleFrame();
});
});
};
}
#endregion
#region ■ 初始化窗帘数据_____________________
///
/// 初始化窗帘数据
///
///
private bool InitCurtainDefultData()
{
//开启进度条
this.ShowProgressBar();
this.curtainData = new CurtainData();
this.receiveUplimit = "0";
this.receiveDownLimit = "0";
bool receiptData = false;
string mainkeys = LocalDevice.Current.GetDeviceMainKeys(curtainDevice);
HdlGatewayReceiveLogic.Current.AddAttributeEvent("CurtainDeviceAttribute", ReceiveComandDiv.A设备属性上报, (device) =>
{
string checkKey = LocalDevice.Current.GetDeviceMainKeys(device);
if (mainkeys != checkKey || device.DeviceStatusReport.CluterID != 258)
{
return;
}
foreach (var attriBute in device.DeviceStatusReport.AttriBute)
{
if (attriBute.AttributeId == 23)
{
//开合帘方向
if (0 < (attriBute.AttriButeData & 0x01))
{
//反向
curtainData.Direction = true;
}
else
{
//正向
curtainData.Direction = false;
}
}
else if (attriBute.AttributeId == 1)
{
//窗帘总长
curtainData.CurtainLength = attriBute.AttriButeData;
}
else if (attriBute.AttributeId == 16)
{
//开限位
curtainData.OpenLimitValue = attriBute.AttriButeData;
}
else if (attriBute.AttributeId == 17)
{
//合限位
curtainData.CloseLimitValue = attriBute.AttriButeData;
//数据接收结束
receiptData = true;
}
}
});
//发送获取窗帘限位配置的命令
HdlDeviceCurtainLogic.Current.SetGetCurtainLimitSettionComand(curtainDevice);
int timeCount = 30;
while (receiptData == false && timeCount >= 0)
{
System.Threading.Thread.Sleep(100);
timeCount--;
}
//关闭进度条
this.CloseProgressBar();
if (timeCount <= 0)
{
//获取窗帘方向与限位设置失败
string msg = Language.StringByID(R.MyInternationalizationString.uGetCurtainDirectionAndLimitSettionFail);
//拼接上【网关回复超时】的Msg
msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, null, "回复超时");
this.ShowMassage(ShowMsgType.Tip, msg);
return false;
}
//移除监听
HdlGatewayReceiveLogic.Current.RemoveEvent("CurtainDeviceAttribute");
//备份窗帘数据
if (this.backupCurtainData == true)
{
this.backupCurtainData = false;
HdlThreadLogic.Current.RunThread(async () =>
{
//方向备份
await HdlDeviceCurtainLogic.Current.BackupCurtainDirection(curtainDevice, curtainData.Direction);
//限位备份
int uplimit= (int)(curtainData.OpenLimitValue / (curtainData.CurtainLength * 1.0) * 100);
int downlimit = (int)(curtainData.CloseLimitValue / (curtainData.CurtainLength * 1.0) * 100);
await HdlDeviceCurtainLogic.Current.BackupCurtainLimitPoint(curtainDevice, uplimit, downlimit);
});
}
return true;
}
#endregion
#region ■ 限位数据接收_______________________
///
/// 限位数据接收
///
private void ReceiveLimitData()
{
if (HdlGatewayReceiveLogic.Current.IsEsixt("ReceiveLimitData") == true)
{
HdlGatewayReceiveLogic.Current.RemoveEvent("ReceiveLimitData");
}
string mainkeys = LocalDevice.Current.GetDeviceMainKeys(curtainDevice);
HdlGatewayReceiveLogic.Current.AddAttributeEvent("ReceiveLimitData", ReceiveComandDiv.A设备属性上报, (device) =>
{
string checkKey = LocalDevice.Current.GetDeviceMainKeys(device);
if (mainkeys != checkKey || device.DeviceStatusReport.CluterID != 258)
{
return;
}
lock (receiveUplimit)
{
if (receiveUplimit == "0" && receiveDownLimit == "0")
{
return;
}
foreach (var attriBute in device.DeviceStatusReport.AttriBute)
{
//窗帘百分比推送
if (attriBute.AttributeId == 8)
{
//上限位
if (receiveUplimit == "1")
{
receiveUplimit = "0";
openSeekBar.Progress = curtainData.OpenLimitPersent;
//确认当前位置{0}为开限位?
string msg = Language.StringByID(R.MyInternationalizationString.uCommitCurtainOpenLimitMsg);
msg = msg.Replace("{0}", curtainData.OpenLimitPersent + "%");
this.ShowMassage(ShowMsgType.Confirm, msg, async () =>
{
//执行确认及覆盖上限位点
var result = await HdlDeviceCurtainLogic.Current.CommitCurtainLimitPoint(curtainDevice, Rollershade.CurtainPrivateInstalledLimi.UpLimit,
curtainData.OpenLimitPersent, curtainData.CloseLimitPersent);
if (result == true)
{
curtainData.DeleteOpenLimit = true;
}
});
}
//合限位
else if (receiveDownLimit == "1")
{
receiveDownLimit = "0";
closeSeekBar.Progress = curtainData.CloseLimitPersent;
//确认当前位置{0}为合限位?
string msg = Language.StringByID(R.MyInternationalizationString.uCommitCurtainCloseLimitMsg);
msg = msg.Replace("{0}", curtainData.CloseLimitPersent + "%");
this.ShowMassage(ShowMsgType.Confirm, msg, async () =>
{
//执行确认及覆盖合限位点
var result = await HdlDeviceCurtainLogic.Current.CommitCurtainLimitPoint(curtainDevice, Rollershade.CurtainPrivateInstalledLimi.DownLimit,
curtainData.OpenLimitPersent, curtainData.CloseLimitPersent);
if (result == true)
{
curtainData.DeleteCloseLimit = true;
}
});
}
}
}
}
});
}
#endregion
#region ■ 界面关闭___________________________
///
/// 界面关闭
///
public override void CloseFormBefore()
{
HdlGatewayReceiveLogic.Current.RemoveEvent("CurtainDeviceAttribute");
HdlGatewayReceiveLogic.Current.RemoveEvent("ReceiveLimitData");
UserView.HomePage.Instance.ScrollEnabled = true;
base.CloseFormBefore();
}
#endregion
#region ■ 结构体_____________________________
///
/// 窗帘数据
///
private class CurtainData
{
///
/// false:电机方向正向;true:电机方向反向
///
public bool Direction = false;
///
/// 是否重置开限位点
///
public bool DeleteOpenLimit = true;
///
/// 是否重置合限位点
///
public bool DeleteCloseLimit = true;
///
/// 开限位的值
///
public int OpenLimitValue = -1;
///
/// 开限位的百分比
///
public int OpenLimitPersent = -1;
///
/// 合限位的值
///
public int CloseLimitValue = -1;
///
/// 合限位的百分比
///
public int CloseLimitPersent = -1;
///
/// 窗帘总长
///
public int CurtainLength = -1;
}
#endregion
}
}