comparison rust/src/main.rs @ 609:7bda01659426 rust

not building, paramwaiter work
author Matt Johnston <matt@ucc.asn.au>
date Sat, 18 Feb 2017 00:21:10 +0800
parents 282fae1c12e4
children f3e39e2107fd
comparison
equal deleted inserted replaced
608:71f045231a07 609:7bda01659426
3 #[macro_use] 3 #[macro_use]
4 extern crate log; 4 extern crate log;
5 extern crate env_logger; 5 extern crate env_logger;
6 extern crate rustc_serialize; 6 extern crate rustc_serialize;
7 extern crate time; 7 extern crate time;
8 extern crate tokio_curl;
9 extern crate curl;
8 10
9 #[macro_use] 11 #[macro_use]
10 extern crate lazy_static; 12 extern crate lazy_static;
11 13
12 #[macro_use] 14 #[macro_use]
16 extern crate toml; 18 extern crate toml;
17 19
18 extern crate docopt; 20 extern crate docopt;
19 21
20 use std::io; 22 use std::io;
21 use std::time::{Duration,Instant};
22 use std::ops::Not;
23 use std::cell::Cell;
24 23
25 use tokio_core::reactor::Core; 24 use tokio_core::reactor::Core;
26 use futures::{Stream,Sink,Future}; 25 use futures::{Stream,Sink,Future};
27 use futures::sync::{mpsc}; 26 use futures::sync::{mpsc};
28 use sensor::Sensor; 27 use sensor::Sensor;
29 28
30 mod config; 29 mod config;
31 mod sensor; 30 mod sensor;
32 pub mod fridge; 31 mod fridge;
33 mod types; 32 mod types;
34 mod paramwaiter; 33 mod paramwaiter;
35 34
36 use types::*; 35 use types::*;
37 use config::Config; 36 use config::Config;
173 std::process::exit(0); 172 std::process::exit(0);
174 } 173 }
175 174
176 run(&config, args.flag_nowait, args.flag_test); 175 run(&config, args.flag_nowait, args.flag_test);
177 } 176 }
178
179 /// Call closures with a rate limit. Useful for log message ratelimiting
180 pub struct NotTooOften {
181 last: Cell<Instant>,
182 limit: Duration,
183 }
184
185 impl NotTooOften {
186 pub fn new(limit_secs: u64) -> Self {
187 NotTooOften {
188 limit: Duration::new(limit_secs, 0),
189 last: Cell::new(Instant::now() - Duration::new(limit_secs+1, 0)),
190 }
191 }
192
193 pub fn and_then<F>(&self, op: F)
194 where F: Fn() {
195 let now = Instant::now();
196 if now - self.last.get() > self.limit {
197 self.last.set(now);
198 op();
199 }
200 }
201 }
202