From 68c80417fbc1ebfb1e1ebc7499b3ed908134bf57 Mon Sep 17 00:00:00 2001 From: KipK Date: Tue, 31 Jan 2023 21:47:40 +0100 Subject: [PATCH 1/3] add limit_default_type & limit_default_value to .config Added default limits at boot ( UI undone ) Default limit set from /config have auto_release set to false add "limit_version" event --- models/Limit.yaml | 5 ++- openevse-gui-v2 | 1 + src/app_config.cpp | 20 ++++++++++++ src/app_config.h | 4 +++ src/limit.cpp | 77 +++++++++++++++++++++++++++++++--------------- src/limit.h | 9 ++++-- src/web_server.cpp | 6 ++-- 7 files changed, 91 insertions(+), 31 deletions(-) create mode 160000 openevse-gui-v2 diff --git a/models/Limit.yaml b/models/Limit.yaml index 7420c574..7cb0a237 100644 --- a/models/Limit.yaml +++ b/models/Limit.yaml @@ -4,9 +4,12 @@ allOf: properties: type: type: string - description: 'Limit type: time, energy, soc, range' + description: 'Limit type: time (minutes), energy (watts/h), soc (%), range (km or miles)' value: type: integer description: 'The limit value' + auto_release: + type: bool + description: 'Release current limit when car is disconnected. (default: true)' x-tags: - Limit diff --git a/openevse-gui-v2 b/openevse-gui-v2 new file mode 160000 index 00000000..3f2c8af5 --- /dev/null +++ b/openevse-gui-v2 @@ -0,0 +1 @@ +Subproject commit 3f2c8af5294e4c4a25140c5229a0c6659ce0626b diff --git a/src/app_config.cpp b/src/app_config.cpp index 5fffaa41..ac6e2d83 100644 --- a/src/app_config.cpp +++ b/src/app_config.cpp @@ -8,6 +8,7 @@ #include "input.h" #include "LedManagerTask.h" #include "current_shaper.h" +#include "limit.h" #include "app_config.h" #include "app_config_mqtt.h" @@ -41,6 +42,10 @@ String www_password; String esp_hostname; String sntp_hostname; +// LIMIT Settings +String limit_default_type; +uint32_t limit_default_value; + // EMONCMS SERVER strings String emoncms_server; String emoncms_node; @@ -132,6 +137,10 @@ ConfigOpt *opts[] = // Time new ConfigOptDefenition(time_zone, "", "time_zone", "tz"), +// Limit + new ConfigOptDefenition(limit_default_type, {}, "limit_default_type", "ldt"), + new ConfigOptDefenition(limit_default_value, {}, "limit_default_value", "ldv"), + // EMONCMS SERVER strings new ConfigOptDefenition(emoncms_server, "https://data.openevse.com/emoncms", "emoncms_server", "es"), new ConfigOptDefenition(emoncms_node, esp_hostname, "emoncms_node", "en"), @@ -287,6 +296,17 @@ void config_changed(String name) } else if(name == "led_brightness") { ledManager.setBrightness(led_brightness); #endif + } else if(name.startsWith("limit_default_")) { + LimitProperties limitprops; + LimitType limitType; + limitType.fromString(limit_default_type.c_str()); + limitprops.setType(limitType); + limitprops.setValue(limit_default_value); + limitprops.setAutoRelease(false); + if (limit.hasLimit()) { + limit.clear(); + } + limit.set(limitprops); } } diff --git a/src/app_config.h b/src/app_config.h index 9648e5cd..a4c2d0a2 100644 --- a/src/app_config.h +++ b/src/app_config.h @@ -28,6 +28,10 @@ extern String esp_hostname; extern String esp_hostname_default; extern String sntp_hostname; +// LIMIT Settings +extern String limit_default_type; +extern uint32_t limit_default_value; + // EMONCMS SERVER strings extern String emoncms_server; extern String emoncms_node; diff --git a/src/limit.cpp b/src/limit.cpp index 21a69a74..4df443e8 100644 --- a/src/limit.cpp +++ b/src/limit.cpp @@ -4,11 +4,7 @@ #include "limit.h" #include "debug.h" -#include -#include -#include -#include "evse_man.h" - +#include "event.h" // --------------------------------------------- // // LimitType Class @@ -71,8 +67,6 @@ LimitProperties::LimitProperties() LimitProperties::~LimitProperties() { - DBUGLN("LimitProperties Destructor"); - }; void LimitProperties::init() @@ -102,6 +96,11 @@ bool LimitProperties::setValue(uint32_t value) return true; }; +bool LimitProperties::setAutoRelease(bool val) { + _auto_release = val; + return true; +} + bool LimitProperties::getAutoRelease() { return _auto_release; }; @@ -139,9 +138,14 @@ bool LimitProperties::serialize(JsonObject &obj) //global instance Limit limit; -Limit::Limit() : Limit::Task() { - _limit_properties.init(); -}; +Limit::Limit() : + MicroTasks::Task(), + _version(0), + _sessionCompleteListener(this) +{ + _limit_properties.init(); +} + Limit::~Limit() { _evse -> release(EvseClient_OpenEVSE_Limit); @@ -154,8 +158,17 @@ void Limit::setup() { void Limit::begin(EvseManager &evse) { // todo get saved default limit DBUGLN("Starting Limit task"); - this -> _evse = &evse; + this -> _evse = &evse; + // retrieve default limit from config + LimitProperties limitprops; + LimitType limittype; + limittype.fromString(limit_default_type.c_str()); + limitprops.setValue(limit_default_value); + // default limits have auto_release set to false + limitprops.setAutoRelease(false); + limit.set(limitprops); MicroTask.startTask(this); + onSessionComplete(&_sessionCompleteListener); }; unsigned long Limit::loop(MicroTasks::WakeReason reason) { @@ -167,7 +180,6 @@ unsigned long Limit::loop(MicroTasks::WakeReason reason) { bool auto_release = _limit_properties.getAutoRelease(); if (_evse->isCharging() ) { - _has_vehicle = true; bool limit_reached = false; switch (type) { case LimitType::Time: @@ -194,19 +206,10 @@ unsigned long Limit::loop(MicroTasks::WakeReason reason) { } } } - - else if ( _has_vehicle && !_evse->isVehicleConnected()) { - _has_vehicle = false; - // if auto release is set, reset Limit properties - if (auto_release) { - _limit_properties.init(); - } - } } - else { if (_evse->clientHasClaim(EvseClient_OpenEVSE_Limit)) { - //remove claim if limit as been deleted + //remove claim if limit has been deleted _evse->release(EvseClient_OpenEVSE_Limit); } } @@ -277,14 +280,38 @@ bool Limit::set(String json) { bool Limit::set(LimitProperties props) { _limit_properties = props; + StaticJsonDocument<32> doc; + doc["limit"] = hasLimit(); + doc["limit_version"] = ++_version; + event_send(doc); return true; }; +LimitProperties Limit::get() { + return _limit_properties; +}; + bool Limit::clear() { _limit_properties.init(); + StaticJsonDocument<32> doc; + doc["limit"] = false; + doc["limit_version"] = ++_version; + event_send(doc); return true; }; -LimitProperties Limit::getLimitProperties() { - return _limit_properties; -}; \ No newline at end of file +uint8_t Limit::getVersion() { + return _version; +} + +void Limit::onSessionComplete(MicroTasks::EventListener *listner) { + _evse -> onSessionComplete(listner); + // disable claim if it has not been deleted already + if (_evse->clientHasClaim(EvseClient_OpenEVSE_Limit)) { + _evse->release(EvseClient_OpenEVSE_Limit); + } + if (_limit_properties.getAutoRelease()){ + clear(); + } + } + \ No newline at end of file diff --git a/src/limit.h b/src/limit.h index 506bb6a1..f4d7c8a3 100644 --- a/src/limit.h +++ b/src/limit.h @@ -45,6 +45,7 @@ class LimitProperties : virtual public JsonSerialize<512> { void init(); bool setType(LimitType type); bool setValue(uint32_t value); + bool setAutoRelease(bool val); LimitType getType(); uint32_t getValue(); bool getAutoRelease(); @@ -61,16 +62,17 @@ class Limit: public MicroTasks::Task private: EvseManager *_evse; LimitProperties _limit_properties; - bool _has_vehicle; + uint8_t _version; + MicroTasks::EventListener _sessionCompleteListener; bool limitTime(uint32_t val); bool limitEnergy(uint32_t val); bool limitSoc(uint32_t val); bool limitRange(uint32_t val); + void onSessionComplete(MicroTasks::EventListener *listner); protected: void setup(); unsigned long loop(MicroTasks::WakeReason reason); - public: Limit(); @@ -79,8 +81,9 @@ class Limit: public MicroTasks::Task bool hasLimit(); bool set(String json); bool set(LimitProperties props); + LimitProperties get(); bool clear(); - LimitProperties getLimitProperties(); + uint8_t getVersion(); }; extern Limit limit; diff --git a/src/web_server.cpp b/src/web_server.cpp index 0cdd61ef..1bc50a23 100644 --- a/src/web_server.cpp +++ b/src/web_server.cpp @@ -252,7 +252,8 @@ void buildStatus(DynamicJsonDocument &doc) { doc["shaper_cur"] = shaper.getMaxCur(); doc["shaper_updated"] = shaper.isUpdated(); doc["service_level"] = static_cast(evse.getActualServiceLevel()); - + doc["limit"] = limit.hasLimit(); + doc["ota_update"] = (int)Update.isRunning(); doc["time"] = String(time); doc["offset"] = String(offset); @@ -262,6 +263,7 @@ void buildStatus(DynamicJsonDocument &doc) { doc["override_version"] = manual.getVersion(); doc["schedule_version"] = scheduler.getVersion(); doc["schedule_plan_version"] = scheduler.getPlanVersion(); + doc["limit_version"] = limit.getVersion(); doc["vehicle_state_update"] = (millis() - evse.getVehicleLastUpdated()) / 1000; if(teslaClient.getVehicleCnt() > 0) { @@ -898,7 +900,7 @@ void handleLimitGet(MongooseHttpServerRequest *request, MongooseHttpServerRespon { if(limit.hasLimit()) { - limit.getLimitProperties().serialize(response); + limit.get().serialize(response); } else { response->setCode(404); response->print("{\"msg\":\"no limit\"}"); From 019c404e20e8567f1dc2a40ed5112d66acd8fcfd Mon Sep 17 00:00:00 2001 From: KipK Date: Sun, 5 Feb 2023 15:19:07 +0100 Subject: [PATCH 2/3] remove guiv2 dir added in previous commit --- openevse-gui-v2 | 1 - 1 file changed, 1 deletion(-) delete mode 160000 openevse-gui-v2 diff --git a/openevse-gui-v2 b/openevse-gui-v2 deleted file mode 160000 index 3f2c8af5..00000000 --- a/openevse-gui-v2 +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 3f2c8af5294e4c4a25140c5229a0c6659ce0626b From bc9f6fbbc67c2bdf86274eeade863d12b157f197 Mon Sep 17 00:00:00 2001 From: KipK Date: Sun, 5 Feb 2023 21:12:07 +0100 Subject: [PATCH 3/3] edited models --- models/Config.yaml | 11 +++++++++++ models/Status.yaml | 6 ++++++ platformio.ini | 2 +- src/app_config.cpp | 8 ++++++-- 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/models/Config.yaml b/models/Config.yaml index 546ff4fb..cf0b6b1a 100644 --- a/models/Config.yaml +++ b/models/Config.yaml @@ -34,6 +34,8 @@ x-examples: hostname: openevse-a7d4 sntp_hostname: pool.ntp.org time_zone: 'Europe/London|GMT0BST,M3.5.0/1,M10.5.0' + limit_default_type: none + limit_default_value": 0 emoncms_server: 'https://emoncms.org' emoncms_node: openevse-a7d4 emoncms_apikey: _DUMMY_PASSWORD @@ -86,6 +88,7 @@ x-examples: ocpp_auth_offline: true mqtt_protocol: mqtt charge_mode: fast + is_threephase: false x-tags: - Config properties: @@ -177,6 +180,12 @@ properties: time_zone: type: string minLength: 1 + limit_default_type: + type: string + minLength: 1 + limit_default_value: + type: integer + minLength: 1 emoncms_server: type: string minLength: 1 @@ -295,3 +304,5 @@ properties: default: 600 description: | The maximum number of seconds to randomly add/subtract from the a scheduled charge start time, eg a value of 600 will adjust the start time my +/- 10 minutes. This is to help prevent large syncrinised loads when multiple verchiles start charging at the same time. + is_threephase: + type: boolean diff --git a/models/Status.yaml b/models/Status.yaml index 9269e8d1..a723121b 100644 --- a/models/Status.yaml +++ b/models/Status.yaml @@ -154,3 +154,9 @@ properties: offset: type: string description: The current timezone + has_limit: + type: boolean + description: A limit has been set + limit_version: + type: integer + description: /limit endpoint current version diff --git a/platformio.ini b/platformio.ini index e320385d..f9d12310 100644 --- a/platformio.ini +++ b/platformio.ini @@ -60,7 +60,7 @@ debug_flags = #-D ENABLE_DEBUG_MQTT #-D ENABLE_DEBUG_EMONCMS #-D ENABLE_DEBUG_TESLA_CLIENT - # -D ENABLE_DEBUG_LIMIT + #-D ENABLE_DEBUG_LIMIT #-D ENABLE_PROFILE src_build_flags = # -D ARDUINOJSON_USE_LONG_LONG diff --git a/src/app_config.cpp b/src/app_config.cpp index ac6e2d83..1dcca688 100644 --- a/src/app_config.cpp +++ b/src/app_config.cpp @@ -303,10 +303,14 @@ void config_changed(String name) limitprops.setType(limitType); limitprops.setValue(limit_default_value); limitprops.setAutoRelease(false); - if (limit.hasLimit()) { + if (limitType == LimitType::None) { + uint32_t val = 0; + config_set("limit_default_value", val); + config_commit(); limit.clear(); } - limit.set(limitprops); + else + limit.set(limitprops); } }