Skip to content

Commit

Permalink
WIP: Rework mqtt to follow new api
Browse files Browse the repository at this point in the history
mqtt: removed /manual_override/set /pilot/set
mqtt: added /override & /claim + related setter ( /override/set [json] | delete | toggle && /claim/set [json] | delete  )
m
event: publishing new event override containing either json or 0.
  • Loading branch information
KipK committed Jun 7, 2022
1 parent 0e209cd commit d9979dd
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 33 deletions.
20 changes: 18 additions & 2 deletions src/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class InputTask : public MicroTasks::Task
private:
MicroTasks::EventListener _evseState;
MicroTasks::EventListener _evseData;

protected:
void setup()
{
Expand Down Expand Up @@ -66,7 +67,9 @@ class InputTask : public MicroTasks::Task
event["flags"] = evse.getFlags();
event["vehicle"] = evse.isVehicleConnected() ? 1 : 0;
event["colour"] = evse.getStateColour();
event["manual_override"] = manual.isActive() ? 1 : 0;
event["manual_override"] = manual.isActive() ? 1 : 0; //TODO: remove this
//replace "manual_override", should be either 0 or json override claim.
event["override"] = getOverride();
event["session_energy"] = evse.getSessionEnergy();
event_send(event);
}
Expand Down Expand Up @@ -131,12 +134,25 @@ void create_rapi_json(JsonDocument &doc)
doc["flags"] = evse.getFlags();
doc["vehicle"] = evse.isVehicleConnected() ? 1 : 0;
doc["colour"] = evse.getStateColour();
doc["manual_override"] = manual.isActive() ? 1 : 0;
doc["manual_override"] = manual.isActive() ? 1 : 0; //TODO: remove this
//replace "manual_override", should be either 0 or json override claim.
doc["override"] = getOverride();
doc["freeram"] = ESPAL.getFreeHeap();
doc["divertmode"] = divertmode;
doc["srssi"] = WiFi.RSSI();
}

String getOverride() {
String override = "";
if(manual.isActive()) {
const size_t capacity = JSON_OBJECT_SIZE(10) + 1024;
DynamicJsonDocument doc_override(capacity);
evse.serializeClaim(doc_override, EvseClient_OpenEVSE_Manual);
serializeJson(doc_override, override);
} else override = "0";
return override;
}

void
handleRapiRead()
{
Expand Down
1 change: 1 addition & 0 deletions src/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ extern String url;
extern String data;
extern String ohm_hour;

extern String getOverride();
extern void handleRapiRead();
extern void create_rapi_json(JsonDocument &data);

Expand Down
128 changes: 98 additions & 30 deletions src/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
#include "mqtt.h"
#include "app_config.h"
#include "divert.h"
#include "evse_man.h"
#include "input.h"
#include "espal.h"
#include "net_manager.h"
#include "web_server.h"
#include "event.h"
#include "manual.h"


#include "openevse.h"

Expand Down Expand Up @@ -108,42 +111,44 @@ void mqttmsg_callback(MongooseString topic, MongooseString payload) {
divertmode_update(newdivert);
}
}
else if (topic_string == mqtt_topic + "/pilot/set")
{
int newchargecurrent = payload_str.toInt();
DBUGF("Set charge_current: %d", newmode);
//using override, the only interest of this is having priority above everything else.
//We have a confusion in web interface to solve as details of the override are not displayed
EvseProperties props(EvseState::Active);
props.setChargeCurrent(newchargecurrent);
evse.claim(EvseClient_OpenEVSE_MQTT, EvseManager_Priority_MQTT, props);
}
else if (topic_string == mqtt_topic + "/max_current/set")
{
//not using override here, this is a main config change.
int newmaxcurrent = payload_str.toInt();
DBUGF("Set max_current: %d", newmode);
evse.setMaxConfiguredCurrent(newmaxcurrent);
}
else if (topic_string == mqtt_topic + "/manual_override/set")
{
DBUGF("Set evse state: %d", newmode);
if (payload_str.equals("stop")) {
// stop/pause using override
EvseProperties props;
props.setState(EvseState::Disabled);
evse.claim(EvseClient_OpenEVSE_MQTT, EvseManager_Priority_MQTT, props);
}
else if (payload_str.equals("start")) {
// start/unpause using override
EvseProperties props;
props.setState(EvseState::Active);
evse.claim(EvseClient_OpenEVSE_MQTT, EvseManager_Priority_MQTT, props);
else if (topic_string == mqtt_topic + "/override/set") {
EvseProperties props;
if (payload_str.equals("delete")) {
if (manual.release()) {
mqtt_publish_override();
}
}
else if (payload_str.equals("toggle")) {
if (manual.toggle()) {
mqtt_publish_override();
}
}
else if(props.deserialize(payload_str)) {
if (manual.claim(props)) {
mqtt_publish_override();
}

}
}
else if (topic_string == mqtt_topic + "/claim/set") {
EvseProperties props;
if (payload_str.equals("delete")) {
if(evse.release(EvseClient_OpenEVSE_MQTT)) {
mqtt_publish_claim();

}
}
else if (props.deserialize(payload_str)) {
if (evse.claim(EvseClient_OpenEVSE_MQTT, EvseManager_Priority_MQTT, props)) {
mqtt_publish_claim();
}
else if (payload_str.equals("delete")) {
// remove override
evse.release(EvseClient_OpenEVSE_MQTT);
}
}
}
else
{
Expand Down Expand Up @@ -237,6 +242,10 @@ mqtt_connect()
DBUGVAR(announce);
mqttclient.publish(mqtt_announce_topic, announce, true);

// Publish MQTT override/claim
mqtt_publish_override();
mqtt_publish_claim();

// MQTT Topic to subscribe to receive RAPI commands via MQTT
String mqtt_sub_topic = mqtt_topic + "/rapi/in/#";

Expand Down Expand Up @@ -281,6 +290,12 @@ mqtt_connect()
mqtt_sub_topic = mqtt_topic + "/manual_override/set"; // MQTT Topic to set manual_override (start/stop/delete)
mqttclient.subscribe(mqtt_sub_topic);

mqtt_sub_topic = mqtt_topic + "/override/set";
mqttclient.subscribe(mqtt_sub_topic);

mqtt_sub_topic = mqtt_topic + "/claim/set";
mqttclient.subscribe(mqtt_sub_topic);

connecting = false;
});

Expand All @@ -305,13 +320,66 @@ mqtt_publish(JsonDocument &data) {
String topic = mqtt_topic + "/";
topic += kv.key().c_str();
String val = kv.value().as<String>();
mqttclient.publish(topic, val);
mqttclient.publish(topic, val, true);
topic = mqtt_topic + "/";
}

Profile_End(mqtt_publish, 5);
}

void
mqtt_publish_claim() {
if(!config_mqtt_enabled() || !mqttclient.connected()) {
return;
}
bool hasclaim = evse.clientHasClaim(EvseClient_OpenEVSE_MQTT);
const size_t capacity = JSON_OBJECT_SIZE(40) + 1024;
DynamicJsonDocument claimdata(capacity);
if(hasclaim) {
evse.serializeClaim(claimdata, EvseClient_OpenEVSE_MQTT);
mqtt_publish_json(claimdata, "/claim");
}
else {
claimdata["claim"] = 0;
mqtt_publish(claimdata);
}

}

void
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;
bool hasoverride = manual.getProperties(props);
props.serialize(override_data);

if(hasoverride) {
mqtt_publish_json(override_data, "/override");
}
else {
override_data["override"] = 0;
mqtt_publish(override_data);
}
}

void
mqtt_publish_json(JsonDocument &data, const char* topic) {
Profile_Start(mqtt_publish_json);
if(!config_mqtt_enabled() || !mqttclient.connected()) {
return;
}

String fulltopic = mqtt_topic + topic;
String doc;
serializeJson(data, doc);
mqttclient.publish(fulltopic,doc, true);
Profile_End(mqtt_publish_json, 5);

}
// -------------------------------------------------------------------
// MQTT state management
//
Expand Down
4 changes: 3 additions & 1 deletion src/mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ extern void mqtt_loop();
// data: a comma seperated list of name:value pairs to send
// -------------------------------------------------------------------
extern void mqtt_publish(JsonDocument &data);

extern void mqtt_publish_claim();
extern void mqtt_publish_override();
extern void mqtt_publish_json(JsonDocument &data, const char* topic);
// -------------------------------------------------------------------
// Restart the MQTT connection
// -------------------------------------------------------------------
Expand Down

0 comments on commit d9979dd

Please sign in to comment.