11use std:: collections:: { HashMap , HashSet } ;
22
33use async_trait:: async_trait;
4+ use eyre:: { bail, OptionExt } ;
45use tokio:: {
56 select,
67 sync:: mpsc:: { channel, Sender } ,
@@ -23,7 +24,48 @@ impl MqttConnection {
2324 action_tx : Sender < super :: ConnectionAction > ,
2425 ) -> eyre:: Result < Self > {
2526 let ( tx, mut rx) = channel :: < ( serde_json:: Value , HashMap < String , String > ) > ( 1 ) ;
26- let options = rumqttc:: MqttOptions :: parse_url ( config. url ) ?;
27+
28+ let options = match config. params {
29+ super :: MqttConnectionParams :: Url { url } => rumqttc:: MqttOptions :: parse_url ( url) ?,
30+ super :: MqttConnectionParams :: Options {
31+ client_id,
32+ host,
33+ port,
34+ credentials,
35+ } => {
36+ let mut options = if let Ok ( url) = url:: Url :: parse ( & host) {
37+ let ( transport, host) = match url. scheme ( ) {
38+ "mqtt" => (
39+ rumqttc:: Transport :: tcp ( ) ,
40+ url. host_str ( ) . ok_or_eyre ( "mqtt url scheme missing host" ) ?,
41+ ) ,
42+ "mqtts" => (
43+ rumqttc:: Transport :: tls_with_default_config ( ) ,
44+ url. host_str ( ) . ok_or_eyre ( "mqtts url scheme missing host" ) ?,
45+ ) ,
46+ "ws" => ( rumqttc:: Transport :: ws ( ) , url. as_str ( ) ) ,
47+ "wss" => ( rumqttc:: Transport :: wss_with_default_config ( ) , url. as_str ( ) ) ,
48+ scheme => bail ! ( "unknown scheme: {scheme}" ) ,
49+ } ;
50+
51+ let mut options =
52+ rumqttc:: MqttOptions :: new ( client_id, host, url. port ( ) . unwrap_or ( 1883 ) ) ;
53+
54+ options. set_transport ( transport) ;
55+
56+ options
57+ } else {
58+ rumqttc:: MqttOptions :: new ( client_id, host, port. unwrap_or ( 1883 ) )
59+ } ;
60+
61+ if let Some ( super :: MqttCredentials { username, password } ) = credentials {
62+ options. set_credentials ( username, password) ;
63+ }
64+
65+ options
66+ }
67+ } ;
68+
2769 let ( client, mut event_loop) = rumqttc:: AsyncClient :: new ( options, 1 ) ;
2870
2971 if let Some ( action_topic) = config. action_topic . as_deref ( ) {
0 commit comments