From 454ac814944cf956ff02b84b70ba2ec68e5e5ea1 Mon Sep 17 00:00:00 2001
From: JLChen <551775569@qq.com>
Date: 星期三, 15 一月 2020 19:38:47 +0800
Subject: [PATCH] 2020-01-15 1.优化MQTT连接。 2.增加本地通信加密。

---
 Crabtree/SmartHome/UI/SimpleControl/Phone/User/UserSettingView.cs  |    1 
 Crabtree/SmartHome/UI/SimpleControl/EquipmentPublicClass.cs        |    4 
 Crabtree/.vs/SmartHome/xs/sqlite3/storage.ide-wal                  |    0 
 Crabtree/SmartHome/HDL/Operation/UserConfig.cs                     |    6 
 Crabtree/SmartHome/HDL/Operation/Packet.cs                         |   27 +-
 Crabtree/.vs/SmartHome/xs/sqlite3/storage.ide                      |    0 
 Crabtree/SmartHome/UI/SimpleControl/Phone/Schedule/HelpView.cs     |   11 +
 Crabtree/.vs/SmartHome/xs/sqlite3/storage.ide-shm                  |    0 
 Crabtree/SmartHome/UI/SimpleControl/Phone/Register/AccountLogin.cs |   21 +-
 Crabtree/SmartHome/UI/SimpleControl/MainPage.cs                    |    2 
 Crabtree/SmartHome/UI/SimpleControl/CommonPage.cs                  |    4 
 Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideAddGateway.cs |  128 ++++++++++++---
 Crabtree/ON/Properties/AndroidManifest.xml                         |    2 
 Crabtree/.vs/SmartHome/xs/UserPrefs.xml                            |   57 ++++--
 Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs                  |  176 ++++++++++-----------
 Crabtree/SmartHome/HDL/Operation/ResponseEntity/ErrorCode.cs       |    9 
 16 files changed, 277 insertions(+), 171 deletions(-)

diff --git a/Crabtree/.vs/SmartHome/xs/UserPrefs.xml b/Crabtree/.vs/SmartHome/xs/UserPrefs.xml
index ee6e0ce..ac980ab 100644
--- a/Crabtree/.vs/SmartHome/xs/UserPrefs.xml
+++ b/Crabtree/.vs/SmartHome/xs/UserPrefs.xml
@@ -1,40 +1,52 @@
-锘�<Properties StartupConfiguration="{1D83BF28-BA88-4152-BA41-D7EFE90A5437}|Default">
-  <MonoDevelop.Ide.ItemProperties.ON.Droid PreferredExecutionTarget="Android.Android_Accelerated_Oreo" />
-  <MonoDevelop.Ide.Workbench ActiveDocument="SmartHome/HDL/Operation/UserInfo.cs">
+锘�<Properties StartupConfiguration="{9D9EF20B-6E65-4A65-92BF-567EBF1E1443}|Default">
+  <MonoDevelop.Ide.ItemProperties.ON.Droid PreferredExecutionTarget="Android.4b65c46595" />
+  <MonoDevelop.Ide.Workbench ActiveDocument="SmartHome/UI/SimpleControl/Phone/User/UserMiddle.cs">
     <Files>
-      <File FileName="SmartHome/UI/SimpleControl/MainPage.cs" Line="362" Column="16" />
-      <File FileName="SmartHome/HDL/Operation/ResponseEntity/SendJson.cs" />
-      <File FileName="SmartHome/UI/SimpleControl/MqttCommon.cs" />
-      <File FileName="SmartHome/HDL/Operation/Control.cs" Line="388" Column="38" />
-      <File FileName="SmartHome/HDL/Operation/Target.cs" />
-      <File FileName="SmartHome/HDL/Operation/UserConfig.cs" Line="299" Column="22" />
-      <File FileName="SmartHome/HDL/Operation/BusSocket.cs" Line="167" Column="1" />
-      <File FileName="SmartHome/HDL/Operation/Packet.cs" Line="145" Column="45" />
-      <File FileName="SmartHome/HDL/Operation/MyEnum.cs" />
-      <File FileName="SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs" />
-      <File FileName="SmartHome/UI/SimpleControl/Phone/Guide/GuideAddResidence.cs" Line="281" Column="42" />
-      <File FileName="SmartHome/UI/SimpleControl/CommonPage.cs" Line="55" Column="57" />
-      <File FileName="SmartHome/UI/SimpleControl/Phone/Guide/GuideAddGateway.cs" Line="372" Column="19" />
-      <File FileName="SmartHome/UI/SimpleControl/Phone/User/UserMiddle.cs" Line="406" Column="43" />
-      <File FileName="SmartHome/HDL/Operation/UserInfo.cs" Line="1" Column="1" />
+      <File FileName="SmartHome/HDL/Operation/ResponseEntity/SendJson.cs" Line="464" Column="8" />
+      <File FileName="SmartHome/UI/SimpleControl/MainPage.cs" Line="43" Column="25" />
+      <File FileName="SmartHome/UI/SimpleControl/MqttCommon.cs" Line="105" Column="16" />
+      <File FileName="SmartHome/UI/SimpleControl/Phone/Room/UserRoom.cs" Line="2270" Column="76" />
+      <File FileName="SmartHome/HDL/Operation/MyEnum.cs" Line="1214" Column="27" />
+      <File FileName="SmartHome/UI/SimpleControl/CommonPage.cs" Line="453" Column="22" />
+      <File FileName="SmartHome/UI/SimpleControl/Phone/Light/UserDeviceToLight.cs" Line="45" Column="56" />
+      <File FileName="SmartHome/UI/SimpleControl/EquipmentPublicClass.cs" Line="120" Column="1" />
+      <File FileName="SmartHome/UI/SimpleControl/Phone/Light/UserDeviceToSocket.cs" Line="44" Column="23" />
+      <File FileName="SmartHome/HDL/Operation/Packet.cs" Line="69" Column="1" />
+      <File FileName="SmartHome/HDL/Operation/Device/ButtonPanel.cs" Line="27" Column="9" />
+      <File FileName="SmartHome/UI/SimpleControl/Phone/User/UserSettingView.cs" Line="482" Column="42" />
+      <File FileName="SmartHome/UI/SimpleControl/Phone/Register/AccountLogin.cs" Line="12" Column="28" />
+      <File FileName="SmartHome/UI/SimpleControl/Phone/Schedule/HelpView.cs" Line="35" Column="40" />
+      <File FileName="SmartHome/HDL/Operation/ResponseEntity/ErrorCode.cs" Line="26" Column="47" />
+      <File FileName="SmartHome/UI/SimpleControl/Phone/Guide/GuideAddGateway.cs" Line="1" Column="1" />
+      <File FileName="SmartHome/UI/SimpleControl/Phone/Register/AccountView.cs" Line="80" Column="29" />
+      <File FileName="SmartHome/UI/SimpleControl/Phone/User/UserMiddle.cs" Line="207" Column="21" />
     </Files>
     <Pads>
       <Pad Id="ProjectPad">
         <State name="__root__">
           <Node name="SmartHome" expanded="True">
