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
use crate::db::Db;
use crate::reading::*;
use crate::threading;
use crate::value::Value;
use crate::Result;
use bus::Bus;
use chrono::Local;
use crossbeam_channel::Sender;
use serde::Deserialize;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
pub struct Clock {
service_id: String,
interval: Duration,
counter: u64,
}
#[derive(Deserialize, Debug, Clone)]
pub struct Settings {
pub interval_ms: Option<u64>,
}
impl Clock {
pub fn new(service_id: &str, settings: &Settings) -> Clock {
Clock {
service_id: service_id.into(),
interval: Duration::from_millis(settings.interval_ms.unwrap_or(1000)),
counter: 0,
}
}
}
impl crate::services::Service for Clock {
fn spawn(mut self: Box<Self>, _db: Arc<Mutex<Db>>, tx: &Sender<Message>, _rx: &mut Bus<Message>) -> Result<()> {
let tx = tx.clone();
threading::spawn(format!("my-iot::clock:{}", &self.service_id), move || loop {
tx.try_send(Message {
type_: Type::Actual,
reading: Reading {
sensor: self.service_id.clone(),
value: Value::Counter(self.counter),
timestamp: Local::now(),
},
})
.unwrap();
self.counter += 1;
thread::sleep(self.interval);
})?;
Ok(())
}
}