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

mqtt interface and system info #107

Merged
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
2 changes: 1 addition & 1 deletion include/schedule.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Schedule {
void setAddress(const uint8_t source);
void setDistance(const uint8_t distance);

void publishRaw(const char *payload);
void publishRaw(const bool enable);
void handleFilter(const char *payload);

void handleSend(const char *payload);
Expand Down
11 changes: 6 additions & 5 deletions include/store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

struct Command {
std::string key; // ebus command as string
std::vector<uint8_t> command; // ebus command as vector ZZ PB SB NN DBx
std::vector<uint8_t> command; // ebus command as vector of "ZZPBSBNNDBx"
std::string unit; // unit of the received data
bool active; // active sending of command
uint32_t interval; // minimum interval between two commands in seconds
Expand All @@ -32,7 +32,7 @@ class Store {

void enqueCommand(const char *payload);
void insertCommand(const char *payload);
void removeCommand(const char *topic);
void removeCommand(const char *payload);

void publishCommands();
const std::string getCommands() const;
Expand All @@ -41,7 +41,8 @@ class Store {

const bool active() const;
Command *nextActiveCommand();
Command *findPassiveCommand(const std::vector<uint8_t> &master);
std::vector<Command *> findPassiveCommands(
const std::vector<uint8_t> &master);

void loadCommands();
void saveCommands() const;
Expand All @@ -68,9 +69,9 @@ class Store {
void deserializeCommands(const char *payload);

static void publishCommand(const std::vector<Command> *commands,
const std::string &key, bool remove);
const std::string &key, const bool remove);

static void publishHomeAssistant(const Command *command, bool remove);
static void publishHomeAssistant(const Command *command, const bool remove);
};

extern Store store;
42 changes: 23 additions & 19 deletions src/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,31 @@ void onMqttConnect(bool sessionPresent) {
// payload: true

#ifdef EBUS_INTERNAL
mqttClient.subscribe("ebus/config/insert/#", 0);
mqttClient.subscribe("ebus/config/insert", 0);
// Insert new command
// topic : ebus/config/insert/NAME_OF_COMMAND
// topic : ebus/config/insert
// payload: ebus command in form of "ZZPBSBNNDBx" for e.g.
// {
// "command": "08b509030d0600",
// "key": "UNIQUE_KEY",
// "command": "fe070009",
// "unit": "°C",
// "active": true,
// "interval": 60,
// "master": false,
// "active": false,
// "interval": 0,
// "master": true,
// "position": 1,
// "datatype": "DATA2c",
// "topic": "Aussentemperatur",
// "datatype": "DATA2b",
// "topic": "outdoor/temperature",
// "ha": true,
// "ha_class": "temperature"
// }

mqttClient.subscribe("ebus/config/remove/#", 0);
mqttClient.subscribe("ebus/config/remove", 0);
// Remove loaded command
// topic : ebus/config/remove/NAME_OF_COMMAND
// payload: true
// topic : ebus/config/remove
// payload: UNIQUE_KEY of ebus command
// {
// "key": "UNIQUE_KEY"
// }

mqttClient.subscribe("ebus/config/list", 0);
// Publish loaded commands
Expand All @@ -43,15 +47,15 @@ void onMqttConnect(bool sessionPresent) {
mqttClient.subscribe("ebus/config/raw", 0);
// Enable/disable the raw data printout
// topic : ebus/config/raw
// payload: true or false
// payload: true

mqttClient.subscribe("ebus/config/filter", 0);
// Insert raw data filter
// topic : ebus/config/filter
// payload: array of sequences for e.g.
// [
// "0700",
// "b509"
// "fe"
// ]

mqttClient.subscribe("ebus/config/load", 0);
Expand All @@ -74,8 +78,8 @@ void onMqttConnect(bool sessionPresent) {
// topic : ebus/config/send
// payload: array of ebus command(s) in form of "ZZPBSBNNDBx" for e.g.
// [
// "08070400",
// "08b509030d0600"
// "05070400",
// "15070400"
// ]
#endif
}
Expand All @@ -94,14 +98,14 @@ void onMqttMessage(const char *topic, const char *payload,
if (String(payload).equalsIgnoreCase("true")) reset();
}
#ifdef EBUS_INTERNAL
else if (tmp.startsWith("ebus/config/insert/")) {
if (tmp.equals("ebus/config/insert")) {
if (String(payload).length() > 0) store.enqueCommand(payload);
} else if (tmp.startsWith("ebus/config/remove/")) {
if (String(payload).equalsIgnoreCase("true")) store.removeCommand(topic);
} else if (tmp.equals("ebus/config/remove")) {
if (String(payload).length() > 0) store.removeCommand(payload);
} else if (tmp.equals("ebus/config/list")) {
if (String(payload).equalsIgnoreCase("true")) store.publishCommands();
} else if (tmp.equals("ebus/config/raw")) {
schedule.publishRaw(payload);
schedule.publishRaw(String(payload).equalsIgnoreCase("true"));
} else if (tmp.equals("ebus/config/filter")) {
if (String(payload).length() > 0) schedule.handleFilter(payload);
} else if (tmp.equals("ebus/config/load")) {
Expand Down
25 changes: 8 additions & 17 deletions src/schedule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <ArduinoJson.h>

#include <sstream>

#include "bus.hpp"
#include "mqtt.hpp"

Expand Down Expand Up @@ -69,18 +71,7 @@ void Schedule::setDistance(const uint8_t distance) {
distanceCommands = distance * 1000;
}

void Schedule::publishRaw(const char *payload) {
JsonDocument doc;
DeserializationError error = deserializeJson(doc, payload);

if (error) {
std::string err = "DeserializationError ";
err += error.c_str();
mqttClient.publish("ebus/config/error", 0, false, err.c_str());
} else {
raw = doc.as<bool>();
}
}
void Schedule::publishRaw(const bool enable) { raw = enable; }

void Schedule::handleFilter(const char *payload) {
JsonDocument doc;
Expand Down Expand Up @@ -276,13 +267,13 @@ void Schedule::processPassive(const std::vector<uint8_t> &master,
}
}

Command *pasCommand = store.findPassiveCommand(master);
if (pasCommand != nullptr) {
if (pasCommand->master)
publishValue(pasCommand,
std::vector<Command *> pasCommands = store.findPassiveCommands(master);
for (Command *command : pasCommands) {
if (command->master)
publishValue(command,
ebus::Sequence::range(master, 4, master.size() - 4));
else
publishValue(pasCommand, slave);
publishValue(command, slave);
}
}

Expand Down
Loading
Loading