54 lines
1.5 KiB
Kotlin
54 lines
1.5 KiB
Kotlin
package fi.schro.data
|
|
|
|
import kotlinx.serialization.SerialName
|
|
import kotlinx.serialization.Serializable
|
|
|
|
interface LightRepository {
|
|
suspend fun getLightStatus(): LightStatus
|
|
suspend fun updateLightStatus(statusChange: LightStatus): LightStatus
|
|
}
|
|
|
|
class LightRepositoryImpl: LightRepository {
|
|
private var lightStatus = LightStatus(
|
|
listOf(
|
|
Light(
|
|
0,
|
|
25,
|
|
166
|
|
)
|
|
)
|
|
)
|
|
|
|
override suspend fun getLightStatus(): LightStatus {
|
|
return lightStatus
|
|
}
|
|
|
|
override suspend fun updateLightStatus(statusChange: LightStatus): LightStatus {
|
|
val lightChanges = statusChange.lights
|
|
val updatedLights = lightStatus.lights.mapIndexed { index, light ->
|
|
light.copy(
|
|
on = lightChanges[index].on ?: light.on,
|
|
brightness = lightChanges[index].brightness ?: light.brightness,
|
|
temperature = lightChanges[index].temperature ?: light.temperature
|
|
)
|
|
}
|
|
lightStatus = LightStatus(
|
|
lights = updatedLights
|
|
)
|
|
|
|
return getLightStatus()
|
|
}
|
|
}
|
|
|
|
@Serializable
|
|
data class LightStatus(
|
|
@SerialName("lights") val lights: List<Light>,
|
|
@SerialName("numberOfLights") val numberOfLights: Int = lights.size
|
|
)
|
|
|
|
@Serializable
|
|
data class Light(
|
|
@SerialName("on") val on: Int? = null,
|
|
@SerialName("brightness") val brightness: Int? = null,
|
|
@SerialName("temperature") val temperature: Int? = null
|
|
) |