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
use super::*;

use std::cell::Cell;
use std::io::{Error, ErrorKind};
use std::time::{Duration, Instant};

use tokio::timer::Delay;
use tokio::util::FutureExt;

pub struct Proxy {
    temperature: Temperature,
    water_content: VolumetricWaterContent,
    permittivity: RelativePermittivity,
    raw_counts: usize,
    delay: Duration,
    next_error: Cell<Option<Error>>,
}

pub trait Driver {
    fn set_delay(&mut self, delay: Duration);

    fn set_next_error(&mut self, next_error: Option<Error>);

    fn set_temperature(&mut self, temperature: Temperature);

    fn set_water_content(&mut self, water_content: VolumetricWaterContent);

    fn set_permittivity(&mut self, permittivity: RelativePermittivity);

    fn set_raw_counts(&mut self, raw_counts: usize);
}

impl Proxy {
    pub fn default_temperature() -> Temperature {
        Temperature::from(20.0)
    }

    pub fn default_water_content() -> VolumetricWaterContent {
        VolumetricWaterContent::from_percent(30.0)
    }

    pub fn default_permittivity() -> RelativePermittivity {
        RelativePermittivity::min()
    }

    pub const fn default_raw_counts() -> usize {
        0
    }

    fn read_value<T>(&self, value: T, timeout: Duration) -> impl Future<Item = T, Error = Error>
    where
        T: 'static,
    {
        let deadline = Instant::now() + self.delay;
        let next_error = self.next_error.replace(None);
        let result = if let Some(error) = next_error {
            Err(error)
        } else {
            Ok(value)
        };
        Delay::new(deadline)
            .then(move |_| result)
            .map_err(|err| Error::new(ErrorKind::Other, format!("reading value failed: {}", err)))
            .timeout(timeout)
            .map_err(move |err| {
                err.into_inner().unwrap_or_else(|| {
                    Error::new(ErrorKind::TimedOut, String::from("reading value timed out"))
                })
            })
    }

    /// Implementation of Capabilities::read_temperature()
    pub fn read_temperature(
        &self,
        timeout: Duration,
    ) -> impl Future<Item = Temperature, Error = Error> {
        self.read_value(self.temperature, timeout)
    }

    /// Implementation of Capabilities::read_water_content()
    pub fn read_water_content(
        &self,
        timeout: Duration,
    ) -> impl Future<Item = VolumetricWaterContent, Error = Error> {
        self.read_value(self.water_content, timeout)
    }

    /// Implementation of Capabilities::read_permittivity()
    pub fn read_permittivity(
        &self,
        timeout: Duration,
    ) -> impl Future<Item = RelativePermittivity, Error = Error> {
        self.read_value(self.permittivity, timeout)
    }

    /// Implementation of Capabilities::read_raw_counts()
    pub fn read_raw_counts(&self, timeout: Duration) -> impl Future<Item = usize, Error = Error> {
        self.read_value(self.raw_counts, timeout)
    }
}

impl Default for Proxy {
    fn default() -> Self {
        Self {
            temperature: Self::default_temperature(),
            water_content: Self::default_water_content(),
            permittivity: Self::default_permittivity(),
            raw_counts: Self::default_raw_counts(),
            delay: Duration::default(),
            next_error: Cell::new(None),
        }
    }
}

impl Driver for Proxy {
    fn set_delay(&mut self, delay: Duration) {
        self.delay = delay;
    }

    fn set_next_error(&mut self, next_error: Option<Error>) {
        self.next_error.set(next_error);
    }

    fn set_temperature(&mut self, temperature: Temperature) {
        self.temperature = temperature;
    }

    fn set_water_content(&mut self, water_content: VolumetricWaterContent) {
        self.water_content = water_content;
    }

    fn set_permittivity(&mut self, permittivity: RelativePermittivity) {
        self.permittivity = permittivity;
    }

    fn set_raw_counts(&mut self, raw_counts: usize) {
        self.raw_counts = raw_counts;
    }
}

impl Capabilities for Proxy {
    fn read_temperature(
        &self,
        timeout: Duration,
    ) -> Box<dyn Future<Item = Temperature, Error = Error>> {
        Box::new(self.read_temperature(timeout))
    }

    fn read_water_content(
        &self,
        timeout: Duration,
    ) -> Box<dyn Future<Item = VolumetricWaterContent, Error = Error>> {
        Box::new(self.read_water_content(timeout))
    }

    fn read_permittivity(
        &self,
        timeout: Duration,
    ) -> Box<dyn Future<Item = RelativePermittivity, Error = Error>> {
        Box::new(self.read_permittivity(timeout))
    }

    fn read_raw_counts(&self, timeout: Duration) -> Box<dyn Future<Item = usize, Error = Error>> {
        Box::new(self.read_raw_counts(timeout))
    }
}