Jelajahi Sumber

优化MQTT配置

carlos 3 bulan lalu
induk
melakukan
7befdedf28

TEMPAT SAMPAH
XPCommonService.apk


+ 14 - 0
app/src/main/java/com/xplora/commonservice/modules/WatchStateChangeImpl.kt

@@ -339,6 +339,8 @@ object WatchStateChangeImpl : WatchStateChangeObserver, NetworkStateChangeObserv
                                 getFlowable(HttpApi.Watch.Power.SET, powerSetReq, null)
                             )
 
+                            getTempT()
+
                             CellManager.iccid?.let {
                                 val uploadIccidReq = UploadIccidReq(it)
                                 XPHttpServiceExecutor.execute(
@@ -358,6 +360,18 @@ object WatchStateChangeImpl : WatchStateChangeObserver, NetworkStateChangeObserv
         }
     }
 
+    private fun getTempT() {
+        GlobalScope.launch {
+            XPHttpServiceExecutor.execute(
+                null,
+                null,
+                getFlowable(HttpApi.Watch.TEMP_CONTROL, null, null)
+            ).let {
+                Logger.d("tempCtrl", "get temp ctrl threshold : $it")
+            }
+        }
+    }
+
     private fun duelNeedInit(): Boolean {
         val year = Calendar.getInstance().get(Calendar.YEAR)
         Logger.d(

+ 6 - 0
app/src/main/java/com/xplora/commonservice/modules/callbacks/LocalReceiver.kt

@@ -48,6 +48,7 @@ import com.xplora.commonservice.modules.database.DatabaseManager.resetStep
 import com.xplora.commonservice.modules.http.FlowableFactory.getFlowable
 import com.xplora.commonservice.modules.http.RetrofitFactory.retrofit
 import com.xplora.commonservice.modules.http.XPHttpServiceExecutor
+import com.xplora.commonservice.modules.mqtt.AwsIotManager
 import com.xplora.commonservice.modules.service.position.Position
 import com.xplora.commonservice.modules.service.position.Position.Companion.MODE_ONCE
 import com.xplora.commonservice.modules.service.position.Position.Companion.MODE_START_TRACKING
@@ -68,6 +69,11 @@ class LocalReceiver : BroadcastReceiver() {
 
     override fun onReceive(context: Context, intent: Intent) {
         Logger.d(TAG, "onReceive: ${intent.action}; ${Thread.currentThread()}")
+        val now = System.currentTimeMillis()
+        val mqttConnectionGap = now - AwsIotManager.lastConnectTime
+        if(!AwsIotManager.isDisconnected && mqttConnectionGap >= 3 * 60 * 60 * 1000 && !AwsIotManager.connecting) {
+            AwsIotManager.connect()
+        }
         when (intent.action) {
             ACTION_POSITION -> {
                 if (watchBaseInfo.activated) {

+ 1 - 0
app/src/main/java/com/xplora/commonservice/modules/http/RefreshToken.kt

@@ -17,6 +17,7 @@ object RefreshToken {
         c.add(this)
     }
 
+
     fun refresh(pushId: String?, initReq: InitReq): BaseResponse<*> {
         val result: BaseResponse<InitRepo> =
             BaseResponse("-1", CkError.ERROR_UNKNOWN, CkMessage.ERROR_UNKNOWN, null)

+ 8 - 3
app/src/main/java/com/xplora/commonservice/modules/mqtt/AwsIotManager.kt

@@ -28,6 +28,8 @@ object AwsIotManager {
     private val client: MyAWSIotMqttClient by lazy { init() }
     var isDisconnected = false
     var lastDisconnectTime = 0L
+    var lastConnectTime = 0L
+    var connecting = false
 
     fun onCloseCallback() {
         isDisconnected = true
@@ -79,6 +81,7 @@ object AwsIotManager {
     fun connect() {
         CoroutineScope(Dispatchers.IO + CoroutineName("MQTT_connect")).launch {
             try {
+                connecting = true
                 var needNewSession = false
                 if (System.currentTimeMillis() - lastDisconnectTime > 24 * 60 * 60 * 1000) {
                     needNewSession = true
@@ -88,18 +91,20 @@ object AwsIotManager {
                 while (!isDisconnected) {
                     delay(100)
                 }
-                isDisconnected = false
                 client.apply {
                     keepAliveInterval =
                         (if (isTest) 3 else 5) * 60 * 1000 - 15 * 1000  // iotInfo.keepAlive - 15 * 1000
-                    maxConnectionRetries = 10
+                    maxConnectionRetries = Int.MAX_VALUE
                     baseRetryDelay = 5 * 1000
-                    maxRetryDelay = 2 * 60 * 1000
+                    maxRetryDelay = 5 * 60 * 1000
                     connectionTimeout = 30 * 1000
                     isCleanSession = false
                 }.apply {
                     connect(10 * 1000, true).also {
+                        connecting = false
+                        isDisconnected = false
                         lastDisconnectTime = System.currentTimeMillis()
+                        lastConnectTime = System.currentTimeMillis()
                         if (needNewSession) {
                             callNewSession()
                         }