using System;
using System.Collections.Generic;
using System.Threading;
using System.Net.NetworkInformation;
using System.Net;
using System.Text;
namespace Shared.SimpleControl.Phone
{
public class SystemMqttGatewaySetting : FrameLayout
{
static byte TYPEStandardMQTT = 5;
static byte TYPEHdlMQTT = 4;
Common gatewayDeicve;
public SystemMqttGatewaySetting()
{
BackgroundColor = SkinStyle.Current.MainColor;
}
public override void RemoveFromParent()
{
base.RemoveFromParent();
}
public void ShowView(Common GatewayCommon)
{
gatewayDeicve = GatewayCommon;
#region 标题
var topView = new FrameLayout()
{
Y = Application.GetRealHeight(36),
Height = Application.GetRealHeight(90),
};
AddChidren(topView);
var title = new Button()
{
TextAlignment = TextAlignment.Center,
Text = GatewayCommon.Name,
TextSize = 19,
TextColor = SkinStyle.Current.TextColor1
};
topView.AddChidren(title);
var logo = new Button()
{
Width = Application.GetRealWidth(154),
Height = Application.GetRealHeight(90),
X = Application.GetRealWidth(486),
UnSelectedImagePath = MainPage.LogoString,
Gravity = Gravity.CenterVertical,
};
topView.AddChidren(logo);
var back = new Button()
{
Height = Application.GetRealHeight(90),
Width = Application.GetRealWidth(85),
UnSelectedImagePath = "Item/Back.png",
Gravity = Gravity.CenterVertical,
};
topView.AddChidren(back);
back.MouseUpEventHandler += (sender, e) => {
UserMiddle.SettingPageView.PageIndex = UserMiddle.SettingPageView.ChildrenCount - 2;
};
#endregion
var BodyView = new FrameLayout () {
Width = LayoutParams.MatchParent,
Height = Application.GetRealHeight (Application.DesignHeight - 126 - 90),
Y = topView.Bottom,
BackgroundColor = SkinStyle.Current.ViewColor,
};
AddChidren (BodyView);
var TipView = new Button () {
Y = topView.Bottom,
Width = LayoutParams.MatchParent,
Height = Application.GetRealHeight (50),
BackgroundColor = 0xFFFFFFE0,
TextSize = 11,
//Text = "一键配置网关远程连接参数",
TextID = R.MyInternationalizationString.OneKeyConfigurationGateway,
TextColor = 0xFF1F1F1F,
IsMoreLines = true
};
AddChidren (TipView);
//var bottomView = new FrameLayout () {
// Y = Application.GetRealHeight (846 + 90 - UserMiddle.userMenuItemHeight),
// Height = Application.GetRealHeight (UserMiddle.userMenuItemHeight + 3),
// BackgroundColor = SkinStyle.Current.MainColor,
//};
//AddChidren (bottomView);
Button SettingButton = new Button () {
Y = Application.GetRealHeight (Application.DesignHeight - 90),
Height = Application.GetRealHeight (90),
TextColor = SkinStyle.Current.TextColor1,
BackgroundColor = SkinStyle.Current.MainColor,
TextID = R.MyInternationalizationString.OneKeyConfiguration,
TextAlignment = TextAlignment.Center
};
AddChidren (SettingButton);
SettingButton.MouseUpEventHandler += (sender, e) => {
RemoteSettingWithHDLMQTT ();
//SetGateWayLocalEncryption (gatewayDeicve, false);
};
}
///
/// 远程配置 HDL MQTT模式
///
void RemoteSettingWithHDLMQTT ()
{
MainPage.Loading.Start (Language.StringByID (R.MyInternationalizationString.disposition) + "...");
System.Threading.Tasks.Task.Run (() => {
try {
//1.修改用户名信息 和修改连接模式
if (SetGateWayModelInfo ()) {
//2.修改管理信息
if (SetGateWayAdminInfo ()) {
Application.RunOnMainThread (() => {
new Alert ("", Language.StringByID (R.MyInternationalizationString.ConfigurationSucceeded), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
});
}
}
} catch (Exception ex) {
Console.WriteLine (ex.Message);
Application.RunOnMainThread (() => {
//修改网关连接模式失败
new Alert ("", Language.StringByID (R.MyInternationalizationString.ConfigurationFailed), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
});
bool canRemove = false;
} finally {
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
//this.RemoveAll ();
});
}
});
//Room.InitAllRoom ();
}
bool SetGateWayModelInfo ()
{
byte [] gatewayBytes = new byte [60];
//gatewayDeicve.Remote_GroupName = MainPage.LoginUser.AccountString;//UserConfig.Instance.CurrentRegion.RegionName;
//gatewayDeicve.Remote_ProjectName = gatewayDeicve.MAC.Replace (".", "");//UserConfig.Instance.CurrentRegion.RegionName;
//gatewayDeicve.Remote_UserName = "Admin";
//gatewayDeicve.Remote_Password = "c" + MainPage.LoginUser.ID;
////gatewayDeicve.Remote_Password = "85521566";
//byte [] ggn = new byte [20];
//byte [] b1 = CommonPage.MyEncodingGB2312.GetBytes (gatewayDeicve.Remote_GroupName);
//gatewayDeicve.Remote_GroupName = CommonPage.MyEncodingGB2312.GetString (b1);
//Array.Copy (b1, 0, ggn, 0, 20 < b1.Length ? 20 : b1.Length);
//byte [] gpn = new byte [20];
//byte [] b2 = CommonPage.MyEncodingGB2312.GetBytes (gatewayDeicve.Remote_ProjectName);
//Array.Copy (b2, 0, gpn, 0, 20 < b2.Length ? 20 : b2.Length);
//byte [] gun = new byte [8];
//byte [] b3 = CommonPage.MyEncodingGB2312.GetBytes (gatewayDeicve.Remote_UserName);
//Array.Copy (b3, 0, gun, 0, 8 < b3.Length ? 8 : b3.Length);
//byte [] gpw = new byte [8];
//byte [] b4 = CommonPage.MyEncodingGB2312.GetBytes (gatewayDeicve.Remote_Password);
//Array.Copy (b4, 0, gpw, 0, 8 < b4.Length ? 8 : b4.Length);
var Remote_GroupName = MainPage.LoginUser.AccountString;//UserConfig.Instance.CurrentRegion.RegionName;
var Remote_ProjectName = gatewayDeicve.MAC.Replace (".", "");//UserConfig.Instance.CurrentRegion.RegionName;
var Remote_UserName = "Admin";
var Remote_Password = "c" + MainPage.LoginUser.ID;
//gatewayDeicve.Remote_Password = "85521566";
byte [] ggn = new byte [20];
byte [] b1 = CommonPage.MyEncodingGB2312.GetBytes (Remote_GroupName);
//Remote_GroupName = CommonPage.MyEncodingGB2312.GetString (b1);
Array.Copy (b1, 0, ggn, 0, 20 < b1.Length ? 20 : b1.Length);
byte [] gpn = new byte [20];
byte [] b2 = CommonPage.MyEncodingGB2312.GetBytes (Remote_ProjectName);
Array.Copy (b2, 0, gpn, 0, 20 < b2.Length ? 20 : b2.Length);
byte [] gun = new byte [8];
byte [] b3 = CommonPage.MyEncodingGB2312.GetBytes (Remote_UserName);
Array.Copy (b3, 0, gun, 0, 8 < b3.Length ? 8 : b3.Length);
byte [] gpw = new byte [8];
byte [] b4 = CommonPage.MyEncodingGB2312.GetBytes (Remote_Password);
Array.Copy (b4, 0, gpw, 0, 8 < b4.Length ? 8 : b4.Length);
gatewayBytes [0] = TYPEHdlMQTT;//HDL MQTT
Array.Copy (ggn, 0, gatewayBytes, 1, 20);
Array.Copy (gpn, 0, gatewayBytes, 21, 20);
Array.Copy (gun, 0, gatewayBytes, 41, 8);
Array.Copy (gpw, 0, gatewayBytes, 49, 8);
byte [] backBytes = Control.ControlBytesSendHasReturn (Command.SetGateWayModelInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, gatewayBytes);
string error1 = Language.StringByID (R.MyInternationalizationString.ConfigurationFailed) + "(1)";
return CheckIsSuccessfulWithBytes (backBytes, error1);
}
/////
///// 修改管理员信息
/////
/////
/////
//bool SetGateWayAdminInfo (byte [] adminBytes)
//{
// byte [] name = CommonPage.MyEncodingGB2312.GetBytes (MainPage.LoginUser.AccountString);
// byte [] backBytes;
// if (adminBytes != null && adminBytes.Length >= 73) {//返回数据正常,只修改账号信息
// Array.Copy (name, 0, adminBytes, 0, 36 < name.Length ? 36 : name.Length);
// backBytes = Control.ControlBytesSendHasReturn (Command.SetGateWayAdminInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, adminBytes);
// } else {
// byte [] currentRegionIdBytes = CommonPage.MyEncodingGB2312.GetBytes (UserConfig.Instance.CurrentRegion.RegionID.ToString ());
// //byte [] currentRegionIdBytes = CommonPage.MyEncodingGB2312.GetBytes (UserConfig.Instance.CurrentRegion.RegionID);
// //var currentRegionIdBytes = Utlis.IntToByteArray (UserConfig.Instance.CurrentRegion.RegionID);
// adminBytes = new byte [73];
// adminBytes [36] = 1;//住宅标志位
// Array.Copy (name, 0, adminBytes, 0, 36 < name.Length ? 36 : name.Length);
// Array.Copy (currentRegionIdBytes, 0, adminBytes, 37, 36 < currentRegionIdBytes.Length ? 36 : currentRegionIdBytes.Length);
// backBytes = Control.ControlBytesSendHasReturn (Command.SetGateWayAdminInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, adminBytes);
// }
// return CheckIsSuccessfulWithBytes (backBytes);
//}
///
/// 修改管理员信息
///
///
///
bool SetGateWayAdminInfo ()
{
byte [] name = CommonPage.MyEncodingGB2312.GetBytes (MainPage.LoginUser.AccountString);
//byte [] currentRegionIdBytes = CommonPage.MyEncodingGB2312.GetBytes (UserConfig.Instance.CurrentRegion.RegionID);
byte [] currentRegionIdBytes = CommonPage.MyEncodingGB2312.GetBytes (UserConfig.Instance.CurrentRegion.RegionID.ToString ());
//var currentRegionIdBytes = Utlis.IntToByteArray (UserConfig.Instance.CurrentRegion.RegionID);
byte [] adminBytes = new byte [73];
adminBytes [36] = 1;//住宅标志位
Array.Copy (name, 0, adminBytes, 0, 36 < name.Length ? 36 : name.Length);
Array.Copy (currentRegionIdBytes, 0, adminBytes, 37, 36 < currentRegionIdBytes.Length ? 36 : currentRegionIdBytes.Length);
byte [] backBytes = Control.ControlBytesSendHasReturn (Command.SetGateWayAdminInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, adminBytes);
string error1 = Language.StringByID (R.MyInternationalizationString.ConfigurationFailed) + "(2)";
return CheckIsSuccessfulWithBytes (backBytes, error1);
}
///
/// 判断是否修复成功 F8 = 成功; F5=失败
///
///
///
private bool CheckIsSuccessfulWithBytes (byte [] backBytes, string errorStr)
{
if (backBytes == null) {
Application.RunOnMainThread (() => {
new Alert ("", Language.StringByID (R.MyInternationalizationString.GatewayNoResponse),
Language.StringByID (R.MyInternationalizationString.Close)).Show ();
});
return false;
} else if (backBytes [0] == 0xF8) {//F8 = 成功; F5=失败
return true;
} else {
Application.RunOnMainThread (() => {
new Alert ("", errorStr,
Language.StringByID (R.MyInternationalizationString.Close)).Show ();
});
return false;
}
}
/////
///// 写配置参数成功后执行下一步操作
/////
//void GatewaySettingSucceeded ()
//{
// //var currentRegionIdBytes = Utlis.IntToByteArray (UserConfig.Instance.CurrentRegion.RegionID);
// //byte [] currentRegionIdBytes = CommonPage.MyEncodingGB2312.GetBytes (UserConfig.Instance.CurrentRegion.RegionID);
// byte [] currentRegionIdBytes = CommonPage.MyEncodingGB2312.GetBytes (UserConfig.Instance.CurrentRegion.RegionID.ToString ());
// byte [] sendBytes = new byte [36];
// Array.Copy (currentRegionIdBytes, 0, sendBytes, 0, 36 < currentRegionIdBytes.Length ? 36 : currentRegionIdBytes.Length);
// //var currentRegionIdBytes = Utlis.IntToByteArray (UserConfig.Instance.CurrentRegion.RegionID);
// var bindReginIdStatus = Control.ControlBytesSendHasReturn (Command.Write_APP_Data_STORE_1D5E_CMD, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, sendBytes);
// if (bindReginIdStatus == null) {
// Application.RunOnMainThread (() => {
// new Alert ("", Language.StringByID (R.MyInternationalizationString.GatewayNoResponse), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
// });
// return;
// } else if (bindReginIdStatus [0] != 0xF8) {
// //new Alert ("", "网关绑定住宅失败!", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
// string error3 = Language.StringByID (R.MyInternationalizationString.ConfigurationFailed) + "(3)";
// Application.RunOnMainThread (() => {
// new Alert ("", error3, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
// });
// return;
// }
// Application.RunOnMainThread (() => {
// new Alert ("", Language.StringByID (R.MyInternationalizationString.ConfigurationSucceeded), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
// });
//}
////static byte [] AseKeyBytes;
/////
///// 修改网关本地加密
/////
/////
/////
/////
//bool SetGateWayLocalEncryption (GatewayBase gatewayDeicve, bool isEncrypt = true)
//{
// if (UserConfig.Instance.CurrentRegion == null) return false;
// byte [] aseKey = new byte [16];
// //生成加密Key
// //byte [] AseKeyBytes;
// byte [] homeId = System.Text.Encoding.ASCII.GetBytes (UserConfig.Instance.CurrentRegion.RegionID.ToString());
// Array.Copy (homeId, 0, aseKey, 0, 16 < homeId.Length ? 16 : homeId.Length);
// byte [] sendBytes = new byte [17];
// sendBytes [0] = isEncrypt ? (byte)1 : (byte)0; //0:不加密 1:加密
// Array.Copy (aseKey, 0, sendBytes, 1, 16);
// byte [] backBytes = Control.ControlBytesSendHasReturn (Command.SetGateWayLocalEncryption, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, sendBytes);
// return CheckIsSuccessfulWithBytes (backBytes,"ddd");
//}
}
}