Add weatherapi collector

This commit is contained in:
2022-08-23 19:48:36 +02:00
parent e7baf561bd
commit d2054caf69
6 changed files with 247 additions and 7 deletions

View File

@ -1,7 +1,7 @@
mod collector;
use clap::Parser;
use collector::{Collector, CollectorConfig, MarkdownWeb};
use collector::{Collector, CollectorConfig, MarkdownWeb, WeatherApi};
use common::{BulbMap, ClientMessage, ServerMessage};
use futures_util::{SinkExt, StreamExt};
use lighter_manager::{
@ -195,6 +195,22 @@ async fn info_collector(state: &State) {
}));
}
if !state.config.collectors.weatherapi_locations.is_empty() {
let api_key = state
.config
.collectors
.weatherapi_key
.as_deref()
.expect("Missing weatherapi_key");
for location in state.config.collectors.weatherapi_locations.iter().cloned() {
collectors.push(Box::new(WeatherApi {
api_key: api_key.to_string(),
location,
}));
}
}
let mut collectors = collectors.into_boxed_slice();
let server_message = &state.server_message;
@ -266,7 +282,17 @@ async fn client_handler(mut socket: WebSocket, state: &State) {
None => return info!("stream closed"),
Some(Err(e)) => return warn!("client error: {e}"),
Some(Ok(message)) => {
let message = ron::from_str(message.to_str().unwrap()).unwrap();
let message = match message.to_str().ok() {
Some(text) => text,
None => continue,
};
let message = match ron::from_str(message) {
Ok(message) => message,
Err(e) => {
return error!("failed to deserialize websocket message: {e}");
}
};
let request = ClientRequest {
message,
@ -277,7 +303,6 @@ async fn client_handler(mut socket: WebSocket, state: &State) {
return error!("client message handlers error: {e}");
}
}
}
}
}