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
use super::kindergarten::*;
use super::server;
use super::*;
use std::error::Error;
use std::io::{Error as ioError, ErrorKind, Result};
use std::sync::{Arc, Mutex};
use std::{thread, time};
pub struct Timer {
name: String,
id: u32,
comm: String,
interval: time::Duration,
}
impl Timer {
fn new(name: &String, id: u32, comm: String, td: time::Duration) -> Self {
Timer {
name: name.clone(),
id: id,
comm: comm,
interval: td,
}
}
pub fn new_from_conf(name: String, conf: child::Config) -> Result<Self> {
if !conf.is_repeat() {
return Err(ioError::new(
ErrorKind::InvalidInput,
format!("config is not repeatable"),
));
}
Ok(Self::new(
&name,
conf.child_id.unwrap(),
format!("{}", conf.repeat_command().unwrap()),
conf.to_duration().unwrap(),
))
}
pub fn run(self, kig: Arc<Mutex<Kindergarten>>) {
thread::sleep(self.interval);
if !self.check(kig.clone()) {
println!(
"{}",
logger::timelog(&format!(
"check failed when timer try to run \"{} {}\"",
self.comm.clone(),
self.name.clone()
))
);
return;
}
match server::day_care(kig, format!("{} {}", self.comm.clone(), self.name.clone())) {
Err(e) => println!(
"{}",
logger::timelog(&format!("Timer is up, but {:?}", e.description()))
),
Ok(m) => println!(
"{}\n{}",
logger::timelog(&format!(
"Timer is up, run \"{} {}\"",
self.comm.clone(),
self.name.clone(),
)),
logger::timelog(&m),
),
}
}
fn check(&self, kig: Arc<Mutex<Kindergarten>>) -> bool {
let mut kg = kig.lock().unwrap();
if let Some(id) = kg.has_child(&self.name) {
if *id == self.id {
return true;
}
}
return false;
}
}