55 lines
1.4 KiB
Gleam
55 lines
1.4 KiB
Gleam
import config
|
|
import gleam/erlang/process
|
|
import gleam/io
|
|
import mqtt_dummy
|
|
|
|
pub fn main() -> Nil {
|
|
case config.load_config() {
|
|
Ok(cfg) -> {
|
|
io.println("Config loaded successfully!")
|
|
io.println("MQTT Host: " <> cfg.mqtt_host)
|
|
io.println("MQTT User: " <> cfg.mqtt_user)
|
|
io.println("MQTT PW: " <> cfg.mqtt_pw)
|
|
io.println("JWT Key: " <> cfg.jwt_key)
|
|
}
|
|
Error(err) -> {
|
|
io.println("Failed to load config: " <> err)
|
|
}
|
|
}
|
|
|
|
let assert Ok(subject) = mqtt_dummy.start()
|
|
let mailbox = mqtt_dummy.subscribe(subject)
|
|
|
|
// Kick off the first message
|
|
process.send_after(subject, 1000, mqtt_dummy.Proc)
|
|
|
|
receive_and_reschedule(mailbox, subject)
|
|
// case process.receive(mailbox, 5000) {
|
|
// Ok(msg) -> {
|
|
// io.println("Got message:" <> msg)
|
|
// // process_messages(mailbox, n - 1)
|
|
// }
|
|
// Error(Nil) -> {
|
|
// io.println("Timeout waiting for message")
|
|
// }
|
|
// }
|
|
Nil
|
|
}
|
|
|
|
fn receive_and_reschedule(
|
|
mailbox: process.Subject(String),
|
|
subject: process.Subject(mqtt_dummy.Message),
|
|
) -> Nil {
|
|
case process.receive(mailbox, 2000) {
|
|
Ok(msg) -> {
|
|
io.println("Got message: " <> msg)
|
|
// Reschedule the next Proc message
|
|
process.send_after(subject, 1000, mqtt_dummy.Proc)
|
|
receive_and_reschedule(mailbox, subject)
|
|
}
|
|
Error(Nil) -> {
|
|
io.println("Timeout - waiting again...")
|
|
receive_and_reschedule(mailbox, subject)
|
|
}
|
|
}
|
|
}
|