summaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorMatt Kohls <mattkohls13@gmail.com>2024-03-17 00:52:56 -0400
committerMatt Kohls <mattkohls13@gmail.com>2024-03-17 00:52:56 -0400
commit7967fe9b0d35b8cc572d33c00004daf7de5e18c0 (patch)
tree354a171ba1a68f7d18252337adec275acf8832ae /src/main.rs
parent7690f350671cd22868d04f91d5423b56c14ad107 (diff)
downloadweather-data-aggregator-master.tar.gz
weather-data-aggregator-master.tar.bz2
weather-data-aggregator-master.zip
Slight naming change, publish hourly topicsHEADmaster
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs14
1 files changed, 7 insertions, 7 deletions
diff --git a/src/main.rs b/src/main.rs
index a5478ba..8b256ce 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -85,7 +85,7 @@ fn main() {
let hourly_response = fetch_forecast(&forecast_url, &user_agent);
match hourly_response {
Err(err) => println!("Error fetching forecast: {} - {:?}", forecast_url, err),
- Ok(forecast) => print_forecast(&forecast),
+ Ok(forecast) => publish_forecast(&forecast, true, root_forecast_topic, &mqtt_client),
}
} else {
println!("No (or missing one of) the following: forecast_office, grid_x, grid_y, skipping forecast lookup & publish");
@@ -133,12 +133,12 @@ fn create_mqtt_client(settings: &HashMap<String, String>) -> mqtt::Client {
}
fn publish_forecast(forecast: &Forecast, hourly: bool, root_topic: &str, client: &mqtt::Client) {
- let mut forecast_topic = root_topic.to_string();
+ let mut base_topic = root_topic.to_string();
if hourly {
- forecast_topic = format!("{forecast_topic}/hourly");
+ base_topic = format!("{base_topic}/hourly");
}
for period in forecast.properties.periods.iter().take(12) {
- forecast_topic = format!("{forecast_topic}/{0}", period.number);
+ let forecast_topic = format!("{base_topic}/{0}", period.number);
if !hourly {
publish_message_to(format!("{0}", period.name).as_str(),
format!("{forecast_topic}/name").as_str(),
@@ -147,7 +147,7 @@ fn publish_forecast(forecast: &Forecast, hourly: bool, root_topic: &str, client:
publish_message_to(format!("{0}", period.startTime).as_str(),
format!("{forecast_topic}/time").as_str(),
client);
- publish_message_to(format!("{0} {1}", period.temperature, period.temperatureUnit).as_str(),
+ publish_message_to(format!("{0}{1}", period.temperature, period.temperatureUnit).as_str(),
format!("{forecast_topic}/temperature").as_str(),
client);
publish_message_to(format!("{0}", period.windSpeed).as_str(),
@@ -159,8 +159,8 @@ fn publish_forecast(forecast: &Forecast, hourly: bool, root_topic: &str, client:
}
}
-fn publish_message_to(message: &str, topic: &str, client: &mqtt::Client) {
- let message = mqtt::Message::new_retained(topic, message, QOS);
+fn publish_message_to(payload: &str, topic: &str, client: &mqtt::Client) {
+ let message = mqtt::Message::new_retained(topic, payload, QOS);
client.publish(message).unwrap_or_else(|err| {
println!("MQTT Client error publishing message: {:?}", err);
});