diff --git a/app/src/main/java/com/example/hpostesting/presentation/NatsManager.kt b/app/src/main/java/com/example/hpostesting/presentation/NatsManager.kt index 433075e..ccc8b51 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/NatsManager.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/NatsManager.kt @@ -1,9 +1,10 @@ package com.example.hpostesting.presentation +import android.content.Context import android.os.Build import android.util.Log 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 io.nats.client.AuthHandler import io.nats.client.Connection @@ -12,20 +13,15 @@ import io.nats.client.NKey import io.nats.client.Nats import io.nats.client.Options import io.nats.client.support.SSLUtils -import java.io.BufferedInputStream -import java.io.File import java.io.FileInputStream import java.io.IOException import java.nio.charset.StandardCharsets -import java.nio.file.Paths import java.security.GeneralSecurityException import java.security.KeyStore import java.security.SecureRandom import java.security.cert.CertificateFactory -import javax.net.ssl.KeyManager import javax.net.ssl.KeyManagerFactory import javax.net.ssl.SSLContext -import javax.net.ssl.TrustManager import javax.net.ssl.TrustManagerFactory @@ -35,6 +31,7 @@ class NatsManager(datacollector: DashboardActivity) { var nc: Connection? = null val datacollector = datacollector var connect = false + var sharedPreferences = datacollector.getSharedPreferences("HEMOCUBE", Context.MODE_PRIVATE) private fun createSSLContext(): SSLContext { val keyStorePassword = "prime24".toCharArray() // Change as necessary @@ -71,16 +68,14 @@ class NatsManager(datacollector: DashboardActivity) { return sslContext } - - @RequiresApi(Build.VERSION_CODES.O) fun connect() { Log.d(TAG, "TRY TO CONNECT") Thread { - val seedString = "SUAJH5VMO6GDRQA6QTXCLIJMS74IWIUTU3NJVYIOTZF2LBWUPD77DA2ZEA" - Log.e("seedString",seedString) - val seedBytes = seedString.toCharArray() + val seedString = sharedPreferences.getString(Constants.NATS_TOKEN, "") + Log.e("seedString", seedString.toString()) + val seedBytes = seedString?.toCharArray() val theNKey = NKey.fromSeed(seedBytes) // really should load from somewhere @@ -125,50 +120,52 @@ class NatsManager(datacollector: DashboardActivity) { datacollector.setConnect(true) if (nc?.status == Connection.Status.CONNECTED) { - Log.e("NATSCONNECTION", "NATS is successfully connected.") + Log.d("NATSCONNECTION", "NATS is successfully connected.") } 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( - "server.hpos.HCV-000-3001.ping", + "server.hpos.${deviceId}.ping", "ALIVE".toByteArray(StandardCharsets.UTF_8) ) nc?.publish( - "server.hpos.HCV-000-3001.health", + "server.hpos.${deviceId}.health", "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? -> - 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) datacollector.setResponse(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) - datacollector.setResponse(response + "uPLOAD") + datacollector.setResponse(response + "UPLOAD") 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) datacollector.setResponse(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) datacollector.setResponse(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) datacollector.setResponse(response) println("Message received (up to 100 times): $response")