Multiple cleanups.
This commit is contained in:
parent
548617c183
commit
b5d6e50399
|
@ -10,6 +10,8 @@ typedef struct {
|
|||
struct mosquito *mosq;
|
||||
} user_mqtt_impl_t;
|
||||
|
||||
int user_mqtt_impl_init(user_mqtt_impl_t *handle);
|
||||
int user_mqtt_impl_deinit(user_mqtt_impl_t *handle);
|
||||
mqtt_influx_ret_t user_mqtt_get_nsec_timestamp_cb(user_mqtt_impl_t *handle, char *timestamp_string);
|
||||
mqtt_influx_ret_t user_mqtt_publish_message_cb(user_mqtt_impl_t *handle, char *data);
|
||||
|
||||
|
|
|
@ -1,17 +1,37 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "impl/user_mqtt_impl.h"
|
||||
|
||||
#include "utils/user_log_util.h"
|
||||
|
||||
int user_mqtt_impl_init(user_mqtt_impl_t *handle) {
|
||||
int mosq_major, mosq_minor, mosq_revision;
|
||||
mosquitto_lib_init();
|
||||
|
||||
mosquitto_lib_version(&mosq_major, &mosq_minor, &mosq_revision);
|
||||
USER_LOG(USER_LOG_INFO, "libmosquitto version %d.%d rev. %d.", mosq_major, mosq_minor, mosq_revision);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int user_mqtt_impl_deinit(user_mqtt_impl_t *handle) {
|
||||
mosquitto_lib_cleanup();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
mqtt_influx_ret_t user_mqtt_get_nsec_timestamp_cb(user_mqtt_impl_t *handle, char *timestamp_string) {
|
||||
sprintf(timestamp_string, "00000000");
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
uint64_t ts_int = ts.tv_sec * 1e9 + ts.tv_nsec;
|
||||
sprintf(timestamp_string, "%lu", ts_int);
|
||||
return MQTT_INFLUX_OK;
|
||||
}
|
||||
|
||||
mqtt_influx_ret_t user_mqtt_publish_message_cb(user_mqtt_impl_t *handle, char *data) {
|
||||
USER_LOG(USER_LOG_INFO, "INFLUX: %s", data);
|
||||
USER_LOG(USER_LOG_DEBUG, "MQTT message: %s", data);
|
||||
|
||||
return MQTT_INFLUX_OK;
|
||||
}
|
|
@ -19,6 +19,7 @@ static void signal_handler(int signo) {
|
|||
if(signo == SIGINT || signo == SIGTERM) {
|
||||
g_running = 0;
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
USER_LOG(USER_LOG_INFO, "Signal [%s] captured, stopping.", strsignal(signo));
|
||||
}
|
||||
|
||||
|
|
|
@ -61,8 +61,6 @@ void *user_dht_task(void *arguments) {
|
|||
|
||||
while(g_running) {
|
||||
bme280_measure(&bme, &res);
|
||||
USER_LOG(USER_LOG_INFO, "BME: %.02f, %.02f, %.02f", res.temperature,
|
||||
res.humidity, res.pressure);
|
||||
meas.key = "temperature";
|
||||
snprintf(value_buf, 32, "%.02f", res.temperature);
|
||||
meas.value = value_buf;
|
||||
|
|
|
@ -41,6 +41,7 @@ int user_mqtt_task_deinit(void) {
|
|||
}
|
||||
|
||||
void *user_mqtt_task(void *arguments) {
|
||||
user_mqtt_impl_init(&s_mqtt_impl);
|
||||
mqtt_influx_init(&g_mqtt_influx);
|
||||
|
||||
while(g_running) {
|
||||
|
|
|
@ -50,7 +50,6 @@ void *user_tvoc_task(void *arguments) {
|
|||
while(g_running) {
|
||||
ccs811_result_t result;
|
||||
ccs811_measure(&ccs, &result);
|
||||
USER_LOG(USER_LOG_INFO, "CCS: %d, %d", result.eco2, result.tvoc);
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,12 +2,14 @@
|
|||
#include <stdarg.h>
|
||||
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "drivers/user_config_driver.h"
|
||||
|
||||
#include "utils/user_log_util.h"
|
||||
|
||||
static user_log_level_t s_current_level = USER_LOG_DEBUG;
|
||||
static int s_lock = 0;
|
||||
|
||||
void user_log_set_level(user_log_level_t level) {
|
||||
s_current_level = level;
|
||||
|
@ -17,6 +19,12 @@ void user_log_print(user_log_level_t level, char *fmt, ...) {
|
|||
|
||||
if(level < s_current_level) return;
|
||||
|
||||
while(s_lock) {
|
||||
usleep(1000);
|
||||
}
|
||||
|
||||
s_lock = 1;
|
||||
|
||||
char *level_str;
|
||||
switch(level) {
|
||||
case USER_LOG_DEBUG:
|
||||
|
@ -42,10 +50,12 @@ void user_log_print(user_log_level_t level, char *fmt, ...) {
|
|||
va_list args;
|
||||
va_start(args, fmt);
|
||||
|
||||
fprintf(stderr, "[% 9ld]%s ", time(NULL), level_str);
|
||||
fprintf(stderr, "[% 9ld]%s", time(NULL), level_str);
|
||||
vfprintf(stderr, fmt, args);
|
||||
fprintf(stderr, "\n");
|
||||
fflush(stderr);
|
||||
|
||||
va_end(args);
|
||||
|
||||
s_lock = 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue