using System;
namespace Shared.SimpleControl.Phone
{
public class GenericDialog
{
///
///
///
private static GenericDialog m_Current = null;
///
///
///
public static GenericDialog Current {
get {
if (m_Current == null) {
m_Current = new GenericDialog ();
}
return m_Current;
}
}
///
///
///
///
///
///
public void ShowModifyRemarksDialog (Common commonDevice, byte[] commonBytes, Button btnEquipment, Action successAction)
{
Dialog dialog = new Dialog ();
var dialogView = new FrameLayout () {
Gravity = Gravity.Center,
Width = Application.GetRealWidth (500),
Height = Application.GetRealHeight (300),
BackgroundColor = SkinStyle.Current.DialogColor,
Radius = 5,
BorderColor = SkinStyle.Current.Transparent,
BorderWidth = 1,
};
dialog.AddChidren (dialogView);
var etDeviceName = new EditText () {
Y = Application.GetRealHeight (80),
Gravity = Gravity.CenterHorizontal,
Width = Application.GetRealWidth (350),
Height = Application.GetRealHeight (70),
//Text = commonDevice.Name,
Text = commonDevice.Name.TrimEnd (),
TextAlignment = TextAlignment.Center,
TextColor = SkinStyle.Current.TextColor,
Radius = 1,
BorderWidth = 1,
BorderColor = SkinStyle.Current.BorderColor,
};
dialogView.AddChidren (etDeviceName);
etDeviceName.TextChangeEventHandler += (sender, e) => {
byte [] remakeBytes = CommonPage.MyEncodingGB2312.GetBytes (etDeviceName.Text.Trim ());
if (remakeBytes.Length > 20) {
etDeviceName.Text = CommonPage.MyEncodingGB2312.GetString (remakeBytes, 0, 20);
new Alert ("", ErrorCode.RemarkLengthExceededTheLimit, "Close").Show ();
}
};
var dialogBottomView = new FrameLayout () {
Y = Application.GetRealHeight (213),
Height = Application.GetRealHeight (90),
BackgroundColor = SkinStyle.Current.Black50Transparent,
};
dialogView.AddChidren (dialogBottomView);
var btnClose = new Button () {
Width = Application.GetRealWidth (250),
TextAlignment = TextAlignment.Center,
TextID = R.MyInternationalizationString.cancel,
TextColor = SkinStyle.Current.TextColor,
BackgroundColor = SkinStyle.Current.ButtonColor,
};
dialogBottomView.AddChidren (btnClose);
btnClose.MouseUpEventHandler += (ddss, dddsss) => {
dialog.Close ();
};
var editor = new Button () {
X = btnClose.Right + 1,
Width = Application.GetRealWidth (250),
TextAlignment = TextAlignment.Center,
TextID = R.MyInternationalizationString.SAVE,
TextColor = SkinStyle.Current.TextColor,
BackgroundColor = SkinStyle.Current.ButtonColor,
};
dialogBottomView.AddChidren (editor);
editor.MouseUpEventHandler += (dff, ffd) => {
string remakeStr = etDeviceName.Text.Trim ();
if (string.IsNullOrEmpty (remakeStr)) {
//备注不能为空
new Alert ("", ErrorCode.RemarksCannotBeEmpty, "Close").Show ();
return;
}
byte [] remakeBytes = CommonPage.MyEncodingGB2312.GetBytes (remakeStr);
if (remakeBytes.Length > 20) {
new Alert ("", ErrorCode.RemarkLengthExceededTheLimit, "Close").Show ();
return;
}
//修改云端备注
var cloudDataLoop = CommonConfig.Current.FunctionList.Find ((obj) => obj.bus.SubnetID == commonDevice.SubnetID
&& obj.bus.DeviceID == commonDevice.DeviceID && obj.bus.LoopId == commonDevice.LoopID);
if (cloudDataLoop == null) {
new Alert (Language.StringByID (R.MyInternationalizationString.Tip), "Data is not synchronized to the cloud.", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
} else {
new System.Threading.Thread (() => {
var http = new HttpServerRequest ();
var pack = http.EditDeviceName (cloudDataLoop.deviceId, UserConfig.Instance.CurrentRegion.Id, remakeStr);
if (pack.Code != "0") {
new Alert (Language.StringByID (R.MyInternationalizationString.Tip), $"Cloud data modification failed({pack.Code}).", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
}
}) { IsBackground = true }.Start ();
}
MainPage.Loading.Start ();
//byte [] remakeBytes = CommonPage.MyEncodingGB2312.GetBytes (etDeviceName.Text.Trim ());
System.Threading.Tasks.Task.Run (() => {
byte [] updateBytes = null;
if (commonDevice.Type == DeviceType.LightSwitchSocket) {
updateBytes = Control.ControlBytesSendHasReturn (Command.ReadDeviceLoopInfo, commonDevice.SubnetID, commonDevice.DeviceID, new byte [] { commonDevice.BigClass, 1, commonDevice.LoopID });
} else {
updateBytes = Control.ControlBytesSendHasReturn (Command.ReadDeviceLoopInfo, commonDevice.SubnetID, commonDevice.DeviceID, new byte [] { commonDevice.BigClass, commonDevice.MinClass, commonDevice.LoopID });
}
if (updateBytes == null) {
Application.RunOnMainThread (() => {
new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.TipEquipmentNotOnline), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
MainPage.Loading.Hide ();
dialog.Close ();
});
return;
}
byte [] uBytes = new byte [20];
Array.Copy (remakeBytes, 0, uBytes, 0, remakeBytes.Length < 20 ? remakeBytes.Length : 20);
Array.Copy (uBytes, 0, updateBytes, 3, 20 < uBytes.Length ? 20 : uBytes.Length);
byte [] reBytes = Control.ControlBytesSendHasReturn (Command.SetDeviceLoopInfo, commonDevice.SubnetID, commonDevice.DeviceID, updateBytes);
if (reBytes != null) {
Application.RunOnMainThread (() => {
btnEquipment.Text = commonDevice.Name = etDeviceName.Text.Trim ();
if (commonDevice.Type == DeviceType.InfraredMode) {
//2020-07-03 修复红外电视修改后变红外模块问题
InfraredMode mTV = Newtonsoft.Json.JsonConvert.DeserializeObject (System.Text.Encoding.UTF8.GetString (commonBytes));
if (mTV == null) {
IO.FileUtils.SaveEquipmentMessage (commonDevice, commonDevice.LoopID.ToString ());
} else {
mTV.Name = etDeviceName.Text.Trim ();
IO.FileUtils.SaveEquipmentMessage (mTV, mTV.LoopID.ToString ());
}
} else {
IO.FileUtils.SaveEquipmentMessage (commonDevice, commonDevice.LoopID.ToString ());
}
successAction?.Invoke ();
//IO.FileUtils.SaveEquipmentMessage (commonDevice, commonDevice.LoopID.ToString ());
MainPage.Loading.Hide ();
dialog.Close ();
});
} else {
Application.RunOnMainThread (() => {
new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.FailureToModify), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
MainPage.Loading.Hide ();
dialog.Close ();
});
}
});
//string remakeStr = etDeviceName.Text.Trim ();
//if (string.IsNullOrEmpty (remakeStr)) {
// //备注不能为空
// new Alert ("", ErrorCode.RemarksCannotBeEmpty, "Close").Show ();
// return;
//}
//byte [] remakeBytes = CommonPage.MyEncodingGB2312.GetBytes (remakeStr);
//if (remakeBytes.Length > 20) {
// new Alert ("", ErrorCode.RemarkLengthExceededTheLimit, "Close").Show ();
// return;
//}
//MainPage.Loading.Start ();
////byte [] remakeBytes = CommonPage.MyEncodingGB2312.GetBytes (etDeviceName.Text.Trim ());
//System.Threading.Tasks.Task.Run (() => {
// byte [] updateBytes = null;
// if (commonDevice.Type == DeviceType.LightSwitchSocket) {
// updateBytes = Control.ControlBytesSendHasReturn (Command.ReadDeviceLoopInfo, commonDevice.SubnetID, commonDevice.DeviceID, new byte [] { commonDevice.BigClass, 1, commonDevice.LoopID });
// } else {
// updateBytes = Control.ControlBytesSendHasReturn (Command.ReadDeviceLoopInfo, commonDevice.SubnetID, commonDevice.DeviceID, new byte [] { commonDevice.BigClass, commonDevice.MinClass, commonDevice.LoopID });
// }
// if (updateBytes == null) {
// Application.RunOnMainThread (() => {
// new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.TipEquipmentNotOnline), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
// MainPage.Loading.Hide ();
// dialog.Close ();
// });
// return;
// }
// byte [] uBytes = new byte [20];
// Array.Copy (remakeBytes, 0, uBytes, 0, remakeBytes.Length < 20 ? remakeBytes.Length : 20);
// Array.Copy (uBytes, 0, updateBytes, 3, 20 < uBytes.Length ? 20 : uBytes.Length);
// byte [] reBytes = Control.ControlBytesSendHasReturn (Command.SetDeviceLoopInfo, commonDevice.SubnetID, commonDevice.DeviceID, updateBytes);
// if (reBytes != null) {
// Application.RunOnMainThread (() => {
// btnEquipment.Text = commonDevice.Name = etDeviceName.Text.Trim ();
// if (commonDevice.Type == DeviceType.InfraredMode) {
// //2020-07-03 修复红外电视修改后变红外模块问题
// InfraredMode mTV = Newtonsoft.Json.JsonConvert.DeserializeObject (System.Text.Encoding.UTF8.GetString (commonBytes));
// if (mTV == null) {
// IO.FileUtils.SaveEquipmentMessage (commonDevice, commonDevice.LoopID.ToString ());
// } else {
// mTV.Name = etDeviceName.Text.Trim ();
// IO.FileUtils.SaveEquipmentMessage (mTV, mTV.LoopID.ToString ());
// }
// } else {
// IO.FileUtils.SaveEquipmentMessage (commonDevice, commonDevice.LoopID.ToString ());
// }
// //IO.FileUtils.SaveEquipmentMessage (commonDevice, commonDevice.LoopID.ToString ());
// MainPage.Loading.Hide ();
// dialog.Close ();
// });
// //修改成功回调
// successAction?.Invoke ();
// } else {
// Application.RunOnMainThread (() => {
// new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.FailureToModify), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
// MainPage.Loading.Hide ();
// dialog.Close ();
// });
// }
//});
};
dialog.Show ();
}
///
/// 修改备注成功后刷新房间设备列表的备注
///
///
public void RefreshRemark (Common commonDevice)
{
try {
foreach (var room in Room.Lists) {
foreach (var common in room.DeviceList) {
if (common.CommonLoopID != commonDevice.CommonLoopID || common.Type != commonDevice.Type) {
continue;
}
common.Name = commonDevice.Name;
}
}
} catch {
Utlis.WriteLine ("RefreshRemark 失败");
}
}
}
}