-            <Node name="ON.Droid" expanded="True" />
+            <Node name="ON.Droid" expanded="True">
+              <Node name="Properties" expanded="True" />
+            </Node>
             <Node name="Shared" expanded="True">
               <Node name="HDL" expanded="True">
                 <Node name="Operation" expanded="True">
-                  <Node name="UserInfo.cs" selected="True" />
+                  <Node name="Device" expanded="True" />
+                  <Node name="ResponseEntity" expanded="True" />
                 </Node>
               </Node>
               <Node name="UI" expanded="True">
                 <Node name="SimpleControl" expanded="True">
                   <Node name="Phone" expanded="True">
                     <Node name="Guide" expanded="True" />
+                    <Node name="Light" expanded="True" />
+                    <Node name="Music" expanded="True" />
                     <Node name="Register" expanded="True" />
-                    <Node name="User" expanded="True" />
+                    <Node name="Room" expanded="True" />
+                    <Node name="Schedule" expanded="True" />
+                    <Node name="User" expanded="True">
+                      <Node name="UserMiddle.cs" selected="True" />
+                    </Node>
                   </Node>
                 </Node>
               </Node>
@@ -55,7 +67,8 @@
   <MonoDevelop.Ide.ItemProperties.ON.Ios automaticSigning="False" PreferredExecutionTarget="MonoDevelop.IPhone.IPhoneDeviceTarget.254b82433419c7ca1850b1dfb524c79b7aeb0640" />
   <MonoDevelop.Ide.DebuggingService.Breakpoints>
     <BreakpointStore>
-      <Breakpoint file="/Users/jlchen/JLChen/ProjectsCode/HDLGit/CrabtreeOn/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideAddGateway.cs" relfile="SmartHome/UI/SimpleControl/Phone/Guide/GuideAddGateway.cs" line="546" column="1" />
+      <Breakpoint file="/Users/jlchen/JLChen/ProjectsCode/HDLGit/CrabtreeOn/Crabtree/SmartHome/UI/SimpleControl/Phone/Light/UserDeviceToLight.cs" relfile="SmartHome/UI/SimpleControl/Phone/Light/UserDeviceToLight.cs" line="69" column="1" />
+      <Breakpoint file="/Users/jlchen/JLChen/ProjectsCode/HDLGit/CrabtreeOn/Crabtree/SmartHome/UI/SimpleControl/Phone/Light/UserDeviceToSocket.cs" relfile="SmartHome/UI/SimpleControl/Phone/Light/UserDeviceToSocket.cs" line="72" column="1" />
     </BreakpointStore>
   </MonoDevelop.Ide.DebuggingService.Breakpoints>
   <MultiItemStartupConfigurations />
diff --git a/Crabtree/.vs/SmartHome/xs/sqlite3/storage.ide b/Crabtree/.vs/SmartHome/xs/sqlite3/storage.ide
index 5b5a945..1171ee8 100644
--- a/Crabtree/.vs/SmartHome/xs/sqlite3/storage.ide
+++ b/Crabtree/.vs/SmartHome/xs/sqlite3/storage.ide
Binary files differ
diff --git a/Crabtree/.vs/SmartHome/xs/sqlite3/storage.ide-shm b/Crabtree/.vs/SmartHome/xs/sqlite3/storage.ide-shm
new file mode 100644
index 0000000..3b736d2
--- /dev/null
+++ b/Crabtree/.vs/SmartHome/xs/sqlite3/storage.ide-shm
Binary files differ
diff --git a/Crabtree/.vs/SmartHome/xs/sqlite3/storage.ide-wal b/Crabtree/.vs/SmartHome/xs/sqlite3/storage.ide-wal
new file mode 100644
index 0000000..2ee4e69
--- /dev/null
+++ b/Crabtree/.vs/SmartHome/xs/sqlite3/storage.ide-wal
Binary files differ
diff --git a/Crabtree/ON/Properties/AndroidManifest.xml b/Crabtree/ON/Properties/AndroidManifest.xml
index 4d884d0..379891b 100644
--- a/Crabtree/ON/Properties/AndroidManifest.xml
+++ b/Crabtree/ON/Properties/AndroidManifest.xml
@@ -1,5 +1,5 @@
 锘�<?xml version="1.0" encoding="utf-8"?>
