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
use std::time::Instant;

use log::*;
use tokio::runtime::Runtime;

use crate::brain::python_like::config::PythonBrainConfig;
use crate::brain::BrainFailure;
use crate::io::IOBundle;
use crate::time_util::mytime::TimeProvider;

use super::equalise::EqualiseMode;
use super::heating_mode::HeatingMode;
use super::intention::Intention;
use super::{InfoCache, Mode};

#[derive(PartialEq, Debug)]
pub struct PreCirculateMode {
    started: Instant,
}

impl PreCirculateMode {
    pub fn start() -> Self {
        Self {
            started: Instant::now(),
        }
    }
}

impl Mode for PreCirculateMode {
    fn enter(
        &mut self,
        config: &PythonBrainConfig,
        _runtime: &tokio::runtime::Runtime,
        _io_bundle: &mut crate::io::IOBundle,
    ) -> Result<(), BrainFailure> {
        info!(
            "Waiting {}s in PreCirculate",
            config.hp_circulation.initial_hp_sleep.as_secs()
        );

        Ok(())
    }

    fn update(
        &mut self,
        _rt: &Runtime,
        config: &PythonBrainConfig,
        info_cache: &mut InfoCache,
        _io_bundle: &mut IOBundle,
        _time: &impl TimeProvider,
    ) -> Result<Intention, BrainFailure> {
        if !info_cache.heating_on() {
            return Ok(Intention::Finish);
        }

        // TODO: Check working range each time.

        if self.started.elapsed() > config.hp_circulation.initial_hp_sleep {
            Ok(Intention::SwitchForce(
                HeatingMode::Equalise(EqualiseMode::start()),
            ))
        }
        else {
            Ok(Intention::YieldHeatUps)
        }
    }
}