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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
use crate::brain::modes::working_temp::{
    find_working_temp_action, CurrentHeatDirection, WorkingTempAction,
};
use crate::brain::modes::{InfoCache, Intention, Mode};
use crate::brain::python_like::config::PythonBrainConfig;
use crate::brain::python_like::control::heating_control::HeatPumpMode;
use crate::brain::BrainFailure;
use crate::expect_available;
use crate::io::IOBundle;
use crate::io::temperatures::Sensor;
use crate::time_util::mytime::TimeProvider;
use crate::time_util::timeslot::ZonedSlot;
use chrono::{DateTime, SecondsFormat, Utc};
use log::{debug, error, info, warn};
use std::fmt::{Display, Formatter};
use std::time::Duration;
use tokio::runtime::Runtime;

use super::working_temp::MixedState;
use super::{allow_dhw_mixed, AllowDhwMixed};
use super::heating_mode::HeatingMode;
use super::mixed::MixedMode;

#[derive(Debug, PartialEq)]
pub struct DhwOnlyMode {
}

impl Mode for DhwOnlyMode {
    fn enter(
        &mut self,
        _config: &PythonBrainConfig,
        _runtime: &Runtime,
        io_bundle: &mut IOBundle,
    ) -> Result<(), BrainFailure> {
        let heating = expect_available!(io_bundle.heating_control())?;
        heating.set_heat_pump(HeatPumpMode::HotWaterOnly, None)
    }

    fn update(
        &mut self,
        rt: &Runtime,
        config: &PythonBrainConfig,
        info_cache: &mut InfoCache,
        io_bundle: &mut IOBundle,
        time: &impl TimeProvider,
    ) -> Result<Intention, BrainFailure> {
        let temps = match rt.block_on(info_cache.get_temps(io_bundle.temperature_manager())) {
            Err(err) => {
                error!("Temperatures not available, stopping overrun {err}");
                return Ok(Intention::off_now());
            },
            Ok(temps) => temps,
        };

        let now = time.get_utc_time();

        let heating_control = expect_available!(io_bundle.heating_control())?;
        let (_hp_on, hp_duration) = heating_control.get_heat_pump_on_with_time()?;
        let short_duration = hp_duration < Duration::from_secs(60 * 10);

        let slot = config.get_overrun_during().find_matching_slot(&now, &temps,
            |temps, temp| temp < temps.max || (short_duration && temp < temps.extra.unwrap_or(temps.max))
        );

        let Some(slot) = slot else {
            info!("No longer matches a DHW slot");
            return Ok(Intention::finish());
        };

        if info_cache.heating_on() {
            let allow_dhw_mixed = allow_dhw_mixed(&temps, slot, false);

            if matches!(allow_dhw_mixed, AllowDhwMixed::Force) {
                return Ok(Intention::SwitchForce(HeatingMode::Mixed(MixedMode::new())))
            }

            match find_working_temp_action(
                &temps,
                &info_cache.get_working_temp_range(),
                &config.hp_circulation,
                CurrentHeatDirection::Falling,
                None,
                Some(slot),
            ) {
                Ok(WorkingTempAction::Cool { .. }) => {
                    debug!("Continuing to heat hot water as we would be circulating.");
                }
                Ok(WorkingTempAction::Heat { mixed_state }) => {
                    match allow_dhw_mixed {
                        AllowDhwMixed::Error  => return Ok(Intention::off_now()),
                        AllowDhwMixed::Can    => {
                            if mixed_state == MixedState::MixedHeating {
                                return Ok(Intention::SwitchForce(HeatingMode::Mixed(MixedMode::new())))
                            }
                            return Ok(Intention::finish());
                        }
                        AllowDhwMixed::Force => error!("Believed impossible"),
                        AllowDhwMixed::Cannot => {}
                        
                    }
                }
                Err(e) => {
                    warn!("Missing sensor {e} to determine whether we are in circulate. But we are fine how we are - staying.");
                }
            };
        }

        if let Some(bypass) = &slot.bypass {
            let diff = temps.get(&Sensor::HPFL).unwrap_or(&0.0) - temps.get(&Sensor::HPRT).unwrap_or(&0.0);
            match heating_control.try_get_heat_pump()? {
                HeatPumpMode::MostlyHotWater => {
                    if diff <= bypass.stop_hp_drop {
                        info!("Bypass no longer required as HPFL-HPRT={diff:.1}");
                        heating_control.set_heat_pump(HeatPumpMode::HotWaterOnly, None)?;
                    }
                },
                HeatPumpMode::HotWaterOnly => {
                    if diff >= bypass.start_hp_drop {
                        info!("Bypass required as HPFL-HPRT={diff:.1}");
                        heating_control.set_heat_pump(HeatPumpMode::MostlyHotWater, None)?;
                    }
                },
                mode => {
                    error!("Unexpected mode {mode:?}");
                },
            }
        }

        Ok(Intention::KeepState)
    }
}

#[derive(Debug, PartialEq, Clone)]
pub enum HeatUpEnd {
    Slot(ZonedSlot),
    Utc(DateTime<Utc>),
}

impl HeatUpEnd {
    pub fn has_expired(&self, now: DateTime<Utc>) -> bool {
        match self {
            HeatUpEnd::Slot(slot) => !slot.contains(&now),
            HeatUpEnd::Utc(expire_time) => now > *expire_time,
        }
    }
}

impl Display for HeatUpEnd {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            HeatUpEnd::Slot(slot) => {
                write!(f, "During {}", slot)
            }
            HeatUpEnd::Utc(time) => {
                write!(
                    f,
                    "Until {}",
                    time.to_rfc3339_opts(SecondsFormat::Millis, true)
                )
            }
        }
    }
}

impl DhwOnlyMode {
    pub fn new() -> Self {
        Self {}
    }
}

#[allow(clippy::zero_prefixed_literal)]
#[cfg(test)]
mod test {
    use super::*;
    use crate::brain::modes::working_temp::{WorkingRange, WorkingTemperatureRange};
    use crate::brain::modes::{HeatingState, InfoCache, Intention, Mode};
    use crate::brain::python_like::config::PythonBrainConfig;
    use crate::io::dummy_io_bundle::new_dummy_io;
    use crate::io::temperatures::dummy::ModifyState as TModifyState;
    use crate::io::temperatures::Sensor;
    use crate::time_util::mytime::DummyTimeProvider;
    use crate::time_util::test_utils::{date, time, utc_datetime, utc_time_slot};
    use chrono::{TimeZone, Utc};
    use crate::brain::python_like::config::overrun_config::DhwBap;

    #[test]
    fn test_results() {
        let rt = Runtime::new().unwrap();

        let mut info_cache = InfoCache::create(
            HeatingState::OFF,
            WorkingRange::from_temp_only(WorkingTemperatureRange::from_delta(45.0, 10.0)),
        );

        let mut config = PythonBrainConfig::default();
        config._add_dhw_slot(DhwBap::_new(
            utc_time_slot(10,00,00, 12,00,00),
            Sensor::TKBT, 10.0, 40.0
        ));

        let mut heat_up_to = DhwOnlyMode::new();

        let (mut io_bundle, mut io_handle) = new_dummy_io();

        let date = date(2022, 02, 13);

        let in_range_time = time(11, 00, 00);
        let out_of_range_time = time(13, 00, 00);

        {
            // Keep state, still heating up.
            io_handle.send_temps(TModifyState::SetTemp(Sensor::TKBT, 35.0));
            let time_provider =
                DummyTimeProvider::new(Utc.from_utc_datetime(&date.and_time(in_range_time)));

            let result = heat_up_to.update(
                &rt,
                &config,
                &mut info_cache,
                &mut io_bundle,
                &time_provider,
            );

            let intention = result.expect("Should not have error");
            assert!(
                matches!(intention, Intention::KeepState),
                "Intention should have been KeepState but was: {:?}",
                intention
            );
            info_cache.reset_cache();
        }

        {
            ///// Check it ends when TKBT is too high. /////
            io_handle.send_temp(Sensor::TKBT, 50.0);

            let time_provider =
                DummyTimeProvider::new(Utc.from_utc_datetime(&date.and_time(in_range_time)));

            let result = heat_up_to.update(
                &rt,
                &config,
                &mut info_cache,
                &mut io_bundle,
                &time_provider,
            );

            let intention = result.expect("Should have not been any error");
            assert!(
                matches!(intention, Intention::Finish),
                "Should have finished due to high temp, actually: {:?}",
                intention
            );
            info_cache.reset_cache();
        }

        {
            ///// Check it ends when time is out of range. /////
            io_handle.send_temps(TModifyState::SetTemp(Sensor::TKBT, 35.0));

            let time_provider =
                DummyTimeProvider::new(Utc.from_utc_datetime(&date.and_time(out_of_range_time)));

            let result = heat_up_to.update(
                &rt,
                &config,
                &mut info_cache,
                &mut io_bundle,
                &time_provider,
            );

            let intention = result.expect("Should have not been any error");
            assert!(
                matches!(intention, Intention::Finish),
                "Should have been finished due to out of time range, actually: {:?}",
                intention
            );
            info_cache.reset_cache();
        }
    }

