Packages

Gleam wrapper of the emqx/emqtt client library

Current section

Files

Jump to
gemqtt src gemqtt.erl
Raw

src/gemqtt.erl

-module(gemqtt).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([new/1, set_tls_opt/3, set_auth/3, set_auto_ack/2, set_clean_start/2, set_client_id/2, set_connect_timeout/2, set_name/2, set_owner/2, set_port/2, set_tls_enabled/2, set_property/3, pid_of/1, start_link/1, connect/1, disconnect/1, stop/1, subscriptions/1]).
-export_type([error/0, options/0, properties/0, qos/0, client/0]).
-type error() :: {already_started, gleam@erlang@process:pid_()} |
{bad_property, gleam@option:option(gleam@erlang@atom:atom_())} |
noproc |
closed |
timeout |
eaddrinuse |
eaddrnotavail |
eafnosupport |
ealready |
econnaborted |
econnrefused |
econnreset |
edestaddrreq |
ehostdown |
ehostunreach |
einprogress |
eisconn |
emsgsize |
enetdown |
enetunreach |
enopkg |
enoprotoopt |
enotconn |
enotty |
enotsock |
eproto |
eprotonosupport |
eprototype |
esocktnosupport |
etimedout |
ewouldblock |
exbadport |
exbadseq |
nxdomain |
access_denied |
bad_certificate |
bad_certificate_hash_value |
bad_certificate_status_response |
bad_record_mac |
certificate_expired |
certificate_revoked |
certificate_unknown |
certificate_unobtainable |
close_notify |
decode_error |
decrypt_error |
export_restriction |
handshake_failure |
illegal_parameter |
inappropriate_fallback |
insufficient_security |
internal_error |
no_application_protocol |
no_renegotiation |
protocol_version |
record_overflow |
unexpected_message |
unknown_ca |
unknown_psk_identity |
unrecognized_name |
unsupported_certificate |
unsupported_extension |
user_canceled.
-opaque options() :: {options,
gleam@dict:dict(gleam@erlang@atom:atom_(), gleam@dynamic:dynamic_()),
gleam@dict:dict(gleam@erlang@atom:atom_(), gleam@dynamic:dynamic_()),
properties()}.
-type properties() :: {properties,
gleam@dict:dict(gleam@erlang@atom:atom_(), gleam@dynamic:dynamic_())}.
-type qos() :: at_most_once | at_least_once | exactly_once.
-opaque client() :: {client, gleam@erlang@process:pid_()}.
-spec new(binary()) -> options().
new(Host) ->
Host_value = begin
_pipe = Host,
_pipe@1 = unicode:characters_to_list(_pipe),
gleam@dynamic:from(_pipe@1)
end,
Opts = begin
_pipe@2 = gleam@dict:new(),
gleam@dict:insert(
_pipe@2,
erlang:binary_to_atom(<<"host"/utf8>>),
Host_value
)
end,
{options, Opts, gleam@dict:new(), {properties, gleam@dict:new()}}.
-spec set_tls_opt(options(), binary(), gleam@dynamic:dynamic_()) -> options().
set_tls_opt(Opts, Name, Value) ->
erlang:setelement(
3,
Opts,
gleam@dict:insert(
erlang:element(3, Opts),
erlang:binary_to_atom(Name),
gleam@dynamic:from(Value)
)
).
-spec set_option(options(), gleam@erlang@atom:atom_(), any()) -> options().
set_option(Opts, Name, Value) ->
erlang:setelement(
2,
Opts,
gleam@dict:insert(
erlang:element(2, Opts),
Name,
gleam@dynamic:from(Value)
)
).
-spec set_auth(options(), binary(), binary()) -> options().
set_auth(Opts, Username, Password) ->
_pipe = Opts,
_pipe@1 = set_option(
_pipe,
erlang:binary_to_atom(<<"username"/utf8>>),
Username
),
set_option(_pipe@1, erlang:binary_to_atom(<<"password"/utf8>>), Password).
-spec set_auto_ack(options(), boolean()) -> options().
set_auto_ack(Opts, Ack) ->
set_option(Opts, erlang:binary_to_atom(<<"auto_ack"/utf8>>), Ack).
-spec set_clean_start(options(), boolean()) -> options().
set_clean_start(Opts, Clean) ->
set_option(Opts, erlang:binary_to_atom(<<"clean_start"/utf8>>), Clean).
-spec set_client_id(options(), binary()) -> options().
set_client_id(Opts, Id) ->
set_option(Opts, erlang:binary_to_atom(<<"clientid"/utf8>>), Id).
-spec set_connect_timeout(options(), integer()) -> options().
set_connect_timeout(Opts, Timeout) ->
set_option(Opts, erlang:binary_to_atom(<<"connect_timeout"/utf8>>), Timeout).
-spec set_name(options(), binary()) -> options().
set_name(Opts, Name) ->
set_option(
Opts,
erlang:binary_to_atom(<<"name"/utf8>>),
erlang:binary_to_atom(Name)
).
-spec set_owner(options(), gleam@erlang@process:pid_()) -> options().
set_owner(Opts, Pid) ->
set_option(Opts, erlang:binary_to_atom(<<"owner"/utf8>>), Pid).
-spec set_port(options(), integer()) -> options().
set_port(Opts, Port) ->
set_option(Opts, erlang:binary_to_atom(<<"port"/utf8>>), Port).
-spec set_tls_enabled(options(), boolean()) -> options().
set_tls_enabled(Opts, Tls) ->
set_option(Opts, erlang:binary_to_atom(<<"ssl"/utf8>>), Tls).
-spec set_property(options(), binary(), any()) -> options().
set_property(Opts, Name, Value) ->
{properties, Props} = erlang:element(4, Opts),
erlang:setelement(
4,
Opts,
{properties,
gleam@dict:insert(
Props,
erlang:binary_to_atom(Name),
gleam@dynamic:from(Value)
)}
).
-spec pid_of(client()) -> gleam@erlang@process:pid_().
pid_of(Client) ->
{client, Pid} = Client,
Pid.
-spec start_link(options()) -> {ok, client()} | {error, error()}.
start_link(Opts) ->
{options, Options, Tls_options, {properties, Properties}} = Opts,
Options@1 = begin
_pipe = Options,
_pipe@1 = gleam@dict:insert(
_pipe,
erlang:binary_to_atom(<<"properties"/utf8>>),
gleam@dynamic:from(Properties)
),
gleam@dict:insert(
_pipe@1,
erlang:binary_to_atom(<<"ssl_opts"/utf8>>),
gleam@dynamic:from(maps:to_list(Tls_options))
)
end,
emqtt_ffi:start_link(Options@1).
-spec connect(client()) -> {ok, nil} | {error, error()}.
connect(Client) ->
emqtt_ffi:connect(Client).
-spec disconnect(client()) -> {ok, nil} | {error, error()}.
disconnect(Client) ->
emqtt_ffi:disconnect(Client).
-spec stop(client()) -> {ok, nil} | {error, error()}.
stop(Client) ->
emqtt_ffi:stop(Client).
-spec subscriptions(client()) -> list({binary(),
gleam@dict:dict(gleam@erlang@atom:atom_(), gleam@dynamic:dynamic_())}).
subscriptions(Client) ->
emqtt_ffi:subscriptions(Client).