1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
use crate::consts::USER_AGENT;
use crate::db::Db;
use crate::reading::Reading;
use crate::services::Service;
use crate::threading;
use crate::value::{PointOfTheCompass, Value};
use crate::Result;
use chrono::{DateTime, Local};
use crossbeam_channel::{Receiver, Sender};
use failure::format_err;
use reqwest::header::{HeaderMap, HeaderValue};
use serde::Deserialize;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;

/// Buienradar JSON feed URL.
const URL: &str = "https://json.buienradar.nl/";
const REFRESH_PERIOD: Duration = Duration::from_millis(60000);

#[derive(Deserialize, Debug, Clone)]
pub struct Settings {
    /// Station ID. Find a one [here](https://json.buienradar.nl/).
    station_id: u32,
}

pub struct Buienradar {
    service_id: String,
    station_id: u32,
    client: reqwest::Client,
}

#[derive(Deserialize, Debug)]
pub struct BuienradarFeed {
    actual: BuienradarFeedActual,
}

#[derive(Deserialize, Debug)]
pub struct BuienradarFeedActual {
    #[serde(rename = "stationmeasurements")]
    station_measurements: Vec<BuienradarStationMeasurement>,
}

#[derive(Deserialize, Debug, Clone)]
pub struct BuienradarStationMeasurement {
    #[serde(rename = "stationid")]
    station_id: u32,

    #[serde(rename = "stationname")]
    name: String,

    temperature: Option<f64>,

    #[serde(rename = "groundtemperature")]
    ground_temperature: Option<f64>,

    #[serde(rename = "feeltemperature")]
    feel_temperature: Option<f64>,

    #[serde(rename = "windspeedBft")]
    wind_speed_bft: Option<u32>,

    #[serde(with = "date_format")]
    timestamp: DateTime<Local>,

    #[serde(default, rename = "winddirection", with = "wind_direction")]
    wind_direction: Option<PointOfTheCompass>,

    #[serde(rename = "weatherdescription")]
    weather_description: String,
}

impl Service for Buienradar {
    fn spawn(self: Box<Self>, _db: Arc<Mutex<Db>>, tx: Sender<Reading>, _rx: Receiver<Reading>) -> Result<()> {
        threading::spawn(self.service_id.clone(), move || loop {
            match self.fetch() {
                Ok(measurement) => self.send_readings(measurement, &tx).unwrap(),
                Err(error) => {
                    log::error!("Buienradar has failed: {}", error);
                }
            }
            thread::sleep(REFRESH_PERIOD);
        })?;
        Ok(())
    }
}

impl Buienradar {
    pub fn new(service_id: &str, settings: &Settings) -> Result<Buienradar> {
        let mut headers = HeaderMap::new();
        headers.insert(reqwest::header::USER_AGENT, HeaderValue::from_static(USER_AGENT));
        Ok(Buienradar {
            service_id: service_id.into(),
            station_id: settings.station_id,
            client: reqwest::Client::builder()
                .gzip(true)
                .timeout(Duration::from_secs(10))
                .default_headers(headers)
                .build()?,
        })
    }

    /// Fetch measurement for the configured station.
    fn fetch(&self) -> Result<BuienradarStationMeasurement> {
        let body = self.client.get(URL).send()?.text()?;
        let feed: BuienradarFeed = serde_json::from_str(&body)?;
        Ok(feed
            .actual
            .station_measurements
            .iter()
            .find(|measurement| measurement.station_id == self.station_id)
            .ok_or_else(|| format_err!("station {} is not found", self.station_id))?
            .clone())
    }

