CrabtreeOn,印度客户定制APP,迁移2.0平台版本
JLChen
2021-01-20 4c993fcb71b92f8e9837ca8f50f3b5e8f9c59cfe
Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs
@@ -157,7 +157,7 @@
                RemoteSettingWithHDLMQTT ();
            } else {
                Utlis.WriteLine ("Assign OnePortWirelessFR");
                RemoteSettingWithOldDevice ();
                //RemoteSettingWithOldDevice ();
                //RemoteSettingWithMQTT ();
            }
        }
@@ -602,134 +602,134 @@
        /// <summary>
        /// 远程配置
        /// </summary>
        void RemoteSettingWithOldDevice ()
        {
            byte [] gatewayBytes = null;
            byte [] serverIPBytes = null;
        ///// <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 {
        //    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 = 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);
        //            //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);
        //            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 [] { });
        //            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);
        //            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 [] 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 [] 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 [] 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 [] { });
        //            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);
        //            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);
        //            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 [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 [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 [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);
        //            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 ();
        }
        //            //远程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 ();
        //}
@@ -757,8 +757,11 @@
                    if (SetGateWayModelInfo()) {
                        //2.修改管理信息
                        if (SetGateWayAdminInfo ()) {
                            //3.写配置成功后下一步操作
                            GatewaySettingSucceeded ();
                            //3.写Mqtt远程域名地址
                            if (SetGateWayMqttUrlAddress ()) {
                                //4.写配置成功后下一步操作
                                GatewaySettingSucceeded ();
                            }
                        }
                    }
@@ -846,10 +849,9 @@
        /// <returns></returns>
        bool SetGateWayAdminInfo ()
        {
            byte [] name = CommonPage.MyEncodingGB2312.GetBytes (MainPage.LoginUser.AccountString);
            //byte [] currentRegionIdBytes = CommonPage.MyEncodingGB2312.GetBytes (UserConfig.Instance.CurrentRegion.RegionID);
            //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 ());
            //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);
@@ -861,6 +863,47 @@
        }
        /// <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>