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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
use crate::cli::mqtt::{publish::MqttPublishCommand, subscribe::MqttSubscribeCommand, MqttError};
use crate::command::{BuildCommand, BuildContext, Command};
use rumqttc::QoS;
use std::time::Duration;
use tedge_config::*;
const PUB_CLIENT_PREFIX: &str = "tedge-pub";
const SUB_CLIENT_PREFIX: &str = "tedge-sub";
const DISCONNECT_TIMEOUT: Duration = Duration::from_secs(2);
#[derive(clap::Subcommand, Debug)]
pub enum TEdgeMqttCli {
/// Publish a MQTT message on a topic.
Pub {
/// Topic to publish
topic: String,
/// Message to publish
message: String,
/// QoS level (0, 1, 2)
#[clap(short, long, parse(try_from_str = parse_qos), default_value = "0")]
qos: QoS,
/// Retain flag
#[clap(short, long = "retain")]
retain: bool,
},
/// Subscribe a MQTT topic.
Sub {
/// Topic to subscribe to
topic: String,
/// QoS level (0, 1, 2)
#[clap(short, long, parse(try_from_str = parse_qos), default_value = "0")]
qos: QoS,
/// Avoid printing the message topics on the console
#[clap(long = "no-topic")]
hide_topic: bool,
},
}
impl BuildCommand for TEdgeMqttCli {
fn build_command(self, context: BuildContext) -> Result<Box<dyn Command>, crate::ConfigError> {
let port = context.config_repository.load()?.query(MqttPortSetting)?;
let host = context
.config_repository
.load()?
.query(MqttBindAddressSetting)?;
let cmd = {
match self {
TEdgeMqttCli::Pub {
topic,
message,
qos,
retain,
} => MqttPublishCommand {
host: host.to_string(),
port: port.into(),
topic,
message,
qos,
client_id: format!("{}-{}", PUB_CLIENT_PREFIX, std::process::id()),
disconnect_timeout: DISCONNECT_TIMEOUT,
retain,
}
.into_boxed(),
TEdgeMqttCli::Sub {
topic,
qos,
hide_topic,
} => MqttSubscribeCommand {
host: host.to_string(),
port: port.into(),
topic,
qos,
hide_topic,
client_id: format!("{}-{}", SUB_CLIENT_PREFIX, std::process::id()),
}
.into_boxed(),
}
};
Ok(cmd)
}
}
fn parse_qos(src: &str) -> Result<QoS, MqttError> {
let int_val: u8 = src.parse().map_err(|_| MqttError::InvalidQoS)?;
match int_val {
0 => Ok(QoS::AtMostOnce),
1 => Ok(QoS::AtLeastOnce),
2 => Ok(QoS::ExactlyOnce),
_ => Err(MqttError::InvalidQoS),
}
}
#[cfg(test)]
mod tests {
use super::parse_qos;
use rumqttc::QoS;
#[test]
fn test_parse_qos_at_most_once() {
let input_qos = "0";
let expected_qos = QoS::AtMostOnce;
assert_eq!(parse_qos(input_qos).unwrap(), expected_qos);
}
#[test]
fn test_parse_qos_at_least_once() {
let input_qos = "1";
let expected_qos = QoS::AtLeastOnce;
assert_eq!(parse_qos(input_qos).unwrap(), expected_qos);
}
#[test]
fn test_parse_qos_exactly_once() {
let input_qos = "2";
let expected_qos = QoS::ExactlyOnce;
assert_eq!(parse_qos(input_qos).unwrap(), expected_qos);
}
}
|