-
Notifications
You must be signed in to change notification settings - Fork 0
/
publisher.gleam
61 lines (53 loc) · 1.35 KB
/
publisher.gleam
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import gemqtt.{type Client, type Properties, Properties}
import gleam/dict.{type Dict}
import gleam/dynamic.{type Dynamic}
import gleam/erlang/atom.{type Atom}
import gleam/option
pub type Publisher {
Publisher(
client: Client,
topic: String,
options: List(PublishOption),
properties: Properties,
)
}
pub type PublishOption {
Retain(Bool)
Qos(gemqtt.Qos)
}
pub fn new(client: Client, topic: String) -> Publisher {
Publisher(
client: client,
topic: topic,
options: [],
properties: Properties(dict.new()),
)
}
pub fn set_qos(publisher: Publisher, qos: gemqtt.Qos) -> Publisher {
let opts = [Qos(qos), ..publisher.options]
Publisher(..publisher, options: opts)
}
pub fn set_retain(publisher: Publisher, retain: Bool) -> Publisher {
let opts = [Retain(retain), ..publisher.options]
Publisher(..publisher, options: opts)
}
pub fn publish(
publisher: Publisher,
payload: BitArray,
) -> Result(option.Option(Int), gemqtt.Error) {
let Publisher(
client: client,
topic: topic,
options: pub_opts,
properties: Properties(props),
) = publisher
publish_(client, topic, props, payload, pub_opts)
}
@external(erlang, "emqtt_ffi", "publish")
fn publish_(
client: Client,
topic: String,
props: Dict(Atom, Dynamic),
payload: BitArray,
opts: List(PublishOption),
) -> Result(option.Option(Int), gemqtt.Error)