This commit is contained in:
@@ -3,17 +3,17 @@ import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence
|
||||
import java.time.Instant
|
||||
|
||||
class MqttSubscriber() : MqttCallbackExtended {
|
||||
private val broker = "tcp://mqtt.k8s.kmlabz.com:1883"
|
||||
private val broker = System.getenv("BROKER") ?: "tcp://mqtt.k8s.kmlabz.com:1883"
|
||||
private val clientId = MqttClient.generateClientId()
|
||||
private val persistence = MemoryPersistence()
|
||||
private val mqttClient = MqttClient(broker, clientId, persistence)
|
||||
private val benchValues = HashMap<Int, Instant>()
|
||||
private val benchValues = LinkedHashMap<Int, Instant>()
|
||||
private val connOpts = MqttConnectOptions()
|
||||
|
||||
init {
|
||||
connOpts.isCleanSession = true
|
||||
connOpts.userName = "birbnetes"
|
||||
connOpts.password = "de4d2182".toCharArray()
|
||||
connOpts.userName = System.getenv("USERNAME") ?: "birbnetes"
|
||||
connOpts.password = System.getenv("PASSWORD").toCharArray()
|
||||
connOpts.isAutomaticReconnect = true
|
||||
mqttClient.setCallback(this)
|
||||
}
|
||||
@@ -27,9 +27,7 @@ class MqttSubscriber() : MqttCallbackExtended {
|
||||
|
||||
@Throws(Exception::class)
|
||||
override fun messageArrived(topic: String, message: MqttMessage) {
|
||||
println("topic: $topic")
|
||||
benchValues.put(topic.split("/")[1].toInt(),Instant.now())
|
||||
println("message: " + String(message.payload))
|
||||
benchValues[topic.split("/")[1].toInt()] = Instant.now()
|
||||
}
|
||||
|
||||
override fun connectComplete(reconnect: Boolean, serverURI: String?) {
|
||||
@@ -50,7 +48,7 @@ class MqttSubscriber() : MqttCallbackExtended {
|
||||
}
|
||||
}
|
||||
|
||||
fun disconnect(): HashMap<Int, Instant> {
|
||||
fun disconnect(): LinkedHashMap<Int, Instant> {
|
||||
println("Disconnecting from broker")
|
||||
try {
|
||||
mqttClient.disconnect()
|
||||
|
||||
Reference in New Issue
Block a user