using System;
using System.Collections.Generic;
using System.Text;
namespace Shared.Phone.UserCenter.Residence
{
///
/// 添加新房间的界面
///
public class AddNewRoomForm : EditorCommonForm
{
#region ■ 变量声明___________________________
///
/// 完成添加房间的事件
///
public Action FinishEditorEvent = null;
///
/// 列表控件
///
private FrameListControl listview = null;
///
/// 楼层主键
///
private string floorKeys = string.Empty;
///
/// 新房间对象
///
private Common.Room newRoom = null;
#endregion
#region ■ 初始化_____________________________
///
/// 画面显示(底层会固定调用此方法,借以完成画面创建)
///
/// 楼层主键
public void ShowForm(string i_floorKeys)
{
this.floorKeys = i_floorKeys;
//设置头部信息
base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uCreatRoom));
//初始化中部信息
this.InitMiddleFrame();
}
///
/// 初始化中部信息
///
private void InitMiddleFrame()
{
//清空bodyFrame
this.ClearBodyFrame();
var listBackControl = new VerticalFrameControl();
listBackControl.Height = bodyFrameLayout.Height;
bodyFrameLayout.AddChidren(listBackControl);
//加一个白色背景
var frameBack1 = new FrameLayout();
frameBack1.Height = Application.GetRealHeight(450);
frameBack1.Y = Application.GetRealHeight(196);
frameBack1.BackgroundColor = UserCenterColor.Current.White;
frameBack1.SetCornerWithSameRadius(Application.GetRealHeight(58), HDLUtils.RectCornerTopLeft | HDLUtils.RectCornerTopRight);
listBackControl.frameTable.AddChidren(frameBack1);
//初始化信息桌布
var tableContr = new InformationEditorControl();
this.listview = tableContr.InitControl(listBackControl.frameTable, Language.StringByID(R.MyInternationalizationString.uInfoEditor), 1175);
//房间图片底部阴影
var btnShadow = new PicViewControl(853, 593);
btnShadow.Y = Application.GetRealHeight(58);
btnShadow.Gravity = Gravity.CenterHorizontal;
btnShadow.UnSelectedImagePath = "Room/RoomShadow.png";
listBackControl.frameTable.AddChidren(btnShadow);
//房间图片
var btnPic = new ImageView();
btnPic.Y = Application.GetRealHeight(60);
btnPic.Width = this.GetPictrueRealSize(824);
btnPic.Height = this.GetPictrueRealSize(549);
btnPic.Radius = (uint)Application.GetRealHeight(17);
btnPic.Gravity = Gravity.CenterHorizontal;
btnPic.ImagePath = "RoomIcon/0.jpg";
listBackControl.frameTable.AddChidren(btnPic);
//图片遮罩
var btnZhezhao = new FrameLayout();
btnZhezhao.Width = btnPic.Width;
btnZhezhao.Height = btnPic.Height;
btnZhezhao.Y = btnPic.Y;
btnZhezhao.Gravity = Gravity.CenterHorizontal;
btnZhezhao.Radius = (uint)Application.GetRealHeight(17);
btnZhezhao.BackgroundColor = UserCenterColor.Current.PictrueZhezhaoColor;
listBackControl.frameTable.AddChidren(btnZhezhao);
btnZhezhao.MouseUpEventHandler += (sender, e) =>
{
//房间图片选择
this.RoomPictrueSelect(btnPic);
};
//变更一下那个白色背景的Y轴,它到图片的中间位置
frameBack1.Y = btnPic.Y + btnPic.Height / 2;
this.newRoom = new Common.Room();
newRoom.FloorId = this.floorKeys;
newRoom.BackgroundImage = btnPic.ImagePath;
newRoom.Humidity = -1000;
newRoom.Temperatrue = -1000;
//添加全部菜单
this.AddAllMenuRow(tableContr, btnPic);
}
#endregion
#region ■ 添加菜单___________________________
///
/// 添加全部菜单
///
private void AddAllMenuRow(InformationEditorControl tableContr, ImageView btnPic)
{
//【房间名称】
var txtRoomName = new FrameCaptionInputControl(Language.StringByID(R.MyInternationalizationString.uRoomName), string.Empty, this.listview.rowSpace / 2);
txtRoomName.txtInput.PlaceholderText = Language.StringByID(R.MyInternationalizationString.uPleaseInputRoomName);
listview.AddChidren(txtRoomName);
txtRoomName.InitControl();
//底线
txtRoomName.AddBottomLine();
//该APP为有楼层的模式才开启这个菜单
if (floorKeys != string.Empty)
{
//【所属楼层】
var btnBelone = new FrameCaptionViewControl(Language.StringByID(R.MyInternationalizationString.uBelongeFloor), Common.Config.Instance.Home.FloorDics[this.floorKeys], this.listview.rowSpace / 2);
listview.AddChidren(btnBelone);
btnBelone.InitControl();
//右箭头
btnBelone.AddRightArrow();
//底线
btnBelone.AddBottomLine();
btnBelone.ButtonClickEvent += (sender, e) =>
{
//楼层选择
this.ShowSelectFloor(btnBelone);
};
}
//【温度】
var rowTemperature = new FrameRowControl(this.listview.rowSpace / 2);
listview.AddChidren(rowTemperature);
rowTemperature.AddLeftCaption(Language.StringByID(R.MyInternationalizationString.uTemperature), 600);
//右箭头
rowTemperature.AddRightArrow();
var btnTemper = rowTemperature.AddMostRightView(Language.StringByID(R.MyInternationalizationString.uNothing), 300);
//底线
rowTemperature.AddBottomLine();
rowTemperature.ButtonClickEvent += (sender, e) =>
{
//显示温度传感器列表
this.ShowTemperatrueDevice(btnTemper);
};
//【湿度】
var rowHumidity = new FrameRowControl(this.listview.rowSpace / 2);
listview.AddChidren(rowHumidity);
rowHumidity.AddLeftCaption(Language.StringByID(R.MyInternationalizationString.uHumidity), 600);
//右箭头
rowHumidity.AddRightArrow();
var btnHumi = rowHumidity.AddMostRightView(Language.StringByID(R.MyInternationalizationString.uNothing), 300);
//底线
rowHumidity.AddBottomLine();
rowHumidity.ButtonClickEvent += (sender, e) =>
{
//显示湿度传感器列表
this.ShowHumidityDevice(btnHumi);
};
//初始化桌布完成
tableContr.FinishInitControl();
tableContr = null;
//保存
var btnFinish = new BottomClickButton();
btnFinish.TextID = R.MyInternationalizationString.uSave;
bodyFrameLayout.AddChidren(btnFinish);
btnFinish.ButtonClickEvent += (sender, e) =>
{
this.newRoom.Name = txtRoomName.Text;
//检测能否保存房间
if (this.CheckCanSaveRoom() == false)
{
return;
}
if (newRoom.BackgroundImageType == 1 || newRoom.BackgroundImageType == 2)
{
//生成图片
string picName = $"RoomIcon_{DateTime.Now.ToString("yyyyMMddHHmmss")}.png";
string picFullName = System.IO.Path.Combine(Common.Config.Instance.FullPath, picName);
Shared.IO.FileUtils.WriteFileByBytes(picFullName, btnPic.ImageBytes);
newRoom.BackgroundImage = picName;
//备份
Phone.UserCenter.HdlAutoBackupLogic.AddOrEditorFile(picName);
}
newRoom.FloorId = this.floorKeys;
if (newRoom.Humidity == -1000)
{
newRoom.Humidity = 0;
}
if (newRoom.Temperatrue == -1000)
{
newRoom.Temperatrue = 0;
}
//添加房间,主页需要重新刷新
UserView.UserPage.Instance.RefreshForm = true;
HdlRoomLogic.Current.AddRoom(newRoom);
this.FinishEditorEvent?.Invoke();
this.CloseForm();
};
}
#endregion
#region ■ 楼层选择___________________________
///
/// 楼层选择
///
///
private void ShowSelectFloor(FrameCaptionViewControl control)
{
var dicFloor = HdlRoomLogic.Current.GetFloorSortList();
var listKeys = new List();
var listName = new List();
int index = 0;
foreach (string keys in dicFloor.Keys)
{
listKeys.Add(keys);
listName.Add(dicFloor[keys]);
if (keys == this.floorKeys)
{
index = listKeys.Count - 1;
}
}
PickerView.Show(listName, (value) =>
{
control.txtView.Text = listName[value];
//主键修改
this.floorKeys = listKeys[value];
},
index,
Language.StringByID(R.MyInternationalizationString.uBelongeFloor),
Language.StringByID(R.MyInternationalizationString.uFinish),
Language.StringByID(R.MyInternationalizationString.uCancel));
}
#endregion
#region ■ 温度传感器相关_____________________
///
/// 显示温度传感器列表
///
///
private void ShowTemperatrueDevice(NormalViewControl btnTemper)
{
var listDevice = new List();
foreach (var device in Common.LocalDevice.Current.listAllDevice)
{
//获取温度传感器
if (device is ZigBee.Device.TemperatureSensor && ((ZigBee.Device.TemperatureSensor)device).SensorDiv == 1)
{
listDevice.Add(device);
}
}
var listSelect = new List() { newRoom.TemperatrueDevice };
var form = new SelectDeviceForm();
form.AddForm(listDevice, listSelect, true, true);
form.ActionSelectDevice += (list) =>
{
if (list.Count == 0)
{
newRoom.TemperatrueDevice = string.Empty;
btnTemper.Text = Language.StringByID(R.MyInternationalizationString.uNothing);
return;
}
newRoom.TemperatrueDevice = Common.LocalDevice.Current.GetDeviceMainKeys(list[0]);
//获取温度值
this.GetTemperatrueValue(btnTemper, newRoom.TemperatrueDevice);
};
}
///
/// 获取温度值
///
///
///
private void GetTemperatrueValue(NormalViewControl btnTemper, string deviceKey)
{
var device = Common.LocalDevice.Current.GetDevice(deviceKey);
if (device == null)
{
return;
}
if (newRoom.Temperatrue == -1000)
{
//没有获取得到过温度
btnTemper.Text = Language.StringByID(R.MyInternationalizationString.Getting);
}
else
{
btnTemper.Text = this.newRoom.Temperatrue == 0 ? "0.0℃" : this.newRoom.Temperatrue.ToString() + "℃";
btnTemper.Text += " " + Language.StringByID(R.MyInternationalizationString.Getting);
}
HdlGatewayReceiveLogic.Current.RemoveEvent("TemperatrueDevice");
HdlGatewayReceiveLogic.Current.AddAttributeEvent("TemperatrueDevice", ReceiveComandDiv.A设备属性上报, ((report) =>
{
string mainKeys = Common.LocalDevice.Current.GetDeviceMainKeys(report);
if (newRoom.TemperatrueDevice != mainKeys || btnTemper == null)
{
return;
}
foreach (var data in report.DeviceStatusReport.AttriBute)
{
if (data.AttributeId == (int)ZigBee.Device.AttriButeId.MeasuredValue)
{
HdlThreadLogic.Current.RunMain(() =>
{
if (data.AttriButeData == 0)
{
//0℃
btnTemper.Text = "0.0℃";
this.newRoom.Temperatrue = 0;
}
else if (data.AttriButeData > 32767)
{
//负数(特殊处理)
string strValue = (data.AttriButeData - 65536).ToString();
//小数点需要一位
strValue = strValue.Substring(0, strValue.Length - 1);
//记录温度
this.newRoom.Temperatrue = Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
btnTemper.Text = this.newRoom.Temperatrue.ToString() + "℃";
}
else
{
//小数点需要一位
string strValue = data.AttriButeData.ToString();
strValue = strValue.Substring(0, strValue.Length - 1);
//记录温度
this.newRoom.Temperatrue = Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
btnTemper.Text = this.newRoom.Temperatrue.ToString() + "℃";
}
});
}
}
}));
//发送获取温度的命令
((ZigBee.Device.TemperatureSensor)device).ReadTemperatureOrHumidity();
}
#endregion
#region ■ 湿度传感器相关_____________________
///
/// 显示湿度传感器列表
///
///
private void ShowHumidityDevice(NormalViewControl btnHumi)
{
var listDevice = new List();
foreach (var device in Common.LocalDevice.Current.listAllDevice)
{
//获取湿度传感器
if (device is ZigBee.Device.TemperatureSensor && ((ZigBee.Device.TemperatureSensor)device).SensorDiv == 2)
{
listDevice.Add(device);
}
else if (device.Type == ZigBee.Device.DeviceType.FreshAirHumiditySensor)
{
listDevice.Add(device);
}
}
var listSelect = new List() { newRoom.HumidityDevice };
var form = new SelectDeviceForm();
form.AddForm(listDevice, listSelect, true, true);
form.ActionSelectDevice += (list) =>
{
if (list.Count == 0)
{
newRoom.HumidityDevice = string.Empty;
btnHumi.Text = Language.StringByID(R.MyInternationalizationString.uNothing);
return;
}
newRoom.HumidityDevice = Common.LocalDevice.Current.GetDeviceMainKeys(list[0]);
//获取湿度值
this.GetHumidityValue(btnHumi, newRoom.HumidityDevice);
};
}
///
/// 获取湿度值
///
///
///
private void GetHumidityValue(NormalViewControl btnHumi, string deviceKey)
{
var device = Common.LocalDevice.Current.GetDevice(deviceKey);
if (device == null)
{
return;
}
if (newRoom.Humidity == -1000)
{
//没有获取得到过湿度
btnHumi.Text = Language.StringByID(R.MyInternationalizationString.Getting);
}
else
{
btnHumi.Text = this.newRoom.Humidity == 0 ? "0.0%" : this.newRoom.Humidity.ToString() + "%";
btnHumi.Text += " " + Language.StringByID(R.MyInternationalizationString.Getting);
}
HdlGatewayReceiveLogic.Current.RemoveEvent("HumidityDevice");
HdlGatewayReceiveLogic.Current.AddAttributeEvent("HumidityDevice", ReceiveComandDiv.A设备属性上报, (report) =>
{
string mainKeys = Common.LocalDevice.Current.GetDeviceMainKeys(report);
if (newRoom.HumidityDevice != mainKeys || btnHumi == null)
{
return;
}
foreach (var data in report.DeviceStatusReport.AttriBute)
{
if (data.AttributeId == (int)ZigBee.Device.AttriButeId.MeasuredValue)
{
HdlThreadLogic.Current.RunMain(() =>
{
decimal humidity = (decimal)device.GetType().InvokeMember("Humidity", System.Reflection.BindingFlags.GetField, null, device, null);
if (humidity == 0)
{
//0
btnHumi.Text = "0.0%";
this.newRoom.Humidity = 0;
}
else
{
//记录湿度
this.newRoom.Humidity = humidity;
btnHumi.Text = this.newRoom.Humidity.ToString() + "%";
}
});
}
}
});
//发送获取湿度的命令
device.GetType().InvokeMember("ReadTemperatureOrHumidity", System.Reflection.BindingFlags.InvokeMethod, null, device, null);
}
#endregion
#region ■ 房间图片选择_______________________
///
/// 房间图片选择
///
///
private void RoomPictrueSelect(ImageView imageContr)
{
var menuContr = new BottomMenuSelectForm();
menuContr.AddForm(3);
//默认图库
menuContr.AddMenu(Language.StringByID(R.MyInternationalizationString.LocalPicture), () =>
{
var form = new SelectLocalRoomImageForm();
form.AddForm();
form.FinishSelectEvent = (imgPath) =>
{
if (string.IsNullOrEmpty(imgPath) == true)
{
return;
}
this.newRoom.BackgroundImageType = 0;
this.newRoom.BackgroundImage = imgPath;
imgPath = IO.FileUtils.GetImageFilePath(imgPath);
imageContr.ImageBytes = IO.FileUtils.ReadFile(imgPath);
};
});
//拍照
menuContr.AddMenu(Language.StringByID(R.MyInternationalizationString.Photograph), () =>
{
//通过相机拍照裁剪
CropImage.TakePicture((imagePath) =>
{
if (string.IsNullOrEmpty(imagePath) == true)
{
return;
}
this.newRoom.BackgroundImageType = 1;
imageContr.ImageBytes = Shared.IO.FileUtils.ReadFile(imagePath);
System.IO.File.Delete(imagePath);
}, "HdlPic", 2, 1);
});
//我的相册
menuContr.AddMenu(Language.StringByID(R.MyInternationalizationString.MyAblums), () =>
{
//从相册选择图片裁剪
CropImage.SelectPicture((imagePath) =>
{
if (string.IsNullOrEmpty(imagePath) == true)
{
return;
}
this.newRoom.BackgroundImageType = 2;
imageContr.ImageBytes = Shared.IO.FileUtils.ReadFile(imagePath);
System.IO.File.Delete(imagePath);
}, "HdlPic", 2, 1);
});
}
#endregion
#region ■ 界面关闭___________________________
///
/// 界面关闭
///
public override void CloseFormBefore()
{
this.FinishEditorEvent = null;
HdlGatewayReceiveLogic.Current.RemoveEvent("TemperatrueDevice");
HdlGatewayReceiveLogic.Current.RemoveEvent("HumidityDevice");
base.CloseFormBefore();
}
#endregion
#region ■ 一般方法___________________________
///
/// 检测能否保存房间
///
///
private bool CheckCanSaveRoom()
{
if (this.newRoom.Name == string.Empty)
{
//请输入房间名称
this.ShowMassage(ShowMsgType.Error, Language.StringByID(R.MyInternationalizationString.uPleaseInputRoomName));
return false;
}
if (string.IsNullOrEmpty(this.floorKeys) == true)
{
var listRoom = HdlRoomLogic.Current.GetAllListRooms();
if (listRoom.Find((obj) => obj.Name == this.newRoom.Name && obj.Id != this.newRoom.Id) != null)
{
this.ShowMassage(ShowMsgType.Error, Language.StringByID(R.MyInternationalizationString.uRoomNameIsExist));
return false;
}
}
else
{
var listRoom = HdlRoomLogic.Current.GetFloorSortRoom(this.floorKeys);
if (listRoom.Find((obj) => obj.Name == this.newRoom.Name && obj.Id != this.newRoom.Id) != null)
{
this.ShowMassage(ShowMsgType.Error, Language.StringByID(R.MyInternationalizationString.uRoomNameIsExist));
return false;
}
}
return true;
}
#endregion
}
}