change stuff
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Torma Kristóf 2020-10-21 17:53:07 +02:00
parent bff9aa5f24
commit 0575771daa
Signed by: tormakris
GPG Key ID: DC83C4F2C41B1047
2 changed files with 12 additions and 2 deletions

View File

@ -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<Int, Instant>()
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<Int, Instant> {
mqttClient.disconnect()
mqttClient.close()
return benchValues
}
}

View File

@ -1,5 +1,6 @@
fun main(){
val mqtt = MqttSubscriber()
mqtt.connect()
println("Hello World!")
Thread.sleep(10000)
mqtt.disconnect()
}