|
@@ -9,38 +9,49 @@ import com.xplora.commonservice.BaseApplication.Companion.globalContext
|
|
|
import com.xplora.commonservice.BaseApplication.Companion.iotConfig
|
|
|
import com.xplora.commonservice.BaseApplication.Companion.isTest
|
|
|
import com.xplora.commonservice.utils.Logger
|
|
|
-import kotlinx.coroutines.*
|
|
|
+import kotlinx.coroutines.CoroutineName
|
|
|
+import kotlinx.coroutines.CoroutineScope
|
|
|
+import kotlinx.coroutines.DelicateCoroutinesApi
|
|
|
+import kotlinx.coroutines.Dispatchers
|
|
|
+import kotlinx.coroutines.cancel
|
|
|
+import kotlinx.coroutines.delay
|
|
|
+import kotlinx.coroutines.launch
|
|
|
import java.security.KeyStore
|
|
|
|
|
|
object AwsIotManager {
|
|
|
const val TAG = "AwsIotManager"
|
|
|
|
|
|
private val client: MyAWSIotMqttClient by lazy { init() }
|
|
|
+ var isDisconnected = false
|
|
|
+
|
|
|
+ fun onCloseCallback() {
|
|
|
+ isDisconnected = true
|
|
|
+ }
|
|
|
|
|
|
private fun init(): MyAWSIotMqttClient {
|
|
|
-/* var certificateFile = ""
|
|
|
- var privateKeyFile = ""
|
|
|
- val paths = context.assets.list("aws_iot_test")
|
|
|
- for (path in paths!!) {
|
|
|
- Logger.d(TAG, path)
|
|
|
- if (path.equals("client.cert.development.pem")) certificateFile = path
|
|
|
- else if (path.equals("client.private.development.key")) privateKeyFile = path
|
|
|
- }
|
|
|
+ /* var certificateFile = ""
|
|
|
+ var privateKeyFile = ""
|
|
|
+ val paths = context.assets.list("aws_iot_test")
|
|
|
+ for (path in paths!!) {
|
|
|
+ Logger.d(TAG, path)
|
|
|
+ if (path.equals("client.cert.development.pem")) certificateFile = path
|
|
|
+ else if (path.equals("client.private.development.key")) privateKeyFile = path
|
|
|
+ }
|
|
|
|
|
|
- // Alternatively, you could load key store directly from a file - see the example included in this README.
|
|
|
- val pair: KeyStoreHelper.KeyStorePasswordPair =
|
|
|
- KeyStoreHelper.getKeyStorePasswordPair(certificateFile, privateKeyFile)!!
|
|
|
- var keyStoreFile: String? = null
|
|
|
- val paths = globalContext.assets.list("aws_iot_test")
|
|
|
- for (path in paths!!) {
|
|
|
- if (path.equals("my.keystore")) keyStoreFile = path
|
|
|
- }*/
|
|
|
+ // Alternatively, you could load key store directly from a file - see the example included in this README.
|
|
|
+ val pair: KeyStoreHelper.KeyStorePasswordPair =
|
|
|
+ KeyStoreHelper.getKeyStorePasswordPair(certificateFile, privateKeyFile)!!
|
|
|
+ var keyStoreFile: String? = null
|
|
|
+ val paths = globalContext.assets.list("aws_iot_test")
|
|
|
+ for (path in paths!!) {
|
|
|
+ if (path.equals("my.keystore")) keyStoreFile = path
|
|
|
+ }*/
|
|
|
|
|
|
val keyStorePassword = "sikey2022"
|
|
|
val keyPassword = "sikey2022"
|
|
|
|
|
|
val keyStore = KeyStore.getInstance(KeyStore.getDefaultType())
|
|
|
- val seyPath = if(!isTest) "my.keystore" else "test_new.keystore"
|
|
|
+ val seyPath = if (!isTest) "my.keystore" else "test_new.keystore"
|
|
|
Logger.d(TAG, "seyPath : $seyPath")
|
|
|
keyStore.load(globalContext.assets.open(seyPath), keyStorePassword.toCharArray())
|
|
|
|
|
@@ -59,8 +70,14 @@ object AwsIotManager {
|
|
|
CoroutineScope(Dispatchers.IO + CoroutineName("MQTT_connect")).launch {
|
|
|
try {
|
|
|
Logger.d(TAG, "connect : ${Thread.currentThread()}")
|
|
|
+ disconnect()
|
|
|
+ while (!isDisconnected) {
|
|
|
+ delay(100)
|
|
|
+ }
|
|
|
+ isDisconnected = false
|
|
|
client.apply {
|
|
|
- keepAliveInterval = (if(isTest) 3 else 5) * 60 * 1000 - 15 * 1000 // iotInfo.keepAlive - 15 * 1000
|
|
|
+ keepAliveInterval =
|
|
|
+ (if (isTest) 3 else 5) * 60 * 1000 - 15 * 1000 // iotInfo.keepAlive - 15 * 1000
|
|
|
maxConnectionRetries = 10
|
|
|
baseRetryDelay = 5 * 1000
|
|
|
maxRetryDelay = 2 * 60 * 1000
|
|
@@ -82,18 +99,22 @@ object AwsIotManager {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @OptIn(DelicateCoroutinesApi::class)
|
|
|
fun disconnect() {
|
|
|
- Logger.d(TAG, "disconnect")
|
|
|
- val scope = CoroutineScope(Dispatchers.IO + CoroutineName("MQTT_disconnect"))
|
|
|
- scope.launch {
|
|
|
- if (client.connectionStatus == AWSIotConnectionStatus.CONNECTED) {
|
|
|
+ CoroutineScope(Dispatchers.IO + CoroutineName("MQTT_disconnect")).launch {
|
|
|
+ Logger.d(TAG, "disconnect")
|
|
|
+ if (client.connectionStatus != AWSIotConnectionStatus.DISCONNECTED) {
|
|
|
try {
|
|
|
client.disconnect(10 * 1000, false)
|
|
|
} catch (e: AWSIotException) {
|
|
|
Logger.e(TAG, "AWSIotException : ${e.printStackTrace()}")
|
|
|
+ } finally {
|
|
|
+ isDisconnected = true
|
|
|
}
|
|
|
+ } else {
|
|
|
+ isDisconnected = true
|
|
|
}
|
|
|
- this.cancel()
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
}
|