    #[test]
    fn test_stay_heatupto_when_circulating() -> Result<(), BrainFailure> {
        let working_range = WorkingTemperatureRange::from_min_max(40.0, 50.0);
        let mut info_cache = InfoCache::create(
            HeatingState::ON,
            WorkingRange::from_temp_only(working_range.clone()),
        );

        let utc_time = utc_datetime(2023, 06, 12, 10, 00, 00);

        let mut config = PythonBrainConfig::default();

        config._add_dhw_slot(DhwBap::_new(
            utc_time_slot(09,00,00, 11,00,00),
            Sensor::TKBT, 0.0, 39.0
        ));

        let mut mode = DhwOnlyMode::new();

        let rt = Runtime::new().unwrap();

        let (mut io_bundle, mut handle) = new_dummy_io();
        let time = DummyTimeProvider::new(utc_time);

        handle.send_temp(Sensor::TKBT, 30.0);
        handle.send_temp(Sensor::HXIF, 50.0);
        handle.send_temp(Sensor::HXIR, 50.0);
        handle.send_temp(Sensor::HXOR, 50.0);

        let next = mode.update(
            &rt,
            &config,
            &mut info_cache,
            &mut io_bundle,
            &time,
        )?;

        assert!(
            matches!(next, Intention::KeepState),
            "Should be KeepState. Was: {:?}",
            next
        );

        Ok(())
    }

    #[test]
    fn test_stay_heatupto_when_below_min() -> Result<(), BrainFailure> {
        let working_range = WorkingTemperatureRange::from_min_max(40.0, 50.0);

        let utc_slot = utc_time_slot(12, 0, 0, 13, 0, 0);

        let mut config = PythonBrainConfig::default();

        config._add_dhw_slot(DhwBap::_new(
            utc_slot.clone(),
            Sensor::TKBT, 30.0, 50.0
        ));

        let mut mode = DhwOnlyMode::new();
        let rt = Runtime::new().unwrap();
        let (mut io_bundle, mut handle) = new_dummy_io();
        let time = DummyTimeProvider::in_slot(&utc_slot);

        handle.send_temp(Sensor::TKBT, 20.0);
        handle.send_temp(Sensor::HXIF, 39.5);
        handle.send_temp(Sensor::HXIR, 39.5);
        handle.send_temp(Sensor::HXOR, 39.5);

        let mut info_cache = InfoCache::create(
            HeatingState::ON,
            WorkingRange::from_temp_only(working_range),
        );

        mode.enter(&config, &rt, &mut io_bundle)?;

        let intention = mode.update(&rt, &config, &mut info_cache, &mut io_bundle, &time)?;

        assert_eq!(intention, Intention::KeepState);

        Ok(())
    }
}