Skip to content

Commit

Permalink
ValloxClient functions are suspending
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanOltmann committed May 11, 2024
1 parent f291ed7 commit 750048e
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down
39 changes: 16 additions & 23 deletions src/main/java/de/stefan_oltmann/smarthome/vallox/ValloxClient.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)

}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

/**
Expand All @@ -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 {
Expand All @@ -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)

Expand All @@ -80,7 +86,7 @@ class ValloxClientImpl(
)
}

override fun setSupplyFanBalanceBase(fanSpeed: Int) {
override suspend fun setSupplyFanBalanceBase(fanSpeed: Int) {

require(fanSpeed in 0..100)

Expand All @@ -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)

Expand All @@ -105,7 +111,7 @@ class ValloxClientImpl(
)
}

override fun setFireplaceExtractFanSpeed(fanSpeed: Int) {
override suspend fun setFireplaceExtractFanSpeed(fanSpeed: Int) {

require(fanSpeed in 0..100)

Expand All @@ -117,7 +123,7 @@ class ValloxClientImpl(
)
}

override fun setFireplaceSupplyFanSpeed(fanSpeed: Int) {
override suspend fun setFireplaceSupplyFanSpeed(fanSpeed: Int) {

require(fanSpeed in 0..100)

Expand All @@ -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)

Expand All @@ -142,7 +148,8 @@ class ValloxClientImpl(
)
}

override fun setBytesBoostTime(boostTimeInMinutes: Int) {
override suspend fun setBytesBoostTime(boostTimeInMinutes: Int) {

sendBytesToService(
requestBytes = ValloxMessageHandler.generateWriteRequestBytesBoostTime(
boostTimeInMinutes
Expand All @@ -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
Expand All @@ -167,7 +178,8 @@ class ValloxClientImpl(
)
}

override fun setFireplaceTimerEnabled(enabled: Boolean) {
override suspend fun setFireplaceTimerEnabled(enabled: Boolean) {

sendBytesToService(
requestBytes = ValloxMessageHandler.generateWriteRequestBytesFireplaceTimerEnabled(
enabled
Expand All @@ -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,
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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),
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 750048e

Please sign in to comment.