Skip to content

Commit

Permalink
Support for lock objects
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanOltmann committed Oct 9, 2022
1 parent 8e23142 commit 659381f
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ interface DeviceStateRepository {

fun updateTargetTemperature(deviceId: DeviceId, temperature: Double)

fun updateLockObject(deviceId: DeviceId, locked: Boolean)

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import de.stefan_oltmann.smarthome.server.model.GroupAddressType
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.File
import java.lang.Exception

const val DEVICES_FILE_NAME = "devices.json"

Expand Down Expand Up @@ -96,6 +95,7 @@ class FileDeviceRepository : DeviceRepository {
device.gaPercentageStatus -> return device to GroupAddressType.PERCENTAGE_STATUS
device.gaCurrentTemperature -> return device to GroupAddressType.CURRENT_TEMPERATURE
device.gaTargetTemperature -> return device to GroupAddressType.TARGET_TEMPERATURE
device.gaLockObject -> return device to GroupAddressType.LOCK_OBJECT
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,21 @@ class FileDeviceStateRepository : DeviceStateRepository {
append = true
)
}

override fun updateLockObject(deviceId: DeviceId, locked: Boolean) {

getOrCreateDeviceStatus(deviceId).locked = locked

val millis = System.currentTimeMillis()

/* Write local object */
_history.add(DeviceStateHistoryEntry(deviceId, millis, locked = locked))

/* Persist */
csvWriter.writeAll(
rows = listOf(listOf(deviceId.value, millis, "locked", if (locked) 1 else 0)),
targetFileName = "$DATA_DIR_NAME/$HISTORY_CSV",
append = true
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,24 @@ class InfluxDbDeviceStateRepository(
runBlocking { client.getWriteKotlinApi().writePoint(point) }
}

override fun updateLockObject(deviceId: DeviceId, locked: Boolean) {

getOrCreateDeviceStatus(deviceId).locked = locked

val millis = System.currentTimeMillis()

/* Write local object */
_history.add(DeviceStateHistoryEntry(deviceId, millis, locked = locked))

/* Persist */

val point = Point.measurement(deviceId.value)
.addField("locked", locked)
.time(Instant.now().toEpochMilli(), WritePrecision.MS)

runBlocking { client.getWriteKotlinApi().writePoint(point) }
}

data class InfluxDbSettings(val url: String, val token: String)

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import li.pitschmann.knx.core.plugin.audit.FileAuditPlugin
import li.pitschmann.knx.core.utils.Sleeper
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.net.http.WebSocketHandshakeException
import java.nio.file.Paths
import kotlin.concurrent.thread

Expand Down Expand Up @@ -141,14 +140,18 @@ class KnxServiceImpl(
device.gaTargetTemperature?.let { groupAddress ->
knxClient.readRequest(GroupAddress.of(groupAddress), READ_TIMEOUT_MS)
}

device.gaLockObject?.let { groupAddress ->
knxClient.readRequest(GroupAddress.of(groupAddress), READ_TIMEOUT_MS)
}
}
}

override fun writePowerState(device: Device, powerState: DevicePowerState) {

knxClient.writeRequest(
GroupAddress.of(device.gaPowerStateWrite),
powerStateDpt.of(powerState == DevicePowerState.ON)
booleanDpt.of(powerState == DevicePowerState.ON)
)
}

Expand Down Expand Up @@ -236,6 +239,11 @@ class KnxServiceImpl(
device.id,
groupAddress
)
GroupAddressType.LOCK_OBJECT -> handleLockObjectItem(
item,
device.id,
groupAddress
)
else -> return
}
}
Expand All @@ -248,7 +256,7 @@ class KnxServiceImpl(

try {

val value = powerStateDpt.of(item.cemi.data).value
val value = booleanDpt.of(item.cemi.data).value

val powerState = if (value) DevicePowerState.ON else DevicePowerState.OFF

Expand Down Expand Up @@ -316,6 +324,23 @@ class KnxServiceImpl(
}
}

private fun handleLockObjectItem(
item: TunnelingRequestBody,
deviceId: DeviceId,
groupAddress: GroupAddress
) {

try {

val locked = booleanDpt.of(item.cemi.data).value

deviceStateRepository.updateLockObject(deviceId, locked)

} catch (ex: DataPointTypeIncompatibleBytesException) {
logger.error(createWrongTypeMessage(groupAddress, item, "DPT1"), ex)
}
}

override fun onError(throwable: Throwable) {
logger.error("KNX client error.", throwable)
}
Expand All @@ -337,7 +362,7 @@ class KnxServiceImpl(
const val KNX_CONTROL_CHANNEL_PORT = 50011
const val KNX_DATA_CHANNEL_PORT = 50012

val powerStateDpt: DPT1 = DPT1.BOOL
val booleanDpt: DPT1 = DPT1.BOOL
val percentageDpt: DPT5 = DPT5.SCALING
val temperatureDpt: DPT9 = DPT9.TEMPERATURE

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ data class Device(
@JsonInclude(JsonInclude.Include.NON_NULL) val gaPercentageWrite: String? = null,
@JsonInclude(JsonInclude.Include.NON_NULL) val gaPercentageStatus: String? = null,
@JsonInclude(JsonInclude.Include.NON_NULL) val gaCurrentTemperature: String? = null,
@JsonInclude(JsonInclude.Include.NON_NULL) val gaTargetTemperature: String? = null
@JsonInclude(JsonInclude.Include.NON_NULL) val gaTargetTemperature: String? = null,
@JsonInclude(JsonInclude.Include.NON_NULL) val gaLockObject: String? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@ class DeviceState(val deviceId: DeviceId) {
@JsonInclude(JsonInclude.Include.NON_NULL)
var targetTemperature: Double? = null

@JsonInclude(JsonInclude.Include.NON_NULL)
var locked: Boolean? = null

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ data class DeviceStateHistoryEntry(
@JsonInclude(JsonInclude.Include.NON_NULL) val powerState: DevicePowerState? = null,
@JsonInclude(JsonInclude.Include.NON_NULL) val percentage: Int? = null,
@JsonInclude(JsonInclude.Include.NON_NULL) val currentTemperature: Double? = null,
@JsonInclude(JsonInclude.Include.NON_NULL) val targetTemperature: Double? = null
@JsonInclude(JsonInclude.Include.NON_NULL) val targetTemperature: Double? = null,
@JsonInclude(JsonInclude.Include.NON_NULL) val locked: Boolean? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ enum class GroupAddressType {
PERCENTAGE_STATUS,
CURRENT_TEMPERATURE,
TARGET_TEMPERATURE,
LOCK_OBJECT

}

0 comments on commit 659381f

Please sign in to comment.