Skip to content

Commit

Permalink
fix: toggling logic
Browse files Browse the repository at this point in the history
  • Loading branch information
OpenSrcerer committed Mar 17, 2024
1 parent fe2d380 commit ab1c1c8
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ScheduledPingPong(

// @Scheduled(every = "15s")
fun pingDevicesPeriodically() {
rxMqttClient.publish("device-reads", "{\"message\":\"Ping!\"}")
?.subscribe()
// rxMqttClient.publish("device-reads", "{\"message\":\"Ping!\"}")
// ?.subscribe()
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package online.danielstefani.paddy.daemon

import com.hivemq.client.mqtt.datatypes.MqttQos
import io.smallrye.mutiny.Uni
import jakarta.enterprise.context.ApplicationScoped
import online.danielstefani.paddy.daemon.dto.CreateDaemonResponse
Expand All @@ -14,7 +15,7 @@ import org.eclipse.microprofile.rest.client.inject.RestClient
class DaemonService(
private val daemonRepository: DaemonRepository,
private val userRepository: UserRepository,
private val rxMqttClient: RxMqttClient,
private val mqtt: RxMqttClient,
@RestClient private val paddyAuth: JwtAuthClient
) {
fun getDaemon(daemonId: String): Daemon? {
Expand Down Expand Up @@ -47,10 +48,10 @@ class DaemonService(
fun toggleDaemon(username: String, daemonId: String): Boolean {
val user = userRepository.get(username)

daemonRepository.updateUserDaemon(user!!, daemonId)
val daemon = daemonRepository.updateUserDaemon(user!!, daemonId)
{ it.on = !it.on } ?: return false

rxMqttClient.publish("toggle", daemonId)
mqtt.publish(daemonId, "toggle", if (daemon.on) "1" else "0", MqttQos.EXACTLY_ONCE)
?.subscribe()

return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MqttController(
fun ping(daemonId: String, body: String?) {
val on = daemonService.getDaemon(daemonId)?.on ?: return

mqtt.publish(daemonId, if (on) "1" else "0", MqttQos.EXACTLY_ONCE)
mqtt.publish(daemonId, "toggle", if (on) "1" else "0", MqttQos.EXACTLY_ONCE)
?.subscribe()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class RxMqttClient(

fun publish(
daemonId: String,
action: String,
message: String = "",
qos: MqttQos = MqttQos.AT_MOST_ONCE,
): Flowable<Mqtt5PublishResult>? {
Expand Down Expand Up @@ -162,7 +163,7 @@ class RxMqttClient(
}
.doOnNext {
// If we didn't send the message - to prevent loops
if (it.topic.toString() != mqttConfig.deviceReadTopic()) {
if (it.topic.toString().startsWith(mqttConfig.deviceReadTopic())) {
mqttRouter.route(it)
}
}
Expand Down

0 comments on commit ab1c1c8

Please sign in to comment.