Skip to content

Commit 994a5f2

Browse files
committed
feat: publish_command.rs read config from file, add CtrlConfig
1 parent b642d9a commit 994a5f2

File tree

3 files changed

+48
-14
lines changed

3 files changed

+48
-14
lines changed

agent/ctrl_config.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
server: 'mqtt://127.0.0.1:1883' # MQTT Server
2+
client_id: test_ctrl #MQTT Client ID
3+
#username: test_ctrl_username #MQTT Username
4+
#password: password #MQTT Password
5+
6+
subscribe_client_id: test_client #MQTT Subscribe Client ID

agent/examples/publish_command.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,24 @@ async fn main() -> anyhow::Result<()> {
1515
"ls -ls".to_string()
1616
};
1717

18-
let username:Option<String> = None;
19-
let password:Option<String> = None;
20-
21-
let config_path:PathBuf = "./config.yml".parse().unwrap();
22-
let publish_client_id = "root";
23-
//let username:Option<String> = Some("".to_string());
24-
//let password:Option<String> = Some("".to_string());
18+
let config_path:PathBuf = "./ctrl_config.yml".parse().unwrap();
19+
//let config_path:PathBuf = "./config.prod.yml".parse().unwrap();
2520

2621
let is_terminal = std::io::stdout().is_terminal();
2722

2823
tracing_subscriber::fmt().with_env_filter(EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::from("debug")),)
2924
.with_ansi(is_terminal).init();
3025

31-
let config = mproxy::config::Config::new(Some(config_path)).unwrap();
26+
let config = mproxy::config::CtrlConfig::new(config_path).unwrap();
3227

33-
let mqtt_url = format!("{}?client_id={}", &config.server, publish_client_id);
28+
let mqtt_url = format!("{}?client_id={}", &config.server, &config.client_id);
3429
let mut options = MqttOptions::parse_url(&mqtt_url).unwrap();
35-
if matches!(password, Some(_)) && matches!(username, Some(_)) {
36-
options.set_credentials(username.unwrap(), password.unwrap());
30+
if matches!(config.username, Some(_)) && matches!(config.password, Some(_)) {
31+
options.set_credentials(config.username.clone().unwrap(), config.password.clone().unwrap());
3732
}
3833
let (client, mut eventloop) = AsyncClient::new(options,20);
3934

40-
let topic = config.get_response_command_topic();
35+
let topic = config.get_subscribe_command_topic();
4136
client.subscribe(topic, QoS::ExactlyOnce).await?;
4237

4338
tokio::spawn(async move {
@@ -63,7 +58,7 @@ async fn main() -> anyhow::Result<()> {
6358
});
6459
let command = RequestMessage::Cmd{command: command.to_string(), request_id: "test_request_id".to_string()};
6560
let command = serde_json::to_vec(&command).unwrap();
66-
client.publish(config.get_command_topic(), QoS::ExactlyOnce, false, command).await?;
61+
client.publish(config.get_publish_command_topic(), QoS::ExactlyOnce, false, command).await?;
6762
tokio::signal::ctrl_c().await?;
6863

6964
Ok(())

agent/src/config.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ pub struct Config {
1414
pub client_id: String,
1515
pub username: Option<String>,
1616
pub password: Option<String>,
17-
1817
}
1918

2019
impl Config {
@@ -49,6 +48,40 @@ impl Config {
4948
}
5049
}
5150

51+
52+
#[derive(Serialize, Deserialize, Debug)]
53+
pub struct CtrlConfig {
54+
pub server: String,
55+
pub client_id: String,
56+
pub username: Option<String>,
57+
pub password: Option<String>,
58+
pub subscribe_client_id: String,
59+
publish_command_topic: Option<String>,
60+
subscribe_command_topic: Option<String>,
61+
}
62+
impl CtrlConfig {
63+
pub fn new(config_path:PathBuf) -> anyhow::Result<Self> {
64+
if !config_path.is_file() {
65+
bail!("config file not found: {:?}", config_path);
66+
}
67+
info!("load config from {:?}", config_path);
68+
let config = std::fs::read_to_string(config_path)?;
69+
let config: Self = serde_yml::from_str(&config)?;
70+
Ok(config)
71+
}
72+
73+
pub fn get_publish_command_topic(&self) -> String {
74+
self.publish_command_topic.clone().unwrap_or_else(||format!("cmd/{}", self.subscribe_client_id))
75+
76+
}
77+
pub fn get_subscribe_command_topic(&self) -> String {
78+
self.subscribe_command_topic.clone().unwrap_or_else(||format!("cmd/{}/resp", self.subscribe_client_id))
79+
}
80+
}
81+
82+
83+
84+
5285
#[cfg(test)]
5386
mod test {
5487
use crate::config::Config;

0 commit comments

Comments
 (0)