    /// Sends out readings based on Buienradar station measurement.
    fn send_readings(&self, measurement: BuienradarStationMeasurement, tx: &Sender<Reading>) -> Result<()> {
        self.send(
            &tx,
            vec![
                Reading {
                    sensor: format!("{}:{}:name", &self.service_id, self.station_id),
                    value: Value::Text(measurement.name.clone()),
                    timestamp: measurement.timestamp,
                    is_persisted: true,
                },
                Reading {
                    sensor: format!("{}:{}:weather_description", &self.service_id, self.station_id),
                    value: Value::Text(measurement.weather_description.clone()),
                    timestamp: measurement.timestamp,
                    is_persisted: true,
                },
            ],
        )?;
        if let Some(degrees) = measurement.temperature {
            tx.send(Reading {
                sensor: format!("{}:{}:temperature", &self.service_id, self.station_id),
                value: Value::Celsius(degrees),
                timestamp: measurement.timestamp,
                is_persisted: true,
            })?;
        }
        if let Some(degrees) = measurement.ground_temperature {
            tx.send(Reading {
                sensor: format!("{}:{}:ground_temperature", &self.service_id, self.station_id),
                value: Value::Celsius(degrees),
                timestamp: measurement.timestamp,
                is_persisted: true,
            })?;
        }
        if let Some(degrees) = measurement.feel_temperature {
            tx.send(Reading {
                sensor: format!("{}:{}:feel_temperature", &self.service_id, self.station_id),
                value: Value::Celsius(degrees),
                timestamp: measurement.timestamp,
                is_persisted: true,
            })?;
        }
        if let Some(bft) = measurement.wind_speed_bft {
            tx.send(Reading {
                sensor: format!("{}:{}:wind_speed_bft", &self.service_id, self.station_id),
                value: Value::Bft(bft),
                timestamp: measurement.timestamp,
                is_persisted: true,
            })?;
        }
        if let Some(point) = measurement.wind_direction {
            tx.send(Reading {
                sensor: format!("{}:{}:wind_direction", &self.service_id, self.station_id),
                value: Value::WindDirection(point),
                timestamp: measurement.timestamp,
                is_persisted: true,
            })?;
        }
        Ok(())
    }
}

/// Implements [custom date/time format](https://serde.rs/custom-date-format.html) with Amsterdam timezone.
mod date_format {
    use chrono::{DateTime, Local, NaiveDateTime, TimeZone};
    use chrono_tz::Europe::Amsterdam;
    use serde::{self, Deserialize, Deserializer};

    const FORMAT: &str = "%Y-%m-%dT%H:%M:%S";

    pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<DateTime<Local>, D::Error> {
        let string = String::deserialize(deserializer)?;
        let datetime = NaiveDateTime::parse_from_str(&string, FORMAT).unwrap();
        Ok(Amsterdam.from_local_datetime(&datetime).unwrap().with_timezone(&Local))
    }
}

/// Translates Dutch wind direction acronyms.
mod wind_direction {
    use crate::value::PointOfTheCompass;
    use serde::de::Error;
    use serde::{self, Deserialize, Deserializer};

    pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Option<PointOfTheCompass>, D::Error> {
        match String::deserialize(deserializer)?.as_ref() {
            "N" => Ok(Some(PointOfTheCompass::North)),
            "NNO" => Ok(Some(PointOfTheCompass::NorthNortheast)),
            "NO" => Ok(Some(PointOfTheCompass::Northeast)),
            "ONO" => Ok(Some(PointOfTheCompass::EastNortheast)),
            "O" => Ok(Some(PointOfTheCompass::East)),
            "OZO" => Ok(Some(PointOfTheCompass::EastSoutheast)),
            "ZO" => Ok(Some(PointOfTheCompass::Southeast)),
            "ZZO" => Ok(Some(PointOfTheCompass::SouthSoutheast)),
            "Z" => Ok(Some(PointOfTheCompass::South)),
            "ZZW" => Ok(Some(PointOfTheCompass::SouthSouthwest)),
            "ZW" => Ok(Some(PointOfTheCompass::Southwest)),
            "WZW" => Ok(Some(PointOfTheCompass::WestSouthwest)),
            "W" => Ok(Some(PointOfTheCompass::West)),
            "WNW" => Ok(Some(PointOfTheCompass::WestNorthwest)),
            "NW" => Ok(Some(PointOfTheCompass::Northwest)),
            "NNW" => Ok(Some(PointOfTheCompass::NorthNorthwest)),
            value => Err(Error::custom(format!("could not translate wind direction: {}", value))),
        }
    }
}