/* * Copyright (c) 2022 Florian Schrofner * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ package fi.schro.data import fi.schro.ui.LightPowerStatus import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable interface LightRepository { suspend fun setLightStatus(lightAddress: String, port: Int? = null, status: LightStatus) suspend fun getLightStatus(lightAddress: String, port: Int? = null): LightStatus } @Serializable data class LightStatus( @SerialName("power") val powerStatus: LightPowerStatus? = null, @SerialName("brightness") val brightness: Int? = null, @SerialName("temperature") val temperature: Int? = null ){ override fun toString(): String { val stringList = mutableListOf() powerStatus?.let { stringList.add("power: ${powerStatus.stringValue}") } brightness?.let { stringList.add("brightness: $brightness") } temperature?.let { stringList.add("temperature: $temperature") } return stringList.joinToString(separator = "\n") } fun getNecessaryChanges(otherStatus: LightStatus): LightStatus? { val statusUpdates = LightStatus( powerStatus = if(powerStatus != otherStatus.powerStatus) otherStatus.powerStatus else null, brightness = if(brightness != otherStatus.brightness) otherStatus.brightness else null, temperature = if(temperature != otherStatus.temperature) otherStatus.temperature else null ) return if(statusUpdates.powerStatus != null || statusUpdates.brightness != null || statusUpdates.temperature != null){ statusUpdates } else null } }