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
use super::kindergarten::*;
use super::server;
use super::*;
use std::io::{Error as ioError, ErrorKind, Result};
use std::sync::{Arc, Mutex};
use std::{thread, time};

/// Timer struct
pub struct Timer {
    name: String,
    id: u32,
    comm: String,
    interval: time::Duration,
}

impl Timer {
    /// new timer
    fn new(name: &String, id: u32, comm: String, td: time::Duration) -> Self {
        Timer {
            name: name.clone(),
            id,
            comm,
            interval: td,
        }
    }

    /// generate new timer from child name and child config
    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(),
        ))
    }

    /// run the timer
    pub fn run(self, kig: Arc<Mutex<Kindergarten>>) {
        thread::sleep(self.interval);

        //check if this timer still works
        if !self.check(kig.clone()) {
            println!(
                "{}",
                logger::timelog(&format!(
                    "check failed when timer try to run \"{} {}\"",
                    self.comm.clone(),
                    self.name.clone()
                ))
            );
            return;
        }

        // call server::day_care to run the repeat command
        match server::day_care(kig, format!("{} {}", self.comm.clone(), self.name.clone())) {
            Err(e) => println!(
                "{}",
                logger::timelog(&format!("Timer is up, but {:?}", e.to_string()))
            ),
            Ok(m) => println!(
                "{}\n{}",
                logger::timelog(&format!(
                    "Timer is up, run \"{} {}\"",
                    self.comm.clone(),
                    self.name.clone(),
                )),
                logger::timelog(&m),
            ),
        }
    }

    /// check this timer is outdate or not
    fn check(&self, kig: Arc<Mutex<Kindergarten>>) -> bool {
        let mut kg = kig.lock().unwrap();
        // if timer.id not equal child.id, means child has already restarted
        // then this timer is outdate
        if let Some(id) = kg.has_child(&self.name) {
            if *id == self.id {
                return true;
            }
        }

        return false;
    }
}