Compare commits
2 commits
af4f5a850c
...
113384b6ce
| Author | SHA1 | Date | |
|---|---|---|---|
| 113384b6ce | |||
| 4b766f54ae |
4 changed files with 70 additions and 5 deletions
|
|
@ -1,9 +1,10 @@
|
|||
import gleam/erlang/process
|
||||
import gleam/list
|
||||
import gleam/otp/actor
|
||||
import sensors
|
||||
|
||||
pub type Subscriber =
|
||||
process.Subject(String)
|
||||
process.Subject(sensors.SensorReading)
|
||||
|
||||
pub type Message {
|
||||
Subscribe(subscriber: Subscriber)
|
||||
|
|
@ -24,7 +25,10 @@ fn handle_message(
|
|||
}
|
||||
Proc -> {
|
||||
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)
|
||||
}
|
||||
|
|
|
|||
42
src/sensors.gleam
Normal file
42
src/sensors.gleam
Normal 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³"
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@ import config
|
|||
import gleam/erlang/process
|
||||
import gleam/io
|
||||
import mqtt_dummy
|
||||
import sensors
|
||||
|
||||
pub fn main() -> Nil {
|
||||
case config.load_config() {
|
||||
|
|
@ -37,13 +38,12 @@ pub fn main() -> Nil {
|
|||
}
|
||||
|
||||
fn receive_and_reschedule(
|
||||
mailbox: process.Subject(String),
|
||||
mailbox: process.Subject(sensors.SensorReading),
|
||||
subject: process.Subject(mqtt_dummy.Message),
|
||||
) -> Nil {
|
||||
case process.receive(mailbox, 2000) {
|
||||
Ok(msg) -> {
|
||||
io.println("Got message: " <> msg)
|
||||
// Reschedule the next Proc message
|
||||
sensors.print_sensor_reading(msg)
|
||||
process.send_after(subject, 1000, mqtt_dummy.Proc)
|
||||
receive_and_reschedule(mailbox, subject)
|
||||
}
|
||||
|
|
|
|||
19
test/sensors_test.gleam
Normal file
19
test/sensors_test.gleam
Normal 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")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue