aqhome-react: periodically check for network file changes and reload if necessary.

This commit is contained in:
Martin Preuss
2024-03-24 14:21:20 +01:00
parent e0476924c1
commit 6f9e20095a
6 changed files with 91 additions and 1 deletions

View File

@@ -90,6 +90,21 @@ int AqHomeReact_GetTimeout(const AQHOME_REACT *aqh)
time_t AqHomeReact_GetLatestNetworkFileTime(const AQHOME_REACT *aqh)
{
return aqh?aqh->latestNetworkFileTime:0;
}
void AqHomeReact_SetLatestNetworkFileTime(AQHOME_REACT *aqh, time_t t)
{
if (aqh)
aqh->latestNetworkFileTime=t;
}
AQHREACT_UNIT *AqHomeReact_GetTimerUnit(const AQHOME_REACT *aqh)
{
return aqh?aqh->timerUnit:NULL;

View File

@@ -30,6 +30,10 @@ void AqHomeReact_SetPidFile(AQHOME_REACT *aqh, const char *s);
int AqHomeReact_GetTimeout(const AQHOME_REACT *aqh);
time_t AqHomeReact_GetLatestNetworkFileTime(const AQHOME_REACT *aqh);
void AqHomeReact_SetLatestNetworkFileTime(AQHOME_REACT *aqh, time_t t);
AQHREACT_UNIT *AqHomeReact_GetTimerUnit(const AQHOME_REACT *aqh);
AQHREACT_UNIT *AqHomeReact_GetVarChangeUnit(const AQHOME_REACT *aqh);

View File

@@ -12,6 +12,9 @@
#include "./aqhome_react.h"
#include <time.h>
#define AQHOME_REACT_DEFAULT_PIDFILE "/var/run/aqhome-react.pid"
#define AQHOME_REACT_DEFAULT_DATADIR "/var/lib/aqhome-react"
@@ -21,7 +24,6 @@
struct AQHOME_REACT {
GWEN_MSG_ENDPOINT *brokerEndpoint;
@@ -33,6 +35,8 @@ struct AQHOME_REACT {
AQHREACT_UNIT *varChangeUnit;
AQHREACT_UNIT_LIST *unitList;
AQHREACT_UNIT_NET_LIST *unitNetList;
time_t latestNetworkFileTime;
};

View File

@@ -13,6 +13,7 @@
#include "./init.h"
#include "./fini.h"
#include "./loop.h"
#include "./net_read.h"
#include "aqhome-react/units/u_timer.h"
#include "aqhome/aqhome.h"
@@ -49,6 +50,8 @@
#define FULL_DEBUG
#define AQHOME_REACT_READNET_INTERVAL 60
/* ------------------------------------------------------------------------------------------------
@@ -140,11 +143,13 @@ void _serve(AQHOME_REACT *aqh)
int timeout;
time_t startTime;
time_t lastTimerTime;
time_t lastFileScanTime;
GWEN_DB_NODE *dbArgs;
AQHREACT_UNIT *timerUnit;
startTime=time(NULL);
lastTimerTime=startTime;
lastFileScanTime=startTime;
dbArgs=AqHomeReact_GetDbArgs(aqh);
timerUnit=AqHomeReact_GetTimerUnit(aqh);
@@ -172,6 +177,20 @@ void _serve(AQHOME_REACT *aqh)
AqHomeReact_ProcessAllUnits(aqh);
if ((now-lastFileScanTime)>AQHOME_REACT_READNET_INTERVAL) {
time_t tNew;
time_t t;
DBG_INFO(NULL, "Checking network files");
tNew=AQHREACT_GetNewestUnitNetFiletime(aqh);
t=AqHomeReact_GetLatestNetworkFileTime(aqh);
if (tNew && tNew>t) {
DBG_INFO(NULL, "Reloading network files");
AqHomeReact_ReloadUnitNets(aqh);
AqHomeReact_SetLatestNetworkFileTime(aqh, tNew);
}
}
if (timeout) {
if ((now-startTime)>timeout) {
DBG_ERROR(NULL, "Timeout, stopping service");

View File

@@ -20,6 +20,11 @@
#include <gwenhywfar/text.h>
#include <gwenhywfar/debug.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
/* ------------------------------------------------------------------------------------------------
@@ -41,6 +46,47 @@ static int _readLink(AQHOME_REACT *aqh, AQHREACT_UNIT_NET *unitNet, GWEN_XMLNODE
* ------------------------------------------------------------------------------------------------
*/
time_t AQHREACT_GetNewestUnitNetFiletime(const AQHOME_REACT *aqh)
{
GWEN_STRINGLIST *sl;
time_t resultTime=0;
sl=AQH_GetListOfMatchingDataFiles("aqhome/react/networks", "*.xml");
if (sl) {
GWEN_STRINGLISTENTRY *se;
se=GWEN_StringList_FirstEntry(sl);
while(se) {
const char *s;
s=GWEN_StringListEntry_Data(se);
if (s && *s) {
struct stat sb;
if (stat(s, &sb)==0) {
time_t t;
t=sb.st_mtim.tv_sec;
if (t>resultTime)
resultTime=t;
}
else {
DBG_WARN(NULL, "Error on stat(%s): %s (%d)", s, strerror(errno), errno);
}
}
se=GWEN_StringListEntry_Next(se);
}
}
else {
DBG_ERROR(NULL, "No unit network files");
}
return resultTime;
}
AQHREACT_UNIT_NET_LIST *AQHREACT_ReadUnitNetFiles(AQHOME_REACT *aqh)
{
GWEN_STRINGLIST *sl;

View File

@@ -15,6 +15,8 @@
AQHREACT_UNIT_NET_LIST *AQHREACT_ReadUnitNetFiles(AQHOME_REACT *aqh);
time_t AQHREACT_GetNewestUnitNetFiletime(const AQHOME_REACT *aqh);
#endif