Initial Version
This commit is contained in:
+33
-53
@@ -1,5 +1,5 @@
|
||||
mod weather_data;
|
||||
|
||||
use chrono::{Local};
|
||||
use crate::weather_data::{DwdWeather, MucWeather};
|
||||
use rumqttc::{AsyncClient, EventLoop, MqttOptions, QoS};
|
||||
use scraper::{Html as HtmlScraper, Selector};
|
||||
@@ -43,6 +43,7 @@ async fn main() {
|
||||
}
|
||||
|
||||
pub async fn meteo_weather(latitude: f64, longitude: f64) -> Result<MucWeather, reqwest::Error> {
|
||||
|
||||
let response = trpl::get(METEO_MUNICH_API_URL).await;
|
||||
let response_text = response.text().await;
|
||||
|
||||
@@ -51,14 +52,14 @@ pub async fn meteo_weather(latitude: f64, longitude: f64) -> Result<MucWeather,
|
||||
let mut humidity = 0.0;
|
||||
let mut pressure = 0.0;
|
||||
let mut precipitation = 0.0;
|
||||
|
||||
let profilwerte = Selector::parse("#messwerte-profilwerte").unwrap();
|
||||
let winddruck = Selector::parse("#messwerte-winddruck").unwrap();
|
||||
let niederschlag = Selector::parse("#messwerte-niederschlag").unwrap();
|
||||
|
||||
let profile_selector = Selector::parse("#messwerte-profilwerte").unwrap();
|
||||
let pressure_selector = Selector::parse("#messwerte-winddruck").unwrap();
|
||||
let precipitation_selector = Selector::parse("#messwerte-niederschlag").unwrap();
|
||||
let row_selector = Selector::parse("tr").unwrap();
|
||||
let cell_selector = Selector::parse("td").unwrap();
|
||||
|
||||
if let Some(table) = fragment.select(&profilwerte).next() {
|
||||
if let Some(table) = fragment.select(&profile_selector).next() {
|
||||
for row in table.select(&row_selector) {
|
||||
let mut row_data = Vec::new();
|
||||
for cell in row.select(&cell_selector) {
|
||||
@@ -74,7 +75,7 @@ pub async fn meteo_weather(latitude: f64, longitude: f64) -> Result<MucWeather,
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(table) = fragment.select(&winddruck).next() {
|
||||
if let Some(table) = fragment.select(&pressure_selector).next() {
|
||||
for row in table.select(&row_selector) {
|
||||
let mut row_data = Vec::new();
|
||||
for cell in row.select(&cell_selector) {
|
||||
@@ -87,7 +88,7 @@ pub async fn meteo_weather(latitude: f64, longitude: f64) -> Result<MucWeather,
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(table) = fragment.select(&niederschlag).next() {
|
||||
if let Some(table) = fragment.select(&precipitation_selector).next() {
|
||||
for row in table.select(&row_selector) {
|
||||
let mut row_data = Vec::new();
|
||||
for cell in row.select(&cell_selector) {
|
||||
@@ -99,13 +100,13 @@ pub async fn meteo_weather(latitude: f64, longitude: f64) -> Result<MucWeather,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if temperature == 999.0 {
|
||||
let url = format!(
|
||||
"https://api.brightsky.dev/current_weather?lat={}&lon={}",
|
||||
latitude, longitude
|
||||
);
|
||||
println!("Fetching {}", url);
|
||||
println!("Fetching DWD {}", url);
|
||||
let result = trpl::get(&url).await.text().await;
|
||||
let dwd_weather: DwdWeather = serde_json::from_str(&result).unwrap();
|
||||
temperature = dwd_weather.weather.temperature.unwrap();
|
||||
@@ -114,64 +115,43 @@ pub async fn meteo_weather(latitude: f64, longitude: f64) -> Result<MucWeather,
|
||||
precipitation = dwd_weather.weather.precipitation;
|
||||
}
|
||||
|
||||
let precipitation = (precipitation * 100.0).round() / 100.00;
|
||||
let cur_date = Local::now();
|
||||
let cur_date_formatted = format!("{}", cur_date.format("%Y-%m-%d %H:%M"));
|
||||
let weather = MucWeather {
|
||||
temperature,
|
||||
pressure,
|
||||
humidity,
|
||||
precipitation,
|
||||
date: cur_date_formatted
|
||||
};
|
||||
println!("Zeile: {:?}", weather);
|
||||
Ok(weather)
|
||||
}
|
||||
|
||||
async fn publish(client: AsyncClient, muc: &MucWeather) {
|
||||
// Serialize it to a JSON string.
|
||||
|
||||
let json = serde_json::to_string(&muc);
|
||||
client
|
||||
.publish("munich/weather", QoS::AtLeastOnce, false, json.unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
client
|
||||
.publish(
|
||||
"munich/temperature",
|
||||
QoS::AtLeastOnce,
|
||||
false,
|
||||
muc.temperature.to_string(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
client
|
||||
.publish(
|
||||
"munich/pressure",
|
||||
QoS::AtLeastOnce,
|
||||
false,
|
||||
muc.pressure.to_string(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
client
|
||||
.publish(
|
||||
"munich/humidity",
|
||||
QoS::AtLeastOnce,
|
||||
false,
|
||||
muc.humidity.to_string(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
client
|
||||
.publish(
|
||||
"munich/precipitation",
|
||||
QoS::AtLeastOnce,
|
||||
false,
|
||||
muc.precipitation.to_string(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
publish_topic(client.clone(), "munich/weather", json.unwrap()).await;
|
||||
publish_topic(client.clone(), "munich/temperature", muc.temperature.to_string()).await;
|
||||
publish_topic(client.clone(), "munich/pressure", muc.pressure.to_string()).await;
|
||||
publish_topic(client.clone(), "munich/humidity", muc.humidity.to_string()).await;
|
||||
publish_topic(client.clone(), "munich/precipitation", muc.precipitation.to_string()).await;
|
||||
time::sleep(Duration::from_secs(2)).await;
|
||||
}
|
||||
|
||||
async fn publish_topic(client: AsyncClient, topic: &str, value: String) {
|
||||
client
|
||||
.publish(
|
||||
topic,
|
||||
QoS::AtLeastOnce,
|
||||
false,
|
||||
value,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn create_conn() -> (AsyncClient, EventLoop) {
|
||||
let mut mqttoptions = MqttOptions::new("rust-mqqt", "192.168.1.37", 1883);
|
||||
mqttoptions
|
||||
|
||||
Reference in New Issue
Block a user