get mqtt reciving working
This commit is contained in:
parent
45b934b3ad
commit
6c0711acd5
4 changed files with 75 additions and 57 deletions
|
|
@ -1,14 +1,12 @@
|
|||
import config
|
||||
import gleam/erlang/process
|
||||
import gleam/option.{None, Some}
|
||||
import readings_broadcaster
|
||||
import spoke/mqtt
|
||||
import spoke/mqtt_actor
|
||||
import spoke/tcp
|
||||
|
||||
pub fn start(
|
||||
cfg: config.Config,
|
||||
updates_subject: process.Subject(mqtt.Update),
|
||||
) -> mqtt_actor.Client {
|
||||
pub fn start(cfg: config.Config) -> mqtt_actor.Client {
|
||||
let client_id = cfg.mqtt_client_id
|
||||
let password = <<cfg.mqtt_pw:utf8>>
|
||||
let auth = mqtt.AuthDetails(username: cfg.mqtt_user, password: Some(password))
|
||||
|
|
@ -18,18 +16,22 @@ pub fn start(
|
|||
client_id: client_id,
|
||||
authentication: Some(auth),
|
||||
transport_options: transport,
|
||||
keep_alive_seconds: 15,
|
||||
server_timeout_ms: 5000,
|
||||
keep_alive_seconds: 120,
|
||||
server_timeout_ms: 15_000,
|
||||
)
|
||||
let assert Ok(started) =
|
||||
mqtt_actor.build(connection_options) |> mqtt_actor.start(100)
|
||||
let client = started.data
|
||||
|
||||
mqtt_actor.subscribe_to_updates(client, updates_subject)
|
||||
let start_up_checker = process.new_subject()
|
||||
mqtt_actor.subscribe_to_updates(client, start_up_checker)
|
||||
mqtt_actor.connect(client, True, None)
|
||||
|
||||
let assert Ok(mqtt.ConnectionStateChanged(mqtt.ConnectAccepted(_))) =
|
||||
process.receive(updates_subject, 5000)
|
||||
process.receive(start_up_checker, 15_000)
|
||||
|
||||
let assert Ok(broadcaster) = readings_broadcaster.start([])
|
||||
mqtt_actor.subscribe_to_updates(client, broadcaster)
|
||||
|
||||
client
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue