diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 14 |
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); }); |