Skip to content

Commit

Permalink
refactor(hub): adjusted ui
Browse files Browse the repository at this point in the history
  • Loading branch information
tassiluca committed Feb 24, 2024
1 parent 8ff4b2a commit b6310bc
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ class Thermostat(

override suspend fun update() {
println("Thermostat updates its state: $state")
state = emptyList()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.github.tassiLuca.hub

import gears.async.default.given
import gears.async.Async
import io.github.tassiLuca.hub.infrastructure.MockedHubManager
import io.github.tassiLuca.hub.adapters.MockedHubManager

/** The launcher of a mocked version of the application, using UI simulators. */
@main def launchUIMockedHub(): Unit =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.github.tassiLuca.hub.infrastructure
package io.github.tassiLuca.hub.adapters

import gears.async.{Async, Future, Task}
import io.github.tassiLuca.hub.core.{SensorSource, TemperatureEntry}
import io.github.tassiLuca.hub.infrastructure.ui.TemperatureSourceUI
import io.github.tassiLuca.hub.adapters.ui.TemperatureSourceUI

class GraphicalTemperatureSource(using Async) extends SensorSource:

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package io.github.tassiLuca.hub.infrastructure
package io.github.tassiLuca.hub.adapters

import gears.async.TaskSchedule.RepeatUntilFailure
import gears.async.{Async, AsyncOperations, ReadableChannel, Task}
import io.github.tassiLuca.hub.core.{LuminosityEntry, TemperatureEntry}
import io.github.tassiLuca.hub.adapters.ui.DashboardUI
import io.github.tassiLuca.rears.groupBy

class MockedHubManager(using Async, AsyncOperations):

private val temperatureSource = GraphicalTemperatureSource()
private val thermostatHub = new MockedThermostatHubManager() with SwingDashboard()
private val lightingHub = new MockedLightingHubManager() with SwingDashboard()
private val ui = DashboardUI()
private val thermostatHub = new MockedThermostatHubManager() with SwingDashboard(ui)
private val lightingHub = new MockedLightingHubManager() with SwingDashboard(ui)

def run(): Unit =
val channelBySensor = temperatureSource.publishingChannel.groupBy(e => e.getClass)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.tassiLuca.hub.infrastructure
package io.github.tassiLuca.hub.adapters

import gears.async.Async
import io.github.tassiLuca.hub.application.LightingHubManager
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.tassiLuca.hub.infrastructure
package io.github.tassiLuca.hub.adapters

import gears.async.Async
import io.github.tassiLuca.hub.application.ThermostatHubManager
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package io.github.tassiLuca.hub.infrastructure
package io.github.tassiLuca.hub.adapters

import io.github.tassiLuca.hub.infrastructure.ui.DashboardUI
import io.github.tassiLuca.hub.adapters.ui.DashboardUI
import io.github.tassiLuca.hub.core.Temperature
import io.github.tassiLuca.hub.core.ports.DashboardServiceComponent

import javax.swing.SwingUtilities

trait SwingDashboard extends DashboardServiceComponent:
trait SwingDashboard(view: DashboardUI) extends DashboardServiceComponent:
override val dashboard: DashboardService = new DashboardService:

private val view = DashboardUI()


override def temperatureUpdated(temperature: Temperature): Unit = SwingUtilities.invokeLater { () =>
view.temperatureLabel.setText(s"$temperature °C")
}
Expand All @@ -24,5 +22,5 @@ trait SwingDashboard extends DashboardServiceComponent:
}

override def alertNotified(msg: String): Unit = SwingUtilities.invokeLater { () =>
view.alertTextArea.setText(msg)
view.alertsModel.insertRow(0, Array[AnyRef](msg))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.github.tassiLuca.hub.adapters.ui

import io.github.tassiLuca.utils.ScalaSwingFacade.{*, given}

import java.awt.{Font, GridLayout}
import javax.swing.*
import javax.swing.table.DefaultTableModel

class DashboardUI extends JFrame:
private val biggerFont = Font("Arial", Font.BOLD, 20)

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
setTitle("Smart hub dashboard")
setSize(915, 460)
setLocationRelativeTo(null)
setLayout(GridLayout(1, 2))
val temperatureLabel: JLabel = JLabel("00°C")
temperatureLabel.setFont(biggerFont)
val heaterLabel: JLabel = JLabel("Off")
heaterLabel.setFont(biggerFont)
val luminosityLabel: JLabel = JLabel("--")
luminosityLabel.setFont(biggerFont)
private val infos = createPanel(
createPanel(),
createPanel(JLabel("Current Temperature:"), temperatureLabel),
createPanel(JLabel("Heater Status:"), heaterLabel),
createPanel(JLabel("Current Luminosity:"), luminosityLabel),
)
infos.setLayout(BoxLayout(infos, BoxLayout.PAGE_AXIS))
val alertsModel: DefaultTableModel = DefaultTableModel(Array[AnyRef]("Alert Messages"), 0)
private val alertsLog: JTable = JTable(alertsModel)
private val alerts = createPanel(JScrollPane(alertsLog))
getContentPane.add(infos)
getContentPane.add(alerts)
setVisible(true)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.tassiLuca.hub.infrastructure.ui
package io.github.tassiLuca.hub.adapters.ui

import java.awt.{BorderLayout, Component, Dimension}
import javax.swing.*
Expand All @@ -17,8 +17,8 @@ class TemperatureSourceUI(publishHandler: (String, Double) => Unit) extends JFra
middle.setLayout(BoxLayout(middle, BoxLayout.Y_AXIS))
addButton.addActionListener(_ => addSensorTo(middle))
private val mainPanel = createPanel(
(addButton, BorderLayout.NORTH),
(middle, BorderLayout.CENTER),
(addButton, BorderLayout.SOUTH),
)(using BorderLayout())
getContentPane.add(mainPanel)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ trait ThermostatHubManager
with HeaterComponent
with AlertSystemComponent
with DashboardServiceComponent:
override val thermostat: Thermostat = ???
override val thermostat: Thermostat = Thermostat(ThermostatScheduler.byHour(19))
override val sensorHealthChecker: SensorHealthChecker = SensorHealthChecker()

def run(source: ReadableChannel[TemperatureEntry])(using Async, AsyncOperations): Unit =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import gears.async.Async
import io.github.tassiLuca.hub.core.ports.{AlertSystemComponent, DashboardServiceComponent}
import io.github.tassiLuca.rears.{Consumer, State}

import java.util.Date
import scala.util.{Failure, Success, Try}

trait SensorHealthCheckerComponent[E <: SensorEvent]:
Expand All @@ -22,17 +23,11 @@ trait SensorHealthCheckerComponent[E <: SensorEvent]:
private class SensorHealthCheckerImpl extends SensorHealthChecker:

override protected def react(e: Try[Seq[E]])(using Async): Seq[E] = e match
case Success(es) =>
if state.isDefined && es.toSet.map(_.name) != state.map(_.toSet.map(_.name)).get then
val alertMessage =
s"""
|Detected a change: ${state
.map(_.toSet -- es.toSet)
.get
.map(_.name)
.foldLeft("")((t, tt) => t + ", " + tt)} missing!
|""".stripMargin
case Success(current) =>
val noMoreActiveSensors = state.map(ex => ex.map(_.name).toSet -- current.map(_.name).toSet)
if noMoreActiveSensors.isDefined then
val alertMessage = s"[${Date()}] Detected ${noMoreActiveSensors.get.mkString(", ")} no more active!"
context.alertSystem.notify(alertMessage)
context.dashboard.alertNotified(alertMessage)
es
current
case Failure(es) => context.alertSystem.notify(es.getMessage); Seq()

This file was deleted.

0 comments on commit b6310bc

Please sign in to comment.