code added related to nats
This commit is contained in:
@@ -157,7 +157,6 @@ dependencies {
|
|||||||
implementation("com.squareup.okhttp3:okhttp:4.9.3")
|
implementation("com.squareup.okhttp3:okhttp:4.9.3")
|
||||||
|
|
||||||
implementation "androidx.preference:preference-ktx:1.2.1"
|
implementation "androidx.preference:preference-ktx:1.2.1"
|
||||||
implementation 'io.nats:jnats:2.11.2'
|
|
||||||
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.1'
|
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.1'
|
||||||
|
|
||||||
implementation("androidx.work:work-runtime-ktx:2.9.0")
|
implementation("androidx.work:work-runtime-ktx:2.9.0")
|
||||||
@@ -166,4 +165,6 @@ dependencies {
|
|||||||
implementation 'com.google.android.play:core:1.10.3'
|
implementation 'com.google.android.play:core:1.10.3'
|
||||||
|
|
||||||
implementation fileTree(dir: 'libs', include: ['*.aar'])
|
implementation fileTree(dir: 'libs', include: ['*.aar'])
|
||||||
|
implementation 'io.nats:jnats:2.11.4'
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
package com.example.hpostesting.presentation
|
package com.example.hpostesting.presentation
|
||||||
|
|
||||||
|
import android.os.Build
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
import androidx.annotation.RequiresApi
|
||||||
import com.example.hpostesting.presentation.assurance.AssuranceControlsActivity
|
import com.example.hpostesting.presentation.assurance.AssuranceControlsActivity
|
||||||
import com.example.hpostesting.presentation.dashboard.DashboardActivity
|
import com.example.hpostesting.presentation.dashboard.DashboardActivity
|
||||||
import io.nats.client.AuthHandler
|
import io.nats.client.AuthHandler
|
||||||
@@ -14,6 +16,7 @@ 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
|
||||||
@@ -25,7 +28,7 @@ import javax.net.ssl.TrustManager
|
|||||||
import javax.net.ssl.TrustManagerFactory
|
import javax.net.ssl.TrustManagerFactory
|
||||||
|
|
||||||
|
|
||||||
class NatsManager(datacollector: AssuranceControlsActivity) {
|
class NatsManager(datacollector: DashboardActivity) {
|
||||||
|
|
||||||
val TAG = "Nats Service"
|
val TAG = "Nats Service"
|
||||||
var nc: Connection? = null
|
var nc: Connection? = null
|
||||||
@@ -37,41 +40,45 @@ class NatsManager(datacollector: AssuranceControlsActivity) {
|
|||||||
val clientCertPath = "/storage/sdcard0/Download/client.p12"
|
val clientCertPath = "/storage/sdcard0/Download/client.p12"
|
||||||
|
|
||||||
// Load client certificate and key
|
// Load client certificate and key
|
||||||
val keyStore = KeyStore.getInstance("PKCS12") // Ensure this matches your file type
|
val keyStore = KeyStore.getInstance("PKCS12")
|
||||||
FileInputStream(clientCertPath).use { keyStoreInputStream ->
|
FileInputStream(clientCertPath).use { keyStoreInputStream ->
|
||||||
keyStore.load(keyStoreInputStream, keyStorePassword)
|
keyStore.load(keyStoreInputStream, keyStorePassword)
|
||||||
}
|
}
|
||||||
|
val caCertPath = "/storage/sdcard0/Android/data/in.sminnovations.hpostesting.quality/files/NATS/clientCertificate/client-cert.pem"
|
||||||
// Initialize key manager factory
|
val caCert = FileInputStream(caCertPath).use { inputStream ->
|
||||||
val keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm())
|
val certificateFactory = CertificateFactory.getInstance("X.509")
|
||||||
keyManagerFactory.init(keyStore, keyStorePassword)
|
certificateFactory.generateCertificate(inputStream)
|
||||||
|
|
||||||
// Load CA certificate
|
|
||||||
val caCertPath = "/storage/sdcard0/Android/data/in.sminnovations.hpostesting.quality/files/extractedCertificates/clientCertificate/client-cert.pem"
|
|
||||||
val trustStore = KeyStore.getInstance(KeyStore.getDefaultType())
|
|
||||||
trustStore.load(null, null) // Initialize the trust store
|
|
||||||
|
|
||||||
// Load the CA certificate as a Certificate object
|
|
||||||
CertificateFactory.getInstance("X.509").generateCertificate(BufferedInputStream(FileInputStream(caCertPath))).also { caCertificate ->
|
|
||||||
trustStore.setCertificateEntry("ca-cert", caCertificate)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val trustStore = KeyStore.getInstance(KeyStore.getDefaultType()).apply {
|
||||||
|
load(null, null) // Initialize the keystore
|
||||||
|
setCertificateEntry("caCert", caCert) // Add the CA certificate
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize key manager factory
|
||||||
|
val kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm())
|
||||||
|
kmf.init(keyStore, keyStorePassword)
|
||||||
|
|
||||||
// Initialize trust manager factory
|
// Initialize trust manager factory
|
||||||
val trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm())
|
val tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm())
|
||||||
trustManagerFactory.init(trustStore)
|
tmf.init(trustStore)
|
||||||
|
|
||||||
// Initialize SSLContext
|
// Initialize SSLContext
|
||||||
val sslContext = SSLContext.getInstance("TLS")
|
val sslContext = SSLContext.getInstance("TLS")
|
||||||
sslContext.init(keyManagerFactory.keyManagers, trustManagerFactory.trustManagers, SecureRandom())
|
sslContext.init(kmf.keyManagers, tmf.trustManagers, SecureRandom())
|
||||||
|
|
||||||
return sslContext
|
return sslContext
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@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 = "SUAEB5PUWNS6C2HUV3MFI6HUDEAPGPFPBHMI73NHIQATCDD2BWALVEZXZ4"
|
val seedString = "SUAJH5VMO6GDRQA6QTXCLIJMS74IWIUTU3NJVYIOTZF2LBWUPD77DA2ZEA"
|
||||||
|
Log.e("seedString",seedString)
|
||||||
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
|
||||||
|
|||||||
@@ -30,9 +30,9 @@ class AssuranceControlsActivity: AppCompatActivity(), IDataCollector {
|
|||||||
.commit()
|
.commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
nats = NatsManager(this)
|
// nats = NatsManager(this)
|
||||||
nats.connect()
|
// nats.connect()
|
||||||
nats.pub("server.hpos.HCV-000-3001.ping", "THIS IS A TEST MSG")
|
// nats.pub("server.hpos.HCV-000-3001.ping", "THIS IS A TEST MSG")
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun setConnect(connect: Boolean) {
|
override fun setConnect(connect: Boolean) {
|
||||||
|
|||||||
@@ -59,9 +59,9 @@ class DashboardActivity : AppCompatActivity(), IDataCollector {
|
|||||||
binding = ActivityDashboardBinding.inflate(layoutInflater)
|
binding = ActivityDashboardBinding.inflate(layoutInflater)
|
||||||
setContentView(binding.root)
|
setContentView(binding.root)
|
||||||
setSupportActionBar(binding.appBarDashboard.toolbar)
|
setSupportActionBar(binding.appBarDashboard.toolbar)
|
||||||
// nats = NatsManager(this)
|
nats = NatsManager(this)
|
||||||
// nats.connect()
|
nats.connect()
|
||||||
// nats.pub("server.hpos.HCV-000-3001.ping", "THIS IS A TEST MSG")
|
nats.pub("server.hpos.HCV-000-3001.ping", "THIS IS A TEST MSG")
|
||||||
|
|
||||||
hemocubeViewModel.deviceUpdate.observe(this) { result ->
|
hemocubeViewModel.deviceUpdate.observe(this) { result ->
|
||||||
when (result) {
|
when (result) {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import android.content.Context
|
|||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.SharedPreferences
|
import android.content.SharedPreferences
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.util.Log
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
@@ -74,6 +75,11 @@ class DeviceProvisionFragment : Fragment() {
|
|||||||
Constants.NATS_TOKEN,
|
Constants.NATS_TOKEN,
|
||||||
response.data.data?.device?.deviceUser?.natsToken
|
response.data.data?.device?.deviceUser?.natsToken
|
||||||
)
|
)
|
||||||
|
response.data.data?.device?.deviceUser?.natsToken?.let {
|
||||||
|
Log.e("natstoken",
|
||||||
|
it
|
||||||
|
)
|
||||||
|
}
|
||||||
putString(
|
putString(
|
||||||
Constants.NATS_TOKEN_EXPIRE_DATE,
|
Constants.NATS_TOKEN_EXPIRE_DATE,
|
||||||
response.data.data?.device?.deviceUser?.natsTokenExpiry
|
response.data.data?.device?.deviceUser?.natsTokenExpiry
|
||||||
|
|||||||
Reference in New Issue
Block a user