From 0575771daaa1e5e929bdf0911dca7c2a38d2f590 Mon Sep 17 00:00:00 2001 From: Torma Date: Wed, 21 Oct 2020 17:53:07 +0200 Subject: [PATCH] change stuff --- src/main/kotlin/MQTTSubscriber.kt | 11 ++++++++++- src/main/kotlin/Main.kt | 3 ++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/MQTTSubscriber.kt b/src/main/kotlin/MQTTSubscriber.kt index cfea40b..6fb9070 100644 --- a/src/main/kotlin/MQTTSubscriber.kt +++ b/src/main/kotlin/MQTTSubscriber.kt @@ -1,12 +1,13 @@ import org.eclipse.paho.client.mqttv3.* 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 clientId = MqttClient.generateClientId() private val persistence = MemoryPersistence() private val mqttClient = MqttAsyncClient(broker, clientId, persistence) + private val benchValues = HashMap() override fun connectionLost(arg0: Throwable) { System.err.println("connection lost") @@ -18,7 +19,9 @@ 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)) + } override fun connectComplete(reconnect: Boolean, serverURI: String?) { @@ -33,4 +36,10 @@ class MqttSubscriber : MqttCallbackExtended { mqttClient.setCallback(MqttSubscriber()) mqttClient.connect(connOpts) } + + fun disconnect(): HashMap { + mqttClient.disconnect() + mqttClient.close() + return benchValues + } } \ No newline at end of file diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index 2786b34..47a038f 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -1,5 +1,6 @@ fun main(){ val mqtt = MqttSubscriber() mqtt.connect() - println("Hello World!") + Thread.sleep(10000) + mqtt.disconnect() } \ No newline at end of file