138 lines
4.1 KiB
C
138 lines
4.1 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include "esp_log.h"
|
|
#include "mqtt_client.h"
|
|
#include "cJSON.h"
|
|
#include "esp_random.h"
|
|
#include "esp_timer.h"
|
|
|
|
#include "led_driver.h"
|
|
#include "led_effects.h"
|
|
#include "creditos.h"
|
|
#include "eeprom_animacao.h"
|
|
|
|
static const char *TAG = "MQTT_CMD";
|
|
extern esp_mqtt_client_handle_t mqtt_client;
|
|
|
|
// Estes tópicos vêm do mqtt_handler.c
|
|
extern char topic_resp[];
|
|
|
|
void mqtt_comandos_handle(cJSON *root)
|
|
{
|
|
cJSON *cmd = cJSON_GetObjectItem(root, "cmd");
|
|
if (!cJSON_IsString(cmd)) {
|
|
ESP_LOGW(TAG, "⚠️ Comando inválido (sem 'cmd')");
|
|
return;
|
|
}
|
|
|
|
const char *c = cmd->valuestring;
|
|
ESP_LOGI(TAG, "📩 CMD: %s", c);
|
|
|
|
// ======================================================
|
|
// LED ON / OFF
|
|
// ======================================================
|
|
if (strcmp(c, "LED_ON") == 0) {
|
|
led_all_on(50, 50, 50);
|
|
}
|
|
|
|
else if (strcmp(c, "LED_OFF") == 0) {
|
|
led_all_off();
|
|
}
|
|
|
|
// ======================================================
|
|
// SPIN
|
|
// ======================================================
|
|
else if (strcmp(c, "SPIN") == 0) {
|
|
uint16_t p = esp_random() % led_get_count();
|
|
led_spin_to(p, 2, 12, 80);
|
|
}
|
|
|
|
// ======================================================
|
|
// JACKPOT
|
|
// ======================================================
|
|
else if (strcmp(c, "JACKPOT") == 0) {
|
|
led_jackpot_animation();
|
|
}
|
|
|
|
// ======================================================
|
|
// DAR CRÉDITO
|
|
// ======================================================
|
|
else if (strcmp(c, "CREDITO") == 0) {
|
|
creditos_dar();
|
|
}
|
|
|
|
// ======================================================
|
|
// ESTADO DO SISTEMA
|
|
// ======================================================
|
|
else if (strcmp(c, "STATUS") == 0) {
|
|
char resp[128];
|
|
snprintf(resp, sizeof(resp),
|
|
"{\"status\":\"ok\",\"heap\":%lu,\"anim\":%u}",
|
|
(unsigned long)esp_get_free_heap_size(),
|
|
animacao);
|
|
|
|
esp_mqtt_client_publish(mqtt_client, topic_resp, resp, 0, 1, false);
|
|
}
|
|
|
|
// ======================================================
|
|
// SET ANIMAÇÃO (NVS)
|
|
// ======================================================
|
|
else if (strcmp(c, "SET_ANIM") == 0) {
|
|
cJSON *v = cJSON_GetObjectItem(root, "value");
|
|
if (!cJSON_IsNumber(v)) {
|
|
ESP_LOGW(TAG, "❌ SET_ANIM sem valor numérico");
|
|
return;
|
|
}
|
|
|
|
uint8_t novo = v->valueint;
|
|
animacao_save(novo); // GRAVA NA NVS
|
|
animacao = novo; // atualiza RAM
|
|
|
|
ESP_LOGI(TAG, "🎨 Animação alterada para %u", novo);
|
|
|
|
char resp[64];
|
|
snprintf(resp, sizeof(resp), "{\"anim\":%u}", novo);
|
|
esp_mqtt_client_publish(mqtt_client, topic_resp, resp, 0, 1, false);
|
|
}
|
|
|
|
// ======================================================
|
|
// REBOOT
|
|
// ======================================================
|
|
else if (strcmp(c, "REBOOT") == 0) {
|
|
|
|
ESP_LOGW(TAG, "♻️ Reinício seguro agendado...");
|
|
|
|
// callback C normal
|
|
void reboot_cb(void *arg) {
|
|
esp_restart();
|
|
}
|
|
|
|
// criar timer
|
|
esp_timer_handle_t reboot_timer;
|
|
const esp_timer_create_args_t args = {
|
|
.callback = reboot_cb,
|
|
.arg = NULL,
|
|
.name = "reboot_safe"
|
|
};
|
|
|
|
esp_timer_create(&args, &reboot_timer);
|
|
esp_timer_start_once(reboot_timer, 200000); // 200 ms
|
|
|
|
return; // evita continuar dentro do handler
|
|
}
|
|
|
|
|
|
// ======================================================
|
|
// COMANDO DESCONHECIDO
|
|
// ======================================================
|
|
else {
|
|
ESP_LOGW(TAG, "⚠️ Comando desconhecido: %s", c);
|
|
|
|
char resp[128];
|
|
snprintf(resp, sizeof(resp),
|
|
"{\"error\":\"unknown_cmd\",\"cmd\":\"%s\"}", c);
|
|
|
|
esp_mqtt_client_publish(mqtt_client, topic_resp, resp, 0, 1, false);
|
|
}
|
|
}
|