create an mqtt_dummy that sends string every second
All checks were successful
Unit Tests / Run Tests (push) Successful in 14s
All checks were successful
Unit Tests / Run Tests (push) Successful in 14s
This commit is contained in:
parent
ed37e1c550
commit
af4f5a850c
6 changed files with 133 additions and 4 deletions
|
|
@ -4,7 +4,13 @@ import gleam/result
|
|||
import simplifile
|
||||
|
||||
pub type Config {
|
||||
Config(mqtt_host: String, mqtt_user: String, mqtt_pw: String, jwt_key: String)
|
||||
Config(
|
||||
mqtt_host: String,
|
||||
mqtt_user: String,
|
||||
mqtt_pw: String,
|
||||
mqtt_client_id: String,
|
||||
jwt_key: String,
|
||||
)
|
||||
}
|
||||
|
||||
fn load_file() -> Result(String, String) {
|
||||
|
|
@ -36,19 +42,33 @@ fn compile_config(doc: glaml.Document) -> Result(Config, String) {
|
|||
glaml.select_sugar(root, "mqtt.user")
|
||||
|> result.map_error(fn(_) { "mqtt.user not found in config.yml" }),
|
||||
)
|
||||
use mqtt_client_id_result <- result.try(
|
||||
glaml.select_sugar(root, "mqtt.client_id")
|
||||
|> result.map_error(fn(_) { "mqtt.client_id not found in config.yml" }),
|
||||
)
|
||||
use jwt_key_result <- result.try(
|
||||
glaml.select_sugar(root, "jwt_key")
|
||||
|> result.map_error(fn(_) { "jwt_key not found in config.yml" }),
|
||||
)
|
||||
|
||||
// Extract strings from nodes
|
||||
case mqtt_host_result, mqtt_user_result, jwt_key_result {
|
||||
glaml.NodeStr(host), glaml.NodeStr(user), glaml.NodeStr(key) -> {
|
||||
case
|
||||
mqtt_host_result,
|
||||
mqtt_user_result,
|
||||
jwt_key_result,
|
||||
mqtt_client_id_result
|
||||
{
|
||||
glaml.NodeStr(host),
|
||||
glaml.NodeStr(user),
|
||||
glaml.NodeStr(key),
|
||||
glaml.NodeStr(client_id)
|
||||
-> {
|
||||
let yaml_config =
|
||||
Config(
|
||||
mqtt_host: host,
|
||||
mqtt_user: user,
|
||||
mqtt_pw: "placeholder",
|
||||
mqtt_client_id: client_id,
|
||||
jwt_key: key,
|
||||
)
|
||||
|
||||
|
|
@ -61,13 +81,15 @@ fn compile_config(doc: glaml.Document) -> Result(Config, String) {
|
|||
|> result.unwrap(yaml_config.mqtt_user),
|
||||
mqtt_pw: envoy.get("MQTT_PW")
|
||||
|> result.unwrap(yaml_config.mqtt_pw),
|
||||
mqtt_client_id: envoy.get("MQTT_CLIENT_ID")
|
||||
|> result.unwrap(yaml_config.mqtt_client_id),
|
||||
jwt_key: envoy.get("JWT_KEY")
|
||||
|> result.unwrap(yaml_config.jwt_key),
|
||||
)
|
||||
|
||||
Ok(final_config)
|
||||
}
|
||||
_, _, _ -> Error("Config values must be strings in config.yml")
|
||||
_, _, _, _ -> Error("Config values must be strings in config.yml")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
48
src/mqtt_dummy.gleam
Normal file
48
src/mqtt_dummy.gleam
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
import gleam/erlang/process
|
||||
import gleam/list
|
||||
import gleam/otp/actor
|
||||
|
||||
pub type Subscriber =
|
||||
process.Subject(String)
|
||||
|
||||
pub type Message {
|
||||
Subscribe(subscriber: Subscriber)
|
||||
Proc
|
||||
}
|
||||
|
||||
pub type Subscribers =
|
||||
List(Subscriber)
|
||||
|
||||
fn handle_message(
|
||||
subscribers: Subscribers,
|
||||
message: Message,
|
||||
) -> actor.Next(Subscribers, Message) {
|
||||
case message {
|
||||
Subscribe(subscriber) -> {
|
||||
let new_subscribers = [subscriber, ..subscribers]
|
||||
actor.continue(new_subscribers)
|
||||
}
|
||||
Proc -> {
|
||||
list.each(subscribers, fn(subscriber) {
|
||||
process.send(subscriber, "Hello from dummy mqtt action")
|
||||
})
|
||||
actor.continue(subscribers)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Subscribe to receive ticks from the dummy actor
|
||||
pub fn subscribe(actor_ref: process.Subject(Message)) -> Subscriber {
|
||||
let mailbox = process.new_subject()
|
||||
process.send(actor_ref, Subscribe(mailbox))
|
||||
mailbox
|
||||
}
|
||||
|
||||
// Start the actor
|
||||
pub fn start() -> Result(process.Subject(Message), actor.StartError) {
|
||||
let assert Ok(started) =
|
||||
actor.new([])
|
||||
|> actor.on_message(handle_message)
|
||||
|> actor.start
|
||||
Ok(started.data)
|
||||
}
|
||||
|
|
@ -1,5 +1,7 @@
|
|||
import config
|
||||
import gleam/erlang/process
|
||||
import gleam/io
|
||||
import mqtt_dummy
|
||||
|
||||
pub fn main() -> Nil {
|
||||
case config.load_config() {
|
||||
|
|
@ -14,4 +16,40 @@ pub fn main() -> Nil {
|
|||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue