mg_sonoff/src/main.c

59 lines
1.6 KiB
C

#include <stdio.h>
#include <time.h>
#include "common/platform.h"
#include "common/cs_file.h"
#include "mgos_app.h"
#include "mgos_gpio.h"
#include "mgos_sys_config.h"
#include "mgos_timers.h"
#include "mgos_hal.h"
#include "mgos_dlsym.h"
#include "mgos_mqtt.h"
#include "mjs.h"
#include "ledHandler.h"
bool mqtt_conn_flag = false;
int mqtt_connected(void) {
return (int) mqtt_conn_flag;
}
//static void pub(struct mg_connection *c, const char *fmt, ...) {
//char msg[200];
//struct json_out jmo = JSON_OUT_BUF(msg, sizeof(msg));
//va_list ap;
//int n;
//va_start(ap, fmt);
//n = json_vprintf(&jmo, fmt, ap);
//va_end(ap);
//mg_mqtt_publish(c, get_cfg()->mqtt.pub, 0, MG_MQTT_QOS(0), msg, n);
//LOG(LL_INFO, ("%s -> %s", get_cfg()->mqtt.pub, msg));
//}
static void mqtt_ev_handler(struct mg_connection *c, int ev, void *p, void *user_data) {
struct mg_mqtt_message *msg = (struct mg_mqtt_message *) p;
if (ev == MG_EV_MQTT_CONNACK) {
LOG(LL_INFO, ("MQTT connected: %d", msg->connack_ret_code));
mqtt_conn_flag = true;
//if (get_cfg()->mqtt.pub == NULL) {
//LOG(LL_ERROR, ("Run 'mos config-set mqtt.pub=... '"));
//} else {
//pub(c, "{timestamp:%.3lf, mem_free:%d}", mg_time(), mgos_get_free_heap_size() ); /* post uptime */
//}
} else if (ev == MG_EV_CLOSE) {
mqtt_conn_flag = false;
}
(void) user_data;
(void) c;
}
enum mgos_app_init_result mgos_app_init(void) {
init_led_handler();
mgos_mqtt_add_global_handler(mqtt_ev_handler, NULL);
LOG(LL_DEBUG, ("SONOFF app initialized"));
return MGOS_APP_INIT_SUCCESS;
}