add dynamic topic based on deviceId
This commit is contained in:
@@ -1,9 +1,10 @@
|
|||||||
package com.example.hpostesting.presentation
|
package com.example.hpostesting.presentation
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.annotation.RequiresApi
|
import androidx.annotation.RequiresApi
|
||||||
import com.example.hpostesting.presentation.assurance.AssuranceControlsActivity
|
import com.example.hpostesting.data.constant.Constants
|
||||||
import com.example.hpostesting.presentation.dashboard.DashboardActivity
|
import com.example.hpostesting.presentation.dashboard.DashboardActivity
|
||||||
import io.nats.client.AuthHandler
|
import io.nats.client.AuthHandler
|
||||||
import io.nats.client.Connection
|
import io.nats.client.Connection
|
||||||
@@ -12,20 +13,15 @@ import io.nats.client.NKey
|
|||||||
import io.nats.client.Nats
|
import io.nats.client.Nats
|
||||||
import io.nats.client.Options
|
import io.nats.client.Options
|
||||||
import io.nats.client.support.SSLUtils
|
import io.nats.client.support.SSLUtils
|
||||||
import java.io.BufferedInputStream
|
|
||||||
import java.io.File
|
|
||||||
import java.io.FileInputStream
|
import java.io.FileInputStream
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.nio.charset.StandardCharsets
|
import java.nio.charset.StandardCharsets
|
||||||
import java.nio.file.Paths
|
|
||||||
import java.security.GeneralSecurityException
|
import java.security.GeneralSecurityException
|
||||||
import java.security.KeyStore
|
import java.security.KeyStore
|
||||||
import java.security.SecureRandom
|
import java.security.SecureRandom
|
||||||
import java.security.cert.CertificateFactory
|
import java.security.cert.CertificateFactory
|
||||||
import javax.net.ssl.KeyManager
|
|
||||||
import javax.net.ssl.KeyManagerFactory
|
import javax.net.ssl.KeyManagerFactory
|
||||||
import javax.net.ssl.SSLContext
|
import javax.net.ssl.SSLContext
|
||||||
import javax.net.ssl.TrustManager
|
|
||||||
import javax.net.ssl.TrustManagerFactory
|
import javax.net.ssl.TrustManagerFactory
|
||||||
|
|
||||||
|
|
||||||
@@ -35,6 +31,7 @@ class NatsManager(datacollector: DashboardActivity) {
|
|||||||
var nc: Connection? = null
|
var nc: Connection? = null
|
||||||
val datacollector = datacollector
|
val datacollector = datacollector
|
||||||
var connect = false
|
var connect = false
|
||||||
|
var sharedPreferences = datacollector.getSharedPreferences("HEMOCUBE", Context.MODE_PRIVATE)
|
||||||
|
|
||||||
private fun createSSLContext(): SSLContext {
|
private fun createSSLContext(): SSLContext {
|
||||||
val keyStorePassword = "prime24".toCharArray() // Change as necessary
|
val keyStorePassword = "prime24".toCharArray() // Change as necessary
|
||||||
@@ -71,16 +68,14 @@ class NatsManager(datacollector: DashboardActivity) {
|
|||||||
return sslContext
|
return sslContext
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@RequiresApi(Build.VERSION_CODES.O)
|
@RequiresApi(Build.VERSION_CODES.O)
|
||||||
fun connect() {
|
fun connect() {
|
||||||
Log.d(TAG, "TRY TO CONNECT")
|
Log.d(TAG, "TRY TO CONNECT")
|
||||||
Thread {
|
Thread {
|
||||||
|
|
||||||
val seedString = "SUAJH5VMO6GDRQA6QTXCLIJMS74IWIUTU3NJVYIOTZF2LBWUPD77DA2ZEA"
|
val seedString = sharedPreferences.getString(Constants.NATS_TOKEN, "")
|
||||||
Log.e("seedString",seedString)
|
Log.e("seedString", seedString.toString())
|
||||||
val seedBytes = seedString.toCharArray()
|
val seedBytes = seedString?.toCharArray()
|
||||||
|
|
||||||
val theNKey = NKey.fromSeed(seedBytes) // really should load from somewhere
|
val theNKey = NKey.fromSeed(seedBytes) // really should load from somewhere
|
||||||
|
|
||||||
@@ -125,50 +120,52 @@ class NatsManager(datacollector: DashboardActivity) {
|
|||||||
datacollector.setConnect(true)
|
datacollector.setConnect(true)
|
||||||
|
|
||||||
if (nc?.status == Connection.Status.CONNECTED) {
|
if (nc?.status == Connection.Status.CONNECTED) {
|
||||||
Log.e("NATSCONNECTION", "NATS is successfully connected.")
|
Log.d("NATSCONNECTION", "NATS is successfully connected.")
|
||||||
} else {
|
} else {
|
||||||
Log.e("NATSCONNECTION", "NATS is not connected. Current status: ${nc?.status}")
|
Log.d("NATSCONNECTION", "NATS is not connected. Current status: ${nc?.status}")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val deviceId = sharedPreferences.getString(Constants.DEVICE_ID, "")
|
||||||
|
|
||||||
nc?.publish(
|
nc?.publish(
|
||||||
"server.hpos.HCV-000-3001.ping",
|
"server.hpos.${deviceId}.ping",
|
||||||
"ALIVE".toByteArray(StandardCharsets.UTF_8)
|
"ALIVE".toByteArray(StandardCharsets.UTF_8)
|
||||||
)
|
)
|
||||||
nc?.publish(
|
nc?.publish(
|
||||||
"server.hpos.HCV-000-3001.health",
|
"server.hpos.${deviceId}.health",
|
||||||
"ALIVE".toByteArray(StandardCharsets.UTF_8)
|
"ALIVE".toByteArray(StandardCharsets.UTF_8)
|
||||||
)
|
)
|
||||||
Log.d(TAG, "Published msg server.hpos.HCV-000-3001.ping on topic Testing")
|
|
||||||
val d = nc?.createDispatcher { msg: Message? ->
|
val d = nc?.createDispatcher { msg: Message? ->
|
||||||
println("PRITIMOI SARKAR $msg")
|
println("Nats dispatcher $msg")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
d?.subscribe("device.hpos.HCV-000-3001.update") { msg ->
|
d?.subscribe("device.hpos.${deviceId}.update") { msg ->
|
||||||
val response = String(msg.data, StandardCharsets.UTF_8)
|
val response = String(msg.data, StandardCharsets.UTF_8)
|
||||||
datacollector.setResponse(response)
|
datacollector.setResponse(response)
|
||||||
println("Message received (up to 100 times): $response")
|
println("Message received (up to 100 times): $response")
|
||||||
}
|
}
|
||||||
|
|
||||||
d?.subscribe("device.hpos.HCV-000-3001.uploadlogs") { msg ->
|
d?.subscribe("device.hpos.${deviceId}.uploadlogs") { msg ->
|
||||||
val response = String(msg.data, StandardCharsets.UTF_8)
|
val response = String(msg.data, StandardCharsets.UTF_8)
|
||||||
datacollector.setResponse(response + "uPLOAD")
|
datacollector.setResponse(response + "UPLOAD")
|
||||||
println("Message received (up to 100 times): $response")
|
println("Message received (up to 100 times): $response")
|
||||||
}
|
}
|
||||||
|
|
||||||
d?.subscribe("device.hpos.HCV-000-3001.disable") { msg ->
|
d?.subscribe("device.hpos.${deviceId}.disable") { msg ->
|
||||||
val response = String(msg.data, StandardCharsets.UTF_8)
|
val response = String(msg.data, StandardCharsets.UTF_8)
|
||||||
datacollector.setResponse(response)
|
datacollector.setResponse(response)
|
||||||
println("Message received (up to 100 times): $response")
|
println("Message received (up to 100 times): $response")
|
||||||
}
|
}
|
||||||
|
|
||||||
d?.subscribe("device.hpos.HCV-000-3001.updatecustomer") { msg ->
|
d?.subscribe("device.hpos.${deviceId}.updatecustomer") { msg ->
|
||||||
val response = String(msg.data, StandardCharsets.UTF_8)
|
val response = String(msg.data, StandardCharsets.UTF_8)
|
||||||
datacollector.setResponse(response)
|
datacollector.setResponse(response)
|
||||||
println("Message received (up to 100 times): $response")
|
println("Message received (up to 100 times): $response")
|
||||||
}
|
}
|
||||||
|
|
||||||
d?.subscribe("device.hpos.HCV-000-3001.checkupdate") { msg ->
|
d?.subscribe("device.hpos.${deviceId}.checkupdate") { msg ->
|
||||||
val response = String(msg.data, StandardCharsets.UTF_8)
|
val response = String(msg.data, StandardCharsets.UTF_8)
|
||||||
datacollector.setResponse(response)
|
datacollector.setResponse(response)
|
||||||
println("Message received (up to 100 times): $response")
|
println("Message received (up to 100 times): $response")
|
||||||
|
|||||||
Reference in New Issue
Block a user