Skip to content

Commit 1d7c6d5

Browse files
committed
added /scheduler /scheduler/set {json} to MQTT.
added /scheduler/clear <id> ( remove related scheduler event ) updated doc
1 parent 7e234b8 commit 1d7c6d5

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

docs/mqtt.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,16 @@ Claims & manual override are read accessible here:
3939
Claim & override properties can be set independantly. Sending json with only some fields will update the current claim properties only.
4040
To remove a selected claim/override property, just send "clear" as property parameter ( i.e. `<base-topic>/claim/set {"charge_current": "clear"}` )
4141

42+
Scheduler data:
43+
`<base-topic>/scheduler/` : get scheduler data ([json data]
44+
`<base-topic>/scheduler/set <json data>` : set/update schedules ( data as refered from API :
45+
https://openevse.stoplight.io/docs/openevse-wifi-v4/e87e6f3f90787-batch-update-schedule )
46+
`<base-topic>/scheduler/clear <id> :`remove related event
47+
4248
Main settings:
4349

4450
`<base-topic>/divertmode/set [1 | 2]` : enable (1)/ disable (2) divert mode
51+
`<base-topic>/shaper/set [0 | 1]` : temporary enable (1)/ disable (0) current shaper ( doesn't survive reboot )
4552

4653

4754
MQTT setup is pre-populated with OpenEnergyMonitor [emonPi default MQTT server credentials](https://guide.openenergymonitor.org/technical/credentials/#mqtt).

src/mqtt.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "web_server.h"
1313
#include "event.h"
1414
#include "manual.h"
15+
#include "scheduler.h"
1516

1617

1718
#include "openevse.h"
@@ -159,6 +160,15 @@ void mqttmsg_callback(MongooseString topic, MongooseString payload) {
159160
mqtt_set_claim(false, claim_props);
160161
}
161162
}
163+
164+
//Schedule
165+
else if (topic_string == mqtt_topic + "/schedule/set") {
166+
mqtt_set_schedule(payload_str);
167+
}
168+
else if (topic_string == mqtt_topic + "/schedule/clear") {
169+
mqtt_clear_schedule(payload_str.toInt());
170+
}
171+
162172
else
163173
{
164174
// If MQTT message is RAPI command
@@ -254,6 +264,7 @@ mqtt_connect()
254264
// Publish MQTT override/claim
255265
mqtt_publish_override();
256266
mqtt_publish_claim();
267+
mqtt_publish_schedule();
257268

258269
// MQTT Topic to subscribe to receive RAPI commands via MQTT
259270
String mqtt_sub_topic = mqtt_topic + "/rapi/in/#";
@@ -305,6 +316,11 @@ mqtt_connect()
305316
mqtt_sub_topic = mqtt_topic + "/claim/set";
306317
mqttclient.subscribe(mqtt_sub_topic);
307318

319+
mqtt_sub_topic = mqtt_topic + "/schedule/set";
320+
mqttclient.subscribe(mqtt_sub_topic);
321+
mqtt_sub_topic = mqtt_topic + "/schedule/clear";
322+
mqttclient.subscribe(mqtt_sub_topic);
323+
308324
connecting = false;
309325
});
310326

@@ -393,6 +409,35 @@ mqtt_publish_override() {
393409
mqtt_publish_json(override_data, "/override");
394410
}
395411

412+
void mqtt_set_schedule(String schedule) {
413+
Profile_Start(mqtt_set_schedule);
414+
scheduler.deserialize(schedule);
415+
mqtt_publish_schedule();
416+
Profile_End(mqtt_set_schedule, 5);
417+
}
418+
419+
void
420+
mqtt_clear_schedule(uint32_t event) {
421+
Profile_Start(mqtt_clear_schedule);
422+
scheduler.removeEvent(event);
423+
Profile_End(mqtt_clear_schedule, 5);
424+
mqtt_publish_schedule();
425+
}
426+
427+
void
428+
mqtt_publish_schedule() {
429+
if(!config_mqtt_enabled() || !mqttclient.connected()) {
430+
return;
431+
}
432+
const size_t capacity = JSON_OBJECT_SIZE(40) + 2048;
433+
DynamicJsonDocument schedule_data(capacity);
434+
EvseProperties props;
435+
bool success = scheduler.serialize(schedule_data);
436+
if (success) {
437+
mqtt_publish_json(schedule_data, "/schedule");
438+
}
439+
}
440+
396441
void
397442
mqtt_publish_json(JsonDocument &data, const char* topic) {
398443
Profile_Start(mqtt_publish_json);

src/mqtt.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ extern void mqtt_publish_claim();
3232
extern void mqtt_set_claim(bool override, EvseProperties &props);
3333
extern void mqtt_publish_override();
3434
extern void mqtt_publish_json(JsonDocument &data, const char* topic);
35+
extern void mqtt_publish_schedule();
36+
extern void mqtt_set_schedule(String schedule);
37+
extern void mqtt_clear_schedule(uint32_t event);
38+
3539
// -------------------------------------------------------------------
3640
// Restart the MQTT connection
3741
// -------------------------------------------------------------------

src/scheduler.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "emonesp.h"
99
#include "app_config.h"
1010
#include "event.h"
11+
#include "mqtt.h"
1112

1213
#include <algorithm>
1314
#include <vector>
@@ -365,10 +366,15 @@ void Scheduler::buildSchedule()
365366
doc["schedule_plan_version"] = ++_plan_version;
366367
event_send(doc);
367368

369+
// publish updated schedule to mqtt
370+
mqtt_publish_schedule();
371+
368372
// wake the main task to see if we actually need to do something
369373
MicroTask.wakeTask(this);
370374
}
371375

376+
377+
372378
Scheduler::EventInstance &Scheduler::getCurrentEvent()
373379
{
374380
int currentDay;

0 commit comments

Comments
 (0)