Skip to content

Commit

Permalink
Merge pull request #20 from destroyedlolo/Dev
Browse files Browse the repository at this point in the history
Add notification
  • Loading branch information
destroyedlolo authored Dec 31, 2023
2 parents 40b5d01 + bbfe19e commit 2ba98f6
Show file tree
Hide file tree
Showing 54 changed files with 323 additions and 171 deletions.
11 changes: 6 additions & 5 deletions Modules/Marcel/MQTT_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,20 @@
#include <MQTTClient.h>


/* Compare 2 strings like strcmp() but s can contain MQTT wildcards
/**
* @brief Compare 2 strings like strcmp() but s can contain MQTT wildcards
* '#' : replace remaining of the line
* '+' : a sub topic and must be enclosed by '/'
*
*
* Wildcards are checked as per mosquitto's source code rules
* (comment in http://git.eclipse.org/c/mosquitto/org.eclipse.mosquitto.git/tree/src/subs.c)
*
* -> pointer to the remaining of the topic.
* @param s string to be compared
* @param t topic to be compared to
* @param remain pointer to the remaining of the topic.
* If the topic ends by a '#', if set, points to the what the '"' wildcard replaces.
* <- 0 : strings match
* <- -1 : wildcard error
* <- others : strings are different
* @return 0 : strings match, -1 : wildcard error, others : strings are different
*/
extern int mqtttokcmp(register const char *s, register const char *t, const char **remain);

Expand Down
6 changes: 4 additions & 2 deletions Modules/Marcel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ gotoall: all

#The compiler (may be customized for compiler's options).
cc=cc
opts=-Wall -O2 -fPIC $(shell pkg-config --libs lua ) $(shell pkg-config --libs json-c ) -lcurl -DPLUGIN_DIR='"/usr/local/lib/Marcel"' -L/usr/local/lib/Marcel -L/home/laurent/Projets/Marcel -lpaho-mqtt3c -lm -ldl -Wl,--export-dynamic -lpthread
opts=-Wall -O2 -fPIC -DDEBUG -DMCHECK="mcheck(NULL)" $(shell pkg-config --libs lua ) $(shell pkg-config --libs json-c ) -lcurl -DPLUGIN_DIR='"/home/laurent/Projets/Marcel"' -L/home/laurent/Projets/Marcel -L/home/laurent/Projets/Marcel -lpaho-mqtt3c -lm -ldl -Wl,--export-dynamic -lpthread

CURL_helpers.o : CURL_helpers.c CURL_helpers.h Marcel.h Makefile
$(cc) -c -o CURL_helpers.o CURL_helpers.c $(opts)
Expand All @@ -24,7 +24,9 @@ Marcel.o : Marcel.c Marcel.h Version.h Module.h Section.h mod_core.h \
Module.o : Module.c Module.h Marcel.h Section.h Makefile
$(cc) -c -o Module.o Module.c $(opts)

Section.o : Section.c Section.h Marcel.h Makefile
Section.o : Section.c Section.h Marcel.h Module.h \
../mod_alert/mod_alert.h ../Marcel/Module.h ../Marcel/Section.h \
../Marcel/DList.h ../mod_Lua/mod_Lua.h MQTT_tools.h Makefile
$(cc) -c -o Section.o Section.c $(opts)

helpers.o : helpers.c Marcel.h MQTT_tools.h Makefile
Expand Down
55 changes: 55 additions & 0 deletions Modules/Marcel/Section.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
*/

#include "Section.h"
#include "Module.h"
#include "../mod_alert/mod_alert.h"
#include "MQTT_tools.h"

#include <assert.h>

Expand Down Expand Up @@ -37,6 +40,57 @@ struct Section *findSectionByName(const char *name){
return NULL; /* Not found */
}


/**
* @brief Enable or disable a section
*
* @param section section to manage
* @param status new status of this section (true = enabled)
*/
void SectionOnOff(struct Section *s, bool v){
s->disabled = !v;

publishSectionStatus(s);
}

/**
* @brief set error status of a section
*
* @param section section to manage
* @param status new error status
*/
void SectionError(struct Section *s, bool v){
bool previous = s->inerror;
s->inerror = v;

if(s->inerror != previous){
publishSectionStatus(s);

uint8_t mod_alertID = findModuleByName("mod_alert");
if(mod_alertID != (uint8_t)-1)
((struct module_alert *)modules[mod_alertID])->sentAlertsCounter();
}
}

