Skip to content

Commit 764b317

Browse files
authored
Merge pull request #637 from KipK/mqtt
add /limit /limit/set to mqtt
2 parents acbd1fc + 602ff0a commit 764b317

File tree

5 files changed

+57
-8
lines changed

5 files changed

+57
-8
lines changed

docs/mqtt.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ Scheduler data:
4444
`<base-topic>/scheduler/set <json data>` : set/update schedules ( data as refered from API: https://openevse.stoplight.io/docs/openevse-wifi-v4/e87e6f3f90787-batch-update-schedule )
4545
`<base-topic>/scheduler/clear <id> :`remove related event
4646

47+
Limit:
48+
`<base-topic>/limit/` : get limit data ([json data]
49+
`<base-topic>/limit/set <json data>` : set/update limit ( data as refered from API:https://openevse.stoplight.io/docs/openevse-wifi-v4/c410fb5e48294-set-charge-limit )
50+
`<base-topic>/limit/set clear` : clear current limit
51+
4752
Main settings:
4853

4954
`<base-topic>/divertmode/set [1 | 2]` : enable (1)/ disable (2) divert mode

gui-v2

src/limit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class LimitType {
2020
Soc,
2121
Range
2222
};
23-
23+
2424
LimitType() = default;
2525
constexpr LimitType(Value value) : _value(value) { }
2626
uint8_t fromString(const char *value);

src/mqtt.cpp

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
MongooseMqttClient mqttclient;
2626
EvseProperties claim_props;
2727
EvseProperties override_props;
28+
LimitProperties limit_props;
2829
DynamicJsonDocument mqtt_doc(4096);
2930

3031
static long nextMqttReconnectAttempt = 0;
@@ -33,6 +34,7 @@ static bool connecting = false;
3334
uint8_t claimsVersion = 0;
3435
uint8_t overrideVersion = 0;
3536
uint8_t scheduleVersion = 0;
37+
uint8_t limitVersion = 0;
3638

3739
String lastWill = "";
3840

@@ -68,14 +70,13 @@ void mqttmsg_callback(MongooseString topic, MongooseString payload) {
6870
if (shaper.getState()) {
6971
shaper.shapeCurrent();
7072
}
71-
7273
}
7374
else if (topic_string == mqtt_grid_ie)
7475
{
7576
grid_ie = payload_str.toInt();
7677
DBUGF("grid:%dW", grid_ie);
7778
divert.update_state();
78-
79+
7980
// if shaper use the same topic as grid_ie
8081
if (mqtt_live_pwr == mqtt_grid_ie) {
8182
shaper.setLivePwr(grid_ie);
@@ -183,6 +184,16 @@ void mqttmsg_callback(MongooseString topic, MongooseString payload) {
183184
mqtt_clear_schedule(payload_str.toInt());
184185
}
185186

187+
else if (topic_string == mqtt_topic + "/limit/set") {
188+
if (payload_str.equals("clear")) {
189+
DBUGLN("clearing limits");
190+
limit.clear();
191+
}
192+
else if (limit_props.deserialize(payload_str)) {
193+
mqtt_set_limit(limit_props);
194+
}
195+
}
196+
186197
// Restart
187198
else if (topic_string == mqtt_topic + "/restart") {
188199
restart_system();
@@ -292,6 +303,7 @@ mqtt_connect()
292303
mqtt_publish_override();
293304
mqtt_publish_claim();
294305
mqtt_publish_schedule();
306+
mqtt_publish_limit();
295307

296308
// MQTT Topic to subscribe to receive RAPI commands via MQTT
297309
String mqtt_sub_topic = mqtt_topic + "/rapi/in/#";
@@ -366,6 +378,10 @@ mqtt_connect()
366378
mqttclient.subscribe(mqtt_sub_topic);
367379
yield();
368380

381+
mqtt_sub_topic = mqtt_topic + "/limit/set";
382+
mqttclient.subscribe(mqtt_sub_topic);
383+
yield();
384+
369385
// ask for a system restart
370386
mqtt_sub_topic = mqtt_topic + "/restart";
371387
mqttclient.subscribe(mqtt_sub_topic);
@@ -489,6 +505,24 @@ mqtt_publish_schedule() {
489505
}
490506
}
491507

508+
void
509+
mqtt_set_limit(LimitProperties &limitProps) {
510+
Profile_Start(mqtt_set_limit);
511+
limit.set(limitProps);
512+
mqtt_publish_limit();
513+
Profile_End(mqtt_set_limit, 5);
514+
}
515+
516+
void
517+
mqtt_publish_limit() {
518+
LimitProperties limitProps;
519+
const size_t capacity = JSON_OBJECT_SIZE(3) + 512;
520+
DynamicJsonDocument limit_data(capacity);
521+
limitProps = limit.get();
522+
bool success = limitProps.serialize(limit_data);
523+
mqtt_publish_json(limit_data, "/limit");
524+
}
525+
492526
void
493527
mqtt_publish_json(JsonDocument &data, const char* topic) {
494528
Profile_Start(mqtt_publish_json);
@@ -555,6 +589,11 @@ mqtt_loop() {
555589
DBUGF("Schedule has changed, publishing to MQTT");
556590
scheduleVersion = scheduler.getVersion();
557591
}
592+
if (limitVersion != limit.getVersion()) {
593+
mqtt_publish_limit();
594+
DBUGF("Limit has changed, publishing to MQTT");
595+
limitVersion = limit.getVersion();
596+
}
558597
}
559598
Profile_End(mqtt_loop, 5);
560599
}

src/mqtt.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
#include <Arduino.h>
99
#include <ArduinoJson.h>
1010
#include "evse_man.h"
11+
#include "limit.h"
1112

12-
#define MQTT_PROTOCOL_MQTT 0
13-
#define MQTT_PROTOCOL_MQTT_SSL 1
14-
#define MQTT_PROTOCOL_WEBSOCKET 2
15-
#define MQTT_PROTOCOL_WEBSOCKET_SSL 3
13+
enum mqtt_protocol {
14+
MQTT_PROTOCOL_MQTT,
15+
MQTT_PROTOCOL_MQTT_SSL,
16+
MQTT_PROTOCOL_WEBSOCKET,
17+
MQTT_PROTOCOL_WEBSOCKET_SSL
18+
};
1619

1720
#define MQTT_LOOP 500
1821

@@ -37,6 +40,8 @@ extern void mqtt_publish_json(JsonDocument &data, const char* topic);
3740
extern void mqtt_publish_schedule();
3841
extern void mqtt_set_schedule(String schedule);
3942
extern void mqtt_clear_schedule(uint32_t event);
43+
extern void mqtt_publish_limit();
44+
extern void mqtt_set_limit(LimitProperties &limitProps);
4045

4146
// -------------------------------------------------------------------
4247
// Restart the MQTT connection

0 commit comments

Comments
 (0)