Compare commits

...

2 commits

Author SHA1 Message Date
113384b6ce
add test for sensor_name and sensor_unit
All checks were successful
Unit Tests / Run Tests (push) Successful in 10s
2026-03-24 11:48:59 +01:00
4b766f54ae
add sensor module that defines some basic sensor metadata 2026-03-24 11:45:02 +01:00
4 changed files with 70 additions and 5 deletions

View file

@ -1,9 +1,10 @@
import gleam/erlang/process import gleam/erlang/process
import gleam/list import gleam/list
import gleam/otp/actor import gleam/otp/actor
import sensors
pub type Subscriber = pub type Subscriber =
process.Subject(String) process.Subject(sensors.SensorReading)
pub type Message { pub type Message {
Subscribe(subscriber: Subscriber) Subscribe(subscriber: Subscriber)
@ -24,7 +25,10 @@ fn handle_message(
} }
Proc -> { Proc -> {
list.each(subscribers, fn(subscriber) { list.each(subscribers, fn(subscriber) {
process.send(subscriber, "Hello from dummy mqtt action") process.send(
subscriber,
sensors.SensorReading(sensors.Temperature, 26.6),
)
}) })
actor.continue(subscribers) actor.continue(subscribers)
} }

42
src/sensors.gleam Normal file
View file

@ -0,0 +1,42 @@
import gleam/float
import gleam/io
pub type Sensor {
Temperature
Humidity
Pressure
ParticalMatterOne
ParticalMatterTwoPointFive
ParticalMatterTen
}
pub type SensorReading {
SensorReading(sensor: Sensor, value: Float)
}
pub fn sensor_name(sensor: Sensor) -> String {
case sensor {
Temperature -> "Temperature"
Humidity -> "Humidity"
Pressure -> "Pressure"
ParticalMatterOne -> "Partical matter 1 micron or less"
ParticalMatterTwoPointFive -> "Partical matter 2.5 micros or less"
ParticalMatterTen -> "Partical matter 10 microns or less"
}
}
pub fn print_sensor_reading(reading: SensorReading) -> Nil {
let sensor_name = sensor_name(reading.sensor)
io.println(sensor_name <> ": " <> float.to_string(reading.value))
}
pub fn sensor_unit(sensor: Sensor) -> String {
case sensor {
Temperature -> "°C"
Humidity -> "%"
Pressure -> "hPa"
ParticalMatterOne -> "µg/m³"
ParticalMatterTwoPointFive -> "µg/m³"
ParticalMatterTen -> "µg/m³"
}
}

View file

@ -2,6 +2,7 @@ import config
import gleam/erlang/process import gleam/erlang/process
import gleam/io import gleam/io
import mqtt_dummy import mqtt_dummy
import sensors
pub fn main() -> Nil { pub fn main() -> Nil {
case config.load_config() { case config.load_config() {
@ -37,13 +38,12 @@ pub fn main() -> Nil {
} }
fn receive_and_reschedule( fn receive_and_reschedule(
mailbox: process.Subject(String), mailbox: process.Subject(sensors.SensorReading),
subject: process.Subject(mqtt_dummy.Message), subject: process.Subject(mqtt_dummy.Message),
) -> Nil { ) -> Nil {
case process.receive(mailbox, 2000) { case process.receive(mailbox, 2000) {
Ok(msg) -> { Ok(msg) -> {
io.println("Got message: " <> msg) sensors.print_sensor_reading(msg)
// Reschedule the next Proc message
process.send_after(subject, 1000, mqtt_dummy.Proc) process.send_after(subject, 1000, mqtt_dummy.Proc)
receive_and_reschedule(mailbox, subject) receive_and_reschedule(mailbox, subject)
} }

19
test/sensors_test.gleam Normal file
View file

@ -0,0 +1,19 @@
import gleeunit
import gleeunit/should
import sensors
pub fn main() -> Nil {
gleeunit.main()
}
// gleeunit test functions end in `_test`
pub fn file_read_test() {
let reading =
sensors.sensor_name(sensors.Temperature)
<> " "
<> "20.5"
<> " "
<> sensors.sensor_unit(sensors.Temperature)
reading
|> should.equal("Temperature 20.5 °C")
}