-<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.hdl.crabtree" android:versionName="2.50114" android:versionCode="20200114">
+<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.hdl.crabtree" android:versionName="2.50115" android:versionCode="20200115">
 	<uses-sdk android:minSdkVersion="23" android:targetSdkVersion="26" />
 	<!-- Required  涓�浜涚郴缁熻姹傜殑鏉冮檺锛屾澶勬槸鏋佸厜鎺ㄩ�侀渶姹傜殑鏉冮檺锛屽璁块棶缃戠粶绛�-->
 	<permission android:name="com.hdl.on.permission.JPUSH_MESSAGE" android:protectionLevel="signature" />
diff --git a/Crabtree/SmartHome/HDL/Operation/Packet.cs b/Crabtree/SmartHome/HDL/Operation/Packet.cs
index ded4f36..923327a 100644
--- a/Crabtree/SmartHome/HDL/Operation/Packet.cs
+++ b/Crabtree/SmartHome/HDL/Operation/Packet.cs
@@ -59,9 +59,6 @@
 		{
             //鍔犲瘑妫�娴�
             EncryptionDetection ();
-
-
-
             //瀵逛簬鎿嶄綔鏁版嵁搴撶殑鏃堕棿姣旇緝闀跨殑锛屽彲浠ュ垱寤哄彟涓�涓嚎绋嬪鐞�
             if (!"HDLMIRACLE".Equals (System.Text.Encoding.ASCII.GetString (Bytes, 4, 10))) {
 				return;
@@ -69,7 +66,6 @@
 
 			byte subnetID = this.Bytes [17]; //婧愬瓙缃戝彿
 			byte deviceID = this.Bytes [18]; //婧愯澶囧彿
-
 
 			//婧愯澶囩被鍨�
 			int deviceType = this.Bytes [19] * 256 + this.Bytes [20];
@@ -130,21 +126,26 @@
             if (IsLocal) {//鏈湴閫氳
                
                 if (UserConfig.Instance.IsLocalEncrypt) {
-                    if ("Passord error" == (System.Text.Encoding.ASCII.GetString (Bytes, 14, 13))) {
-                        //缃戝叧杩斿洖锛屽姞瀵嗗瘑鐮乲ey涓嶅
-
-                        return;
+                    if (Bytes.Length == 28) {
+                        var mes = System.Text.Encoding.ASCII.GetString (Bytes, 14, 14);
+                        if ("Password error" == mes) {
+                            //缃戝叧杩斿洖锛屽姞瀵嗗瘑鐮乲ey涓嶅
+                            UserConfig.Instance.EncryptedPasswordCorrect = false;
+                            return;
+                        }
                     }
-                    var messageGet = Shared.Securitys.EncryptionService.AesEncryptPayload (Bytes, UserConfig.Instance.LocalEncryptKey);
+                    var messageGet = Shared.Securitys.EncryptionService.AesDecryptPayload (Bytes, UserConfig.Instance.LocalEncryptKey);
                     Bytes = messageGet;
 
                    
                 } else {
-                    if ("Locked" == (System.Text.Encoding.ASCII.GetString (Bytes, 14, 6))) {
-                        //缃戝叧宸插姞瀵嗭紝鑷姩鍒囨崲涓哄姞瀵�
-                        UserConfig.Instance.IsLocalEncrypt = true;
+                    if (Bytes.Length == 20) {
+                        var mes = System.Text.Encoding.ASCII.GetString (Bytes, 14, 6);
+                        if ("Locked" == mes) {
+                            //缃戝叧宸插姞瀵嗭紝鑷姩鍒囨崲涓哄姞瀵�
+                            UserConfig.Instance.IsLocalEncrypt = true;
+                        }
                     }
-
                 }
 
             }
diff --git a/Crabtree/SmartHome/HDL/Operation/ResponseEntity/ErrorCode.cs b/Crabtree/SmartHome/HDL/Operation/ResponseEntity/ErrorCode.cs
index 1312266..8090ec3 100644
--- a/Crabtree/SmartHome/HDL/Operation/ResponseEntity/ErrorCode.cs
+++ b/Crabtree/SmartHome/HDL/Operation/ResponseEntity/ErrorCode.cs
@@ -58,9 +58,9 @@
         public readonly static string NoAddMainAccount = "Can't bind master account!";
         //鑾峰彇浣忓畢鐨勭綉鍏冲垪琛ㄥけ璐ワ紒
         public readonly static string FailedGetGatewaysForHome = "Failed to get list of gateways for home!";
-        
-        //姝ょ綉鍏冲凡缁戝畾鍒颁竴涓綇鎵�锛岃鍦ㄧ粦瀹氬埌鍙︿竴涓綇鎵�涔嬪墠灏嗗叾閲嶇疆銆傛垨鑰呴暱鎸夌綉鍏虫寜閿�3S锛屽彉鎴愮孩鐏父浜繘鍏ュ己鍒剁粦瀹氭ā寮忥紒
-        public readonly static string UnableToBindGateway = "This gateway is already bound to one residence, please reset it before binding to another residence.Or long press the gateway button 3S, the red light is always on to enter the forced binding mode!";
+
+        //姝ょ綉鍏冲凡缁戝畾鍒颁竴涓綇鎵�锛岃鍦ㄧ粦瀹氬埌鍙︿竴涓綇鎵�涔嬪墠灏嗗叾閲嶇疆銆傛垨鑰呴暱鎸夌綉鍏虫寜閿�3S锛屽彉鎴愮孩鐏父浜紝鐒跺悗閲嶈瘯锛�
+        public readonly static string UnableToBindGateway = "This gateway is already bound to one residence, please reset it before binding to another residence.Or long press the gateway button 3S, the red light is always on, and then try again!";
         //缃戝叧鍥轰欢鐗堟湰杩囦綆锛岃鍏堝崌绾э紒
         public readonly static string GatewayVersionLow = "The gateway firmware version is too low, please upgrade first!";
         //璇峰厛缁戝畾鏃犵嚎缃戝叧  2020-01-11
@@ -69,6 +69,7 @@
         public readonly static string LoginInAnotherDevice = "Your account is signed in with a remote connection on another device!";
         //鎮ㄥ苟涓嶅叿鏈夋鍒嗕韩鑰呮浣忓畢鐨勫畬鍏ㄦ帶鍒舵潈闄�!
         public readonly static string InsufficientAuthority = "You don't have full control over this sharer's home!";
-        
+        //瀵嗙爜閿欒鎴栬�呯綉鍏宠鍏跺畠浣忓畢缁戝畾锛屼笌缃戝叧閫氳澶辫触! 璇烽暱鎸夌綉鍏虫寜閽�3S锛岃繘鍏ョ孩鐏父浜紝鐒跺悗閲嶆柊鎼滅储锛�
+        public readonly static string GatewayPasswordError = "The password is wrong or the gateway is bound by another home, communication with the gateway failed! Please long press the gateway button 3S, the red light is always on, and then search again!";
     }
 }
diff --git a/Crabtree/SmartHome/HDL/Operation/UserConfig.cs b/Crabtree/SmartHome/HDL/Operation/UserConfig.cs
index a052da0..0a8f065 100644
--- a/Crabtree/SmartHome/HDL/Operation/UserConfig.cs
+++ b/Crabtree/SmartHome/HDL/Operation/UserConfig.cs
@@ -266,6 +266,12 @@
         /// </summary>
         public string LocalEncryptKey = string.Empty;
 
+
+        /// <summary>
+        /// 鏄惁鍔犲瘑瀵嗙爜楠岃瘉閫氳繃
+        /// </summary>
+        public bool EncryptedPasswordCorrect = true;
+
         /// <summary>
         /// 鏄惁鍔犲瘑
         /// </summary>
diff --git a/Crabtree/SmartHome/UI/SimpleControl/CommonPage.cs b/Crabtree/SmartHome/UI/SimpleControl/CommonPage.cs
index 3991130..91d2ce6 100644
--- a/Crabtree/SmartHome/UI/SimpleControl/CommonPage.cs
+++ b/Crabtree/SmartHome/UI/SimpleControl/CommonPage.cs
@@ -326,7 +326,7 @@
                         }
                     }
                     break;
-                case Command. ReadAnalogValueACK:
+                case Command.ReadAnalogValueACK:
                     foreach (var room in Room.Lists) {
                         foreach (var common in room.DeviceList) {
                             if (common.SubnetID != subnetID || common.DeviceID != deviceID || common.LoopID != usefullBytes [1]) {
@@ -350,7 +350,7 @@
 
 
 
-                                int dddd = usefullBytes [0] & 0x3F;
+                            int dddd = usefullBytes [0] & 0x3F;
 
                             // 0xXX锛氬叕鍙稿唴閮ㄥ崗璁搴旂殑搴忓彿
                             // common.Type锛氬搴旂殑鏄紪杈戣蒋浠剁殑搴忓彿(娌℃湁涓�涓�瀵瑰簲锛屾槸涓轰簡灏嗙浉杩戠殑璋冩暣鍦ㄤ竴璧凤紝鏂逛究瀹㈡埛浣跨敤)
diff --git a/Crabtree/SmartHome/UI/SimpleControl/EquipmentPublicClass.cs b/Crabtree/SmartHome/UI/SimpleControl/EquipmentPublicClass.cs
index f308fc1..98f134f 100644
--- a/Crabtree/SmartHome/UI/SimpleControl/EquipmentPublicClass.cs
+++ b/Crabtree/SmartHome/UI/SimpleControl/EquipmentPublicClass.cs
@@ -62,7 +62,7 @@
                         });
                         bool canRemote = false;
                         if (CommonPage.IsRemote) {
-                            await SmartHome.MqttCommon.StartCloudMqtt ();
+                            //await SmartHome.MqttCommon.StartCloudMqtt ();
                             SmartHome.MqttCommon.MqttRemoteSend (new byte [] { }, 3);
                         }
                         foreach (var gatewayFileName in gateWayList) {
@@ -131,7 +131,7 @@
                             if (UserConfig.Instance.CheckHomeGateways ()) {
                                 if (!string.IsNullOrEmpty (UserConfig.Instance.CurrentRegion.HomeGateways[0].GatewayUniqueId)) {
                                     Shared.SimpleControl.CommonPage.IsRemote = true;
-                                    await SmartHome.MqttCommon.StartCloudMqtt ();
+                                    //await SmartHome.MqttCommon.StartCloudMqtt ();
                                 }
                             }
                         }
diff --git a/Crabtree/SmartHome/UI/SimpleControl/MainPage.cs b/Crabtree/SmartHome/UI/SimpleControl/MainPage.cs
index 5e75840..39af71c 100644
--- a/Crabtree/SmartHome/UI/SimpleControl/MainPage.cs
+++ b/Crabtree/SmartHome/UI/SimpleControl/MainPage.cs
@@ -39,7 +39,7 @@
 
         //public static Button LogoButton = new Button ();
 
-        public static string RequestVersion = "20200114";
+        public static string RequestVersion = "20200115";
         public static UserInfo LoginUser;
         /// <summary>
         /// 鏄惁鏄鐞嗗憳鏉冮檺(鍙樻洿浜�,鎴愬憳鐨勬椂鍊�,杩欎釜涔熶负ture銆備负浠�涔堜細澹版槑杩欐牱鍙橀噺,鍥犱负鏈変簺鎺ュ彛蹇呴』浣跨敤鍘熸潵鐨凾oken)
diff --git a/Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs b/Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs
index 7d6ebf0..2536317 100644
--- a/Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs
+++ b/Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs
@@ -41,6 +41,73 @@
         //static bool thisShowTip = true;
         static string mqttRequestParToken = "";
 
+        static MqttCommon ()
+        {
+            InitMqtt ();
+        }
+
+        public static async System.Threading.Tasks.Task InitMqtt ()
+        {
+            new System.Threading.Thread (async () => {
+                while (true) {
+                    System.Threading.Thread.Sleep (100);
+                    if (!CommonPage.IsRemote) continue;
+
+                    await StartCloudMqtt ();
+                    await CheckingSubscribeTopics ();
+
+                }
+            }) { IsBackground = true }.Start ();
+        }
+
+        /// <summary>
+        /// 鏄惁璁㈤槄鎴愬姛
+        /// </summary>
+        static bool isSubscribeTopicSuccess = false;
+        /// <summary>
+        /// 妫�鏌ヤ富棰樻槸鍚﹁闃呭け璐�
+        /// </summary>
+        /// <returns></returns>
+        static async Task CheckingSubscribeTopics ()
+        {
+            if (!remoteIsConnected) {
+                return;
+            }
+            try {
+                if (!isSubscribeTopicSuccess) {
+
+                    var topicFilterCommon = new TopicFilter () {
+                        Topic = $"/BusGateWayToClient/{CurRemoteMACInfo.macMark}/Common/#",
+                        QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce
+                    };
+
+                    //缃戝叧閲嶆柊鐧诲綍涓婚
+                    var topicFilterGateWayInfoChange = new TopicFilter () {
+                        Topic = $"/BusGateWayToClient/{CurRemoteMACInfo.macMark}/NotifyBusGateWayInfoChange",
+                        QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce
+                    };
+
+                    //鎸や笅绾夸富棰�
+                    var topicFilterNotifySqueeze = new TopicFilter () {
+                        Topic = $"/BusGateWayToClient/{mMqttInfo.connEmqClientId}/Push/NotifySqueeze",
+                        QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce
+                    };
+
+
+
+                    var topicFilters = new TopicFilter [] { topicFilterCommon, topicFilterGateWayInfoChange, topicFilterNotifySqueeze };
+                    var result = await RemoteMqttClient.SubscribeAsync (topicFilters);
+                    if (result.Items [0].ResultCode == MQTTnet.Client.Subscribing.MqttClientSubscribeResultCode.GrantedQoS2) {
+                        isSubscribeTopicSuccess = true;
+                    }
+                }
+            } catch (Exception e) {
+               
+            }
+        }
+
+
+
         /// <summary>
         /// 鏂紑杩滅▼Mqtt鐨勯摼鎺�
         /// </summary>
@@ -92,16 +159,16 @@
             //杩藉姞锛氭病鏈夎繙绋嬭繛鎺ョ殑鏉冮檺
             if (remoteMqttIsConnecting
                || remoteIsConnected) {
-                System.Console.WriteLine ($"============>MqttremoteMqttIsConnecting:{remoteMqttIsConnecting}  remoteIsConnected:{remoteIsConnected} ");
+                //System.Console.WriteLine ($"============>MqttremoteMqttIsConnecting:{remoteMqttIsConnecting}  remoteIsConnected:{remoteIsConnected} ");
 
                 return;
             }
 
+            remoteMqttIsConnecting = true;
             await System.Threading.Tasks.Task.Factory.StartNew (async () => {
                 try {
-                    lock (RemoteMqttClient) {
+                    //lock (RemoteMqttClient) {
                         //琛ㄧず鍚庨潰灏嗚繘琛岃繛鎺�
-                        remoteMqttIsConnecting = true;
 
                         #region 鍒濆鍖栬繙绋婱qtt
                         //(3)褰揫杩炴帴浜戠鐨凪qtt鎴愬姛鍚嶿鎴栬�匸浠ュ強鍚庨潰App閫氳繃浜戠Mqtt杞彂鏁版嵁缁欑綉鍏虫垚鍔熷悗],澶勭悊鎺ユ敹鍒颁簯绔暟鎹寘鍝嶅簲鏃跺湪mqttServerClient_ApplicationMessageReceived杩欎釜鏂规硶澶勭悊
@@ -175,6 +242,7 @@
                         if (RemoteMqttClient.DisconnectedHandler == null) {
                             RemoteMqttClient.UseDisconnectedHandler (async (e) => {
                                 System.Console.WriteLine ($"============>Mqtt杩滅▼杩炴帴鏂紑");
+                                isSubscribeTopicSuccess = false;
                                 await DisConnectRemoteMqttClient ("StartRemoteMqtt.DisconnectedHandler");
                                 //await StartRemoteMqtt();
                                 //if (thisShowTip) {
@@ -211,8 +279,7 @@
                             });
                         }
                         #endregion
-                    }
-                    try {
+                    //}
 
                         mqttRequestParToken = MainPage.LoginUser.LoginTokenString;
                         //--绗竴姝ワ細鑾峰彇mqtt閾炬帴鍙傛暟
@@ -271,11 +338,6 @@
                             }
                         }
 
-
-
-                    } catch (Exception ex) {
-                        Console.WriteLine (ex.Message);
-                    }
                 } catch (Exception ex) {
                     System.Console.WriteLine ($"============>Mqtt 杩滅▼杩炴帴閫氳杩炴帴鍑哄紓甯�:{ex.Message}");
                 } finally {
@@ -310,22 +372,14 @@
                 await DisConnectRemoteMqttClient ("StartRemoteMqtt");
                 await RemoteMqttClient.ConnectAsync (options1);
                 remoteIsConnected = true;
-                await MqttRemoteSend (new byte [] { 0 }, 1);
-                await MqttRemoteSend (new byte [] { 0 }, 2);
-                await MqttRemoteSend (new byte [] { 0 }, 4);
+                //await MqttRemoteSend (new byte [] { 0 }, 1);
+                //await MqttRemoteSend (new byte [] { 0 }, 2);
+                //await MqttRemoteSend (new byte [] { 0 }, 4);
 
             }
         }
 
-        //public static async System.Threading.Tasks.Task InitMqtt () {
-
-        //    while (true) {
-        //        await StartCloudMqtt ();
-        //        System.Threading.Thread.Sleep (100);
-        //    }
-        //}
-
-
+     
         /// <summary>
         /// 
         /// </summary>
@@ -351,59 +405,14 @@
                     try {
                          RemoteMqttClient.PublishAsync (m);
                     } catch (Exception e) {
-                        await DisConnectRemoteMqttClient (e.Message);
-                        await StartCloudMqtt ();
-                        if (remoteIsConnected) {
-                            RemoteMqttClient.PublishAsync (m);
-                        }
+                        //await DisConnectRemoteMqttClient (e.Message);
+                        //await StartCloudMqtt ();
+                        //if (remoteIsConnected) {
+                        //    RemoteMqttClient.PublishAsync (m);
+                        //}
                     }
                     //}
                     break;
-                case 1:
-                    topicName = $"/BusGateWayToClient/{CurRemoteMACInfo.macMark}/Common/#";
-                    //if (remoteIsConnected) {
-                    try {
-                        await RemoteMqttClient.SubscribeAsync (topicName);
-                    } catch (Exception e) {
-                        await DisConnectRemoteMqttClient (e.Message);
-                        await StartCloudMqtt ();
-                        if (remoteIsConnected) {
-                            await RemoteMqttClient.SubscribeAsync (topicName);
-                        }
-                    }
-                    // }
-                    break;
-                case 2:
-                    var macStr = CurRemoteMACInfo.mac.ToUpper ();
-                    char [] cArrs = macStr.ToCharArray ();
-                    Array.Reverse (cArrs);
-                    var sss = string.Join (string.Empty, cArrs);
-
-                    using (var provider = new MD5CryptoServiceProvider ()) {
-                        byte [] buffer = provider.ComputeHash (Encoding.Default.GetBytes (sss));
-                        StringBuilder builder = new StringBuilder ();
-                        for (int i = 0; i < buffer.Length; i++) {
-                            builder.Append (buffer [i].ToString ("x2"));
-                        }
-                        CurRemoteMACInfo.md5_mac_string = builder.ToString ().ToUpper ();
-                    }
-
-                    //topicName = $"/NotifyBusGateWayInfoChagne/{CurRemoteMACInfo.md5_mac_string}";
-                    //2020-01-11 淇敼璁㈤槄涓婚鍦板潃
-                    topicName = $"/BusGateWayToClient/{CurRemoteMACInfo.macMark}/NotifyBusGateWayInfoChange";
-                    //if (remoteIsConnected) {
-                    try {
-                        await RemoteMqttClient.SubscribeAsync (topicName);
-                    } catch (Exception e) {
-                        await DisConnectRemoteMqttClient (e.Message);
-                        await StartCloudMqtt ();
-                        if (remoteIsConnected) {
-                            await RemoteMqttClient.SubscribeAsync (topicName);
-                        }
-                    }
-                    //}
-                    break;
-
                 case 3:
                     topicName = $"/ClientToBusGateWay/{CurRemoteMACInfo.macMark}/Common/CheckGateway";
 
@@ -411,29 +420,12 @@
 
                     try {
                         Console.WriteLine ("CheckGateway");
-                        await RemoteMqttClient.PublishAsync (m1);
+                         RemoteMqttClient.PublishAsync (m1);
                     } catch (Exception e) {
                         Console.WriteLine ($"CheckGateway Fail:{e.Message}");
-                        await DisConnectRemoteMqttClient (e.Message);
-                        await StartCloudMqtt ();
+                        //await DisConnectRemoteMqttClient (e.Message);
+                        //await StartCloudMqtt ();
                     }
-                    break;
-
-                case 4:
-                    //2020-01-13 淇敼鎸や笅绾夸富棰�
-                    topicName = $"/BusGateWayToClient/{mMqttInfo.connEmqClientId}/Push/NotifySqueeze";
-                    //if (remoteIsConnected) {
-                    try {
-                        await RemoteMqttClient.SubscribeAsync (topicName);
-                    } catch (Exception e) {
-                        await DisConnectRemoteMqttClient (e.Message);
-                        await StartCloudMqtt ();
-                        if (remoteIsConnected) {
-                            await RemoteMqttClient.SubscribeAsync (topicName);
-                        }
-                    }
-
-                    //}
                     break;
                 }
 
diff --git a/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideAddGateway.cs b/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideAddGateway.cs
index 6d97bb6..4340598 100644
--- a/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideAddGateway.cs
+++ b/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideAddGateway.cs
@@ -348,6 +348,8 @@
         Alert checkInternetAlert = new Alert ("", "Your phone in not connected to WIFI network, please connet", "Close", "Search again");
         Alert confirmAlert = new Alert ("", "Please make sure gateway is powered up and comect to WiFi router.", "Close", "Search again");
         Alert confirmAlert1 = new Alert ("", "If not all the gateways are in the list,you can search again.", "Close", "Search again");
