Skip to content

Commit

Permalink
add limit_default_type & limit_default_value to .config
Browse files Browse the repository at this point in the history
Added default limits at boot ( UI undone )
Default limit set from /config have auto_release set to false
add "limit_version" event
  • Loading branch information
KipK committed Feb 5, 2023
1 parent 5dd90df commit 68c8041
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 31 deletions.
5 changes: 4 additions & 1 deletion models/Limit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions openevse-gui-v2
Submodule openevse-gui-v2 added at 3f2c8a
20 changes: 20 additions & 0 deletions src/app_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -132,6 +137,10 @@ ConfigOpt *opts[] =
// Time
new ConfigOptDefenition<String>(time_zone, "", "time_zone", "tz"),

// Limit
new ConfigOptDefenition<String>(limit_default_type, {}, "limit_default_type", "ldt"),
new ConfigOptDefenition<uint32_t>(limit_default_value, {}, "limit_default_value", "ldv"),

// EMONCMS SERVER strings
new ConfigOptDefenition<String>(emoncms_server, "https://data.openevse.com/emoncms", "emoncms_server", "es"),
new ConfigOptDefenition<String>(emoncms_node, esp_hostname, "emoncms_node", "en"),
Expand Down Expand Up @@ -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);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/app_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
77 changes: 52 additions & 25 deletions src/limit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@

#include "limit.h"
#include "debug.h"
#include <Arduino.h>
#include <ArduinoJson.h>
#include <MicroTasks.h>
#include "evse_man.h"

#include "event.h"
// ---------------------------------------------
//
// LimitType Class
Expand Down Expand Up @@ -71,8 +67,6 @@ LimitProperties::LimitProperties()

LimitProperties::~LimitProperties()
{
DBUGLN("LimitProperties Destructor");

};

void LimitProperties::init()
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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:
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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;
};
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();
}
}

9 changes: 6 additions & 3 deletions src/limit.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions src/web_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ void buildStatus(DynamicJsonDocument &doc) {
doc["shaper_cur"] = shaper.getMaxCur();
doc["shaper_updated"] = shaper.isUpdated();
doc["service_level"] = static_cast<uint8_t>(evse.getActualServiceLevel());

doc["limit"] = limit.hasLimit();

doc["ota_update"] = (int)Update.isRunning();
doc["time"] = String(time);
doc["offset"] = String(offset);
Expand All @@ -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) {
Expand Down Expand Up @@ -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\"}");
Expand Down

0 comments on commit 68c8041

Please sign in to comment.