Jelajahi Sumber

无网重连优化

weizhengliang 3 tahun lalu
induk
melakukan
e88932eb4e

+ 28 - 26
callingbed/src/main/java/com/wdkl/app/ncs/callingbed/activity/CallingbedActivity.kt

@@ -249,6 +249,29 @@ class CallingbedActivity :BaseActivity<CallingbedActivityPresenter, CallingbedMa
         })
     }
 
+    override fun setTcpServerHost(tcpSeverDTO: TcpSeverDTO) {
+        Constant.TCP_SERVER_URL = tcpSeverDTO.localIp
+        Constant.TCP_PORT = tcpSeverDTO.tcpPort
+        Constant.TCP_HEART_BEAT = tcpSeverDTO.readerIdleTime
+
+        //开启TCP连接
+        startTcp()
+        showMessage("tcp开始连接...host: " + Constant.TCP_SERVER_URL + ", port: " + Constant.TCP_PORT)
+
+        Thread(Runnable {
+            while (!initialized) {
+                runOnUiThread(Runnable {
+                    initDevice()
+                })
+                try {
+                    Thread.sleep(8000)
+                } catch (e: Exception) {
+                    //
+                }
+            }
+        }).start()
+    }
+
     //显示设备信息
     override fun showDeviceInfo(deviceInfo: BedDeviceInfoVO) {
         Constant.SIP_ID = deviceInfo.sipId
@@ -316,29 +339,6 @@ class CallingbedActivity :BaseActivity<CallingbedActivityPresenter, CallingbedMa
         updateSettings(true)
     }
 
-    override fun setTcpServerHost(tcpSeverDTO: TcpSeverDTO) {
-        Constant.TCP_SERVER_URL = tcpSeverDTO.localIp
-        Constant.TCP_PORT = tcpSeverDTO.tcpPort
-        Constant.TCP_HEART_BEAT = tcpSeverDTO.readerIdleTime
-
-        //开启TCP连接
-        startTcp()
-        showMessage("tcp开始连接...host: " + Constant.TCP_SERVER_URL + ", port: " + Constant.TCP_PORT)
-
-        Thread(Runnable {
-            while (!initialized) {
-                runOnUiThread(Runnable {
-                    initDevice()
-                })
-                try {
-                    Thread.sleep(8000)
-                } catch (e: Exception) {
-                    //
-                }
-            }
-        }).start()
-    }
-
     override fun updateNurseConfig(list: List<NurseConfigDto>) {
         if (list.isNotEmpty()) {
             for ((index, e) in list.withIndex()) {
@@ -1068,9 +1068,8 @@ class CallingbedActivity :BaseActivity<CallingbedActivityPresenter, CallingbedMa
             if (intent.action == Intent.ACTION_TIME_TICK
                     || intent.action == Intent.ACTION_TIME_CHANGED
                     || intent.action == Intent.ACTION_TIMEZONE_CHANGED) {
-                //设备完成初始化才刷新
+                updateNetState()
                 if (initialized) {
-                    updateNetState()
                     updateSettings(false)
                     updateTcpState()
 
@@ -1083,10 +1082,13 @@ class CallingbedActivity :BaseActivity<CallingbedActivityPresenter, CallingbedMa
                     if (Constant.LATER_RESTART && Constant.CALL_STATE == Constant.CALL_STANDBY) {
                         AppUpdateHelper.restartApp(activity)
                     }
+                } else if (TextUtils.isEmpty(Constant.TCP_SERVER_URL)) {
+                    //还未连接tcp服务器
+                    presenter.loadTcpServerHost()
                 }
             } else if (intent.action == ConnectivityManager.CONNECTIVITY_ACTION) {
+                updateNetState()
                 if (initialized) {
-                    updateNetState()
                     if (NetHelper.getInstance().networkAvailable()) {
                         if (!Constant.TCP_CONNECTED) {
                             TcpClient.getInstance().doConnect()

+ 1 - 1
middleware/src/main/code/com/wdkl/ncs/android/middleware/common/Constant.java

@@ -5,7 +5,7 @@ public class Constant {
     public static String APP_PATH = "";
 
     //TCP服务器地址:
-    public static String TCP_SERVER_URL = "172.28.100.100";
+    public static String TCP_SERVER_URL = "";
 
     //数据接口端口:
     public static int URL_PORT = 8006;

+ 1 - 1
middleware/src/main/code/com/wdkl/ncs/android/middleware/tcp/TcpClientHandler.java

@@ -52,9 +52,9 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<String> {
     @Override
     public void channelInactive(ChannelHandlerContext ctx) throws Exception {
         super.channelInactive(ctx);
-        EventBus.getDefault().post(new MessageEvent(0, Constant.EVENT_TCP_STATE));
         connected = false;
         Constant.TCP_CONNECTED = false;
+        EventBus.getDefault().post(new MessageEvent(0, Constant.EVENT_TCP_STATE));
         Log.i(TAG, "失去连接");
         TcpClient.getInstance().doConnect();
         //reConnect(ctx);