+        Alert wrongPasswordAlert = new Alert ("", ErrorCode.GatewayPasswordError, "Close", "Search again");
+
         void SearchGatewayList ()
         {
             if (UserConfig.Instance.internetStatus == 0 || UserConfig.Instance.internetStatus == 1) {
@@ -370,6 +372,7 @@
                     #region 鎼滅储璁惧
                     ////2020-01-14 榛樿鏄庢枃寮�濮嬫悳绱�
                     UserConfig.Instance.IsLocalEncrypt = false;
+                    UserConfig.Instance.EncryptedPasswordCorrect = true;
 
                     CommonPage.LocalPhoneFindDevice = true;
                     CommonPage.RandomHigh = (byte)new Random ().Next (255);
@@ -433,13 +436,9 @@
                         if (CommonPage.GateWayList.Count == 0) {
                             MainPage.Loading.Hide ();
                             if (this != null && this.Parent != null) {
-                                confirmAlert.Show ();
-                                confirmAlert.ResultEventHandler += (ddf, ddd) => {
-                                    MainPage.Loading.Hide ();
-                                    if (ddd) {
-                                        SearchGatewayList ();
-                                    }
-                                };
+                                ShowConfirmAlert ();
+                                
+                               
                             }
                         } else {
                             if (this != null && this.Parent != null) {
@@ -459,7 +458,49 @@
             }) { IsBackground = true }.Start ();
         }
 
+        /// <summary>
+        /// 鎻愮ず
+        /// Please make sure gateway is powered up and comect to WiFi router.
+        /// 鎴栬��
+        /// 
+        /// </summary>
+        static void ShowConfirmAlert () {
+            if (UserConfig.Instance.IsLocalEncrypt) {
+                //瀵嗙爜妫�楠屾纭�
+                if (UserConfig.Instance.EncryptedPasswordCorrect) {
 
+                    inView.confirmAlert.Show ();
+                    inView.confirmAlert.ResultEventHandler += (ddf, ddd) => {
+                        MainPage.Loading.Hide ();
+                        if (ddd) {
+                            inView.SearchGatewayList ();
+                        }
+                    };
+                } else {
+
+
+                    inView.wrongPasswordAlert.Show ();
+                    inView.wrongPasswordAlert.ResultEventHandler += (ddf, ddd) => {
+                        MainPage.Loading.Hide ();
+                        if (ddd) {
+                            inView.SearchGatewayList ();
+                        }
+                    };
+
+                }
+            } else {
+                inView.confirmAlert.Show ();
+                inView.confirmAlert.ResultEventHandler += (ddf, ddd) => {
+                    MainPage.Loading.Hide ();
+                    if (ddd) {
+                        inView.SearchGatewayList ();
+                    }
+                };
+
+            }
+           
+
+        }
 
 
         //static byte [] AseKeyBytes;
@@ -532,19 +573,19 @@
             });
 
             bool goNext = false;
-            if (!UserConfig.Instance.IsLocalEncrypt) {
-                //濡傛灉娌″姞瀵嗭紝璧颁慨鏀瑰姞瀵嗘柟娉�
-                var result = SetGateWayLocalEncryption (common);
-                if (!result) {
-                    MainPage.LoadingHide ();
-                    return result;//淇敼澶辫触 杩斿洖goNext false
-                }
-                //淇敼鎴愬姛,璁剧疆鏈湴涓哄姞瀵嗘柟寮�
-                UserConfig.Instance.IsLocalEncrypt = true;
-                //string aseKeyStr = CommonPage.MyEncodingUTF8.GetString (AseKeyBytes);
-                //UserConfig.Instance.LocalEncryptKey = aseKeyStr;
-                System.Console.WriteLine ($"============>LocalEncryptKey 鍔犲瘑鎴愬姛Key涓猴細{UserConfig.Instance.LocalEncryptKey}");
-            }
+            //if (!UserConfig.Instance.IsLocalEncrypt) {
+            //    //濡傛灉娌″姞瀵嗭紝璧颁慨鏀瑰姞瀵嗘柟娉�
+            //    var result = SetGateWayLocalEncryption (common);
+            //    if (!result) {
+            //        MainPage.LoadingHide ();
+            //        return result;//淇敼澶辫触 杩斿洖goNext false
+            //    }
+            //    //淇敼鎴愬姛,璁剧疆鏈湴涓哄姞瀵嗘柟寮�
+            //    UserConfig.Instance.IsLocalEncrypt = true;
+            //    //string aseKeyStr = CommonPage.MyEncodingUTF8.GetString (AseKeyBytes);
+            //    //UserConfig.Instance.LocalEncryptKey = aseKeyStr;
+            //    System.Console.WriteLine ($"============>LocalEncryptKey 鍔犲瘑鎴愬姛Key涓猴細{UserConfig.Instance.LocalEncryptKey}");
+            //}
 
             var backBytes = Control.ControlBytesSendHasReturn (Command.Read_APP_Data_STORE_1D5C_CMD, common.SubnetID, common.DeviceID, new byte [] { });
           
@@ -552,9 +593,8 @@
                 Application.RunOnMainThread (() => {
                     new Alert ("", "No response from gateway. Please make sure the gateway is online and its firmware is up to date.", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                 });
+               
             }
