Skip to content

Commit

Permalink
eco: Add eco mode module
Browse files Browse the repository at this point in the history
This is currently only for testing and as a ui prototype.
  • Loading branch information
borg42 committed Nov 24, 2024
1 parent f7c399a commit 9ac85b4
Show file tree
Hide file tree
Showing 8 changed files with 521 additions and 0 deletions.
99 changes: 99 additions & 0 deletions software/src/modules/eco/eco.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/* esp32-firmware
* Copyright (C) 2024 Olaf Lüke <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "eco.h"

#include <time.h>
#include <type_traits>

#include "event_log_prefix.h"
#include "module_dependencies.h"
#include "build.h"


void Eco::pre_setup()
{
this->trace_buffer_index = logger.alloc_trace_buffer("eco", 1 << 20);

config = ConfigRoot{Config::Object({
{"charge_plan_active", Config::Bool(false)},
{"mode_after_charge_plan", Config::Uint(3, 0, 3)},
{"charge_below_active", Config::Bool(false)},
{"charge_below", Config::Int32(0)}, // in ct
{"block_above_active", Config::Bool(false)},
{"block_above", Config::Int32(20)} // in ct
}), [this](Config &update, ConfigSource source) -> String {
task_scheduler.scheduleOnce([this]() {
this->update();
});
return "";
}};

charge_plan = Config::Object({
{"enabled",Config::Bool(false)},
{"day", Config::Uint(0, 0, 2)},
{"time", Config::Int(8*60)}, // localtime in minutes since 00:00
{"hours", Config::Uint(4, 1, 48)}
});
charge_plan_update = charge_plan;

state = Config::Object({
{"last_charge_plan_save", Config::Uint(0)},
});
}

void Eco::setup()
{
api.restorePersistentConfig("eco/config", &config);
// TODO: Set user defined default charge_plan?

initialized = true;
}

void Eco::register_urls()
{
api.addPersistentConfig("eco/config", &config);
api.addState("eco/state", &state);

api.addState("eco/charge_plan", &charge_plan);
api.addCommand("eco/charge_plan_update", &charge_plan_update, {}, [this](String &/*errmsg*/) {
charge_plan = charge_plan_update;
state.get("last_charge_plan_save")->updateUint(rtc.timestamp_minutes());
update();
}, false);

task_scheduler.scheduleWallClock([this]() {
this->update();
}, 1_m, 0_ms, true);
}

void Eco::update()
{
current_decision = Decision::Normal;
}

Eco::Decision Eco::get_decision()
{
if (!config.get("charge_planning_active")->asBool() &&
!config.get("charge_below_active")->asBool() &&
!config.get("block_above_active")->asBool()) {
return Decision::Normal;
}

return current_decision;
}
52 changes: 52 additions & 0 deletions software/src/modules/eco/eco.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* esp32-firmware
* Copyright (C) 2024 Olaf Lüke <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/

#pragma once

#include "module.h"
#include "config.h"


class Eco final : public IModule
{
private:
void update();

ConfigRoot config;
ConfigRoot charge_plan;
ConfigRoot charge_plan_update;
ConfigRoot state;

size_t trace_buffer_index;

public:
enum class Decision : uint8_t {
Normal = 0,
Fast = 1,
Block = 2
};

Eco(){}
void pre_setup() override;
void setup() override;
void register_urls() override;
Decision get_decision();

Decision current_decision = Decision::Normal;
};
7 changes: 7 additions & 0 deletions software/src/modules/eco/module.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[Dependencies]
Requires = Task Scheduler
Event Log
API
Day Ahead Prices
Solar Forecast
Rtc
19 changes: 19 additions & 0 deletions software/web/src/modules/eco/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export interface config {
charge_plan_active: boolean;
mode_after_charge_plan: number;
charge_below_active: boolean;
charge_below: number;
block_above_active: boolean;
block_above: number;
}

export interface charge_plan {
enabled: boolean;
day: number;
time: number;
hours: number;
}

export interface state {
last_charge_plan_save: number;
}
Loading

0 comments on commit 9ac85b4

Please sign in to comment.