add: mongoose for connections and mqtt

This commit is contained in:
Tobias Reisinger 2020-06-24 11:41:12 +02:00
parent f5f9be803c
commit 1d1ae61310
14 changed files with 22620 additions and 233 deletions
src/handlers

View file

@ -2,6 +2,7 @@
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <uuid/uuid.h>
#include <logger.h>
#include <models/controller.h>
@ -13,20 +14,35 @@
#include <wiring_debug.h>
void
handler_loop(controller_t *controller)
handler_loop(struct mg_connection *c_mqtt)
{
char topic_buf[100];
char payload_buf[2];
char controller_uid[UUID_STR_LEN];
uuid_unparse(global_controller->id, controller_uid);
time_t timestamp = time(NULL);
struct tm *time_struct = localtime(&timestamp);
LOG_DEBUG("===== IDLE LOOP START =====\n");
for(uint_fast8_t i = 0; i < controller->relay_count; ++i)
for(uint_fast8_t i = 0; i < global_controller->relay_count; ++i)
{
relay_t *relay = controller->relays[i];
relay_t *relay = global_controller->relays[i];
int is_active = 0;
if(relay_is_active(relay, time_struct))
{
LOG_DEBUG("relay %d is active\n", i);
is_active = 1;
}
if(relay->is_on != is_active)
{
sprintf(topic_buf, "controller/%s/relay/%u", controller_uid, i);
sprintf(payload_buf, "%u", is_active);
mg_mqtt_publish(c_mqtt, topic_buf, 0, MG_MQTT_QOS(0), payload_buf, strlen(payload_buf));
}
relay->is_on = is_active;
if(global_config.relay_configs[i].inverted)
{
is_active = !is_active;