Files
project-vesper/vesper/MQTT_Message_Handling.hpp

93 lines
3.3 KiB
C++

#pragma once
void setRelayDurations(JsonDocument& doc);
void updateRelayTimings();
// - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Handles the incoming payload. Returns it into a "JsonDocument" format.
JsonDocument handleJSON(char * payload) {
JsonDocument doc;
DeserializationError error = deserializeJson(doc, payload);
if (error) {
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
}
return doc;
}
// Subscribes to certain topics on the MQTT Server.
void SuscribeMqtt() {
String topicPlayback = String("vesper/") + DEV_ID + "/control/playback";
String topicSetMelody = String("vesper/") + DEV_ID + "/control/setMelody";
String topicAddMelody = String("vesper/") + DEV_ID + "/control/addMelody";
String topicAddSchedule = String("vesper/") + DEV_ID + "/control/addSchedule";
String topicRelayTimers = String("vesper/") + DEV_ID + "/control/settings/relayTimers";
uint16_t control_id = mqttClient.subscribe(topicPlayback.c_str(), 2);
LOG_INFO("Subscribing to Playback Control topic, QoS 2, packetId: %d", control_id);
uint16_t set_melody_id = mqttClient.subscribe(topicSetMelody.c_str(), 2);
LOG_INFO("Subscribing to Set-Melody topic, QoS 2, packetId: %d", set_melody_id);
// doesn't work yet:
uint16_t add_melody_id = mqttClient.subscribe(topicAddMelody.c_str(), 2);
LOG_INFO("Subscribing to Add-Melody topic, QoS 2, packetId: %d", add_melody_id);
uint16_t add_schedule_id = mqttClient.subscribe(topicAddSchedule.c_str(), 2);
LOG_INFO("Subscribing to Add-Schedule topic, QoS 2, packetId: %d", add_schedule_id);
uint16_t relay_timers_id = mqttClient.subscribe(topicRelayTimers.c_str(), 2);
LOG_INFO("Subscribing to Relay-Timers topic, QoS 2, packetId: %d", relay_timers_id);
}
// Handles incoming MQTT messages on subscribed topics.
// Could move logic out of this into a dedicated function.
void OnMqttReceived(char * topic, char * payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
String topicPlayback = String("vesper/") + DEV_ID + "/control/playback";
String topicSetMelody = String("vesper/") + DEV_ID + "/control/setMelody";
String topicAddMelody = String("vesper/") + DEV_ID + "/control/addMelody";
String topicAddSchedule = String("vesper/") + DEV_ID + "/control/addSchedule";
String topicRelayTimers = String("vesper/") + DEV_ID + "/control/settings/relayTimers";
// Don't know what this is. Check it out later.
//String payloadContent = String(payload).substring(0, len);
if (String(topic) == topicPlayback){
player.command(payload);
}
else if (String(topic) == topicSetMelody) {
player.setMelodyAttributes(handleJSON(payload));
player.loadMelodyInRAM(melody_steps);
}
else if (String(topic) == topicAddMelody) {
// Handle adding melody
LOG_INFO("Adding melody...");
// You can call a function here to handle adding the melody
}
else if (String(topic) == topicAddSchedule) {
updateSchedule(handleJSON(payload));
}
else if (String(topic) == topicRelayTimers) {
updateRelayTimings(handleJSON(payload));
}
else {
// Handle unknown topics
LOG_WARNING("Unknown topic received.");
}
}
// Publishes a message on the MQTT server. Message passed as an argument.
void PublishMqtt(const char * data) {
String topicData = String("vesper/") + DEV_ID + "/data";
mqttClient.publish(topicData.c_str(), 0, true, data);
}