wip
This commit is contained in:
parent
942f0cef30
commit
7e04386a2b
2
Makefile
2
Makefile
|
@ -1,4 +1,4 @@
|
|||
SRCS = tic2mqtt.c tic.c mqtt.c homeassistant.c
|
||||
SRCS = tic2mqtt.c tic.c mqtt.c homeassistant.c logger.c
|
||||
|
||||
OBJS = $(SRCS:%.c=%.o)
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include "tic2mqtt.h"
|
||||
#include "mqtt.h"
|
||||
#include "homeassistant.h"
|
||||
#include "mqtt.h"
|
||||
#include "logger.h"
|
||||
|
||||
#include <json-c/json.h>
|
||||
|
||||
|
@ -95,7 +96,7 @@ void ha_config_init()
|
|||
if (mosq_tic) {
|
||||
int res = mqtt_publish(mosq_tic, topic, NULL, payload, TIC_QOS);
|
||||
if (res != 0)
|
||||
logger(LOG_ERROR, "Cannot publish topic %s: %s\n", topic, mqtt_strerror(res));
|
||||
log_error("Cannot publish topic %s: %s\n", topic, mqtt_strerror(res));
|
||||
}
|
||||
json_object_put(obj);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
#include "logger.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
|
||||
static char* log_level_str[] = {
|
||||
[LOG_DEBUG] = "DEBUG",
|
||||
[LOG_INFO] = "INFO",
|
||||
[LOG_WARNING] = "WARNING",
|
||||
[LOG_ERROR] = "ERROR",
|
||||
};
|
||||
|
||||
static int log_level;
|
||||
|
||||
|
||||
void set_loglevel(int level)
|
||||
{
|
||||
log_level = level;
|
||||
}
|
||||
|
||||
void logger(int priority, const char *fmt, ...)
|
||||
{
|
||||
if (priority < log_level)
|
||||
return;
|
||||
|
||||
printf("%s: ", log_level_str[priority]);
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
vprintf(fmt, args);
|
||||
va_end(args);
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
#ifndef __LOGGER_H__
|
||||
#define __LOGGER_H__
|
||||
|
||||
#define LOG_DEBUG 1
|
||||
#define LOG_INFO 2
|
||||
#define LOG_WARNING 3
|
||||
#define LOG_ERROR 4
|
||||
|
||||
#define log_debug(...) logger(LOG_DEBUG, __VA_ARGS__)
|
||||
#define log_info(...) logger(LOG_INFO, __VA_ARGS__)
|
||||
#define log_warning(...) logger(LOG_WARNING, __VA_ARGS__)
|
||||
#define log_error(...) logger(LOG_ERROR, __VA_ARGS__)
|
||||
|
||||
extern void set_loglevel(int level);
|
||||
extern void logger(int priority, const char *fmt, ...);
|
||||
|
||||
#endif
|
15
mqtt.c
15
mqtt.c
|
@ -1,5 +1,6 @@
|
|||
#include "tic2mqtt.h"
|
||||
#include "mqtt.h"
|
||||
#include "logger.h"
|
||||
|
||||
#include <mosquitto.h>
|
||||
|
||||
|
@ -12,11 +13,11 @@ static void mosq_log_callback(struct mosquitto *mosq, void *userdata, int level,
|
|||
{
|
||||
switch (level) {
|
||||
case MOSQ_LOG_WARNING:
|
||||
logger(LOG_WARNING, "%s", str);
|
||||
log_warning("%s", str);
|
||||
break;
|
||||
|
||||
case MOSQ_LOG_ERR:
|
||||
logger(LOG_ERROR, "%s", str);
|
||||
log_error("%s", str);
|
||||
break;
|
||||
|
||||
default:
|
||||
|
@ -34,8 +35,8 @@ struct mosquitto *mqtt_open(const char *host, int port, int keepalive)
|
|||
/* Create MQTT client. */
|
||||
mosq = mosquitto_new(NULL, 1, NULL);
|
||||
if (mosq == NULL) {
|
||||
logger(LOG_ERROR, "Cannot create mosquitto client: %s", strerror(errno));
|
||||
return NULL;
|
||||
log_error("Cannot create mosquitto client: %s", strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mosquitto_log_callback_set(mosq, mosq_log_callback);
|
||||
|
@ -43,14 +44,14 @@ struct mosquitto *mqtt_open(const char *host, int port, int keepalive)
|
|||
/* Connect to broker. */
|
||||
res = mosquitto_connect(mosq, host, port, keepalive);
|
||||
if (res != MOSQ_ERR_SUCCESS) {
|
||||
logger(LOG_ERROR, "Unable to connect to MQTT broker %s:%d: %s", host, port, mosquitto_strerror(res));
|
||||
log_error("Unable to connect to MQTT broker %s:%d: %s", host, port, mosquitto_strerror(res));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Start network loop. */
|
||||
res = mosquitto_loop_start(mosq);
|
||||
if (res != MOSQ_ERR_SUCCESS) {
|
||||
logger(LOG_ERROR, "Unable to start loop: %s", mosquitto_strerror(res));
|
||||
log_error("Unable to start loop: %s", mosquitto_strerror(res));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@ -86,7 +87,7 @@ int mqtt_publish(struct mosquitto *mosq, const char *topic_prefix, const char *t
|
|||
|
||||
res = mosquitto_publish(mosq, NULL, topic, strlen(payload), payload, qos, 1);
|
||||
if (res != 0)
|
||||
logger(LOG_ERROR, "Cannot publish topic %s: %s\n", topic, mosquitto_strerror(res));
|
||||
log_error("Cannot publish topic %s: %s\n", topic, mosquitto_strerror(res));
|
||||
|
||||
return res;
|
||||
}
|
||||
|
|
28
tic.c
28
tic.c
|
@ -1,5 +1,5 @@
|
|||
#include "tic2mqtt.h"
|
||||
#include "tic.h"
|
||||
#include "logger.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -132,16 +132,16 @@ tic_info_t* tic_init(const char *fname)
|
|||
{
|
||||
tic_info_t* tic = calloc(1, sizeof(tic_info_t));
|
||||
if (tic == NULL) {
|
||||
logger(LOG_ERROR, "cannot allocate memory\n");
|
||||
log_error("cannot allocate memory\n");
|
||||
return NULL;
|
||||
}
|
||||
tic->cb_data = NULL;
|
||||
|
||||
logger(LOG_INFO, "opening %s\n", fname);
|
||||
log_info("opening %s\n", fname);
|
||||
|
||||
tic->fd = open(fname, O_RDWR | O_NOCTTY);
|
||||
if (tic->fd < 0) {
|
||||
logger(LOG_ERROR, "Cannot open %s: %s\n", fname, strerror(errno));
|
||||
log_error("Cannot open %s: %s\n", fname, strerror(errno));
|
||||
free(tic);
|
||||
return NULL;
|
||||
}
|
||||
|
@ -151,23 +151,23 @@ tic_info_t* tic_init(const char *fname)
|
|||
if (tic->is_tty) {
|
||||
int ret;
|
||||
|
||||
logger(LOG_DEBUG, "trying standard mode\n");
|
||||
log_debug("trying standard mode\n");
|
||||
tic_init_tty(tic->fd, B9600);
|
||||
|
||||
ret = tic_check_read(tic->fd);
|
||||
if (ret == 0) {
|
||||
tic->mode = TIC_MODE_STD;
|
||||
logger(LOG_INFO, "TIC standard mode detected\n");
|
||||
log_info("TIC standard mode detected\n");
|
||||
} else {
|
||||
logger(LOG_DEBUG, "trying historical mode\n");
|
||||
log_debug("trying historical mode\n");
|
||||
tic_init_tty(tic->fd, B1200);
|
||||
|
||||
ret = tic_check_read(tic->fd);
|
||||
if (ret == 0) {
|
||||
tic->mode = TIC_MODE_HIST;
|
||||
logger(LOG_INFO, "TIC historical mode detected\n");
|
||||
log_info("TIC historical mode detected\n");
|
||||
} else {
|
||||
logger(LOG_ERROR, "no valid TIC stream detected\n");
|
||||
log_error("no valid TIC stream detected\n");
|
||||
free(tic);
|
||||
return NULL;
|
||||
}
|
||||
|
@ -201,14 +201,14 @@ int tic_read_frame(tic_info_t *tic)
|
|||
|
||||
do {
|
||||
if (read(tic->fd, &c, 1) <= 0) {
|
||||
logger(LOG_ERROR, "read error (waiting for STX)\n");
|
||||
log_error("read error (waiting for STX)\n");
|
||||
return -1;
|
||||
}
|
||||
} while (c != TIC_STX);
|
||||
|
||||
for (int i = 0; i < TIC_FRAME_MAX; i++) {
|
||||
if (read(tic->fd, &c, 1) <= 0) {
|
||||
logger(LOG_ERROR, "read error (reading frame)\n");
|
||||
log_error("read error (reading frame)\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -218,7 +218,7 @@ int tic_read_frame(tic_info_t *tic)
|
|||
}
|
||||
}
|
||||
|
||||
logger(LOG_ERROR, "frame too long\n");
|
||||
log_error("frame too long\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -280,7 +280,7 @@ void tic_process_frame(tic_info_t *tic)
|
|||
sum = (sum & 0x3f) + 0x20;
|
||||
|
||||
if (sum != frame[grp_end - 1]) {
|
||||
logger(LOG_WARNING, "checksum error\n");
|
||||
log_warning("checksum error\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -300,7 +300,7 @@ void tic_process_frame(tic_info_t *tic)
|
|||
}
|
||||
}
|
||||
if (nsep > 3 || (nsep == 3 && tic->mode == TIC_MODE_HIST) || nsep < 2) {
|
||||
logger(LOG_WARNING, "bad format group\n");
|
||||
log_warning("bad format group\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
28
tic2mqtt.c
28
tic2mqtt.c
|
@ -1,4 +1,5 @@
|
|||
#include "tic2mqtt.h"
|
||||
#include "logger.h"
|
||||
#include "tic.h"
|
||||
#include "mqtt.h"
|
||||
#include "homeassistant.h"
|
||||
|
@ -80,27 +81,6 @@ static int ha_config = 0;
|
|||
char *tic_name = DEFAULT_TIC_NAME;
|
||||
|
||||
|
||||
static char* log_level_str[] = {
|
||||
[LOG_DEBUG] = "DEBUG",
|
||||
[LOG_INFO] = "INFO",
|
||||
[LOG_WARNING] = "WARNING",
|
||||
[LOG_ERROR] = "ERROR",
|
||||
};
|
||||
|
||||
|
||||
void logger(int priority, const char *fmt, ...)
|
||||
{
|
||||
if (priority < log_level)
|
||||
return;
|
||||
|
||||
printf("%s: ", log_level_str[priority]);
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
vprintf(fmt, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
|
||||
static void tic2mqtt_process_group(const char *tag, const char *data, time_t date)
|
||||
{
|
||||
struct tag_desc *ptag_desc;
|
||||
|
@ -110,7 +90,7 @@ static void tic2mqtt_process_group(const char *tag, const char *data, time_t dat
|
|||
if (ptag_desc->data == NULL) {
|
||||
ptag_desc->data = calloc(1, ptag_desc->len + 1);
|
||||
if (ptag_desc->data == NULL) {
|
||||
logger(LOG_ERROR, "Cannot alloc data for tag %s: %s\n", tag, strerror(errno));
|
||||
log_error("Cannot alloc data for tag %s: %s\n", tag, strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -130,7 +110,7 @@ static void tic2mqtt_process_group(const char *tag, const char *data, time_t dat
|
|||
if (mosq_tic) {
|
||||
int res = mqtt_publish(mosq_tic, topic, NULL, data, TIC_QOS);
|
||||
if (res != 0)
|
||||
logger(LOG_ERROR, "Cannot publish topic %s: %s\n", topic, mqtt_strerror(res));
|
||||
log_error("Cannot publish topic %s: %s\n", topic, mqtt_strerror(res));
|
||||
}
|
||||
}
|
||||
return; // No more processing.
|
||||
|
@ -142,7 +122,7 @@ static void tic2mqtt_process_group(const char *tag, const char *data, time_t dat
|
|||
|
||||
static void sighandler(int signum)
|
||||
{
|
||||
logger(LOG_INFO, "Catch signal #%d (%s)\n", signum, strsignal(signum));
|
||||
log_info("Catch signal #%d (%s)\n", signum, strsignal(signum));
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,15 +1,8 @@
|
|||
#ifndef __TIC2MQTT_H__
|
||||
#define __TIC2MQTT_H__
|
||||
|
||||
#define LOG_DEBUG 1
|
||||
#define LOG_INFO 2
|
||||
#define LOG_WARNING 3
|
||||
#define LOG_ERROR 4
|
||||
|
||||
extern char *tic_name;
|
||||
extern int verbose;
|
||||
extern struct mosquitto *mosq_tic;
|
||||
|
||||
extern void logger(int priority, const char *fmt, ...);
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue