using System;
|
using System.Collections.Generic;
|
using System.Threading;
|
|
namespace Shared.SimpleControl.Phone
|
{
|
public class GuideSettingGateway : FrameLayout
|
{
|
|
static byte TYPEStandardMQTT = 5;
|
static byte TYPEHdlMQTT = 4;
|
static GuideSettingGateway curView;
|
static GatewayBase gatewayDeicve;
|
static VerticalScrolViewLayout bodyView;
|
static Button btnCloseLoading;
|
static Loading myLoading;
|
bool isReBind = false;
|
static List<Common> addedCommon;
|
static bool needRefresh;
|
public GuideSettingGateway (GatewayBase gd)
|
{
|
gatewayDeicve = gd as GatewayBase;
|
addedCommon = new List<Common> ();
|
BackgroundColor = SkinStyle.Current.MainColor;
|
curView = this;
|
myLoading = new Loading ();
|
|
myLoading.TextColor = 0xFFA9A9A9;
|
myLoading.LodingBackgroundColor = SkinStyle.Current.MainColor;
|
|
//#if __IOS__
|
//myLoading.LodingBackgroundColor = 0x20999999;
|
//#else
|
// myLoading.LodingBackgroundColor = SkinStyle.Current.ViewColor;
|
//#endif
|
btnCloseLoading = new Button ();
|
btnCloseLoading.MouseUpEventHandler += (sender, e) => {
|
Application.RunOnMainThread (() => {
|
myLoading.Hide ();
|
btnCloseLoading.RemoveFromParent ();
|
WirelessConfig (new byte [] { 1 });//close
|
//2020-07-02 关闭自动搜索线程
|
StopSearchDeviceThead ();
|
if (needRefresh) {
|
needRefresh = false;
|
SearchDeviceList ();
|
}
|
});
|
};
|
|
}
|
|
public override void RemoveFromParent ()
|
{
|
curView = null;
|
addedCommon.Clear ();
|
BackgroundColor = SkinStyle.Current.MainColor;
|
//StopSearchDeviceThead ();
|
base.RemoveFromParent ();
|
}
|
|
public void ShowPage ()
|
{
|
#region 标题
|
var topView = new FrameLayout () {
|
Y = Application.GetRealHeight (36),
|
Height = Application.GetRealHeight (90),
|
Width = Application.GetRealWidth (640),
|
};
|
AddChidren (topView);
|
|
var title = new Button () {
|
TextAlignment = TextAlignment.Center,
|
Text = gatewayDeicve.Name,
|
TextColor = SkinStyle.Current.TextColor1,
|
TextSize = 19,
|
};
|
topView.AddChidren (title);
|
|
var back = new Button () {
|
Height = Application.GetRealHeight (90),
|
Width = Application.GetRealWidth (85),
|
UnSelectedImagePath = "Item/Back.png",
|
SelectedImagePath = "Item/BackSelected.png",
|
};
|
topView.AddChidren (back);
|
|
//Refresh.png
|
back.MouseUpEventHandler += (sender, e) => {
|
(Parent as PageLayout).PageIndex -= 1;
|
|
if (gatewayDeicve.MAC.Replace (".", "") == UserConfig.Instance.GatewayMAC || MainPage.LoginUser.AccountString == "464027401@qq.com") {
|
var delFile = IO.FileUtils.ReadFiles ().FindAll ((w) => {
|
return (w.Split ('_') [0] == "Equipment") && (w.Split ('_') [2] == gatewayDeicve.SubnetID.ToString ());
|
});
|
for (int k = 0; k < delFile.Count; k++) {
|
IO.FileUtils.DeleteFile (delFile [k]);
|
}
|
}
|
};
|
|
var btnSearch = new Button () {
|
Width = Application.GetMinRealAverage (75),
|
Height = Application.GetMinRealAverage (75),
|
X = Application.GetRealWidth (520),
|
Y = Application.GetRealHeight (10),
|
UnSelectedImagePath = "Item/Refresh.png",
|
};
|
topView.AddChidren (btnSearch);
|
btnSearch.MouseUpEventHandler += (sd, fs) => {
|
bodyView.RemoveAll ();
|
addedCommon.Clear ();
|
SearchDeviceList ();
|
};
|
#endregion
|
|
bodyView = new VerticalScrolViewLayout () {
|
Y = topView.Bottom,
|
Height = Application.GetRealHeight (1136 - 126 - 95),
|
BackgroundColor = SkinStyle.Current.ViewColor,
|
};
|
AddChidren (bodyView);
|
|
var bottomView = new FrameLayout () {
|
BackgroundColor = SkinStyle.Current.Black50Transparent,
|
Y = bodyView.Bottom,
|
Height = Application.GetRealHeight (100),
|
};
|
AddChidren (bottomView);
|
|
var btnAssignRoom = new Button () {
|
Y = 1,
|
BackgroundColor = SkinStyle.Current.ButtonColor,
|
Text = "Assign Room",
|
TextSize = 16,
|
TextAlignment = TextAlignment.Center,
|
TextColor = SkinStyle.Current.TextColor1
|
};
|
bottomView.AddChidren (btnAssignRoom);
|
btnAssignRoom.MouseUpEventHandler += (sender, e) => {
|
//bodyView.RemoveAll ();
|
|
btnAssignRoomAction ();
|
};
|
SearchDeviceList ();
|
}
|
|
/// <summary>
|
/// 判断设备类型 兼容旧设备
|
/// </summary>
|
void btnAssignRoomAction ()
|
{
|
if (gatewayDeicve.Type == DeviceType.OnePortMqttFR) {
|
|
Utlis.WriteLine ("Assign OnePortMqttFR,HDL MQTT");
|
//RemoteSetting ();
|
RemoteSettingWithHDLMQTT ();
|
} else {
|
Utlis.WriteLine ("Assign OnePortWirelessFR");
|
//RemoteSettingWithOldDevice ();
|
//RemoteSettingWithMQTT ();
|
}
|
}
|
|
|
|
|
/// <summary>
|
/// 开关无线配置
|
/// </summary>
|
void WirelessConfig (byte [] switchStatus)
|
{
|
Control.ControlBytesSend (Command.CheckConfigSuccess, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, switchStatus);
|
}
|
|
/// <summary>
|
/// 搜索功能模块设备
|
/// </summary>
|
void SearchDeviceList ()
|
{
|
//var delFile = IO.FileUtils.ReadFiles ().FindAll ((w) => {
|
// return (w.Split ('_') [0] == "Equipment") && (w.Split ('_') [2] == gatewayDeicve.SubnetID.ToString ());
|
//});
|
//for (int k = 0; k < delFile.Count; k++) {
|
// IO.FileUtils.DeleteFile (delFile [k]);
|
//}
|
//MainPage.Loading.Start ("正在搜索设备列表。。。");
|
MainPage.Loading.Start ("Searching for existing Devices...");
|
CommonPage.DeviceList.Clear ();
|
int countDevcie = 0;
|
//全部重新更新设备下面所有的回路信息
|
if (th != null) {
|
th.Abort ();
|
th = null;
|
}
|
ReadDevices (() => {
|
MainPage.Loading.Hide ();
|
for (int i = 0; i < CommonPage.DeviceList.Count; i++) {
|
Common common = CommonPage.DeviceList [i];
|
|
if (common.Type == DeviceType.MusicModel)
|
continue;
|
if (common.isMixBox) {
|
Utlis.WriteLine ("IsMixBox");
|
continue;
|
}
|
countDevcie++;
|
}
|
|
notLoopCommons.Clear ();
|
notLoopCommons.AddRange (CommonPage.DeviceList);
|
|
th = new Thread ((obj) => {
|
Waiting = false;
|
CommonPage.DeviceLoopList.Clear ();
|
var listCommonEquipment = new List<Common> ();
|
listCommonEquipment.AddRange (notLoopCommons);
|
int progress = 0;
|
for (int i = 0; i < listCommonEquipment.Count; i++) {
|
var common = listCommonEquipment [i];
|
if (common == null)
|
continue;
|
Application.RunOnMainThread (() => {
|
if (!common.isMixBox) {
|
progress++;
|
}
|
MainPage.Loading.Start ("Loading:" + common.Name + " ,Progress " + progress.ToString () + "/" + CommonPage.searchTotal.ToString ());
|
});
|
byte bigClass = common.BigClass;
|
byte minClass = common.MinClass;
|
int count = common.LoopCount;
|
for (int k = 1, j = 0; k <= count; k++) {
|
while (Waiting) {
|
Thread.Sleep (100);
|
}
|
if (Control.ControlBytesSendHasReturn (Command.ReadDeviceLoopInfo, common.SubnetID, common.DeviceID, new byte [] { bigClass, minClass, (byte)k }) == null) {
|
j++;
|
//连续两次读取不到数据则跳出该循环,代表设备离线2020-01-11
|
if (j == 2 && k == 2)
|
break;
|
} else {
|
notLoopCommons.Remove (common);
|
}
|
}
|
}
|
th = null;
|
Application.RunOnMainThread (() => {
|
MainPage.Loading.Hide ();
|
if (gatewayDeicve.Type == DeviceType.OnePortWirelessFR || gatewayDeicve.Type == DeviceType.OnePortMqttFR) {
|
Control.ControlBytesSend (Command.GotoConfigMode, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, new byte [] { });
|
//new Alert ("", "网关已经进入配频模式,请手动新设备进入配频模式。", "Close").Show ();
|
new Alert ("", ErrorCode.SearchingForNewDevices, "OK").Show ();
|
|
this.AddChidren (myLoading);
|
//myLoading.Start ("点击屏幕关闭配频模式");
|
myLoading.Start (ErrorCode.SearchingExitTheSearchingMode);
|
//myLoading.Start ("Waiting for new device, Please touch the here to exit the waiting mode.");
|
|
this.AddChidren (btnCloseLoading);
|
|
//2020-07-02 网关配频模式时,开启自动搜索线程
|
StartSearchDeviceThead ();
|
}
|
});
|
});
|
th.Start ();
|
});
|
}
|
static List<Common> notLoopCommons = new List<Common> ();
|
|
public static void InitSearchDeviceList (Common common, bool config = false)
|
{
|
if (curView == null || addedCommon == null)
|
return;
|
lock (addedCommon) {
|
if (addedCommon.Find ((obj) => obj.Type == common.Type && obj.CommonLoopID == common.CommonLoopID) != null)
|
return;
|
addedCommon.Add (common);
|
if (common.SubnetID == gatewayDeicve.SubnetID) {
|
var deviceView = new RowLayout () {
|
Height = Application.GetRealHeight (110),
|
};
|
bodyView.AddChidren (deviceView);
|
#region Show Device
|
var btnIndex = new Button () {
|
Width = Application.GetRealWidth (120),
|
TextColor = SkinStyle.Current.TextColor1,
|
TextAlignment = TextAlignment.Center,
|
Text = bodyView.ChildrenCount.ToString ()
|
};
|
deviceView.AddChidren (btnIndex);
|
|
var btnDeviceName = new Button () {
|
X = btnIndex.Right,
|
Text = common.Name,
|
TextAlignment = TextAlignment.CenterLeft,
|
Width = Application.GetRealWidth (342),
|
TextColor = SkinStyle.Current.TextColor1,
|
};
|
deviceView.AddChidren (btnDeviceName);
|
|
//var btnDeviceOnline = new Button () {
|
// X = Application.GetRealWidth(500),
|
// Y = Application.GetRealHeight(70),
|
// Width = Application.GetRealWidth(100),
|
// Height = Application.GetRealHeight(40),
|
// TextSize = 12,
|
// Text = "Online",
|
// TextColor = 0xFF5DBA46,
|
// TextAlignment = TextAlignment.CenterLeft
|
//};
|
//deviceView.AddChidren (btnDeviceOnline);
|
//if (!common.Online) {
|
// btnDeviceOnline.Text = "Offline";
|
// btnDeviceOnline.TextColor = SkinStyle.Current.DelColor;
|
//}
|
|
EventHandler<MouseEventArgs> eHandler = (sender, e) => {
|
Dialog dialog = new Dialog ();//修改设备备注
|
FrameLayout dialogBodyView = new FrameLayout () {
|
BackgroundColor = SkinStyle.Current.DialogColor,
|
Width = Application.GetRealWidth (494),
|
Height = Application.GetRealHeight (488),
|
Radius = 5,
|
BorderColor = SkinStyle.Current.Transparent,
|
BorderWidth = 0,
|
Gravity = Gravity.Center
|
};
|
dialog.AddChidren (dialogBodyView);
|
Button btnEquipmentTitle = new Button () {
|
Height = Application.GetRealHeight (70),
|
TextAlignment = TextAlignment.Center,
|
Text = "Device Setting",
|
BackgroundColor = SkinStyle.Current.DialogTitle,
|
TextColor = SkinStyle.Current.TextColor1,
|
};
|
dialogBodyView.AddChidren (btnEquipmentTitle);
|
Button lblChangeName = new Button () {
|
X = Application.GetRealWidth (40),
|
Y = btnEquipmentTitle.Bottom + Application.GetRealHeight (10),
|
Width = dialog.Width - 2 * btnEquipmentTitle.X,
|
Height = btnEquipmentTitle.Height,
|
TextID = R.MyInternationalizationString.EquipmentName,
|
TextAlignment = TextAlignment.CenterLeft,
|
TextColor = SkinStyle.Current.TextColor
|
};
|
dialogBodyView.AddChidren (lblChangeName);
|
EditText btnChangeName = new EditText () {
|
X = lblChangeName.X,
|
Y = lblChangeName.Bottom + Application.GetRealHeight (10),
|
Width = Application.GetRealWidth (425),
|
Height = Application.GetRealHeight (50),
|
TextAlignment = TextAlignment.CenterLeft,
|
Text = " " + common.Name,
|
BorderColor = SkinStyle.Current.BorderColor,
|
BorderWidth = 1,
|
Radius = 5,
|
TextColor = SkinStyle.Current.TextColor,
|
};
|
dialogBodyView.AddChidren (btnChangeName);
|
|
Button btnFindDevcie = new Button () {
|
Y = btnChangeName.Bottom + Application.GetRealHeight (50),
|
Gravity = Gravity.CenterHorizontal,
|
Width = Application.GetRealWidth (300),
|
Height = Application.GetRealHeight (70),
|
TextID = R.MyInternationalizationString.PositioningEquipment,
|
TextAlignment = TextAlignment.Center,
|
Radius = (uint)Application.GetRealHeight (5),
|
BackgroundColor = SkinStyle.Current.ButtonColor,
|
SelectedBackgroundColor = SkinStyle.Current.SelectedColor,
|
TextColor = SkinStyle.Current.TextColor1
|
};
|
dialogBodyView.AddChidren (btnFindDevcie);
|
btnFindDevcie.MouseDownEventHandler += (sender1, e1) => {
|
btnFindDevcie.IsSelected = true;
|
};
|
btnFindDevcie.MouseUpEventHandler += (sender1, e1) => {
|
btnFindDevcie.IsSelected = false;
|
//查找设备,发送命令让设备闪烁,面板设备与其他设备发送的数据不一样,多了一个byte,固定为:0
|
Control.ControlBytesSend (Command.PositioningEquipment, common.SubnetID, common.DeviceID, new byte [] { 5 });
|
};
|
//---bottom--
|
Button btnBack = new Button () {
|
Width = Application.GetRealWidth (250),
|
Height = Application.GetRealHeight (85),
|
Y = Application.GetRealHeight (490 - 85),
|
BackgroundColor = SkinStyle.Current.DialogTitle,
|
TextID = R.MyInternationalizationString.Close,
|
TextColor = SkinStyle.Current.TextColor1
|
};
|
btnBack.MouseUpEventHandler += (s, e1) => {
|
dialog.Close ();
|
};
|
dialogBodyView.AddChidren (btnBack);
|
Button btnLineV = new Button () {
|
Width = 1,
|
Height = btnBack.Height,
|
BackgroundColor = SkinStyle.Current.White20Transparent,
|
X = btnBack.Right,
|
Y = btnBack.Y,
|
};
|
dialogBodyView.AddChidren (btnLineV);
|
Button btnSave = new Button () {
|
Width = Application.GetRealWidth (250),
|
Height = Application.GetRealHeight (85),
|
X = btnLineV.Right,
|
Y = btnBack.Y,
|
TextID = R.MyInternationalizationString.SAVE,
|
TextColor = SkinStyle.Current.TextColor1,
|
BackgroundColor = SkinStyle.Current.DialogTitle,
|
};
|
dialogBodyView.AddChidren (btnSave);
|
btnSave.MouseUpEventHandler += (sender2, e2) => {
|
//CommonPage.UpdateRemark (common.SubnetID, common.DeviceID, btnChangeName.Text.Trim ());
|
|
byte [] updateBytes = new byte [20];
|
string changeName = btnChangeName.Text.Trim ();
|
byte [] remakeBytes = CommonPage.MyEncodingGB2312.GetBytes (changeName);
|
Array.Copy (remakeBytes, 0, updateBytes, 0, remakeBytes.Length < 20 ? remakeBytes.Length : 20);
|
Control.ControlBytesSend (Command.Write_DeviceRamarkCMD, common.SubnetID, common.DeviceID, updateBytes);
|
|
//修改云端备注
|
new Thread (() => {
|
var http = new HttpServerRequest ();
|
http.EditDeviceName (common.DeviceID.ToString(), UserConfig.Instance.CurrentRegion.Id,changeName);
|
}) { IsBackground = true }.Start ();
|
|
|
btnDeviceName.Text = btnChangeName.Text;
|
dialog.Close ();
|
common.Name = btnChangeName.Text.Trim ();
|
IO.FileUtils.SaveEquipmentMessage (common);
|
};
|
dialog.Show ();
|
|
};
|
if (common.Type != DeviceType.InfraredMode) {
|
btnDeviceName.MouseUpEventHandler += eHandler;
|
}
|
|
var btnDelDevice = new Button () {
|
TextID = R.MyInternationalizationString.Del,
|
BackgroundColor = SkinStyle.Current.DelColor,
|
};
|
deviceView.AddRightView (btnDelDevice);
|
|
btnDelDevice.MouseUpEventHandler += (sender, e) => {
|
Alert alert = new Alert ("", Language.StringByID (R.MyInternationalizationString.TipDeleteEquipmentMessage)
|
, Language.StringByID (R.MyInternationalizationString.Cancel), Language.StringByID (R.MyInternationalizationString.Confrim));
|
alert.Show ();
|
alert.ResultEventHandler += (sender1, e1) => {
|
if (e1) {
|
MainPage.Loading.Start ("Please wait...");
|
System.Threading.Tasks.Task.Run (() => {
|
try {
|
var delRev = Control.ControlBytesSendHasReturn (Command.DelDeviceCMD, common.SubnetID, 0, new byte [] { 1, 0, 0, 0, 0, 0, 0, 0, 0, common.SubnetID, common.DeviceID });
|
|
string delFlag = "_" + common.SubnetID + "_" + common.DeviceID;
|
var delFile = IO.FileUtils.ReadFiles ().FindAll ((w) => {
|
return w.Contains (delFlag);
|
});
|
foreach (var roomToDelDevice in Room.Lists) {
|
var dett = roomToDelDevice.DeviceFilePathList.FindAll ((xxs) => {
|
return xxs.Contains (delFlag);
|
});
|
if (dett != null) {
|
foreach (var delT in dett) {
|
roomToDelDevice.DeviceFilePathList.Remove (delT);
|
}
|
roomToDelDevice.Save (roomToDelDevice.RoomFilePath);
|
}
|
}
|
|
for (int i = 0; i < delFile.Count; i++) {
|
IO.FileUtils.DeleteFile (delFile [i]);
|
}
|
Application.RunOnMainThread (() => {
|
btnDelDevice.Parent.RemoveFromParent ();
|
});
|
} catch (Exception ex) {
|
Utlis.WriteLine (ex.Message);
|
} finally {
|
Application.RunOnMainThread (() => {
|
MainPage.Loading.Hide ();
|
});
|
}
|
});
|
}
|
};
|
};
|
|
//if (common.Type == DeviceType.InfraredMode) {
|
// notLoopCommons = new List<Common> ();
|
// EventHandler<MouseEventArgs> mouseEvent = (sender, e) => {
|
// //new SystemEquipmentBase ().EquipmentBaseViewShow (common);
|
// var systemEquipmentBase = new GuideSettingInfraredModule ();
|
// systemEquipmentBase.Show ();
|
// systemEquipmentBase.EquipmentBaseViewShow (common);
|
// };
|
// deviceView.MouseUpEventHandler += mouseEvent;
|
//}
|
|
if (config) {
|
needRefresh = true;
|
//System.Threading.Tasks.Task.Factory.StartNew (() => {
|
// try {
|
// byte bigClass = common.BigClass;
|
// byte minClass = common.MinClass;
|
// int count = common.LoopCount;
|
// for (int k = 1, j = 0; k <= count; k++) {
|
// if (Control.ControlBytesSendHasReturn (Command.ReadDeviceLoopInfo, common.SubnetID, common.DeviceID, new byte [] { bigClass, minClass, (byte)k }) == null) {
|
// j++;
|
// //连续两次读取不到数据则跳出该循环
|
// if (j == 2 && k == 2)
|
// break;
|
// }
|
// }
|
// } catch (Exception ex) {
|
// Utlis.WriteLine ("Cinfig Read Device Loop Info " + ex.Message);
|
// }
|
//});
|
}
|
|
#endregion
|
} else {
|
CommonPage.searchTotal--;
|
}
|
}
|
}
|
Thread th;
|
//等待红外读取红外类型
|
bool Waiting;
|
|
void ReadDevices (Action action)
|
{
|
new System.Threading.Thread (() => {
|
try {
|
//这里搜索设备,直到完成就退出
|
readDevice ();
|
} catch (Exception ex) {
|
Utlis.WriteLine (ex.Message);
|
}
|
Application.RunOnMainThread (action);
|
}) { IsBackground = true }.Start ();
|
}
|
|
///// <summary>
|
///// 2020-02-11
|
///// 搜索重复次数 由4次增加为6次
|
///// </summary>
|
//static readonly int READ_COUNT = 6;
|
/// <summary>
|
/// 读取设备
|
/// </summary>
|
void readDevice ()
|
{
|
CommonPage.FindGateway = false;
|
CommonPage.FindGatewayChilren = true;
|
CommonPage.LocalPhoneFindDevice = true;
|
CommonPage.RandomHigh = (byte)new Random ().Next (255);
|
CommonPage.RandomLow = (byte)new Random ().Next (255);
|
CommonPage.DeviceList.Clear ();
|
CommonPage.searchTotal = 0;
|
//如果两次都没有数据反馈,就不读取
|
//2020 - 02 - 11搜索重复次数 由4次增加为6次
|
int readCount = 6;
|
while (0 < readCount) {
|
readCount--;
|
var ms = new System.IO.MemoryStream ();
|
ms.WriteByte (CommonPage.RandomHigh);
|
ms.WriteByte (CommonPage.RandomLow);
|
|
var list = CommonPage.DeviceList;
|
int tempCount = list.Count;
|
string s = ";";
|
for (int i = 0; i < list.Count; i++) {
|
Common common = list [i];
|
if (common.SubnetID != gatewayDeicve.SubnetID)
|
continue;
|
if (s.Contains (";" + common.SubnetID.ToString () + ":" + common.DeviceID.ToString ()))
|
continue;
|
s += common.SubnetID.ToString () + ":" + common.DeviceID.ToString () + ";";
|
ms.WriteByte (common.SubnetID);
|
ms.WriteByte (common.DeviceID);
|
}
|
Utlis.WriteLine ("搜索非网关设备,网络地址是:" + CommonPage.EndPoint.ToString ());
|
var control = new Control ();
|
control.Send (new Target () {
|
IPEndPoint = CommonPage.EndPoint,
|
Command = Command.ReadDeviceModul,
|
SubnetID = gatewayDeicve.SubnetID,
|
DeviceID = 0xFF,
|
AddData = ms.ToArray ()
|
}, SendCount.Zero, false);
|
|
ms.Close ();
|
|
System.Threading.Thread.Sleep (1000);
|
//如果数量不相等,就重置次数
|
if (tempCount != CommonPage.DeviceList.Count) {
|
readCount = 6;
|
}
|
}
|
CommonPage.DeviceList = CommonPage.DeviceList.FindAll ((obj) => { return obj != null && obj.SubnetID == gatewayDeicve.SubnetID; });
|
CommonPage.LocalPhoneFindDevice = false;
|
CommonPage.FindGatewayChilren = false;
|
}
|
|
|
|
///// <summary>
|
///// 远程配置
|
///// </summary>
|
//void RemoteSettingWithOldDevice ()
|
//{
|
// byte [] gatewayBytes = null;
|
// byte [] serverIPBytes = null;
|
|
// string [] strServerIP = (MainPage.SeviceIP).Split ('.');
|
// string [] strServerIP1 = (MainPage.SeviceIP).Split ('.');
|
// MainPage.Loading.Start ("Configuring device...");
|
// System.Threading.Tasks.Task.Run (() => {
|
// try {
|
|
// //var currentRegionIdBytes = new byte [] {
|
// // (byte)(UserConfig.Instance.CurrentRegion.RegionID / 256 / 256 / 256),
|
// // (byte)(UserConfig.Instance.CurrentRegion.RegionID / 256 / 256),
|
// // (byte)(UserConfig.Instance.CurrentRegion.RegionID / 256),
|
// // (byte)(UserConfig.Instance.CurrentRegion.RegionID % 256),
|
// //};
|
|
|
|
|
// //var currentRegionIdBytes = Utlis.IntToByteArray (UserConfig.Instance.CurrentRegion.RegionID);
|
|
// byte [] RegionIDBytes = CommonPage.MyEncodingGB2312.GetBytes (UserConfig.Instance.CurrentRegion.Id.ToString ());
|
// var currentRegionIdBytes = new byte [4];
|
// Array.Copy (RegionIDBytes, 0, currentRegionIdBytes, 0, 4 < RegionIDBytes.Length ? 4 : RegionIDBytes.Length);
|
|
// gatewayBytes = Control.ControlBytesSendHasReturn (Command.ReadGateWayModelInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, new byte [] { });
|
// serverIPBytes = Control.ControlBytesSendHasReturn (Command.ReadGatewayServerIP, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, new byte [] { });
|
|
// 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;
|
// 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);
|
|
|
// byte [] macAddress = Control.ControlBytesSendHasReturn (Command.ReadDeviceMac, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, new byte [] { });
|
|
// gatewayBytes [0] = 2;//远程标示
|
// Array.Copy (ggn, 0, gatewayBytes, 1, 20 < ggn.Length ? 20 : ggn.Length);
|
// Array.Copy (gpn, 0, gatewayBytes, 21, 20 < gpn.Length ? 20 : gpn.Length);
|
// Array.Copy (gun, 0, gatewayBytes, 41, 8 < gun.Length ? 8 : gun.Length);
|
// Array.Copy (gpw, 0, gatewayBytes, 49, 8 < gpw.Length ? 8 : gpw.Length);
|
|
// Control.ControlBytesSend (Command.SetGateWayModelInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, gatewayBytes);
|
|
// serverIPBytes [0] = Convert.ToByte (strServerIP [0]);
|
// serverIPBytes [1] = Convert.ToByte (strServerIP [1]);
|
// serverIPBytes [2] = Convert.ToByte (strServerIP [2]);
|
// serverIPBytes [3] = Convert.ToByte (strServerIP [3]);
|
// int point1 = 9999;
|
|
// serverIPBytes [4] = Convert.ToByte (point1 / 256);
|
// serverIPBytes [5] = Convert.ToByte (point1 % 256);
|
|
// serverIPBytes [6] = Convert.ToByte (strServerIP1 [0]);
|
// serverIPBytes [7] = Convert.ToByte (strServerIP1 [1]);
|
// serverIPBytes [8] = Convert.ToByte (strServerIP1 [2]);
|
// serverIPBytes [9] = Convert.ToByte (strServerIP1 [3]);
|
// int point2 = 9999;
|
|
// serverIPBytes [10] = Convert.ToByte (point2 / 256);
|
// serverIPBytes [11] = Convert.ToByte (point2 % 256);
|
|
// //远程ip地址的设置
|
// var mobytes = Control.ControlBytesSendHasReturn (Command.SetGateWayModelInternetInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, serverIPBytes);
|
// if (mobytes == null) {
|
// Application.RunOnMainThread (() => {
|
// new Alert ("", ErrorCode.GatewayNoResponse,
|
// Language.StringByID (R.MyInternationalizationString.Close)).Show ();
|
// });
|
// } else if (mobytes [0] == 0xF5) {
|
// Application.RunOnMainThread (() => {
|
// new Alert ("", Language.StringByID (R.MyInternationalizationString.OperationFailed),
|
// Language.StringByID (R.MyInternationalizationString.Close)).Show ();
|
// });
|
// } else {
|
// if (gatewayDeicve.Type == DeviceType.OnePortWirelessFR) {
|
// var bindReginIdStatus = Control.ControlBytesSendHasReturn (Command.Write_APP_Data_STORE_1D5E_CMD, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, currentRegionIdBytes);
|
// if (bindReginIdStatus == null) {
|
// Application.RunOnMainThread (() => {
|
// new Alert ("", ErrorCode.GatewayNoResponse, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
|
// });
|
// return;
|
// } else if (bindReginIdStatus [0] != 0xF8) {
|
// //new Alert ("", "网关绑定住宅失败!", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
|
// Application.RunOnMainThread (() => {
|
// new Alert ("", "Gateway failed to bind residence!", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
|
// });
|
// return;
|
// }
|
// }
|
// GotoHomePage ();
|
// }
|
// } catch (Exception ex) {
|
// Utlis.WriteLine (ex.Message);
|
// Application.RunOnMainThread (() => {
|
// new Alert ("", Language.StringByID (R.MyInternationalizationString.OperationFailed), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
|
// });
|
// bool canRemove = false;
|
// } finally {
|
// Application.RunOnMainThread (() => {
|
// MainPage.Loading.Hide ();
|
// //this.RemoveAll ();
|
// });
|
// }
|
// });
|
// //Room.InitAllRoom ();
|
//}
|
|
|
|
/// <summary>
|
/// 远程配置 HDL MQTT模式
|
/// </summary>
|
void RemoteSettingWithHDLMQTT ()
|
{
|
byte [] gatewayBytes = null;
|
string [] strServerIP = (MainPage.SeviceIP).Split ('.');
|
string [] strServerIP1 = (MainPage.SeviceIP).Split ('.');
|
MainPage.Loading.Start ("Configuring gateway...");
|
System.Threading.Tasks.Task.Run (() => {
|
try {
|
|
////读取一端口交换机、无线网关的配置信息
|
//gatewayBytes = Control.ControlBytesSendHasReturn (Command.ReadGateWayModelInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, new byte [] { });
|
//if (gatewayBytes == null) {
|
// //读取一端口交换机、无线网关的配置信息失败
|
// new Alert ("", "Read gateway configuration failed!", "Close").Show ();
|
// return;
|
//}
|
|
//1.修改用户名信息 和修改连接模式
|
if (SetGateWayModelInfo()) {
|
//2.修改管理信息
|
if (SetGateWayAdminInfo ()) {
|
//3.写Mqtt远程域名地址
|
if (SetGateWayMqttUrlAddress ()) {
|
//4.写配置成功后下一步操作
|
GatewaySettingSucceeded ();
|
}
|
}
|
}
|
|
} catch (Exception ex) {
|
Utlis.WriteLine (ex.Message);
|
Application.RunOnMainThread (() => {
|
//修改网关连接模式失败
|
new Alert ("", "Failed to modify gateway connection mode!", 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);
|
|
|
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);
|
return CheckIsSuccessfulWithBytes (backBytes, "Failed to modify gateway connection mode.");
|
}
|
|
/// <summary>
|
/// 修改管理员信息
|
/// </summary>
|
/// <param name="adminBytes"></param>
|
/// <returns></returns>
|
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.Id.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);
|
}
|
|
/// <summary>
|
/// 修改管理员信息
|
/// </summary>
|
/// <param name="adminBytes"></param>
|
/// <returns></returns>
|
bool SetGateWayAdminInfo ()
|
{
|
//byte [] name = CommonPage.MyEncodingGB2312.GetBytes (MainPage.LoginUser.AccountString);
|
byte [] name = CommonPage.MyEncodingGB2312.GetBytes (UserConfig.Instance.HomeGateway.gatewayId);//帐号名改成gatewayId
|
byte [] currentRegionIdBytes = CommonPage.MyEncodingGB2312.GetBytes (UserConfig.Instance.CurrentRegion.Id.ToString ());
|
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);
|
|
return CheckIsSuccessfulWithBytes (backBytes, "Failed to modify gateway administrator information!");
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="bytes"></param>
|
/// <returns></returns>
|
private byte GetCheckSum (byte [] bytes)
|
{
|
//byte checksum = 0x00;
|
//foreach (byte bt in bytes) {
|
// checksum ^= bt;
|
//}
|
|
int iSum = 0;
|
for (int i = 0; i < bytes.Length; i++) {
|
iSum += bytes [i];
|
}
|
return (byte)(0xff & (0x100 - iSum));
|
}
|
|
/// <summary>
|
/// 修改Mqtt域名地址
|
/// </summary>
|
/// <returns></returns>
|
bool SetGateWayMqttUrlAddress ()
|
{
|
byte [] utlBytes = new byte [65];
|
var url = new Uri (UserConfig.Instance.CurrentRegion.regionUrl);
|
var host = "";
|
if (url != null) {
|
host = url.Host;
|
}
|
byte [] hostBytes = CommonPage.MyEncodingGB2312.GetBytes (host);
|
Array.Copy (hostBytes, 0, utlBytes, 0, 64 < hostBytes.Length ? 64 : hostBytes.Length);
|
var sum = GetCheckSum (hostBytes);
|
utlBytes [64] = sum;//校验位
|
byte [] backBytes = Control.ControlBytesSendHasReturn (Command.SetGateWayMqttURLAddress, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, utlBytes);
|
|
return CheckIsSuccessfulWithBytes (backBytes, "Failed to modify gateway remote address!");
|
}
|
|
|
/// <summary>
|
/// 判断是否修复成功 F8 = 成功; F5=失败
|
/// </summary>
|
/// <param name="backBytes"></param>
|
/// <returns></returns>
|
private bool CheckIsSuccessfulWithBytes (byte [] backBytes, string errorStr = "Modify gateway configuration failed!")
|
{
|
if (backBytes == null) {
|
Application.RunOnMainThread (() => {
|
new Alert ("", ErrorCode.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;
|
}
|
}
|
|
|
/// <summary>
|
/// 写配置参数成功后执行下一步操作
|
/// </summary>
|
void GatewaySettingSucceeded ()
|
{
|
|
if (gatewayDeicve.Type == DeviceType.OnePortMqttFR) {
|
//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.Id.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 ("", ErrorCode.GatewayNoResponse, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
|
});
|
return;
|
} else if (bindReginIdStatus [0] != 0xF8) {
|
//new Alert ("", "网关绑定住宅失败!", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
|
Application.RunOnMainThread (() => {
|
new Alert ("", "Gateway failed to bind residence!", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
|
});
|
return;
|
}
|
}
|
|
//GotoHomePage ();
|
UploadOidAndSidList ();
|
|
}
|
|
/// <summary>
|
/// 上传oid和sid列表
|
/// </summary>
|
void UploadOidAndSidList ()
|
{
|
|
Application.RunOnMainThread (() => {
|
MainPage.Loading.Start ("Uploading device list...");
|
});
|
var RES = HDLLinkUtlis.Current.UploadOidAndSidList ();
|
Application.RunOnMainThread (() => {
|
if (RES) {
|
//Utlis.ShowTip ("Device list upload successfully!");
|
} else {
|
Utlis.ShowTip ("Device list upload failed!");
|
}
|
GotoHomePage ();
|
});
|
|
}
|
|
/// <summary>
|
/// 跳转主页面
|
/// </summary>
|
void GotoHomePage ()
|
{
|
MainPage.LoginUser.LastTime = DateTime.Now;
|
Application.RunOnMainThread (() => {
|
this.RemoveFromParent ();
|
//MainPage.WiFiStatus = "CrabtreeAdd/WiFi.png";
|
UserMiddle.Init (true);
|
MainPage.WiFiStatus = "CrabtreeAdd/WiFi.png";
|
UserMiddle.btnLinkStatus.UnSelectedImagePath = MainPage.WiFiStatus;
|
});
|
}
|
|
|
///// <summary>
|
///// Gateway setting succeeded,data up.Please wait...
|
///// </summary>
|
//void BindGateways ()
|
//{
|
|
// Application.RunOnMainThread (() => {
|
// MainPage.Loading.Start ("Gateway setting succeeded,data up.Please wait...");
|
// });
|
// IO.FileUtils.SaveEquipmentMessage (gatewayDeicve);
|
// var gatewayMAC = gatewayDeicve.MAC.Replace (".", "");
|
// var oldMac = "";
|
// //2020-01-11
|
// if (UserConfig.Instance.CheckHomeGatewaysNotEmpty ()) {
|
// //当前住宅存在Mac 修改MAC接口
|
// oldMac = UserConfig.Instance.CurrentRegion.HomeGateways [0].GatewayUniqueId;
|
// if (oldMac == gatewayMAC) {//和绑定mac一致,不用修改
|
// AddMacToUserConfig (gatewayMAC);
|
// return;
|
// }
|
|
// var mUpdateMacObj = new UpdateMacObj () {
|
// LoginAccessToken = MainPage.LoginUser.LoginTokenString,
|
// HomeId = UserConfig.Instance.CurrentRegion.Id,
|
// NewMac = gatewayMAC,
|
// OldMac = oldMac
|
// };
|
// string urlHead = MainPage.RequestHttpsHost;
|
// if (mUpdateMacObj.IsOtherAccountCtrl) {
|
// urlHead = UserConfig.Instance.MasterAccountRequestBaseUrl;
|
// mUpdateMacObj.LoginAccessToken = UserConfig.Instance.MasterAccountToken;
|
// }
|
|
// var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (mUpdateMacObj);
|
|
// //var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (new EditMACByHomeId { RegionID = UserConfig.Instance.CurrentRegion.Id, MAC = gatewayMAC, IsReBind = true });
|
// var respone = MainPage.RequestHttps (API.UpdateMac, requestJson, urlHead);
|
// if (respone.StateCode.ToUpper () != StateCode.SUCCESS) {
|
// GetNowHomeGatewayAfterBindMacFailed ();//2020-01-11
|
// Application.RunOnMainThread (() => {
|
// new Alert ("", "Failed to update Mac address, gateway binding failed!", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
|
// if (MainPage.LoginUser.AccountString == "464027401@qq.com") {
|
// new Alert (respone.StateCode, respone.ErrorInfo, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
|
// }
|
// });
|
// return;
|
// }
|
// //GetNowHomeGateway ();
|
// AddMacToUserConfig (gatewayMAC);
|
// } else {
|
// //走新绑定接口
|
// var mBindMacObj = new BindGatewayToHomeObj () {
|
// LoginAccessToken = MainPage.LoginUser.LoginTokenString,
|
// HomeId = UserConfig.Instance.CurrentRegion.Id,
|
|
// };
|
// mBindMacObj.BindGateways.Add (gatewayMAC);
|
// string urlHead = MainPage.RequestHttpsHost;
|
// if (mBindMacObj.IsOtherAccountCtrl) {
|
// urlHead = UserConfig.Instance.MasterAccountRequestBaseUrl;
|
// mBindMacObj.LoginAccessToken = UserConfig.Instance.MasterAccountToken;
|
// }
|
|
|
// var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (mBindMacObj);
|
|
// //var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (new EditMACByHomeId { RegionID = UserConfig.Instance.CurrentRegion.Id, MAC = gatewayMAC, IsReBind = true });
|
// var respone = MainPage.RequestHttps (API.BindGatewayToHome, requestJson, urlHead);
|
// if (respone.StateCode.ToUpper () != StateCode.SUCCESS && respone.StateCode != "BindGatewaysExists") {
|
// GetNowHomeGatewayAfterBindMacFailed ();//2020-01-11
|
// Application.RunOnMainThread (() => {
|
// new Alert ("", "Failed to bind Mac address, gateway binding failed!", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
|
// if (MainPage.LoginUser.AccountString == "464027401@qq.com") {
|
// new Alert (respone.StateCode, respone.ErrorInfo, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
|
// }
|
// });
|
// return;
|
// }
|
// //GetNowHomeGateway ();
|
// AddMacToUserConfig (gatewayMAC);
|
// }
|
|
//}
|
|
|
///// <summary>
|
///// 绑定失败之后,查询刷新一次当前住宅 网关列表
|
///// 2020-01-11
|
///// </summary>
|
//void GetNowHomeGatewayAfterBindMacFailed ()
|
//{
|
|
// var requestObj3 = new GetSingleHomeGatewayPaggerObj ();
|
// requestObj3.ReqDto.LoginAccessToken = MainPage.LoginUser.LoginTokenString;
|
// requestObj3.ReqDto.HomeId = UserConfig.Instance.CurrentRegion.Id;
|
// requestObj3.ReqDto.PageSetting.Page = 1;
|
// requestObj3.ReqDto.PageSetting.PageSize = 10;
|
// string urlHead = MainPage.RequestHttpsHost;
|
// if (requestObj3.IsOtherAccountCtrl) {
|
// urlHead = UserConfig.Instance.MasterAccountRequestBaseUrl;
|
// requestObj3.ReqDto.LoginAccessToken = UserConfig.Instance.MasterAccountToken;
|
// }
|
|
// var requestJson3 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj3);
|
// var revertObj3 = MainPage.RequestHttps (API.GetSingleHomeGatewayPagger, requestJson3, urlHead);
|
// if (revertObj3.StateCode.ToUpper () == StateCode.SUCCESS) {
|
// var infoResult = Newtonsoft.Json.JsonConvert.DeserializeObject<GetGatewayResult> (revertObj3.ResponseData.ToString ());
|
// //2020-01-11
|
// UserConfig.Instance.SetNowHomeGateways (infoResult.PageData);
|
|
// }
|
|
|
//}
|
|
#region 2020-07-02
|
/// <summary>
|
/// 定时搜索设备线程
|
/// </summary>
|
Thread searchDeviceThead;
|
bool bStartSearch;
|
|
/// <summary>
|
/// 2020-07-02
|
/// 网关配频模式时,开启自动搜索线程
|
/// </summary>
|
void StartSearchDeviceThead ()
|
{
|
StopSearchDeviceThead ();
|
CommonPage.LocalPhoneFindDevice = true;
|
CommonPage.FindGatewayChilren = true;
|
CommonPage.RandomHigh = (byte)new Random ().Next (255);
|
CommonPage.RandomLow = (byte)new Random ().Next (255);
|
|
bStartSearch = true;
|
searchDeviceThead = new Thread ((obj) => {
|
while (bStartSearch) {
|
try {
|
//定时4S
|
Thread.Sleep (4000);
|
|
var ms = new System.IO.MemoryStream ();
|
ms.WriteByte (CommonPage.RandomHigh);
|
ms.WriteByte (CommonPage.RandomLow);
|
|
var list = CommonPage.DeviceList;
|
int tempCount = list.Count;
|
string s = ";";
|
for (int i = 0; i < list.Count; i++) {
|
Common common = list [i];
|
if (common.SubnetID != gatewayDeicve.SubnetID)
|
continue;
|
if (s.Contains (";" + common.SubnetID.ToString () + ":" + common.DeviceID.ToString ()))
|
continue;
|
s += common.SubnetID.ToString () + ":" + common.DeviceID.ToString () + ";";
|
ms.WriteByte (common.SubnetID);
|
ms.WriteByte (common.DeviceID);
|
}
|
Utlis.WriteLine ("SearchDeviceThead搜索非网关设备,网络地址是:" + CommonPage.EndPoint.ToString ());
|
var control = new Control ();
|
control.Send (new Target () {
|
IPEndPoint = CommonPage.EndPoint,
|
Command = Command.ReadDeviceModul,
|
SubnetID = gatewayDeicve.SubnetID,
|
DeviceID = 0xFF,
|
AddData = ms.ToArray ()
|
}, SendCount.Zero, false);
|
|
ms.Close ();
|
|
} catch {
|
|
}
|
}
|
});
|
searchDeviceThead.Start ();
|
|
|
Utlis.WriteLine ("----StartSearchDeviceThead----");
|
}
|
|
/// <summary>
|
/// 暂停搜索设备线程
|
/// </summary>
|
void StopSearchDeviceThead ()
|
{
|
CommonPage.LocalPhoneFindDevice = false;
|
CommonPage.FindGatewayChilren = false;
|
bStartSearch = false;
|
if (searchDeviceThead != null)
|
searchDeviceThead.Abort ();
|
|
Utlis.WriteLine ("----StopSearchDeviceThead----");
|
|
}
|
#endregion
|
|
///// <summary>
|
///// 查询当前住宅 网关列表
|
///// </summary>
|
//void GetNowHomeGateway ()
|
//{
|
|
// var requestObj3 = new GetSingleHomeGatewayPaggerObj ();
|
// requestObj3.ReqDto.LoginAccessToken = MainPage.LoginUser.LoginTokenString;
|
// requestObj3.ReqDto.HomeId = UserConfig.Instance.CurrentRegion.Id;
|
// requestObj3.ReqDto.PageSetting.Page = 1;
|
// requestObj3.ReqDto.PageSetting.PageSize = 999;
|
// string urlHead = MainPage.RequestHttpsHost;
|
// if (requestObj3.IsOtherAccountCtrl) {
|
// urlHead = UserConfig.Instance.MasterAccountRequestBaseUrl;
|
// requestObj3.ReqDto.LoginAccessToken = UserConfig.Instance.MasterAccountToken;
|
// }
|
|
|
// //var requestObj3 = new GatewayByRegionListObj () { RegionID = UserConfig.Instance.CurrentRegion.Id };
|
// var requestJson3 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj3);
|
// var revertObj3 = MainPage.RequestHttps (API.GetSingleHomeGatewayPagger, requestJson3, urlHead);
|
// if (revertObj3.StateCode.ToUpper () == StateCode.SUCCESS) {
|
// var infoResult = Newtonsoft.Json.JsonConvert.DeserializeObject<GetGatewayResult> (revertObj3.ResponseData.ToString ());
|
// var responseGatewayDataObj = infoResult.PageData;
|
// if (responseGatewayDataObj != null && responseGatewayDataObj.Count > 0) {
|
|
// UserConfig.Instance.GatewayList.Clear ();
|
// UserConfig.Instance.GatewayList.AddRange (responseGatewayDataObj);
|
// UserConfig.Instance.GatewayMAC = gatewayDeicve.MAC;
|
// UserConfig.Instance.SaveUserConfig ();
|
// MainPage.LoginUser.LastTime = DateTime.Now;
|
// Application.RunOnMainThread (() => {
|
// bodyView.RemoveAll ();
|
// UserMiddle.Init (true);
|
// });
|
// } else {
|
// Application.RunOnMainThread (() => {
|
// new Alert ("", "Query gateway failed, gateway failed to bind residence.Please try again.", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
|
// if (MainPage.LoginUser.AccountString == "464027401@qq.com") {
|
// new Alert ("22:" + revertObj3.StateCode, revertObj3.ErrorInfo, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
|
// }
|
// });
|
// }
|
// } else {
|
|
// Application.RunOnMainThread (() => {
|
// new Alert ("", "Query gateway failed, gateway failed to bind residence.Please try again.", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
|
// if (MainPage.LoginUser.AccountString == "464027401@qq.com") {
|
// new Alert ("22:" + revertObj3.StateCode, revertObj3.ErrorInfo, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
|
// }
|
// });
|
// }
|
|
|
//}
|
|
|
|
|
///// <summary>
|
/////
|
///// </summary>
|
///// <param name="stateCodeStr"></param>
|
//void ShowGetAllBindGatewarysPaggerErrorInfo (string stateCodeStr)
|
//{
|
// string mes = "";
|
// //2020-01-06 待补充
|
// if (stateCodeStr == ErrorCode.NetworkError) {
|
// mes = ErrorCode.NetworkError;
|
// } else {
|
// mes = ErrorCode.OperationFailed + ErrorCode.Reason + stateCodeStr;
|
|
// }
|
// if (!string.IsNullOrEmpty (mes)) {
|
// Application.RunOnMainThread (() => {
|
// new Alert ("", mes, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
|
// });
|
// }
|
|
|
//}
|
|
//*****************StandardMQTT 配置方式 目前用不上*****************
|
///// <summary>
|
///// 远程配置 Standard MQTT模式
|
///// </summary>
|
//void RemoteSettingWithStandardMQTT ()
|
//{
|
// byte [] gatewayBytes = null;
|
// string [] strServerIP = (MainPage.SeviceIP).Split ('.');
|
// string [] strServerIP1 = (MainPage.SeviceIP).Split ('.');
|
// MainPage.Loading.Start ("Configuring device...");
|
// System.Threading.Tasks.Task.Run (() => {
|
// try {
|
|
// //读取一端口交换机、无线网关的配置信息
|
// gatewayBytes = Control.ControlBytesSendHasReturn (Command.ReadGateWayModelInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, new byte [] { });
|
// //连接类型配置为5 Standard MQTT模式
|
// gatewayBytes [0] = TYPEStandardMQTT;//Standard MQTT
|
// Control.ControlBytesSend (Command.SetGateWayModelInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, gatewayBytes);
|
// //1.修改URL
|
// //2.修改读取客户端ID,用户名,密码
|
// //3.修改AES加密
|
// //4.修改主题
|
|
// //1.修改URL
|
// if (SetGateWayURL ("https://developer.hdlcontrol.com/api/")) {
|
// //2.修改客户端ID,用户名,密码
|
// if (SetGateWayClientIDUsernamePassword ("85521566", "Admin", "85521566")) {
|
// //3.修改AES加密
|
// if (SetGateWayAESEncryption (true, "85521566")) {
|
// //4.1修改订阅主题
|
// if (SetGateWayTopic ("SubscribeTopic")) {
|
// //4.2修改发布主题
|
// if (SetGateWayTopic ("PublishTopic", true)) {
|
// //5.写配置成功后下一步操作
|
// GatewaySettingSucceeded ();
|
// }
|
// }
|
// }
|
// }
|
// }
|
|
// } catch (Exception ex) {
|
// Utlis.WriteLine (ex.Message);
|
// Application.RunOnMainThread (() => {
|
// new Alert ("", Language.StringByID (R.MyInternationalizationString.OperationFailed), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
|
// });
|
// bool canRemove = false;
|
// } finally {
|
// Application.RunOnMainThread (() => {
|
// MainPage.Loading.Hide ();
|
// //this.RemoveAll ();
|
// });
|
// }
|
// });
|
// //Room.InitAllRoom ();
|
//}
|
|
|
|
///// <summary>
|
///// 修改URL
|
///// </summary>
|
///// <returns></returns>
|
//bool SetGateWayURL (string urlStr = "", int pointID = 1883)
|
//{
|
// byte [] sendBytes = new byte [63];
|
// byte [] url = CommonPage.MyEncodingGB2312.GetBytes (urlStr);
|
// Array.Copy (url, 0, sendBytes, 1, 60 < url.Length ? 60 : url.Length);
|
// sendBytes [0] = GuideSettingGateway.TYPEStandardMQTT;
|
// sendBytes [61] = Convert.ToByte (pointID / 256);
|
// sendBytes [62] = Convert.ToByte (pointID % 256);
|
// var backBytes = Control.ControlBytesSendHasReturn (Command.SetGateWayURL, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, sendBytes);
|
|
// return CheckIsSuccessfulWithBytes (backBytes);
|
//}
|
|
|
///// <summary>
|
///// 修改客户端ID,用户名,密码
|
///// </summary>
|
///// <returns></returns>
|
//bool SetGateWayClientIDUsernamePassword (string clientIDStr = "", string usernameStr = "", string passwordStr = "")
|
//{
|
|
// byte [] userInfoBytes = new byte [64];
|
// byte [] clientID = CommonPage.MyEncodingGB2312.GetBytes (clientIDStr);
|
// Array.Copy (clientID, 0, userInfoBytes, 0, 32 < clientID.Length ? 32 : clientID.Length);
|
// byte [] username = CommonPage.MyEncodingGB2312.GetBytes (usernameStr);
|
// Array.Copy (username, 0, userInfoBytes, 32, 16 < username.Length ? 16 : username.Length);
|
// byte [] password = CommonPage.MyEncodingGB2312.GetBytes (passwordStr);
|
// Array.Copy (password, 0, userInfoBytes, 48, 16 < password.Length ? 16 : password.Length);
|
// var setUserInfoBytes = Control.ControlBytesSendHasReturn (Command.SetGateWayClientIDUsernamePassword, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, userInfoBytes);
|
|
// return CheckIsSuccessfulWithBytes (setUserInfoBytes);
|
//}
|
|
///// <summary>
|
///// 修改AES加密
|
///// </summary>
|
///// <returns></returns>
|
//bool SetGateWayAESEncryption (bool enable = false, string passwordStr = "")
|
//{
|
// byte [] sendBytes = new byte [17];
|
// sendBytes [0] = enable ? (byte)1 : (byte)0; //0:不使能 1 - 255:使能
|
// byte [] password = CommonPage.MyEncodingGB2312.GetBytes (passwordStr);
|
// Array.Copy (password, 0, sendBytes, 1, 16 < password.Length ? 16 : password.Length);
|
// var backBytes = Control.ControlBytesSendHasReturn (Command.SetGateWayAESEncryption, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, sendBytes);
|
|
// return CheckIsSuccessfulWithBytes (backBytes);
|
//}
|
|
///// <summary>
|
///// 修改主题
|
///// </summary>
|
///// <param name="bPublish">是否为发布主题</param>
|
///// <param name="bDefault">是否默认</param>
|
///// <returns></returns>
|
//bool SetGateWayTopic (string topicInfoStr = "", bool bPublish = false, bool bDefault = false)
|
//{
|
// byte [] sendBytes = new byte [50];
|
// sendBytes [0] = bPublish ? (byte)2 : (byte)1; //1:订阅 2:发布
|
// sendBytes [1] = bDefault ? (byte)0 : (byte)1; //1:非默认 0:默认
|
|
// byte [] topicInfo = CommonPage.MyEncodingGB2312.GetBytes (topicInfoStr);
|
// Array.Copy (topicInfo, 0, sendBytes, 2, 48 < topicInfo.Length ? 48 : topicInfo.Length);
|
// var backBytes = Control.ControlBytesSendHasReturn (Command.SetGateWayTopic, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, sendBytes);
|
|
// if (backBytes != null && backBytes.Length >= 2) {
|
// if (backBytes [0] == sendBytes [0] && backBytes [1] == sendBytes [1]) {
|
// return true;
|
// }
|
// }
|
// return CheckIsSuccessfulWithBytes (backBytes);
|
//}
|
|
}
|
}
|