2023-07-11 13:27:59 +02:00

179 lines
5.4 KiB
C++

#pragma once
#include <queue>
#include <AsyncMqttClient.h>
#include <ArduinoJson.h>
#include "ha.h"
#include "bmp.h"
#include "display.h"
#define MQTT_HOST IPAddress(192, 168, 5, 11)
#define MQTT_PORT 1883
namespace Mqtt {
void publishConfig();
void publishCommand();
void publishBmp280();
void connect();
void disconnect();
Task tReConnect(5 * TASK_MINUTE, TASK_FOREVER, connect, &ts);
Task tPublishConfig(TASK_IMMEDIATE, TASK_ONCE, publishConfig, &ts);
Task tPublishBmp(5 * TASK_MINUTE, TASK_FOREVER, publishBmp280, &ts);
Task tPublishCommand(TASK_SECOND, TASK_FOREVER, publishCommand, &ts);
AsyncMqttClient client;
struct {
const char* topic = "esp_clock/sensor/ir/value";
std::queue<uint8_t> queue;
uint8_t getCurrent() {
return queue.empty() ? 0 : queue.front();
}
void push(uint8_t el) {
if (client.connected()) queue.push(el);
}
void pop() {
queue.pop();
}
} commands;
const char* espClockTopic = "homeassistant/+/esp_clock/#";
void connect() {
client.connect();
}
void disconnect() {
client.unsubscribe(espClockTopic);
client.disconnect();
}
Ha::Sensor* sensors[] = {
new Ha::TemperatureSensor{"Living room Temperature", "temperature"},
new Ha::PressureSensor{"Living room Pressure", "pressure"},
new Ha::AltitudeSensor{"Living room Altitude", "altitude"}
};
Ha::Command* ledMqtt = (new Ha::Switch{"ESP Clock Led", "led",
[](const char* msg) {
String{ "ON" }.equals(msg) ? digitalWrite(LED_BUILTIN, LOW) : digitalWrite(LED_BUILTIN, HIGH);
}
})->withStateTopic();
Ha::Command* brightnessMqtt = (new Ha::Brightness{ "ESP Clock Brightness", "brightness",
[](const char* msg) {
Display::Brightness::set(String{ msg }.toInt());
}
})->withStateTopic();
Ha::Command* switches[] = {
new Ha::Button{"ESP Clock Restart", "restart",
[](const char* msg) {
if (String { "PRESS" }.equals(msg)) ESP.restart();
}
},
ledMqtt,
new Ha::Switch{"ESP Clock Format 24h", "format_24h",
[](const char* msg) {
if (String{ "ON" }.equals(msg)) {
Display::hourFormat24 = true;
Display::drawTime();
} else {
Display::hourFormat24 = false;
Display::drawTime();
}
}
},
brightnessMqtt
};
uint16_t publish(const char* topic, const char* message) {
return client.publish(topic, 0, true, message);
}
void publishComponentConfig(Ha::Component& component) {
StaticJsonDocument<JSON_SIZE> jsonDoc;
component.buildConfig(jsonDoc);
char message[JSON_SIZE];
serializeJson(jsonDoc, message);
publish(component.configTopic, message);
}
void publishBrightness() {
if (!client.connected()) return ;
char message[32];
sprintf(message, "%u", Display::Brightness::current);
publish(brightnessMqtt->stateTopic, message);
}
void publishConfig() {
for (Ha::Component* cmp : sensors) {
publishComponentConfig(*cmp);
}
for (Ha::Component* cmp : switches) {
publishComponentConfig(*cmp);
}
publishBrightness();
ts.deleteTask(tPublishConfig);
}
void publishBmp280() {
Bmp::data.readAll();
StaticJsonDocument<255> jsonDoc;
jsonDoc["temperature"] = Bmp::data.temp;
jsonDoc["pressure"] = Bmp::data.pressure;
jsonDoc["altitude"] = Bmp::data.altitude;
char message[255];
serializeJson(jsonDoc, message);
publish(Ha::Sensor::stateTopic, message);
}
void publishCommand() {
if (uint8_t cmd = commands.getCurrent()) {
char message[32];
sprintf(message, "%X", cmd);
if (publish(commands.topic, message) != 0) {
Serial.print(cmd, HEX);
Serial.println();
commands.queue.pop();
}
}
}
void onMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
char msg[len + 1];
memcpy(msg, payload, len);
msg[len] = 0;
for (Ha::Command* cmd : switches) {
if (String{ cmd->commandTopic }.equals(topic)) {
cmd->f(msg);
return;
}
}
}
void setup() {
Display::Brightness::onChanged(publishBrightness);
client.onConnect([](bool sessionPresent) {
tPublishConfig.enable();
tPublishBmp.enableIfNot();
tPublishCommand.enableDelayed();
client.subscribe(espClockTopic, 0);
tReConnect.disable();
Serial.println("Connected to MQTT");
});
client.onDisconnect([](AsyncMqttClientDisconnectReason reason) {
tReConnect.enableDelayed();
tPublishCommand.disable();
tPublishBmp.disable();
Serial.println("Disconnected from MQTT");
});
client.onMessage(onMessage);
client.setServer(MQTT_HOST, MQTT_PORT);
Serial.println("Connecting to MQTT...");
}
}