add sensor module that defines some basic sensor metadata
This commit is contained in:
parent
af4f5a850c
commit
4b766f54ae
3 changed files with 47 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)
|
||||
}
|
||||
|
|
|
|||
38
src/sensors.gleam
Normal file
38
src/sensors.gleam
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
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 print_sensor_reading(reading: SensorReading) -> Nil {
|
||||
let sensor_name = case reading.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"
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue