From 74a047ffed93a7a19ec716963e1c65ba23e42fe2 Mon Sep 17 00:00:00 2001 From: Daniel Stefani Date: Sun, 17 Mar 2024 20:40:53 +0200 Subject: [PATCH] feat: add logic to reply with daemon status on ping --- .../online/danielstefani/paddy/daemon/DaemonService.kt | 4 ++++ .../online/danielstefani/paddy/mqtt/MqttController.kt | 10 +++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/online/danielstefani/paddy/daemon/DaemonService.kt b/src/main/kotlin/online/danielstefani/paddy/daemon/DaemonService.kt index d1edbdf..8f9f028 100644 --- a/src/main/kotlin/online/danielstefani/paddy/daemon/DaemonService.kt +++ b/src/main/kotlin/online/danielstefani/paddy/daemon/DaemonService.kt @@ -17,6 +17,10 @@ class DaemonService( private val rxMqttClient: RxMqttClient, @RestClient private val paddyAuth: JwtAuthClient ) { + fun getDaemon(daemonId: String): Daemon? { + return daemonRepository.get(daemonId) + } + fun getAllUserDaemons(username: String): List { val user = userRepository.get(username) return daemonRepository.getAllUserDaemons(user!!) diff --git a/src/main/kotlin/online/danielstefani/paddy/mqtt/MqttController.kt b/src/main/kotlin/online/danielstefani/paddy/mqtt/MqttController.kt index 563919c..78455b3 100644 --- a/src/main/kotlin/online/danielstefani/paddy/mqtt/MqttController.kt +++ b/src/main/kotlin/online/danielstefani/paddy/mqtt/MqttController.kt @@ -2,14 +2,18 @@ package online.danielstefani.paddy.mqtt import io.quarkus.logging.Log import jakarta.enterprise.context.ApplicationScoped +import online.danielstefani.paddy.daemon.DaemonService @ApplicationScoped class MqttController( - private val mqtt: RxMqttClient + private val mqtt: RxMqttClient, + private val daemonService: DaemonService ) { - @DaemonAction("hello") + @DaemonAction("ping") fun hello(daemonId: String, body: String?) { - mqtt.publish(daemonId, "hello back!") + val on = daemonService.getDaemon(daemonId)?.on ?: return + + mqtt.publish(daemonId, if (on) "1" else "0") ?.subscribe() }