From 701f51a334ebef8ba143e806021b5ddf4a97bb49 Mon Sep 17 00:00:00 2001 From: Torma Date: Wed, 21 Oct 2020 19:12:19 +0200 Subject: [PATCH] add envvars --- src/main/kotlin/MQTTSubscriber.kt | 14 ++++++-------- src/main/kotlin/Main.kt | 8 ++++++-- src/main/kotlin/ProcessResults.kt | 16 ++++++++++++++++ 3 files changed, 28 insertions(+), 10 deletions(-) create mode 100644 src/main/kotlin/ProcessResults.kt diff --git a/src/main/kotlin/MQTTSubscriber.kt b/src/main/kotlin/MQTTSubscriber.kt index 5eebd05..bed594f 100644 --- a/src/main/kotlin/MQTTSubscriber.kt +++ b/src/main/kotlin/MQTTSubscriber.kt @@ -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() + private val benchValues = LinkedHashMap() 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 { + fun disconnect(): LinkedHashMap { println("Disconnecting from broker") try { mqttClient.disconnect() diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index d82904f..df25333 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -7,8 +7,12 @@ fun main() = runBlocking{ GlobalScope.launch { val mqtt = MqttSubscriber() mqtt.connect() - delay(10000L) - mqtt.disconnect() + val waitTime = System.getenv("WAITTIME").toLong() ?: 10000L + delay(waitTime) + val resultMap = mqtt.disconnect() + val resultProcessor = ProcessResults() + resultProcessor.mapToProcess=resultMap + resultProcessor.process() } delay(20000L) } \ No newline at end of file diff --git a/src/main/kotlin/ProcessResults.kt b/src/main/kotlin/ProcessResults.kt new file mode 100644 index 0000000..c3ca538 --- /dev/null +++ b/src/main/kotlin/ProcessResults.kt @@ -0,0 +1,16 @@ +import java.text.SimpleDateFormat +import java.time.Instant + + +class ProcessResults{ + var mapToProcess = LinkedHashMap() + private var formatter = SimpleDateFormat("yyyy-MM-dd_HH-mm-ss_SSS") + + + fun process(){ + for((id, instant) in this.mapToProcess){ + val currInstantString = formatter.format(instant) + println("$id, $currInstantString") + } + } +} \ No newline at end of file