Skip to content

Commit

Permalink
Merge pull request #485 from KipK/auto-release
Browse files Browse the repository at this point in the history
improvment & fixes of MQTT /override topic
  • Loading branch information
jeremypoulter authored Nov 17, 2022
2 parents 67a924a + 71663dd commit 13f7183
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 16 deletions.
16 changes: 8 additions & 8 deletions src/evse_man.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "event_log.h"
#include "divert.h"
#include "current_shaper.h"
#include "manual.h"

static EvseProperties nullProperties;

Expand Down Expand Up @@ -79,6 +80,7 @@ bool EvseProperties::deserialize(JsonObject &obj)

if(obj.containsKey("auto_release")) {
_auto_release = obj["auto_release"];
_has_auto_release = true;
}

return true;
Expand Down Expand Up @@ -266,15 +268,7 @@ bool EvseManager::evaluateClaims(EvseProperties &properties)
DynamicJsonDocument event(capacity);
event["manual_override"] = 1;
event_send(event);
// update /override topic to mqtt
event.clear();
EvseState state = properties.getState();
if(state != EvseState::None) {
properties.serialize(event);
}
else {
event["state"] = "null";
}
mqtt_publish_json(event, "/override");
}
}
Expand Down Expand Up @@ -445,6 +439,12 @@ unsigned long EvseManager::loop(MicroTasks::WakeReason reason)
{
_evaluateTargetState = false;
setTargetState(_targetProperties);

if ( manual.isActive() ) {
// update /override topic to mqtt
mqtt_publish_override();
}

}
return MicroTask.Infinate;
}
Expand Down
11 changes: 11 additions & 0 deletions src/evse_man.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class EvseProperties : virtual public JsonSerialize<512>
uint32_t _energy_limit;
uint32_t _time_limit;
bool _auto_release;
bool _has_auto_release = false;
public:
EvseProperties();
EvseProperties(EvseState state);
Expand Down Expand Up @@ -125,8 +126,14 @@ class EvseProperties : virtual public JsonSerialize<512>
bool isAutoRelease() {
return _auto_release;
}

bool hasAutoRelease() {
return _has_auto_release;
}

void setAutoRelease(bool auto_release) {
_auto_release = auto_release;
_has_auto_release = true;
}

EvseProperties & operator = (EvseProperties &rhs);
Expand Down Expand Up @@ -225,6 +232,10 @@ class EvseManager : public MicroTasks::Task
return _properties.isAutoRelease();
}

bool hasAutoRelease() {
return _properties.hasAutoRelease();
}

EvseProperties &getProperties() {
return _properties;
}
Expand Down
2 changes: 1 addition & 1 deletion src/manual.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ bool ManualOverride::claim()

bool ManualOverride::claim(EvseProperties &props)
{
props.setAutoRelease(true);
if (!props.hasAutoRelease()) props.setAutoRelease(true);
return _evse->claim(EvseClient_OpenEVSE_Manual, EvseManager_Priority_Manual, props);
}

Expand Down
10 changes: 3 additions & 7 deletions src/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,22 +404,18 @@ mqtt_publish_claim() {

void
mqtt_publish_override() {
DBUGLN("MQTT publish_override()");
if(!config_mqtt_enabled() || !mqttclient.connected()) {
return;
}
const size_t capacity = JSON_OBJECT_SIZE(40) + 1024;
DynamicJsonDocument override_data(capacity);
EvseProperties props;
//check if there an override claim
if (evse.clientHasClaim(EvseClient_OpenEVSE_Manual)) {
if (evse.clientHasClaim(EvseClient_OpenEVSE_Manual) || manual.isActive()) {
props = evse.getClaimProperties(EvseClient_OpenEVSE_Manual);
//check if there's state property in override
if(props.getState() != 0) {
props.serialize(override_data);
}
else {
override_data["state"] = "null";
}
props.serialize(override_data);
}
else override_data["state"] = "null";
mqtt_publish_json(override_data, "/override");
Expand Down
2 changes: 2 additions & 0 deletions src/web_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,7 @@ void handleOverrideDelete(MongooseHttpServerRequest *request, MongooseHttpServer
if(manual.release()) {
response->setCode(200);
response->print("{\"msg\":\"Deleted\"}");
mqtt_publish_override(); // update override state to mqtt
} else {
response->setCode(500);
response->print("{\"msg\":\"Failed to release manual overide\"}");
Expand All @@ -931,6 +932,7 @@ void handleOverridePatch(MongooseHttpServerRequest *request, MongooseHttpServerR
{
response->setCode(200);
response->print("{\"msg\":\"Updated\"}");
mqtt_publish_override(); // update override state to mqtt
} else {
response->setCode(500);
response->print("{\"msg\":\"Failed to toggle manual overide\"}");
Expand Down

0 comments on commit 13f7183

Please sign in to comment.