-
-            MainPage.LoadingHide ();
 
             if (backBytes.Length >= 37) {
                 //******************浣忓畢ID string绫诲瀷*************
@@ -565,19 +605,57 @@
                 //2020-01-12
                 backIDStr = backIDStr.Replace ("\0", "");
                 if (enable || backIDStr == UserConfig.Instance.CurrentRegion.Id || string.IsNullOrEmpty (backIDStr)) {
+                   
+
+                    if (!UserConfig.Instance.IsLocalEncrypt) {
+                        //濡傛灉娌″姞瀵嗭紝璧颁慨鏀瑰姞瀵嗘柟娉�
+                        var result = SetGateWayLocalEncryption (common);
+                        if (!result) {
+                            MainPage.LoadingHide ();
+                            return result;//淇敼澶辫触 杩斿洖goNext false
+                        }
+                        //淇敼鎴愬姛,璁剧疆鏈湴涓哄姞瀵嗘柟寮�
+                        UserConfig.Instance.IsLocalEncrypt = true;
+                        System.Console.WriteLine ($"============>LocalEncryptKey 鍔犲瘑鎴愬姛Key涓猴細{UserConfig.Instance.LocalEncryptKey}");
+                    }
                     goNext = true;
+
                 } else {
-                    Application.RunOnMainThread (() => {
-                        new Alert ("", ErrorCode.UnableToBindGateway, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
-                    });
+                    UnableToBindGatewayAlertShow ();
                 }
             } else {
                 Application.RunOnMainThread (() => {
                     new Alert ("", ErrorCode.GatewayVersionLow, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                 });
             }
+            MainPage.LoadingHide ();
             return goNext;
 
         }
+
+
+        static void UnableToBindGatewayAlertShow ()
+        {
+            if (inView == null)
+                return;
+            Application.RunOnMainThread (() => {
+
+                //if (this != null && this.Parent != null) {
+                var alter = new Alert ("", ErrorCode.UnableToBindGateway, Language.StringByID (R.MyInternationalizationString.Close));
+                alter.ResultEventHandler += (sender1, e1) => {
+                    if (e1) {
+                        if (inView == null)
+                            return;
+                        //閲嶇疆涓哄姞瀵嗘ā寮�
+                        UserConfig.Instance.IsLocalEncrypt = true;
+                        (inView.Parent as PageLayout).PageIndex -= 1;
+                    }
+                };
+                alter.Show ();
+                //}
+
+            });
+
+        }
     }
 }
\ No newline at end of file
diff --git a/Crabtree/SmartHome/UI/SimpleControl/Phone/Register/AccountLogin.cs b/Crabtree/SmartHome/UI/SimpleControl/Phone/Register/AccountLogin.cs
index bdc396d..e231673 100644
--- a/Crabtree/SmartHome/UI/SimpleControl/Phone/Register/AccountLogin.cs
+++ b/Crabtree/SmartHome/UI/SimpleControl/Phone/Register/AccountLogin.cs
@@ -74,12 +74,14 @@
                 TextAlignment = TextAlignment.CenterLeft,
                 Radius = (uint)Application.GetRealHeight (0),
                 TextColor = SkinStyle.Current.TextColor1,
-                //Text = account,
-#if DEBUG
-                //Text = "18926269190@189.cn"
-                Text = "721343314@qq.com"
-#endif
+                Text = account,
+
             };
+#if DEBUG
+            //Text = "18926269190@189.cn"
+            etAccount.Text = "721343314@qq.com";
+#endif
+
             accountView.AddChidren (etAccount);
 
             var userConfigBytes = IO.FileUtils.ReadFile ("AccountListDB");
@@ -121,11 +123,12 @@
                 TextAlignment = TextAlignment.CenterLeft,
                 SecureTextEntry = true,
                 TextColor = SkinStyle.Current.TextColor1,
-#if DEBUG
-                Text = "123456"
-#endif
-                //Text = password,
+
+                Text = password,
             };
+#if DEBUG
+            etPasswrod.Text = "123456";
+#endif
             passwrodView.AddChidren (etPasswrod);
 
             Button btnShowPassword = new Button () {
diff --git a/Crabtree/SmartHome/UI/SimpleControl/Phone/Schedule/HelpView.cs b/Crabtree/SmartHome/UI/SimpleControl/Phone/Schedule/HelpView.cs
index 1e0b479..5ea1451 100644
--- a/Crabtree/SmartHome/UI/SimpleControl/Phone/Schedule/HelpView.cs
+++ b/Crabtree/SmartHome/UI/SimpleControl/Phone/Schedule/HelpView.cs
@@ -29,6 +29,17 @@
             };
             topView.AddChidren (title);
 
+            title.MouseLongEventHandler += (sd, ds) => {
+//#if DEBUG
+                try {
+                    //2020-01-15 闀挎寜鏄剧ず褰撳墠浣忓畢ID
+                    new Alert ("", UserConfig.Instance.CurrentRegion.Id, "close").Show ();
+                } catch { }
+//#endif
+
+            };
+
+
             var back = new Button () {
                 Height = Application.GetRealHeight (100),
                 Width = Application.GetRealWidth (85),
diff --git a/Crabtree/SmartHome/UI/SimpleControl/Phone/User/UserSettingView.cs b/Crabtree/SmartHome/UI/SimpleControl/Phone/User/UserSettingView.cs
index 2e1fb7b..47d179f 100644
--- a/Crabtree/SmartHome/UI/SimpleControl/Phone/User/UserSettingView.cs
+++ b/Crabtree/SmartHome/UI/SimpleControl/Phone/User/UserSettingView.cs
@@ -475,6 +475,7 @@
             btnhelpTitle.MouseLongEventHandler += (sd, ds) => {
                 //var uc = UserConfig.Instance.RometoUserInfoKeys.Find ((obj) => obj.Contains ("HiddenJson"));
                 //new Alert ("", uc, "close").Show ();
+
             };
 
             EventHandler<MouseEventArgs> helpEventHandler = (sender, e) => {

--
Gitblit v1.8.0