using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json.Linq;
using Shared;
namespace Shared.SimpleControl.Phone
{
public class MigrationServer
{
///
/// 标记完成迁移
///
private bool finish = false;
private string SeverAddr = "https://bahrain-gateway.hdlcontrol.com";
//private string SeverAddr = "https://test-gz.hdlcontrol.com";
FrameLayout contentView;
Button btnTipTitle;
Button btnTipMsg;
Loading loading;
EditText etPwd;
string pwd;
string newUserId;
Button btnSave;
public MigrationServer ()
{
loading = new Loading ();
}
public void ShowDialog()
{
#region 弹窗
Dialog dialog = new Dialog ();
FrameLayout dialogBodyView = new FrameLayout () {
Gravity = Gravity.Center,
Width = Application.GetRealWidth (500),
Height = Application.GetRealHeight (500),
BackgroundColor = SkinStyle.Current.DialogColor,
Radius = 5,
BorderColor = SkinStyle.Current.Transparent,
BorderWidth = 0,
};
dialog.AddChidren (dialogBodyView);
Button btnTitle = new Button () {
Height = Application.GetRealHeight (80),
BackgroundColor = SkinStyle.Current.DialogTitle,
TextAlignment = TextAlignment.Center,
TextID = R.MyInternationalizationString.Tip,
TextColor = SkinStyle.Current.DialogTextColor
};
dialogBodyView.AddChidren (btnTitle);
contentView = new FrameLayout () {
Y = Application.GetRealHeight (80),
Height = Application.GetRealHeight (340),
BackgroundColor = SkinStyle.Current.DialogColor,
};
dialogBodyView.AddChidren (contentView);
btnTipTitle = new Button () {
Gravity = Gravity.CenterHorizontal,
Y = Application.GetRealHeight (20),
Width = Application.GetRealWidth (400),
Height = Application.GetRealHeight (80),
Text = "请输入密码,确认迁移",
TextAlignment = TextAlignment.CenterLeft,
TextColor = SkinStyle.Current.TextColor,
IsMoreLines = true,
};
contentView.AddChidren (btnTipTitle);
etPwd = new EditText () {
Gravity = Gravity.CenterHorizontal,
Y = btnTipTitle.Bottom,
Width = Application.GetRealWidth (400),
Height = Application.GetRealHeight (80),
TextAlignment = TextAlignment.Center,
Radius = 5,
BorderColor = SkinStyle.Current.BorderColor,
BorderWidth = 1,
TextColor = SkinStyle.Current.TextColor,
SecureTextEntry = true
};
contentView.AddChidren (etPwd);
etPwd.EditorEnterAction += (obj) => {
Application.HideSoftInput ();
};
btnTipMsg = new Button () {
Gravity = Gravity.CenterHorizontal,
Y = etPwd.Bottom,
Width = Application.GetRealWidth (400),
Height = Application.GetRealHeight (80),
TextAlignment = TextAlignment.CenterLeft,
TextColor = SkinStyle.Current.DelColor,
IsMoreLines = true,
};
contentView.AddChidren (btnTipMsg);
dialogBodyView.AddChidren (loading);
FrameLayout bottomView = new FrameLayout () {
Y = Application.GetRealHeight (420),
Height = Application.GetRealHeight (85),
BackgroundColor = SkinStyle.Current.DialogTitle
};
dialogBodyView.AddChidren (bottomView);
Button btnClose = new Button () {
Width = Application.GetRealWidth (249),
TextID = R.MyInternationalizationString.Close,
TextAlignment = TextAlignment.Center
};
bottomView.AddChidren (btnClose);
btnClose.MouseUpEventHandler += (send2er, e2) => {
dialog.Close ();
};
Button btnBottomLine = new Button () {
X = btnClose.Right,
Width = 1,
BackgroundColor = SkinStyle.Current.Black50Transparent,
};
bottomView.AddChidren (btnBottomLine);
btnSave = new Button () {
X = btnBottomLine.Right,
Width = Application.GetRealWidth (249),
TextID = R.MyInternationalizationString.Confrim,
TextAlignment = TextAlignment.Center
};
bottomView.AddChidren (btnSave);
etPwd.TextChangeEventHandler = (sender, e) => {
btnTipMsg.Text = "";
};
btnSave.MouseUpEventHandler += (sender2, e2) => {
if (finish) {
dialog.Close ();
return;
}
#if DEBUG
//WriteSecretKey (1, 0, new byte [] {1,1,2,3,2,3,4,5,2,3,5,6 });
//CheckGateway ();
//var newHomeId = Home2New ();
//if (newHomeId == "") { } else { }
//Account2New (etPwd.Text.Trim());
//return;
#endif
if (etPwd.Text.Trim() == "") {
btnTipMsg.Text = "请输入密码";
return;
}
loading.Start ("");
pwd = etPwd.Text.Trim ();
//验证密码
new System.Threading.Thread (() => {
try {
var verResult = AcountVer (pwd);
if (verResult) {
Application.RunOnMainThread (() => {
btnTipMsg.Text = "验证通过,正在检测网关配置";
btnTipMsg.TextColor = SkinStyle.Current.TextColor;
CheckGateway ();
});
} else {
Application.RunOnMainThread (() => {
btnTipMsg.Text = "密码错误,验证失败,请重试。";
loading.Hide ();
});
}
} catch {
Application.RunOnMainThread (() => {
loading.Hide ();
});
} finally {
}
}) { IsBackground = true }.Start ();
};
dialog.Show ();
if (MainPage.WiFiStatus != "CrabtreeAdd/WiFi.png") {
//status = btnSave.Text = "ReCheck";
btnTipMsg.Text = "请在局域网内连接网关设备";
etPwd.Visible = false;
btnSave.MouseUpEventHandler = (sender2, e2) => {
dialog.Close ();
};
} else {
}
//2.进入网关升级模式
//3.接收升级文件获取请求
//迁移账号
#endregion
}
#region 云端
///
/// 账号登录验证
///
///
///
private bool AcountVer(string pwd)
{
var requestObj = new LoginObj () { Account = MainPage.LoginUser.AccountString, Password = pwd, Company = MainPage.SoftSmsType };
var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj);
var revertObj = MainPage.RequestHttps ("Login", requestJson, false);
if (revertObj.StateCode == "SUCCESS") {
return true;
} else {
return false;
}
}
///
/// 账号迁移
///
///
///
public bool Account2New (string pwd)
{
Dictionary dicAccount = new Dictionary ();
dicAccount.Add ("email", MainPage.LoginUser.AccountString);
dicAccount.Add ("language", "ENGLISH");
dicAccount.Add ("appCode", "HDL-HOME-IND-APP");
dicAccount.Add ("pwd", pwd);
dicAccount.Add ("tenantId", "202106");
var requestJson = HttpUtil.GetSignRequestJson (dicAccount);// Newtonsoft.Json.JsonConvert.SerializeObject (dicAccount);
var revertObj2 = MainPage.RequestHttps ("/home-wisdom/data/move/user/save", requestJson, false, false, SeverAddr);
if (revertObj2.code == "0") {
var info = Newtonsoft.Json.JsonConvert.DeserializeObject (revertObj2.data.ToString ());
if (info != null) {
newUserId = info.memberId;
}
return true;
} else {
return false;
}
}
///
/// 住宅迁移
///
/// new homeId
private string Home2New ()
{
Dictionary dic = new Dictionary ();
dic.Add ("homeName", UserConfig.Instance.CurrentRegion.RegionName);
dic.Add ("userId", newUserId);
dic.Add ("homeType", "BUSPRO");
dic.Add ("tenantId", "202106");
var requestJson = HttpUtil.GetSignRequestJson (dic);// Newtonsoft.Json.JsonConvert.SerializeObject (dic);
var revertObj = MainPage.RequestHttps ("/home-wisdom/data/move/home/save", requestJson, false, false, SeverAddr);
if (revertObj != null) {
if (revertObj.data != null) {
var newHomeInfo = Newtonsoft.Json.JsonConvert.DeserializeObject (revertObj.data.ToString ());
return newHomeInfo.homeId;
}
}
return "";
}
///
/// 迁移网关
///
///
/// 新平台的住宅id
///
private bool Gateway2New (string mac,string newHomeId)
{
Dictionary dic = new Dictionary ();
dic.Add ("mac", mac);
dic.Add ("homeId", newHomeId);
dic.Add ("userId", newUserId);
dic.Add ("gatewayType", 0);
dic.Add ("tenantId", "202106");
var requestJson = HttpUtil.GetSignRequestJson (dic);
var revertObj = MainPage.RequestHttps ("/home-wisdom/data/move/gateway/save", requestJson, false, false, SeverAddr);
if (revertObj!= null && revertObj.code == "0") {
return true;
}
return false;
}
///
/// 获取上网密秘钥
///
///
private string GetInternetAccessKey (string mac)
{
Dictionary dic = new Dictionary ();
dic.Add ("supplier", "HDL");
dic.Add ("mac", mac);
dic.Add ("spk", "BUSUDPGATEWAY");
var requestJson = HttpUtil.GetSignRequestJson (dic);
//var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (dic);
var revertObj = MainPage.RequestHttps ("/smart-open/third/device/authByMac", requestJson, false, false, SeverAddr);
if (revertObj.code == "0") {
return revertObj.data.ToString ();
} else { }
return "";
}
///
/// 迁移备份
///
///
///
///
private string moveFolder2New (string newHomeId,string newUserId)
{
Dictionary dic = new Dictionary ();
dic.Add ("backupDataType", "HDL_ON");
dic.Add ("homeId",newHomeId);
dic.Add ("userId",newUserId);
dic.Add ("backupClassify", "USER_DEFINED_BACKUP");
dic.Add ("folderName", "迁移备份" + DateTime.Now.ToString ());
dic.Add ("tenantId", "202106");
var requestJson = HttpUtil.GetSignRequestJson (dic);
var revertObj = MainPage.RequestHttps ("/home-wisdom/data/move/folder/save", requestJson, false, false, SeverAddr);
if(revertObj!= null) {
if(revertObj.code == "0") {
var resultObj = Newtonsoft.Json.JsonConvert.DeserializeObject (revertObj.data.ToString ());
return resultObj.id;
}
}
Application.RunOnMainThread (() => {
btnTipTitle.Text = "备份迁移失败.";
btnTipTitle.TextColor = SkinStyle.Current.DelColor;
});
return "";
}
private string MoveFile2New(string newHomeId, string newUserId,string backupId)
{
Dictionary dic = new Dictionary ();
dic.Add ("backupId", backupId);//1534728347497418754//1534728860322385922
dic.Add ("homeId", newHomeId);
dic.Add ("userId", newUserId);
dic.Add ("backupClassify", "USER_DEFINED_BACKUP");
dic.Add ("tenantId", "202106");
List fileObjs = new List ();
var backuplist = IO.FileUtils.ReadFiles ();
int index = 0;
foreach (var fileName in backuplist) {
index++;
///
/// 如果是特殊的注册登陆文件,则不需要备份到服务器
///
if (fileName == UserInfo.GlobalRegisterFile) {
continue;
}
BackupFileObj backupFileObj = new BackupFileObj () { fileName = fileName, content = IO.FileUtils.ReadFile (fileName) };
fileObjs.Add (backupFileObj);
if(fileObjs.Count > 9) {
if (!dic.ContainsKey ("list")) {
dic.Add ("list", fileObjs);
//} else {
// dic ["list"] = fileObjs;
}
var json = HttpUtil.GetSignRequestJson (dic);
var revertObj_foreach = MainPage.RequestHttps ("/home-wisdom/data/move/file/save", json, false, false, SeverAddr);
if(revertObj_foreach!= null) {
if(revertObj_foreach.code == "0") {
fileObjs.Clear ();
continue;
}
}
}
}
if (!dic.ContainsKey ("list")) {
dic.Add ("list", fileObjs);
//} else {
// dic ["list"] = fileObjs;
}
var requestJson = HttpUtil.GetSignRequestJson (dic);
var revertObj = MainPage.RequestHttps ("/home-wisdom/data/move/file/save", requestJson, false, false, SeverAddr);
if (revertObj != null) {
if (revertObj.code == "0") {
return "true";
}
}
return "";
}
///
/// 标记2.0平台数据
///
///
private bool Mark(string newHomeId)
{
Dictionary dic = new Dictionary ();
dic.Add ("homeId", newHomeId);
dic.Add ("mark", false);
var requestJson = HttpUtil.GetSignRequestJson (dic);
var revertObj = MainPage.RequestHttps ("/home-wisdom/app/home/markVoice", requestJson, false, false, SeverAddr);
if (revertObj != null) {
if(revertObj.code == "0") {
if(revertObj.data.ToString() == "true") {
return true;
}
}
}
return false;
}
#endregion
///
/// 检测一端口信息
///
///
///
public void CheckGateway()
{
Application.RunOnMainThread (() => {
loading.Start ("");
});
new System.Threading.Thread (() => {
//1.检测一端口固件
var localFileList = IO.FileUtils.ReadFiles ();
var gateWayList = localFileList.FindAll ((obj) => {
return (obj.StartsWith ("Equipment_")) && (
obj.Split ('_') [1].ToString () == DeviceType.OnePortBus.ToString () ||
obj.Split ('_') [1].ToString () == DeviceType.RCU.ToString () ||
obj.Split ('_') [1].ToString () == DeviceType.OnePortWirelessFR.ToString ());
});
List linkList = new List ();
GatewayBase common = null;
string gateWayString = "";
if (gateWayList.Count > 0) {
foreach (var gatewayFileName in gateWayList) {
var tempStrings = gatewayFileName.Split ('_');
if (tempStrings [1].ToString () == DeviceType.OnePortBus.ToString () || tempStrings [1].ToString () == DeviceType.RCU.ToString () ||
tempStrings [1].ToString () == DeviceType.OnePortWirelessFR.ToString ()) {
gateWayString = CommonPage.MyEncodingUTF8.GetString (IO.FileUtils.ReadFile (gatewayFileName));
common = Newtonsoft.Json.JsonConvert.DeserializeObject (gateWayString);
var bytes = Control.ControlBytesSendHasReturn (Command.readGatewayVision, common.SubnetID, common.DeviceID, new byte [] { });
if(bytes == null) {
Application.RunOnMainThread (() => {
btnTipMsg.Text = "无法搜索到网关,请确定是否与网关在同一网络。";
btnTipMsg.TextColor = SkinStyle.Current.DelColor;
loading.Hide ();
});
return;
} else {
Application.RunOnMainThread (() => {
btnTipMsg.Text = "网关连接成功,正在检查网关固件。";
btnTipMsg.TextColor = SkinStyle.Current.TextColor;
});
}
var visionString = Encoding.GetEncoding ("gb2312").GetString (bytes);
int result = -99;
if (visionString.Contains( "Ind_V03.01U_2021/07/12")) {//FW_MCIP-L1_RF.01_V03.01_210712_STM32F107VCT6_U2_india_beta") {
Application.RunOnMainThread (() => {
btnTipMsg.Text = "网关固件已升级,正在初始化网关";
btnTipMsg.TextColor = SkinStyle.Current.TextColor;
});
result = 100;
} else {
Application.RunOnMainThread (() => {
btnTipMsg.Text = "正在升级网关";
btnTipMsg.TextColor = SkinStyle.Current.TextColor;
});
//需要升级一端口
List> upgradeData = ReadUpgradeData ();
for (int i = 0; i < 7; i++) {
Control.ControlBytesSend (Command.enjoyUpgrade, common.SubnetID, common.DeviceID, new byte [] { });
}
byte [] arrayTemp = new byte [4];
arrayTemp [0] = Convert.ToByte ((upgradeData.Count & 0xFF000000) >> 24);
arrayTemp [1] = Convert.ToByte ((upgradeData.Count & 0xFF0000) >> 16);
arrayTemp [2] = Convert.ToByte ((upgradeData.Count & 0xFF00) >> 8);
arrayTemp [3] = Convert.ToByte (upgradeData.Count & 0xFF);
//丢失次数
int lostCount = 0;
while (true) {
var ub = MainPage.GatewayStatus.Split ("_");
if (ub.Length > 1) {
result = Convert.ToInt32 (ub [1]);
//if (result < result0 && result0 < 100)
{
//result = result0;
Application.RunOnMainThread (() => {
btnTipMsg.Text = "正在升级网关 " + result + "/" + upgradeData.Count;
});
}
}
if (MainPage.GatewayStatus.Contains ("upgrading") && result == 0) {
SendUpgradeData (common.SubnetID, common.DeviceID, arrayTemp);
Application.RunOnMainThread (() => {
btnTipMsg.Text = "正在升级网关 " + result + "/" + upgradeData.Count;
});
} else if (result == -99) {
System.Threading.Thread.Sleep (100);
continue;
} else if (result == 100) {
Application.RunOnMainThread (() => {
btnTipMsg.Text = "网关升级成功.正在初始化网关";
btnTipMsg.TextColor = SkinStyle.Current.TextColor;
});
break;
}
//if (result == -1) {
// if (lostCount > 3) {
// Application.RunOnMainThread (() => {
// btnTipMsg.Text = "网关升级失败,请重试.";
// loading.Hide ();
// });
// return;
// }
//} else if (result == -2) {
// if (lostCount > 3) {
// Application.RunOnMainThread (() => {
// btnTipMsg.Text = "网关无响应。";
// loading.Hide ();
// });
// return;
// }
//} else if (result == 999999) {
// Application.RunOnMainThread (() => {
// btnTipMsg.Text = "网关升级成功.正在初始化网关";
// btnTipMsg.TextColor = SkinStyle.Current.TextColor;
// });
// break;
//}
else {
if (upgradeData.Count >= result) {
var listPack = upgradeData [result-1];//
byte [] packData = new byte [2 + listPack.Count];
packData [0] = Convert.ToByte (result / 256);
packData [1] = Convert.ToByte (result % 256 );
Array.Copy (listPack.ToArray (), 0, packData, 2, listPack.Count);
Console.WriteLine ("packId"+result);
SendUpgradeData (common.SubnetID, common.DeviceID, packData);
}
}
}
}
if (result == 100) {
//初始化网关
var initialBytes = new byte [12];
initialBytes [0] = 0x00;
initialBytes [1] = 0x00;
string [] mac = common.MAC.Split ('.');
for (int i = 0; i < mac.Length; i++)
initialBytes [i + 2] = Convert.ToByte (mac [i], 16);
initialBytes [10] = Convert.ToByte (common.SubnetID);
initialBytes [11] = 0x00;
//初始化网关命令发送
InitializationGateway (common.SubnetID, common.DeviceID, initialBytes);
while (true) {
if (MainPage.GatewayStatus != "Initialization_complete") {
System.Threading.Thread.Sleep (100);
} else {
Application.RunOnMainThread (() => {
btnTipMsg.Text = "初始化网关成功,正在打开网关远程配置。";
btnTipMsg.TextColor = SkinStyle.Current.TextColor;
});
break;
}
}
//开启网关远程
var setRemoteResult = SetGatewayRemote (common.SubnetID, common.DeviceID);
if (setRemoteResult) {
Application.RunOnMainThread (() => {
btnTipMsg.Text = "网关远程已开启,正在迁移账号信息。";
btnTipMsg.TextColor = SkinStyle.Current.TextColor;
});
} else {
Application.RunOnMainThread (() => {
btnTipMsg.Text = "网关远程开启失败。";
btnTipMsg.TextColor = SkinStyle.Current.DelColor;
});
return;
}
//迁移账号
var moveAccontResult = Account2New (pwd);
if (moveAccontResult) {
Application.RunOnMainThread (() => {
btnTipMsg.Text = "账号信息迁移成功,正在迁移住宅。";
btnTipMsg.TextColor = SkinStyle.Current.TextColor;
});
} else {
Application.RunOnMainThread (() => {
btnTipMsg.Text = "账号迁移失败。";
btnTipMsg.TextColor = SkinStyle.Current.DelColor;
});
return;
}
//迁移住宅,获取新的homeid
var newHomeId = Home2New ();
if (newHomeId == "") {
Application.RunOnMainThread (() => {
btnTipMsg.Text = "住宅迁移失败。";
btnTipMsg.TextColor = SkinStyle.Current.DelColor;
loading.Hide ();
});
return;
} else {
btnTipMsg.Text = "住宅迁移成功,正在配置网关住宅信息。";
btnTipMsg.TextColor = SkinStyle.Current.TextColor;
}
//写入homeId
var homeIdSendBytes = new byte [73];
var homeIdByte = new byte [36];
var homeStrs = newHomeId;// UserConfig.Instance.CurrentRegion.RegionID.ToString ();//需要使用新平台生成的homeId
for (int i = 0; i < UserConfig.Instance.CurrentRegion.RegionID.ToString ().Length; i++) {
homeIdByte [i] = Convert.ToByte (homeStrs [i].ToString (), 16);
}
Array.Copy (homeIdByte, homeIdSendBytes, homeIdByte.Length);
var writeHomeIdResult = WriteHomeId (common.SubnetID, common.DeviceID, homeIdSendBytes);
if (writeHomeIdResult) {
Application.RunOnMainThread (() => {
btnTipMsg.Text = "网关住宅信息配置成功,正在获取上网秘钥。";
btnTipMsg.TextColor = SkinStyle.Current.TextColor;
});
} else {
Application.RunOnMainThread (() => {
btnTipMsg.Text = "网关住宅信息配置失败.";
btnTipMsg.TextColor = SkinStyle.Current.DelColor;
});
}
//获取上网秘钥
var netKet = GetInternetAccessKey (common.MAC.Replace (".", ""));
if (!string.IsNullOrEmpty (netKet)) {
Application.RunOnMainThread (() => {
btnTipMsg.Text = "获取上网秘钥成功。正在写入";
btnTipMsg.TextColor = SkinStyle.Current.TextColor;
});
var secretkeySendBytes = new byte [netKet.Length + 1];
var secretkeyByte = new byte [netKet.Length];
for (int i = 0; i < netKet.Length; i++) {
secretkeyByte [i] = Convert.ToByte (netKet [i].ToString (), 16);
}
Array.Copy (secretkeyByte, 0, secretkeySendBytes, 1, secretkeyByte.Length);
secretkeySendBytes [0] = 1;
//写入上网秘钥
var writeSecretKeyResult = WriteSecretKey (common.SubnetID, common.DeviceID, secretkeySendBytes);
if (writeSecretKeyResult) {
Application.RunOnMainThread (() => {
btnTipMsg.Text = "上网秘钥写入成功,正在迁移网关。";
});
} else {
Application.RunOnMainThread (() => {
btnTipMsg.Text = "上网秘钥写入失败。";
btnTipMsg.TextColor = SkinStyle.Current.DelColor;
return;
});
}
} else {
Application.RunOnMainThread (() => {
btnTipMsg.Text = "上网秘钥获取失败。";
btnTipMsg.TextColor = SkinStyle.Current.DelColor;
});
}
//迁移网关
var moveGatewayResult = Gateway2New (common.MAC.Replace (".", ""), newHomeId);
if (moveGatewayResult) {
Application.RunOnMainThread (() => {
btnTipMsg.Text = "网关迁移成功,正在迁移备份数据。";
btnTipMsg.TextColor = SkinStyle.Current.TextColor;
});
} else {
Application.RunOnMainThread (() => {
btnTipMsg.Text = "网关迁移失败。";
btnTipMsg.TextColor = SkinStyle.Current.DelColor;
loading.Hide ();
});
return;
}
//创建迁移备份文件夹
var backId = moveFolder2New (newHomeId, newUserId);
//迁移备份文件
var moveFileResult = MoveFile2New (newHomeId, newUserId, backId);
if (moveFileResult == "true") {
//标记流程完成
//var markResult = Mark (newHomeId);
//if (markResult) {
Application.RunOnMainThread (() => {
btnTipTitle.Text = "备份数据迁移成功,平台迁移完成。";
//btnTipMsg.TextColor = SkinStyle.Current.TextColor;
btnTipMsg.Text = "";
etPwd.Visible = false;
loading.Hide ();
});
} else {
Application.RunOnMainThread (() => {
btnTipMsg.Text = "备份数据迁移失败。";
btnTipMsg.TextColor = SkinStyle.Current.DelColor;
loading.Hide ();
});
}
//}
}
Application.RunOnMainThread (() => {
});
break;//只升级一个
}
}
} else {
//没有网关
Application.RunOnMainThread (() => {
btnTipTitle.Text = "未检测到网关,请检查本地数据.";
});
return;
}
}) { IsBackground = true }.Start ();
//Control.ControlBytesSend (Command.enjoyUpgrade2, common.SubnetID, common.DeviceID, new byte [] { });
}
///
/// 读取固件数据
///
///
private static List> ReadUpgradeData ()
{
byte [] buffer = new byte [1024];
List> upgradeData = new List> ();
System.IO.Stream stream = Application.Activity.Assets.Open ("FW_MCIP-L1_RF.01_V03.01_210712_STM32F107VCT6_U2_india_beta.bin");
int length = 0;
try {
while ((length = stream.Read (buffer, 0, buffer.Length)) != 0) {
List bbb = new List ();
for (int i = 0; i < length; i++) {
bbb.Add (buffer [i]);
}
upgradeData.Add (bbb);
}
} catch {
} finally {
stream.Close ();
}
return upgradeData;
}
///
/// 发送网关固件数据
///
///
///
///
///
public void SendUpgradeData (byte subnetId,byte deviceId,byte[] sendByets)
{
var resutl = Control.ControlBytesSendHasReturn (Command.enjoyUpgrade2, subnetId, deviceId, sendByets);
//if(resutl == null) {
// return -2;
//}
//if (resutl.Length == 0) {
// return -1;
//}
//if (resutl [0] == 0xF8) {
// return 999999;
//} else {
// if (resutl.Length > 1) {
// var packId = resutl [0] * 256 + resutl [1];
// Console.WriteLine ("packId:"+packId);
// return packId;
// }
// return 1;
//}
}
///
/// 初始化网关
///
///
///
private void InitializationGateway(byte subnetId, byte deviceId, byte [] sendByets)
{
Control.ControlBytesSend (Command.InitializationGateway, subnetId, deviceId, sendByets);
}
///
/// 设备网关开启远程
///
private bool SetGatewayRemote(byte subnetId, byte deviceId)
{
var sendByte = new byte [67];
sendByte [0] = 4;
var result = Control.ControlBytesSendHasReturn (Command.SetGateWayModelInfo, subnetId, deviceId, sendByte);
if (result == null) {
} else {
if(result.Length>0 && result[0] == 248) {
return true;
}
}
return false;
}
///
/// 写入homeId
///
///
///
///
private bool WriteHomeId (byte subnetId, byte deviceId, byte [] sendByets)
{
var result = Control.ControlBytesSendHasReturn (Command.WriteHomeId, subnetId, deviceId, sendByets);
if (result == null)
return false;
if (result.Length == 3) {
if (result [2] == 0xF8) {
return true;
}
} else if (result.Length == 1) {
if (result [0] == 0xF8) {
return true;
}
}
return false;
}
///
/// 写入上网秘钥
///
///
///
///
///
private bool WriteSecretKey (byte subnetId, byte deviceId, byte [] sendByets)
{
var result = Control.ControlBytesSendHasReturn (Command.WriteSecretKey, subnetId, deviceId, sendByets);
if (result!= null ) {
return true;
//if (result.Length > 26) {
// if (result [26] == 0xF8) {
// return true;
// }
//} else {
// if (result.Length > 3) {
// if (result [1] == 0xF8) {
// return true;
// }
// }
//}
}
return false;
}
}
}