using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ZigBee.Device;
namespace Shared.Phone.UserCenter.Device
{
///
/// 编辑要添加到网关的设备的信息(这里修改的是MAC名,这个画面会更改MAC的物理名字)
///
public class DeviceMacInfoAddForm : UserCenterCommonForm
{
#region ■ 变量声明___________________________
///
/// 设备对象
///
private List listNewDevice = null;
///
/// 保存默认名字(主要针对没有修改名字的时候,必须要给一个默认名字给网关)
///
private string saveDefultName = string.Empty;
#endregion
#region ■ 初始化_____________________________
///
/// 画面显示(底层会固定调用此方法,借以完成画面创建)
///
/// 设备列表
public void ShowForm(List listdevices)
{
this.listNewDevice = listdevices;
//设置标题信息
base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uDeviceInfo));
//初始化中部控件
this.InitMiddleFrame();
//设置全部回路的默认名字
this.SetAllEpointName();
}
///
/// 初始化中部控件
///
private void InitMiddleFrame()
{
this.bodyFrameLayout.RemoveAll();
//进度条一样的东西,没什么逻辑
var line = new ProgressLine();
bodyFrameLayout.AddChidren(line);
line.SetValue(80);
//设备图片
var btnpictrue = new PicViewControl(510, 320);
btnpictrue.Gravity = Gravity.CenterHorizontal;
btnpictrue.Y = Application.GetRealHeight(100);
Common.LocalDevice.Current.SetRealDeviceIconToControl(btnpictrue, listNewDevice);
bodyFrameLayout.AddChidren(btnpictrue);
//下一步
var btnNext = new BottomClickButton();
btnNext.TextID = R.MyInternationalizationString.uNextway;
bodyFrameLayout.AddChidren(btnNext);
var listview = new VerticalScrolViewLayout();
listview.Y = btnpictrue.Bottom + Application.GetRealHeight(100);
listview.Height = btnNext.Y - btnpictrue.Bottom - Application.GetRealHeight(200);
bodyFrameLayout.AddChidren(listview);
//生产商名称
var row = new RowLayout();
row.Height = ControlCommonResourse.ListViewRowHeight;
listview.AddChidren(row);
var btnManuView = new RowTopGrayView(false);
btnManuView.TextID = R.MyInternationalizationString.uManufacturerName;
row.AddChidren(btnManuView);
var btnManu = new RowBottomBlackView(false);
btnManu.Text = listNewDevice[0].ManufacturerName;
row.AddChidren(btnManu);
//设备型号
row = new RowLayout();
row.Height = ControlCommonResourse.ListViewRowHeight;
listview.AddChidren(row);
var btnModelIdView = new RowTopGrayView(false);
btnModelIdView.TextID = R.MyInternationalizationString.uModelIdentifier;
row.AddChidren(btnModelIdView);
var btnModelId = new RowBottomBlackView(false);
btnModelId.Text = listNewDevice[0].ModelIdentifier;
row.AddChidren(btnModelId);
//序列号
row = new RowLayout();
row.Height = ControlCommonResourse.ListViewRowHeight;
listview.AddChidren(row);
var btnSerialView = new RowTopGrayView(false);
btnSerialView.TextID = R.MyInternationalizationString.uSerialNumber;
row.AddChidren(btnSerialView);
var btnSerial = new RowBottomBlackView(false);
btnSerial.Text = listNewDevice[0].SerialNumber;
if (string.IsNullOrEmpty(btnSerial.Text) == true)
{
btnSerial.Text = listNewDevice[0].DeviceAddr;
}
row.AddChidren(btnSerial);
//产品名称
string caption = Language.StringByID(R.MyInternationalizationString.uProductName);
string nameValue = Common.LocalDevice.Current.GetDeviceMacName(listNewDevice[0]);
this.saveDefultName = nameValue;
var btnDeviceView = new EditorNameValueRow(caption, nameValue);
listview.AddChidren(btnDeviceView);
btnDeviceView.InitControl();
//请输入产品名称
btnDeviceView.SetEmptyNameTip(Language.StringByID(R.MyInternationalizationString.uProductNameMastInput));
//编辑产品名称
btnDeviceView.SetDialogTitle(Language.StringByID(R.MyInternationalizationString.uEditorProductName));
btnDeviceView.ActionNameChangedEvent += (deviceName) =>
{
//设备重命名
this.DeviceReName(deviceName);
};
//只有是河东的设备的时候
if (Common.LocalDevice.Current.IsHdlDevice(listNewDevice[0]) == true)
{
//添加【背光设置】行
this.AddBackLightRow(listview);
}
//下一步
btnNext.MouseUpEventHandler += (sender, e) =>
{
//网关成功添加设备的显示画面
var form = new AddDeviceSuccessForm();
base.AddFromAndRemoveNowForm(form, listNewDevice);
};
}
#endregion
#region ■ 背光设置___________________________
///
/// 添加【背光设置】行
///
///
private void AddBackLightRow(VerticalScrolViewLayout listview)
{
bool canAddRow = false;
foreach (var device in listNewDevice)
{
if (device.Type == DeviceType.OnOffSwitch)
{
canAddRow = true;
break;
}
}
if (canAddRow == false)
{
//必须有一路回路是OnOffSwitch才行
return;
}
//获取这一堆设备时属于什么类型的
var myDeviceType = Common.LocalDevice.Current.GetMyDeviceEnumInfo(listNewDevice);
//如果是控制面板
if (myDeviceType.BeloneType == DeviceBeloneType.A按键面板)
{
//背光设置
var statuRowLight = new StatuRowLayout(listview);
var btnBackLight = new RowCenterView(false);
btnBackLight.TextID = R.MyInternationalizationString.uBackLightSettion;
statuRowLight.AddChidren(btnBackLight);
//背光设置向右的图标
statuRowLight.AddRightIconControl();
statuRowLight.MouseUpEvent += (sender, e) =>
{
var form = new DevicePanel.PanelBackLightSettionForm();
this.AddForm(form, listNewDevice);
};
}
}
#endregion
#region ■ 修改名字___________________________
///
/// 设备重命名然后打开新的画面
///
/// deviceName.
/// 是否显示错误
private async void DeviceReName(string i_deviceName, ShowErrorMode mode = ShowErrorMode.YES)
{
//修改MAC名
string deviceName = i_deviceName.Trim();
var result = await Common.LocalDevice.Current.ReMacName(this.listNewDevice, deviceName, mode);
if (result == false)
{
return;
}
//更改过一次之后,则界面关闭后不再更改
this.saveDefultName = string.Empty;
//如果它的回路只有一个的话,则在设备上报的时候,修改MAC名字之后,连同端点名字也一起修改
if (this.listNewDevice.Count == 1)
{
//修改端点名字
result = await Common.LocalDevice.Current.ReName(this.listNewDevice[0], deviceName, mode);
if (result == false)
{
return;
}
}
}
///
/// 设置全部回路的默认名字
///
private async void SetAllEpointName()
{
if (this.listNewDevice.Count <= 1)
{
return;
}
var tempValue = Common.LocalDevice.Current.GetDeviceObjectText(this.listNewDevice);
var arry = tempValue.Split(new string[] { "(" }, StringSplitOptions.RemoveEmptyEntries);
var objName = arry[0].Trim();
foreach (var device in this.listNewDevice)
{
if (Common.LocalDevice.Current.GetSimpleEpointName(device) != string.Empty)
{
continue;
}
//XXXXX(N回路)
var epointName = objName + "(" + device.DeviceEpoint + Language.StringByID(R.MyInternationalizationString.uDeviceCircuit) + ")";
await Common.LocalDevice.Current.ReName(device, epointName, ShowErrorMode.NO);
}
}
#endregion
#region ■ 画面关闭___________________________
///
/// 画面关闭
///
/// 是否关闭界面,false的时候,只会调用关闭函数里面的附加功能
public override void CloseForm(bool isCloseForm = true)
{
if (this.saveDefultName != string.Empty)
{
//必须给它一个默认名字才行
this.DeviceReName(this.saveDefultName, ShowErrorMode.NO);
}
//搜到设备之后,不管之后怎么处理,先添加到主界面的列表及缓存中
this.LoadFormMethodByName("DeviceManagementMainForm", "AddDeviceToFormTable", listNewDevice);
base.CloseForm(isCloseForm);
}
#endregion
}
}