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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use crate::brain::modes::intention::Intention;
use crate::time_util::mytime::TimeProvider;
use crate::{BrainFailure, IOBundle, PythonBrainConfig, Sensor, TemperatureManager};
use log::*;
use std::collections::HashMap;
use std::fmt::{Display, Formatter};
use std::sync::atomic::{AtomicBool, Ordering};
use tokio::runtime::Runtime;

use self::working_temp::WorkingRange;

use super::python_like::config::overrun_config::DhwBap;

pub mod circulate;
pub mod dhw_only;
pub mod mixed;
pub mod equalise;
mod off;
pub mod on;
pub mod pre_circulate;
pub mod try_circulate;
pub mod turning_on;
pub mod working_temp;

pub mod heating_mode;

pub mod intention;

pub trait Mode: PartialEq {
    fn enter(
        &mut self,
        config: &PythonBrainConfig,
        runtime: &Runtime,
        io_bundle: &mut IOBundle,
    ) -> Result<(), BrainFailure>;

    fn update(
        &mut self,
        rt: &Runtime,
        config: &PythonBrainConfig,
        info_cache: &mut InfoCache,
        io_bundle: &mut IOBundle,
        time: &impl TimeProvider,
    ) -> Result<Intention, BrainFailure>;
}

pub struct InfoCache {
    heating_state: HeatingState,
    temps: Option<Result<HashMap<Sensor, f32>, String>>,
    working_temp_range: WorkingRange,
    working_temp_range_printed: AtomicBool,
}

impl InfoCache {
    pub fn create(heating_state: HeatingState, working_range: WorkingRange) -> Self {
        Self {
            heating_state,
            temps: None,
            working_temp_range: working_range,
            working_temp_range_printed: AtomicBool::new(false),
        }
    }

    pub fn heating_on(&self) -> bool {
        self.heating_state.is_on()
    }

    /// Whether the wiser is calling for space heating
    pub fn heating_state(&self) -> &HeatingState {
        &self.heating_state
    }

    pub fn get_working_temp_range(&self) -> WorkingRange {
        if !self
            .working_temp_range_printed
            .swap(true, Ordering::Relaxed)
        {
            info!("{}", self.working_temp_range);
        }
        self.working_temp_range.clone()
    }

    pub async fn get_temps(
        &mut self,
        temperature_manager: &dyn TemperatureManager,
    ) -> Result<HashMap<Sensor, f32>, String> {
        if self.temps.is_none() {
            self.temps = Some(temperature_manager.retrieve_temperatures().await);
        }
        self.temps.as_ref().unwrap().clone()
    }

    #[cfg(test)]
    pub fn reset_cache(&mut self) {
        self.temps = None;
    }
}

/// Whether the wiser is calling for space heating
/// Makes code more understandable and implements display.
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct HeatingState(bool);

impl HeatingState {
    /// A state representing the wiser NOT calling for space heating
    pub const OFF: HeatingState = HeatingState::new(false);
    /// A state representing the wiser calling for space heating
    pub const ON: HeatingState = HeatingState::new(true);

    /// Create a new heating state. If on is true, the heating state is ON.
    pub const fn new(on: bool) -> Self {
        Self(on)
    }

    /// Check whether this heating state is on.
    pub fn is_on(&self) -> bool {
        self.0
    }

    /// Check whether this heating state is off.
    pub fn is_off(&self) -> bool {
        !self.is_on()
    }
}

impl Display for HeatingState {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", if self.is_on() { "on" } else { "off" })
    }
}

/// Whether it is allowed to be in DHW mixed mode
/// This does not consider whether there is demand for heat, just the DHW dynamics
fn allow_dhw_mixed(temps: &HashMap<Sensor, f32>, slot: &DhwBap, mixing: bool) -> AllowDhwMixed {
    let temp = match temps.get(&slot.temps.sensor) {
        Some(temp) => temp,
        None => {
            error!("Sensor {} targeted by overrun didn't have a temperature associated.", slot.temps.sensor);
            return AllowDhwMixed::Error;
        }
    };

    if let Some(mixed) = &slot.mixed {
        let diff = temps.get(&Sensor::HPFL).unwrap_or(&0.0) - temps.get(&Sensor::TKTP).unwrap_or(&0.0);
        let ref_diff = if mixing { mixed.stop_hpfl_tktp_diff } else { mixed.start_hpfl_tktp_diff };
        if diff >= ref_diff {
            info!("HPFL-TKTP={diff} >= {ref_diff} so forcing mixed regardless of minimum of {:.2?}",
                slot.temps.min);
            return AllowDhwMixed::Force;
        }
    }

    if *temp >= slot.temps.min {
        return AllowDhwMixed::Can;
    }


    info!("Below minimum of {:.2?} - Overriding call for heat", slot.temps.min);

    return AllowDhwMixed::Cannot;
}

enum AllowDhwMixed {
    Error,
    Can,
    Force,
    Cannot,
}