/**
* @brief Publish the status of a section
*
* @param section section to publish
*/
void publishSectionStatus(struct Section *s){
char ttopic[ strlen(cfg.ClientID) + 9 + strlen(s->uid) ]; /* + "/Change/" */
sprintf(ttopic, "%s/Change/%s", cfg.ClientID, s->uid);

char t[4];
sprintf(t, "%c,%c", s->disabled ? '0':'1', s->inerror ? '1':'0');

mqttpublish(cfg.client, ttopic, 3, t, false);

#ifdef DEBUG
publishLog('d', "\"%s\" status is \"%s\"", s->uid, t);
#endif
}

/**
* @brief Initialize mandatory (only) field of a structure
*
Expand All @@ -58,6 +112,7 @@ void initSection( struct Section *section, int8_t module_id, uint8_t section_id,
section->h = chksum(name);

section->thread = 0;
section->inerror = false;
section->disabled = false;
section->immediate = false;
section->quiet = false;
Expand Down
7 changes: 6 additions & 1 deletion Modules/Marcel/Section.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ struct Section {
int h; /* hash code for this id */
pthread_t thread; /* Child to handle this section */

bool inerror; /* This section is in error */

/* options */
bool disabled; /* this section is currently disabled */

Expand Down Expand Up @@ -54,5 +56,8 @@ struct Section {
extern struct Section *sections;

extern struct Section *findSectionByName(const char *name);
extern void initSection( struct Section *sec, int8_t module_id, uint8_t section_id, const char *name, const char *kind);
extern void initSection(struct Section *sec, int8_t module_id, uint8_t section_id, const char *name, const char *kind);
extern void SectionOnOff(struct Section *, bool);
extern void SectionError(struct Section *, bool);
extern void publishSectionStatus(struct Section *);
#endif
3 changes: 2 additions & 1 deletion Modules/Marcel/Version.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@
* 14/10/2023 - LF - v8.01 - Improve Lua support
* - expose section status
* 27/12/2023 - LF - v8.02 - expose NamedNotification status
* 30/12/2023 - LF - v8.03 - Notify when a status changes
*/

#ifndef MARCEL_VERSION_H
#define MARCEL_VERSION_H

#define MARCEL_VERSION "8.0202" /* Need to stay numerique as exposed to Lua
#define MARCEL_VERSION "8.0302" /* Need to stay numerique as exposed to Lua
* VV.SSMM :
* VV - Version
* SS - SubVersion
Expand Down
10 changes: 5 additions & 5 deletions Modules/mod_1wire/1WAlarm.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static void processProbe( struct section_1wAlarm *s){

if(!(f = fopen( s->common.file, "r" ))){ /* probe is not reachable */
char *emsg = strerror(errno);
s->common.inerror = true;
SectionError((struct Section *)s, true);

publishLog('E', "[%s] %s : %s", s->common.section.uid, s->common.file, emsg);

Expand Down Expand Up @@ -84,10 +84,9 @@ static void processProbe( struct section_1wAlarm *s){
} else {
if(!fgets(l, MAXLINE, f)){
publishLog('E', "[%s] : %s -> Unable to read a float value.", s->common.section.uid, s->common.file);
s->common.inerror = true;
SectionError((struct Section *)s, true);
} else {
bool publish = true;
s->common.inerror = false;

#ifdef LUA
if(mod_Lua && s->common.section.funcid != LUA_REFNIL){
Expand All @@ -106,6 +105,7 @@ static void processProbe( struct section_1wAlarm *s){
}
#endif

SectionError((struct Section *)s, false);
if(publish){
publishLog('T', "[%s] -> %s", s->common.section.uid, l);
mqttpublish(cfg.client, s->common.section.topic, strlen(l), l, s->common.section.retained );
Expand All @@ -128,12 +128,12 @@ void *scanAlertDir(void *amod){
struct dirent *de;
DIR *d = opendir( mod_1wire->OwAlarm );
if( !d ){
mod_1wire->alerm_in_error = true;
mod_1wire->alarm_in_error = true;
publishLog(mod_1wire->OwAlarmKeep ? 'E' : 'F', "[1-wire Alarm] : %s", strerror(errno));
if(!mod_1wire->OwAlarmKeep)
pthread_exit(0);
} else {
mod_1wire->alerm_in_error = false;
mod_1wire->alarm_in_error = false;
while(( de = readdir(d) )){
if( de->d_type == 4 && *de->d_name != '.' ){ /* 4 : directory */
publishLog('T', "%s : in alert", de->d_name);
Expand Down
22 changes: 13 additions & 9 deletions Modules/mod_1wire/FFV.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,23 @@

void *processFFV(void *actx){
struct section_FFV *s = (struct section_FFV *)actx;
s->common.inerror = true; /* in case of issue during the initialisation */

/* Sanity checks */
if(!s->common.section.topic){
publishLog('F', "[%s] Topic must be set. Dying ...", s->common.section.uid);
SectionError((struct Section *)s, true);
pthread_exit(0);
}

if(!s->common.section.sample){
publishLog('E', "[%s] Sample time can't be 0. Dying ...", s->common.section.uid);
publishLog('F', "[%s] Sample time can't be 0. Dying ...", s->common.section.uid);
SectionError((struct Section *)s, true);
pthread_exit(0);
}

if(!s->common.file){
publishLog('E', "[%s] File must be set. Dying ...", s->common.section.uid);
publishLog('F', "[%s] File must be set. Dying ...", s->common.section.uid);
SectionError((struct Section *)s, true);
pthread_exit(0);
}

Expand All @@ -46,20 +48,22 @@ void *processFFV(void *actx){
if(s->common.section.funcname){ /* if an user function defined ? */
if( (s->common.section.funcid = mod_Lua->findUserFunc(s->common.section.funcname)) == LUA_REFNIL ){
publishLog('E', "[%s] configuration error : user function \"%s\" is not defined. This thread is dying.", s->common.section.uid, s->common.section.funcname);
SectionError((struct Section *)s, true);
pthread_exit(NULL);
}
}

if(s->common.failfunc){ /* if an user function defined ? */
if( (s->common.failfuncid = mod_Lua->findUserFunc(s->common.failfunc)) == LUA_REFNIL ){
publishLog('E', "[%s] configuration error : fail function \"%s\" is not defined. This thread is dying.", s->common.section.uid, s->common.failfunc);
SectionError((struct Section *)s, true);
pthread_exit(NULL);
}
}
}
#endif

s->common.inerror = false; /* Initialisation completed */
SectionError((struct Section *)s, false); /* Initialisation completed */

for(bool first=true;; first=false){ /* Infinite publishing loop */
if(s->common.section.disabled){
Expand All @@ -73,7 +77,7 @@ void *processFFV(void *actx){

if(!(f = fopen( s->common.file, "r" ))){ /* probe is not reachable */
char *emsg = strerror(errno);
s->common.inerror = true;
SectionError((struct Section *)s, true);

publishLog('E', "[%s] %s : %s", s->common.section.uid, s->common.file, emsg);

Expand Down Expand Up @@ -122,15 +126,14 @@ void *processFFV(void *actx){
float val;
if(!fscanf(f, "%f", &val)){
publishLog('E', "[%s] : %s -> Unable to read a float value.", s->common.section.uid, s->common.file);
s->common.inerror = true;
SectionError((struct Section *)s, true);
} else { /* Only to normalize the response */
bool publish = true;
s->common.inerror = false;
float compensated = val + s->offset;

if(s->safe85 && val == 85.0){
publishLog('E', "[%s] The probe replied 85° implying powering issue.", s->common.section.uid);
s->common.inerror = true;
SectionError((struct Section *)s, true);
} else {
#ifdef LUA
if(s->common.section.funcid != LUA_REFNIL){
Expand All @@ -141,7 +144,7 @@ void *processFFV(void *actx){
mod_Lua->pushNumber( val );
mod_Lua->pushNumber( compensated );
if(mod_Lua->exec(4, 1)){
s->common.inerror = true;
SectionError((struct Section *)s, true);
publishLog('E', "[%s] FFV : %s", s->common.section.uid, mod_Lua->getStringFromStack(-1));
mod_Lua->pop(1); /* pop error message from the stack */
mod_Lua->pop(1); /* pop NIL from the stack */
Expand All @@ -151,6 +154,7 @@ void *processFFV(void *actx){
}
#endif

SectionError((struct Section *)s, false);
if(publish){
publishLog('T', "[%s] -> %f", s->common.section.uid, compensated);
sprintf(l,"%.1f", compensated);
Expand Down
2 changes: 1 addition & 1 deletion Modules/mod_1wire/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ gotoall: all

#The compiler (may be customized for compiler's options).
cc=cc
opts=-Wall -O2 -fPIC $(shell pkg-config --cflags lua ) -DLUA
opts=-Wall -O2 -fPIC $(shell pkg-config --cflags lua ) -DLUA -DDEBUG -DMCHECK="mcheck(NULL)"

1WAlarm.o : 1WAlarm.c mod_1wire.h ../Marcel/Module.h \
../Marcel/Section.h ../mod_Lua/mod_Lua.h ../Marcel/MQTT_tools.h \
Expand Down
10 changes: 4 additions & 6 deletions Modules/mod_1wire/mod_1wire.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static int publishCustomFiguresFFV(struct Section *asection){
lua_rawset(mod_Lua->L, -3); /* Add it in the table */

lua_pushstring(mod_Lua->L, "Error state"); /* Push the index */
lua_pushboolean(mod_Lua->L, s->common.inerror); /* the value */
lua_pushboolean(mod_Lua->L, s->common.section.inerror); /* the value */
lua_rawset(mod_Lua->L, -3); /* Add it in the table */

return 1;
Expand Down Expand Up @@ -154,7 +154,6 @@ static enum RC_readconf readconf(uint8_t mid, const char *l, struct Section **se
nsection->common.section.sample = mod_1wire.defaultsampletime;
nsection->common.file = NULL;
nsection->common.failfunc = NULL;
nsection->common.inerror = false;
nsection->offset = 0.0;
nsection->safe85 = false;

Expand All @@ -175,15 +174,14 @@ static enum RC_readconf readconf(uint8_t mid, const char *l, struct Section **se
nsection->common.section.publishCustomFigures = publishCustomFigures1WAlrm;
nsection->common.file = NULL;
nsection->common.failfunc = NULL;
nsection->common.inerror = false;
nsection->initfunc = NULL;
nsection->latch = NULL;

if(cfg.verbose) /* Be verbose if requested */
publishLog('C', "\tEntering 1-wire Alarm section '%s' (%04x)", nsection->common.section.uid, nsection->common.section.id);

mod_1wire.alarm_in_use = false;
mod_1wire.alerm_in_error = false;
mod_1wire.alarm_in_error = false;
*section = (struct Section *)nsection; /* we're now in a section */
return ACCEPTED;
} else if(*section){
Expand Down Expand Up @@ -296,7 +294,7 @@ static int s1_inError(lua_State *L){
s = luaL_testudata(L, 1, "1WAlarm");
luaL_argcheck(L, s != NULL, 1, "'FFV' expected");

lua_pushboolean(L, (*s)->inerror);
lua_pushboolean(L, (*s)->section.inerror);
return 1;
}

Expand All @@ -306,7 +304,7 @@ static const struct luaL_Reg s1M[] = {
};

static int m1_inError(lua_State *L){
lua_pushboolean(L, mod_1wire.alerm_in_error);
lua_pushboolean(L, mod_1wire.alarm_in_error);
return 1;
}

Expand Down
3 changes: 1 addition & 2 deletions Modules/mod_1wire/mod_1wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct module_1wire {

/* Alarm handling */
bool alarm_in_use; /* At last an alarm is defined */
bool alerm_in_error; /* Error while trying to read alarm */
bool alarm_in_error; /* Error while trying to read alarm */
const char *OwAlarm; /* 1w alarm directory */
float OwAlarmSample; /* Delay b/w 2 sample on alarm directory */
bool OwAlarmKeep; /* Alarm thread doesn't die in case of error */
Expand All @@ -46,7 +46,6 @@ struct OwCommon {
const char *file; /* File containing the data to read */
const char *failfunc; /* User function to call on data arrival */
int failfuncid; /* Function id in Lua registry */
bool inerror; /* Last attempt failed */
};

/* Float value exposed as a file
Expand Down
2 changes: 1 addition & 1 deletion Modules/mod_Lua/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ gotoall: all

#The compiler (may be customized for compiler's options).
cc=cc
opts=-Wall -O2 -fPIC $(shell pkg-config --cflags lua ) -DLUA $(shell pkg-config --libs lua )
opts=-Wall -O2 -fPIC $(shell pkg-config --cflags lua ) -DLUA -DDEBUG -DMCHECK="mcheck(NULL)" $(shell pkg-config --libs lua )

exposedFunction.o : exposedFunction.c mod_Lua.h ../Marcel/Module.h \
../Marcel/Section.h ../Marcel/Version.h ../Marcel/MQTT_tools.h \
Expand Down
2 changes: 1 addition & 1 deletion Modules/mod_Lua/mod_Lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ static int exposeObjMethods( lua_State *L, const char *name, const struct luaL_R
}


#if LUA_VERSION_NAUM <= 501
#if LUA_VERSION_NUM <= 501
/* from lua-compat library */
static void *testudata(lua_State *L, int i, const char *tname){
void *p = lua_touserdata(L, i);
Expand Down
2 changes: 1 addition & 1 deletion Modules/mod_OnOff/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ gotoall: all

#The compiler (may be customized for compiler's options).
cc=cc
opts=-Wall -O2 -fPIC $(shell pkg-config --cflags lua ) -DLUA
opts=-Wall -O2 -fPIC $(shell pkg-config --cflags lua ) -DLUA -DDEBUG -DMCHECK="mcheck(NULL)"

mod_OnOff.o : mod_OnOff.c mod_OnOff.h ../Marcel/Module.h \
../Marcel/Section.h ../mod_alert/mod_alert.h ../Marcel/DList.h \
Expand Down
Loading

0 comments on commit 2ba98f6

Please sign in to comment.