Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvment & fixes of MQTT /override topic #485

Merged
merged 3 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not super keen on this, will leave it for now but we need to consider how to make this more generic, I would prefer not to have these special cases in the EVSE manager... We probably need to give the clients text names and event them all or something like that...

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