Added basic WebSocket Functionality

This commit is contained in:
2025-07-12 18:14:40 +03:00
parent 516eeab751
commit c1fa1d5e57
27 changed files with 138489 additions and 906 deletions

View File

@@ -16,30 +16,45 @@ JsonDocument handleJSON(char * payload) {
return doc;
}
void replyOnWebSocket(AsyncWebSocketClient *client, String list) {
client->text(list);
}
// Handles the JSON Commands
void handleCommand(JsonDocument json, AsyncWebSocketClient *client = nullptr){
String cmd = json["cmd"];
JsonVariant contents = json["contents"];
if (cmd == "playback") {
player.command(contents);
} else if (cmd == "set_melody") {
player.setMelodyAttributes(contents);
player.loadMelodyInRAM(melody_steps);
} else if (cmd == "list_melodies") {
String list = listFilesAsJson("/melodies");
PublishMqtt(list.c_str());
if (client) {
replyOnWebSocket(client, list); // Only reply via WS if client exists
}
} else if (cmd == "set_relay_timers") {
updateRelayTimings(contents);
} else if (cmd == "add_melody"){
addMelody(contents);
} else {
LOG_WARNING("Unknown Command Received");
}
}
// 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);
String command = String("vesper/") + DEV_ID + "/control";
uint16_t command_id = mqttClient.subscribe(command.c_str(), 2);
LOG_INFO("Subscribing to Command topic, QoS 2, packetId: %d", command_id);
}
@@ -47,42 +62,37 @@ void SuscribeMqtt() {
// 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";
String command = String("vesper/") + DEV_ID + "/control";
// Don't know what this is. Check it out later.
//String payloadContent = String(payload).substring(0, len);
if (String(topic) == topicPlayback){
player.command(payload);
if (String(topic) == command){
JsonDocument json = handleJSON(payload);
handleCommand(json);
}
}
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
}
// Handles incoming WebSocket messages on subscribed topics.
// Could move logic out of this into a dedicated function.
void onWebSocketReceived(AsyncWebSocketClient *client, void *arg, uint8_t *data, size_t len) {
AwsFrameInfo *info = (AwsFrameInfo*)arg;
if (info->final && info->index == 0 && info->len == len && info->opcode == WS_TEXT) {
data[len] = '\0'; // Null-terminate the received data
Serial.printf("Received message: %s\n", (char*)data);
else if (String(topic) == topicAddSchedule) {
updateSchedule(handleJSON(payload));
}
JsonDocument json = handleJSON((char*)data);
handleCommand(json, client);
}
}
else if (String(topic) == topicRelayTimers) {
updateRelayTimings(handleJSON(payload));
}
else {
// Handle unknown topics
LOG_WARNING("Unknown topic received.");
}
void onWebSocketEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) {
if (type == WS_EVT_DATA) {
onWebSocketReceived(client, arg, data, len);
}
}
// Publishes a message on the MQTT server. Message passed as an argument.