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
use crate::brain::python_like::control::devices::Device;
use crate::io::devices::dummy::ActiveDevicesMessage;
use crate::io::dummy::DummyAllOutputs;
use crate::io::dummy_io_bundle::new_dummy_io;
use crate::io::temperatures::dummy::ModifyState::SetTemp;
use crate::io::temperatures::Sensor;
use crate::io::wiser::dummy::ModifyState;
use crate::time_util::mytime::{DummyTimeProvider, TimeProvider};
use crate::{brain, LoggingHandle};
use chrono::{NaiveDate, NaiveTime, TimeZone, Utc};
use log::debug;
use std::time::Duration;
use tokio::runtime::Builder;
use tracing::Subscriber;
use tracing_subscriber::EnvFilter;

const SIMULATION_CONFIG: &'static str = r#"[[overrun_during.slots]]
slot = { type = "Utc", start="04:00:00", end="15:00:05" }
sensor = "TKBT"
temp = 50.0
min_temp = 30.0

[[boost_active_rooms.parts]]
room = "JohnsRoom"
device = "JohnsPhone"
increase = 3.0
"#;

pub fn simulate(logging_handle: LoggingHandle<EnvFilter, impl Subscriber>) {
    let backup_heating_supplier = || DummyAllOutputs::default();
    let (io_bundle, mut io_handle) = new_dummy_io();

    debug!("{}", SIMULATION_CONFIG);

    let config = toml::from_str(SIMULATION_CONFIG).expect("Failed to deserialize config");

    let brain = brain::python_like::PythonBrain::new(config);

    let rt = Builder::new_multi_thread()
        .worker_threads(1)
        .enable_time()
        .enable_io()
        .build()
        .expect("Expected to be able to make runtime");

    //rt.spawn(io::gpio::update_db_with_gpio::run(pool.clone(), receiver));

    //sender.try_send(PinUpdate::new(1, GPIOState::LOW)).unwrap();

    let overrun_time = Utc.from_utc_datetime(
        &NaiveDate::from_ymd_opt(2022, 05, 19)
            .unwrap()
            .and_time(NaiveTime::from_hms_opt(04, 18, 00).unwrap()),
    );
    let no_overrun_time = Utc.from_utc_datetime(
        &NaiveDate::from_ymd_opt(2022, 05, 19)
            .unwrap()
            .and_time(NaiveTime::from_hms_opt(18, 0, 0).unwrap()),
    );

    let time_provider = DummyTimeProvider::new(no_overrun_time);

    println!("Current time {:?}", time_provider.get_utc_time());
    io_handle.send_devices(ActiveDevicesMessage::SetActiveDevices(vec![
        Device::new("StevesPhone".into()),
        Device::new("JohnsPhone".into()),
    ]));

    rt.spawn(async move {
        tokio::time::sleep(Duration::from_secs(5)).await;

        println!("## Set temp to 30C at the bottom.");
        io_handle.send_temps(SetTemp(Sensor::TKBT, 30.0));
        io_handle.send_temps(SetTemp(Sensor::TKTP, 30.0));
        io_handle.send_temps(SetTemp(Sensor::HPRT, 25.0));
        //tokio::time::sleep(Duration::from_secs(20)).await;

        println!("## Set temp to 50C at TKTP.");
        io_handle.send_temps(SetTemp(Sensor::TKTP, 50.5));
        //tokio::time::sleep(Duration::from_secs(30)).await;

        println!("Test TurningOn state");
        io_handle.send_temps(SetTemp(Sensor::TKBT, 50.5)); // Make sure up to finish any heat ups
        tokio::time::sleep(Duration::from_secs(10)).await;
        io_handle.send_temps(SetTemp(Sensor::TKBT, 48.0)); // Then make sure we will turn on.
        io_handle.send_temps(SetTemp(Sensor::HXOR, 30.0));
        io_handle.send_temps(SetTemp(Sensor::HXIF, 32.0));
        io_handle.send_temps(SetTemp(Sensor::HXIR, 31.0));
        tokio::time::sleep(Duration::from_secs(10)).await;
        io_handle.send_wiser(ModifyState::SetHeatingOffTime(
            Utc::now() + chrono::Duration::seconds(1000),
        ));
        //tokio::time::sleep(Duration::from_secs(100)).await;

        println!("## Turning off wiser - expect overrun");
        io_handle.send_wiser(ModifyState::TurnOffHeating);
        //tokio::time::sleep(Duration::from_secs(60)).await;

        println!("## Turning on fake wiser heating");
        tokio::time::sleep(Duration::from_secs(10)).await;
        io_handle.send_temps(SetTemp(Sensor::HPRT, 31.0));
        io_handle.send_wiser(ModifyState::SetHeatingOffTime(
            Utc::now() + chrono::Duration::seconds(1000),
        ));
        /*tokio::time::sleep(Duration::from_secs(90)).await;

        println!("## Set temp to 55C at HXIF/R, expect circulation");
        io_handle.send_temps(SetTemp(Sensor::HXIF, 60.0));
        io_handle.send_temps(SetTemp(Sensor::HXIR, 60.0));
        io_handle.send_temps(SetTemp(Sensor::HXOR, 45.0));
        tokio::time::sleep(Duration::from_secs(60)).await;
        io_handle.send_temps(SetTemp(Sensor::HXIF, 35.0));
        io_handle.send_temps(SetTemp(Sensor::HXIR, 33.0));
        io_handle.send_temps(SetTemp(Sensor::HXOR, 20.0));

        println!("## Turning off fake wiser heating");
        io_handle.send_wiser(ModifyState::TurnOffHeating);
        tokio::time::sleep(Duration::from_secs(60)).await;

        println!("## Turning on fake wiser heating");
        io_handle.send_wiser(ModifyState::SetHeatingOffTime(
            Utc::now() + chrono::Duration::seconds(1000),
        ));
        tokio::time::sleep(Duration::from_secs(30)).await;
        io_handle.send_temps(SetTemp(Sensor::TKBT, 47.0));
        io_handle.send_temps(SetTemp(Sensor::TKTP, 47.0));
        tokio::time::sleep(Duration::from_secs(60)).await;*/

        println!("## Setting HXIF/R to above the turn off temp.");
        io_handle.send_temps(SetTemp(Sensor::HXIF, 60.0));
        io_handle.send_temps(SetTemp(Sensor::HXIR, 60.0));
        io_handle.send_temps(SetTemp(Sensor::HXOR, 60.0));
        io_handle.send_temps(SetTemp(Sensor::TKBT, 61.0));
        tokio::time::sleep(Duration::from_secs(60 * 5 + 30)).await;
        println!("## Now turning back down.");
        io_handle.send_temps(SetTemp(Sensor::HXIF, 32.0));
        io_handle.send_temps(SetTemp(Sensor::HXIR, 32.0));
        tokio::time::sleep(Duration::from_secs(30)).await;

        println!("## Testing Off -> Circulate");
        io_handle.send_wiser(ModifyState::TurnOffHeating);
        tokio::time::sleep(Duration::from_secs(10)).await;
        io_handle.send_temps(SetTemp(Sensor::TKBT, 55.0));
        io_handle.send_wiser(ModifyState::SetHeatingOffTime(
            Utc::now() + chrono::Duration::seconds(1000),
        ));
        tokio::time::sleep(Duration::from_secs(200)).await;
        println!("## Setting HXIF/R to above turn off temp and setting TKBT too low to circulate");
        io_handle.send_temps(SetTemp(Sensor::HXIF, 60.0));
        io_handle.send_temps(SetTemp(Sensor::HXIR, 60.0));
        io_handle.send_temps(SetTemp(Sensor::HXOR, 60.0));
        io_handle.send_temps(SetTemp(Sensor::TKBT, 15.0));
        tokio::time::sleep(Duration::from_secs(60 * 10 + 30)).await;

        println!("## Turning TKTP below desired temp");
        io_handle.send_temps(SetTemp(Sensor::TKTP, 20.0));
        tokio::time::sleep(Duration::from_secs(60)).await;
        println!("## Turning TKTP below desired temp");
        io_handle.send_temps(SetTemp(Sensor::TKTP, 35.0));
        tokio::time::sleep(Duration::from_secs(60)).await;
    });

    let imaginary_handle = rt.spawn(async {});
    crate::main_loop(
        brain,
        io_bundle,
        rt,
        backup_heating_supplier,
        time_provider,
        logging_handle,
        imaginary_handle,
    );

    //sleep(Duration::from_secs(30));
    //println!("Turning off heating.");
    //wiser_handle.send(ModifyState::TurnOffHeating).unwrap();
}