fix parser and code style

This commit is contained in:
Empire 2023-08-19 00:05:19 +02:00
parent bccfc22770
commit 178ab684fa
3 changed files with 41 additions and 33 deletions

View File

@ -2,20 +2,20 @@
use bit::BitIndex; use bit::BitIndex;
use chrono_tz::Europe::Berlin; use chrono_tz::Europe::Berlin;
use chrono::{DateTime, NaiveDateTime, Utc, Timelike}; use chrono::{DateTime, NaiveDateTime, Utc, Timelike};
use openweathermap::{forecast::{Weather, List}}; use openweathermap::forecast::Weather;
use substring::Substring; use substring::Substring;
use tinybmp::Bmp; use tinybmp::Bmp;
use core::time; use core::time;
use embedded_graphics::{ use embedded_graphics::{
image::{Image}, image::Image,
mono_font::{iso_8859_1::FONT_6X10, MonoTextStyle}, mono_font::{iso_8859_1::FONT_6X10, MonoTextStyle},
pixelcolor::{BinaryColor}, pixelcolor::BinaryColor,
prelude::*, prelude::*,
primitives::{PrimitiveStyle}, primitives::PrimitiveStyle,
text::Text, text::Text,
}; };
use std::{net::UdpSocket, time::{Instant, Duration}}; use std::net::UdpSocket;
use std::{env, thread}; use std::{env, thread};
use openweathermap::forecast::Forecast; use openweathermap::forecast::Forecast;
@ -115,7 +115,7 @@ impl DrawTarget for UdpDisplay {
} }
fn renderWeather(display: &mut UdpDisplay ,data: &Option<Result<Forecast, String>>){ fn render_weather(display: &mut UdpDisplay ,data: &Option<Result<Forecast, String>>){
let text_style = MonoTextStyle::new(&FONT_6X10, BinaryColor::On); let text_style = MonoTextStyle::new(&FONT_6X10, BinaryColor::On);
match data { match data {
@ -140,7 +140,7 @@ fn renderWeather(display: &mut UdpDisplay ,data: &Option<Result<Forecast, String
let minute = europe_time.minute(); let minute = europe_time.minute();
let cur_time = DateTime::<Utc>::default(); let cur_time = DateTime::<Utc>::default();
if(zoned_time > cur_time){ if zoned_time > cur_time {
println!("Skipping old result {hour}:{minute} @{time_s}"); println!("Skipping old result {hour}:{minute} @{time_s}");
} }
@ -164,7 +164,7 @@ fn renderWeather(display: &mut UdpDisplay ,data: &Option<Result<Forecast, String
println!("Weather info: {} desc: {} icon {}", condition.main, condition.description, condition.icon); println!("Weather info: {} desc: {} icon {}", condition.main, condition.description, condition.icon);
renderWeatherIcon(&condition, display); render_weather_icon(&condition, display);
} }
} }
}, },
@ -178,7 +178,7 @@ fn renderWeather(display: &mut UdpDisplay ,data: &Option<Result<Forecast, String
} }
fn renderWeatherIcon(condition: &Weather, display: &mut UdpDisplay ){ fn render_weather_icon(condition: &Weather, display: &mut UdpDisplay ){
let text_style = MonoTextStyle::new(&FONT_6X10, BinaryColor::On); let text_style = MonoTextStyle::new(&FONT_6X10, BinaryColor::On);
let short_icon_code = condition.icon.substring(0,2); let short_icon_code = condition.icon.substring(0,2);
let icon_image: Result<Bmp<BinaryColor>, tinybmp::ParseError> = match short_icon_code { let icon_image: Result<Bmp<BinaryColor>, tinybmp::ParseError> = match short_icon_code {
@ -235,7 +235,7 @@ fn send_package(ipaddress: String, data: &Option<Result<Forecast, String>>) {
// .into_styled(PRIMITIVE_STYLE) // .into_styled(PRIMITIVE_STYLE)
// .draw(&mut display) // .draw(&mut display)
// .unwrap(); // .unwrap();
renderWeather(&mut display, data); render_weather(&mut display, data);
@ -278,7 +278,7 @@ fn main() {
let mut last_data = Option::None; let mut last_data = Option::None;
// Test Webcrawler for public transportataion // Test Webcrawler for public transportataion
straba::fetchData(); straba::fetch_data();
loop { loop {
let delay = time::Duration::from_millis(10000); let delay = time::Duration::from_millis(10000);

View File

@ -1,10 +1,10 @@
/* @file straba.rs /* @file straba.rs
* @brief fetch next depature of light rail vehicle * @brief fetch next depature of light rail vehicle
*/ */
use std::error::Error;
use serde::Deserialize; use serde::Deserialize;
use serde_json::Value;
const stationURL:&str = "https://www.rnv-online.de/rest/departure/2494"; const STATION_URL:&str = "https://www.rnv-online.de/rest/departure/2494";
#[derive(Default, Debug, Clone, PartialEq, Deserialize)] #[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
@ -20,9 +20,9 @@ pub struct Station {
pub struct Line { pub struct Line {
pub id: i64, pub id: i64,
pub text: String, pub text: String,
pub iconOutlined: i64, pub icon_outlined: i64,
pub iconColor: String, pub icon_color: String,
pub iconTextColor: i64, pub icon_text_color: i64,
} }
#[derive(Default, Debug, Clone, PartialEq, Deserialize)] #[derive(Default, Debug, Clone, PartialEq, Deserialize)]
@ -30,10 +30,10 @@ pub struct Line {
pub struct Journey { pub struct Journey {
pub line: Line, pub line: Line,
pub destination: String, pub destination: String,
pub barrierLevel: String, pub barrier_level: String,
pub loadsForecastType: String, pub loads_forecast_type: String,
pub realtimeDeparture: i64, pub realtime_departure: i64,
pub scheduledDeparture: i64, pub scheduled_departure: i64,
pub difference: i64, pub difference: i64,
pub canceled: bool, pub canceled: bool,
} }
@ -44,20 +44,28 @@ pub struct NextDeparture {
} }
pub fn fetchData() -> Result<String, reqwest::Error> { pub fn fetch_data() -> Option<String> {
let result = reqwest::blocking::get(stationURL); let result = reqwest::blocking::get(STATION_URL);
let response = match result { if result.is_err() {
Ok(res) => res, println!("Could not read station response {:?}", result.err());
Err(err) => return reqwest::Error(err), return Option::None;
}; }
let text = result.unwrap().text();
let body = serde_json::from_str(&text); if text.is_err() {
println!("Could not convert response {:?}", text.err());
let json = match body { return Option::None;
Ok(json) => json, }
Err(err) => return reqwest::Error(err),
};
let date = json["id"].to_string(); let body: std::result::Result<Value, serde_json::Error> = serde_json::from_str(&text.unwrap());
Ok(date)
if body.is_err() {
println!("Could not parse json {:?}", body.err());
return Option::None;
}
let json = body.unwrap();
let date = json["id"].to_string();
Some(date)
} }

View File

@ -1,4 +1,4 @@
use serde::{Deserialize, __private::de}; use serde::Deserialize;
#[derive(Default, Debug, Clone, PartialEq, Deserialize)] #[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]