-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hub): implement kotlin luminosity source
- Loading branch information
Showing
16 changed files
with
217 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
smart-hub-direct-kt/src/main/kotlin/io/github/tassiLuca/hub/adapters/GraphicalSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package io.github.tassiLuca.hub.adapters | ||
|
||
import io.github.tassiLuca.hub.adapters.ui.SourceUI | ||
import io.github.tassiLuca.hub.core.Luminosity | ||
import io.github.tassiLuca.hub.core.LuminosityEntry | ||
import io.github.tassiLuca.hub.core.SensorEvent | ||
import io.github.tassiLuca.hub.core.SensorSource | ||
import io.github.tassiLuca.hub.core.Temperature | ||
import io.github.tassiLuca.hub.core.TemperatureEntry | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.SharedFlow | ||
import kotlinx.coroutines.flow.asSharedFlow | ||
import kotlinx.coroutines.launch | ||
import scala.runtime.BoxedUnit | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
/** A graphical temperature source. */ | ||
class GraphicalSource : SensorSource<SensorEvent>, CoroutineScope { | ||
|
||
private val views = setOf( | ||
SourceUI("temperature") { s, d -> | ||
publishTemperatureEntry(s, d as Double) | ||
BoxedUnit.UNIT | ||
}, | ||
SourceUI("luminosity") { s, d -> | ||
publishLuminosityEntry(s, d as Double) | ||
BoxedUnit.UNIT | ||
}, | ||
) | ||
private val entries = MutableSharedFlow<SensorEvent>() | ||
|
||
override val sensorEvents: SharedFlow<SensorEvent> = entries.asSharedFlow() | ||
override val coroutineContext: CoroutineContext | ||
get() = Dispatchers.Main | ||
|
||
init { | ||
views.forEach { | ||
it.pack() | ||
it.isVisible = true | ||
} | ||
} | ||
|
||
private fun publishTemperatureEntry(sensorName: String, temperature: Temperature) = launch { | ||
entries.emit(TemperatureEntry(sensorName, temperature)) | ||
} | ||
|
||
private fun publishLuminosityEntry(sensorName: String, luminosity: Luminosity) = launch { | ||
entries.emit(LuminosityEntry(sensorName, luminosity)) | ||
} | ||
} |
35 changes: 0 additions & 35 deletions
35
...-direct-kt/src/main/kotlin/io/github/tassiLuca/hub/adapters/GraphicalTemperatureSource.kt
This file was deleted.
Oops, something went wrong.
32 changes: 22 additions & 10 deletions
32
smart-hub-direct-kt/src/main/kotlin/io/github/tassiLuca/hub/adapters/MockedHubManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,41 @@ | ||
package io.github.tassiLuca.hub.adapters | ||
|
||
import io.github.tassiLuca.hub.application.ThermostatHubManager | ||
import io.github.tassiLuca.hub.application.LightingManager | ||
import io.github.tassiLuca.hub.application.ThermostatManager | ||
import io.github.tassiLuca.hub.core.LuminosityEntry | ||
import io.github.tassiLuca.hub.core.TemperatureEntry | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.coroutineScope | ||
import kotlinx.coroutines.flow.filter | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.launch | ||
import kotlin.coroutines.CoroutineContext | ||
import kotlin.coroutines.coroutineContext | ||
|
||
/** A mocked hub manager. */ | ||
class MockedHubManager(coroutineContext: CoroutineContext) { | ||
class MockedHubManager(override val coroutineContext: CoroutineContext) : CoroutineScope { | ||
|
||
/** The dashboard. */ | ||
val dashboard = SwingDashboard() | ||
val dashboard = SwingDashboardService() | ||
|
||
/** The thermostat hub. */ | ||
val thermostatHub = ThermostatHubManager(dashboard, coroutineContext) | ||
/** The thermostat manager. */ | ||
val thermostatManager = ThermostatManager(dashboard, coroutineContext) | ||
|
||
/** The lighting system manager. */ | ||
val lightingManager = LightingManager(dashboard, coroutineContext) | ||
|
||
/** The sensor source. */ | ||
val sensorSource = GraphicalTemperatureSource() | ||
val sensorSource = GraphicalSource() | ||
|
||
/** Runs the mocked hub manager. */ | ||
suspend fun run() { | ||
val temperatureFlow: Flow<TemperatureEntry> = sensorSource.sensorEvents | ||
suspend fun run() = coroutineScope { | ||
val temperatureFlow = sensorSource.sensorEvents | ||
.filter { it is TemperatureEntry } | ||
.map { it as TemperatureEntry } | ||
thermostatHub.run(temperatureFlow) | ||
val luminosityFlow = sensorSource.sensorEvents | ||
.filter { it is LuminosityEntry } | ||
.map { it as LuminosityEntry } | ||
launch { thermostatManager.run(temperatureFlow) } | ||
lightingManager.run(luminosityFlow) | ||
} | ||
} |
26 changes: 0 additions & 26 deletions
26
smart-hub-direct-kt/src/main/kotlin/io/github/tassiLuca/hub/adapters/SwingDashboard.kt
This file was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
...t-hub-direct-kt/src/main/kotlin/io/github/tassiLuca/hub/adapters/SwingDashboardService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.github.tassiLuca.hub.adapters | ||
|
||
import io.github.tassiLuca.hub.adapters.ui.DashboardUI | ||
import io.github.tassiLuca.hub.core.Luminosity | ||
import io.github.tassiLuca.hub.core.Temperature | ||
import io.github.tassiLuca.hub.core.ports.DashboardService | ||
import javax.swing.SwingUtilities | ||
|
||
/** Swing dashboard. */ | ||
class SwingDashboardService : DashboardService { | ||
private val view = DashboardUI() | ||
|
||
override fun temperatureUpdated(newTemperature: Temperature) = SwingUtilities.invokeLater { | ||
view.temperatureLabel().text = "$newTemperature °C" | ||
} | ||
|
||
override fun luminosityUpdated(newLuminosity: Luminosity) { | ||
view.luminosityLabel().text = "$newLuminosity lux" | ||
} | ||
|
||
override fun onHeaterNotified() = SwingUtilities.invokeLater { | ||
view.heaterLabel().text = "On" | ||
} | ||
|
||
override fun offHeaterNotified() = SwingUtilities.invokeLater { | ||
view.heaterLabel().text = "Off" | ||
} | ||
|
||
override fun alertNotified(message: String) = SwingUtilities.invokeLater { | ||
view.alertsModel().insertRow(0, arrayOf(message)) | ||
} | ||
} |
74 changes: 0 additions & 74 deletions
74
smart-hub-direct-kt/src/main/kotlin/io/github/tassiLuca/hub/adapters/ui/DashboardUI.kt
This file was deleted.
Oops, something went wrong.
54 changes: 0 additions & 54 deletions
54
...-hub-direct-kt/src/main/kotlin/io/github/tassiLuca/hub/adapters/ui/TemperatureSourceUI.kt
This file was deleted.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
smart-hub-direct-kt/src/main/kotlin/io/github/tassiLuca/hub/application/LightingManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.github.tassiLuca.hub.application | ||
|
||
import io.github.tassiLuca.hub.core.LightingSystem | ||
import io.github.tassiLuca.hub.core.LuminosityEntry | ||
import io.github.tassiLuca.hub.core.ports.DashboardService | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlin.coroutines.CoroutineContext | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
/** Manages the thermostat and the temperature sensors. */ | ||
class LightingManager(dashboardService: DashboardService, coroutineContext: CoroutineContext) { | ||
|
||
/** The thermostat. */ | ||
val lightingSystem = LightingSystem(SAMPLING_WINDOW, coroutineContext, dashboardService) | ||
|
||
/** Runs the thermostat and the temperature sensors. */ | ||
suspend fun run(sensorSource: Flow<LuminosityEntry>) { | ||
println("LightingManager: run") | ||
lightingSystem.run() | ||
sensorSource.collect { | ||
println("LightingManager: $it") | ||
lightingSystem.react(it) | ||
} | ||
} | ||
|
||
companion object { | ||
private val SAMPLING_WINDOW = 5.seconds | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.