From 750048e1dfe270b014040d9725cd31dcd857e466 Mon Sep 17 00:00:00 2001 From: Stefan Oltmann Date: Sat, 11 May 2024 13:46:57 +0200 Subject: [PATCH] ValloxClient functions are suspending --- .../smarthome/vallox/Profile.kt | 2 +- .../smarthome/vallox/ValloxClient.kt | 39 ++++------ .../smarthome/vallox/ValloxClientImpl.kt | 75 +++++++++++-------- .../smarthome/vallox/ValloxDataMode.kt | 6 +- .../smarthome/vallox/ValloxException.kt | 2 +- .../smarthome/vallox/ValloxMessageHandler.kt | 2 +- .../smarthome/vallox/ValloxStatus.kt | 2 +- 7 files changed, 68 insertions(+), 60 deletions(-) diff --git a/src/main/java/de/stefan_oltmann/smarthome/vallox/Profile.kt b/src/main/java/de/stefan_oltmann/smarthome/vallox/Profile.kt index 0d60857..a553df3 100644 --- a/src/main/java/de/stefan_oltmann/smarthome/vallox/Profile.kt +++ b/src/main/java/de/stefan_oltmann/smarthome/vallox/Profile.kt @@ -1,6 +1,6 @@ /** * Copyright (c) 2010-2021 Contributors to the openHAB project - * Copyright (c) 2021 Stefan Oltmann + * Copyright (c) 2024 Stefan Oltmann * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at diff --git a/src/main/java/de/stefan_oltmann/smarthome/vallox/ValloxClient.kt b/src/main/java/de/stefan_oltmann/smarthome/vallox/ValloxClient.kt index 6f34a07..14d14e1 100644 --- a/src/main/java/de/stefan_oltmann/smarthome/vallox/ValloxClient.kt +++ b/src/main/java/de/stefan_oltmann/smarthome/vallox/ValloxClient.kt @@ -1,6 +1,6 @@ /** * Copyright (c) 2010-2021 Contributors to the openHAB project - * Copyright (c) 2021 Stefan Oltmann + * Copyright (c) 2024 Stefan Oltmann * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -10,43 +10,36 @@ */ package de.stefan_oltmann.smarthome.vallox -import java.net.URI - interface ValloxClient { - fun readStatus(): ValloxStatus - - fun turnOn() - - fun turnOff() + suspend fun readStatus(): ValloxStatus? - fun switchProfile(profile: Profile) + suspend fun turnOn() - fun setFanSpeed(profile: Profile, fanSpeed: Int) + suspend fun turnOff() - fun setExtractFanBalanceBase(fanSpeed: Int) + suspend fun switchProfile(profile: Profile) - fun setSupplyFanBalanceBase(fanSpeed: Int) + suspend fun setFanSpeed(profile: Profile, fanSpeed: Int) - fun setFireplaceExtractFanSpeed(fanSpeed: Int) + suspend fun setExtractFanBalanceBase(fanSpeed: Int) - fun setFireplaceSupplyFanSpeed(fanSpeed: Int) + suspend fun setSupplyFanBalanceBase(fanSpeed: Int) - fun setTargetTemperature(profile: Profile, targetTemperature: Int) + suspend fun setFireplaceExtractFanSpeed(fanSpeed: Int) - fun setBytesBoostTime(boostTimeInMinutes: Int) + suspend fun setFireplaceSupplyFanSpeed(fanSpeed: Int) - fun setBoostTimerEnabled(enabled: Boolean) + suspend fun setTargetTemperature(profile: Profile, targetTemperature: Int) - fun setFireplaceTime(fireplaceTimeInMinutes: Int) + suspend fun setBytesBoostTime(boostTimeInMinutes: Int) - fun setFireplaceTimerEnabled(enabled: Boolean) + suspend fun setBoostTimerEnabled(enabled: Boolean) - fun setWeeklyTimerEnabled(enabled: Boolean) + suspend fun setFireplaceTime(fireplaceTimeInMinutes: Int) - companion object { + suspend fun setFireplaceTimerEnabled(enabled: Boolean) - fun createURI(ip: String) = URI("ws://$ip:80") + suspend fun setWeeklyTimerEnabled(enabled: Boolean) - } } diff --git a/src/main/java/de/stefan_oltmann/smarthome/vallox/ValloxClientImpl.kt b/src/main/java/de/stefan_oltmann/smarthome/vallox/ValloxClientImpl.kt index 771c9d5..16e05e7 100644 --- a/src/main/java/de/stefan_oltmann/smarthome/vallox/ValloxClientImpl.kt +++ b/src/main/java/de/stefan_oltmann/smarthome/vallox/ValloxClientImpl.kt @@ -1,6 +1,6 @@ /** * Copyright (c) 2010-2021 Contributors to the openHAB project - * Copyright (c) 2021 Stefan Oltmann + * Copyright (c) 2024 Stefan Oltmann * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -17,7 +17,6 @@ import io.ktor.client.plugins.websocket.webSocket import io.ktor.http.HttpMethod import io.ktor.util.moveToByteArray import io.ktor.websocket.send -import kotlinx.coroutines.runBlocking import java.nio.ByteBuffer /** @@ -27,7 +26,7 @@ import java.nio.ByteBuffer * @author Stefan Oltmann - Refactorings and Kotlin conversion */ class ValloxClientImpl( - val host: String + private val host: String ) : ValloxClient { private val httpClient = HttpClient { @@ -39,36 +38,43 @@ class ValloxClientImpl( install(WebSockets) } - override fun readStatus(): ValloxStatus { - - return sendBytesToService( + override suspend fun readStatus(): ValloxStatus? = + sendBytesToService( ValloxMessageHandler.generateReadRequestBytes(), ValloxDataMode.READ_TABLES - )!! - } + ) + + override suspend fun turnOn() { - override fun turnOn() { sendBytesToService( - requestBytes = ValloxMessageHandler.generateWriteRequestBytesOnOff(1), + requestBytes = ValloxMessageHandler.generateWriteRequestBytesOnOff( + updateState = 1 + ), dataMode = ValloxDataMode.WRITE_DATA ) } - override fun turnOff() { + override suspend fun turnOff() { + sendBytesToService( - requestBytes = ValloxMessageHandler.generateWriteRequestBytesOnOff(0), + requestBytes = ValloxMessageHandler.generateWriteRequestBytesOnOff( + updateState = 0 + ), dataMode = ValloxDataMode.WRITE_DATA ) } - override fun switchProfile(profile: Profile) { + override suspend fun switchProfile(profile: Profile) { + sendBytesToService( - requestBytes = ValloxMessageHandler.generateWriteRequestBytesSwitchToProfile(profile), + requestBytes = ValloxMessageHandler.generateWriteRequestBytesSwitchToProfile( + profile = profile + ), dataMode = ValloxDataMode.WRITE_DATA ) } - override fun setExtractFanBalanceBase(fanSpeed: Int) { + override suspend fun setExtractFanBalanceBase(fanSpeed: Int) { require(fanSpeed in 0..100) @@ -80,7 +86,7 @@ class ValloxClientImpl( ) } - override fun setSupplyFanBalanceBase(fanSpeed: Int) { + override suspend fun setSupplyFanBalanceBase(fanSpeed: Int) { require(fanSpeed in 0..100) @@ -92,7 +98,7 @@ class ValloxClientImpl( ) } - override fun setFanSpeed(profile: Profile, fanSpeed: Int) { + override suspend fun setFanSpeed(profile: Profile, fanSpeed: Int) { require(fanSpeed in 0..100) @@ -105,7 +111,7 @@ class ValloxClientImpl( ) } - override fun setFireplaceExtractFanSpeed(fanSpeed: Int) { + override suspend fun setFireplaceExtractFanSpeed(fanSpeed: Int) { require(fanSpeed in 0..100) @@ -117,7 +123,7 @@ class ValloxClientImpl( ) } - override fun setFireplaceSupplyFanSpeed(fanSpeed: Int) { + override suspend fun setFireplaceSupplyFanSpeed(fanSpeed: Int) { require(fanSpeed in 0..100) @@ -129,7 +135,7 @@ class ValloxClientImpl( ) } - override fun setTargetTemperature(profile: Profile, targetTemperature: Int) { + override suspend fun setTargetTemperature(profile: Profile, targetTemperature: Int) { require(targetTemperature in 5..25) @@ -142,7 +148,8 @@ class ValloxClientImpl( ) } - override fun setBytesBoostTime(boostTimeInMinutes: Int) { + override suspend fun setBytesBoostTime(boostTimeInMinutes: Int) { + sendBytesToService( requestBytes = ValloxMessageHandler.generateWriteRequestBytesBoostTime( boostTimeInMinutes @@ -151,14 +158,18 @@ class ValloxClientImpl( ) } - override fun setBoostTimerEnabled(enabled: Boolean) { + override suspend fun setBoostTimerEnabled(enabled: Boolean) { + sendBytesToService( - requestBytes = ValloxMessageHandler.generateWriteRequestBytesBoostTimerEnabled(enabled), + requestBytes = ValloxMessageHandler.generateWriteRequestBytesBoostTimerEnabled( + enabled + ), dataMode = ValloxDataMode.WRITE_DATA ) } - override fun setFireplaceTime(fireplaceTimeInMinutes: Int) { + override suspend fun setFireplaceTime(fireplaceTimeInMinutes: Int) { + sendBytesToService( requestBytes = ValloxMessageHandler.generateWriteRequestBytesFireplaceTime( fireplaceTimeInMinutes @@ -167,7 +178,8 @@ class ValloxClientImpl( ) } - override fun setFireplaceTimerEnabled(enabled: Boolean) { + override suspend fun setFireplaceTimerEnabled(enabled: Boolean) { + sendBytesToService( requestBytes = ValloxMessageHandler.generateWriteRequestBytesFireplaceTimerEnabled( enabled @@ -176,19 +188,20 @@ class ValloxClientImpl( ) } - override fun setWeeklyTimerEnabled(enabled: Boolean) { + override suspend fun setWeeklyTimerEnabled(enabled: Boolean) { + sendBytesToService( requestBytes = ValloxMessageHandler.generateWriteRequestBytesWeeklyTimerEnabled(enabled), dataMode = ValloxDataMode.WRITE_DATA ) } - private fun sendBytesToService( + private suspend fun sendBytesToService( requestBytes: ByteBuffer, dataMode: ValloxDataMode - ): ValloxStatus? = runBlocking { + ): ValloxStatus? { - var status: ValloxStatus? = null + var valloxStatus: ValloxStatus? = null httpClient.webSocket( method = HttpMethod.Get, @@ -201,9 +214,9 @@ class ValloxClientImpl( val responseBytes = incoming.receive().data - status = ValloxMessageHandler.readMessage(dataMode, responseBytes) + valloxStatus = ValloxMessageHandler.readMessage(dataMode, responseBytes) } - return@runBlocking status + return valloxStatus } } diff --git a/src/main/java/de/stefan_oltmann/smarthome/vallox/ValloxDataMode.kt b/src/main/java/de/stefan_oltmann/smarthome/vallox/ValloxDataMode.kt index 1019e90..514a3da 100644 --- a/src/main/java/de/stefan_oltmann/smarthome/vallox/ValloxDataMode.kt +++ b/src/main/java/de/stefan_oltmann/smarthome/vallox/ValloxDataMode.kt @@ -1,6 +1,6 @@ /** * Copyright (c) 2010-2021 Contributors to the openHAB project - * Copyright (c) 2021 Stefan Oltmann + * Copyright (c) 2024 Stefan Oltmann * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -10,7 +10,9 @@ */ package de.stefan_oltmann.smarthome.vallox; -enum class ValloxDataMode(val value: Int) { +enum class ValloxDataMode( + val value: Int +) { READ_TABLES(246), WRITE_DATA(249), diff --git a/src/main/java/de/stefan_oltmann/smarthome/vallox/ValloxException.kt b/src/main/java/de/stefan_oltmann/smarthome/vallox/ValloxException.kt index 25d154c..1f8cdaa 100644 --- a/src/main/java/de/stefan_oltmann/smarthome/vallox/ValloxException.kt +++ b/src/main/java/de/stefan_oltmann/smarthome/vallox/ValloxException.kt @@ -1,6 +1,6 @@ /** * Copyright (c) 2010-2021 Contributors to the openHAB project - * Copyright (c) 2021 Stefan Oltmann + * Copyright (c) 2024 Stefan Oltmann * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at diff --git a/src/main/java/de/stefan_oltmann/smarthome/vallox/ValloxMessageHandler.kt b/src/main/java/de/stefan_oltmann/smarthome/vallox/ValloxMessageHandler.kt index 700ccae..9e44b5c 100644 --- a/src/main/java/de/stefan_oltmann/smarthome/vallox/ValloxMessageHandler.kt +++ b/src/main/java/de/stefan_oltmann/smarthome/vallox/ValloxMessageHandler.kt @@ -1,6 +1,6 @@ /** * Copyright (c) 2010-2021 Contributors to the openHAB project - * Copyright (c) 2021 Stefan Oltmann + * Copyright (c) 2024 Stefan Oltmann * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at diff --git a/src/main/java/de/stefan_oltmann/smarthome/vallox/ValloxStatus.kt b/src/main/java/de/stefan_oltmann/smarthome/vallox/ValloxStatus.kt index face10b..08d9fb9 100644 --- a/src/main/java/de/stefan_oltmann/smarthome/vallox/ValloxStatus.kt +++ b/src/main/java/de/stefan_oltmann/smarthome/vallox/ValloxStatus.kt @@ -1,6 +1,6 @@ /** * Copyright (c) 2010-2021 Contributors to the openHAB project - * Copyright (c) 2021 Stefan Oltmann + * Copyright (c) 2024 Stefan Oltmann * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at