Skip to content

Commit

Permalink
Merge branch 'limits' of github.com:KipK/ESP32_WiFi_V4.x into pr/KipK…
Browse files Browse the repository at this point in the history
…/535
  • Loading branch information
jeremypoulter committed Feb 5, 2023
2 parents 2ef3b87 + bc9f6fb commit f8a265d
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 18 deletions.
11 changes: 11 additions & 0 deletions models/Config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -86,6 +88,7 @@ x-examples:
ocpp_auth_offline: true
mqtt_protocol: mqtt
charge_mode: fast
is_threephase: false
x-tags:
- Config
properties:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
6 changes: 6 additions & 0 deletions models/Status.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 24 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,21 @@ 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 (limitType == LimitType::None) {
uint32_t val = 0;
config_set("limit_default_value", val);
config_commit();
limit.clear();
}
else
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
39 changes: 24 additions & 15 deletions src/limit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,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 @@ -154,11 +159,16 @@ void Limit::begin(EvseManager &evse) {
// todo get saved default limit
DBUGLN("Starting Limit task");
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);
StaticJsonDocument<16> doc;
doc["limit_version"] = _version;
event_send(doc);
onSessionComplete(&_sessionCompleteListener);
};

unsigned long Limit::loop(MicroTasks::WakeReason reason) {
Expand Down Expand Up @@ -270,15 +280,21 @@ bool Limit::set(String json) {

bool Limit::set(LimitProperties props) {
_limit_properties = props;
StaticJsonDocument<16> doc;
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<16> doc;
StaticJsonDocument<32> doc;
doc["limit"] = false;
doc["limit_version"] = ++_version;
event_send(doc);
return true;
Expand All @@ -295,14 +311,7 @@ void Limit::onSessionComplete(MicroTasks::EventListener *listner) {
_evse->release(EvseClient_OpenEVSE_Limit);
}
if (_limit_properties.getAutoRelease()){
_limit_properties.init();
++_version;
StaticJsonDocument<16> doc;
doc["limit_version"] = ++_version;
event_send(doc);
clear();
}
}

LimitProperties Limit::getLimitProperties() {
return _limit_properties;
};

3 changes: 2 additions & 1 deletion 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 Down Expand Up @@ -80,8 +81,8 @@ class Limit: public MicroTasks::Task
bool hasLimit();
bool set(String json);
bool set(LimitProperties props);
LimitProperties get();
bool clear();
LimitProperties getLimitProperties();
uint8_t getVersion();
};

Expand Down
5 changes: 3 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 Down Expand Up @@ -899,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 f8a265d

Please sign in to comment.