diff --git a/apps/aqhome-react/aqhome_react.c b/apps/aqhome-react/aqhome_react.c index 82b4a86..baf0695 100644 --- a/apps/aqhome-react/aqhome_react.c +++ b/apps/aqhome-react/aqhome_react.c @@ -25,6 +25,8 @@ #include "aqhome-react/units/u_timeprogram.h" #include "aqhome-react/units/u_statfns.h" +#include "aqhome/data/vars_dbwrite.h" + #include #include @@ -295,6 +297,22 @@ int AqHomeReact_DecIntValue(AQHOME_REACT *aqh, const char *path, int startValue, +int AqHomeReact_WriteVarsFile(AQHOME_REACT *aqh) +{ + if (aqh && aqh->localVars && aqh->varsFile) { + int rv; + + rv=AQH_Vars_WriteDbFile(aqh->localVars, aqh->varsFile); + if (rv<0) { + DBG_INFO(NULL, "here (%d)", rv); + return rv; + } + } + return 0; +} + + + AQHREACT_UNIT *AqHomeReact_CreateUnitByName(AQHOME_REACT *aqh, const char *unitType) { /* this does not include u_timer and u_varchanges, because those are only created once globally in init.c */ diff --git a/apps/aqhome-react/aqhome_react.h b/apps/aqhome-react/aqhome_react.h index c15608a..66085e5 100644 --- a/apps/aqhome-react/aqhome_react.h +++ b/apps/aqhome-react/aqhome_react.h @@ -60,6 +60,7 @@ int AqHomeReact_GetIntValue(AQHOME_REACT *aqh, const char *path, int idx, int de int AqHomeReact_IncIntValue(AQHOME_REACT *aqh, const char *path, int startValue, int defaultValue); int AqHomeReact_DecIntValue(AQHOME_REACT *aqh, const char *path, int startValue, int defaultValue); +int AqHomeReact_WriteVarsFile(AQHOME_REACT *aqh); #endif diff --git a/apps/aqhome-react/main.c b/apps/aqhome-react/main.c index b325cce..18e7558 100644 --- a/apps/aqhome-react/main.c +++ b/apps/aqhome-react/main.c @@ -57,7 +57,7 @@ #define FULL_DEBUG #define AQHOME_REACT_READNET_INTERVAL 300 - +#define AQHOME_REACT_SAVE_INTERVAL 300 /* ------------------------------------------------------------------------------------------------ @@ -166,12 +166,14 @@ void _serve(AQHOME_REACT *aqh) time_t startTime; time_t lastTimerTime; time_t lastFileScanTime; + time_t lastSaveTime; GWEN_DB_NODE *dbArgs; AQHREACT_UNIT *timerUnit; startTime=time(NULL); lastTimerTime=startTime; lastFileScanTime=startTime; + lastSaveTime=startTime; AqHomeReact_SetLatestNetworkFileTime(aqh, AQHomeReact_GetNewestUnitNetFiletime()); @@ -216,6 +218,15 @@ void _serve(AQHOME_REACT *aqh) lastFileScanTime=now; } + if (now-lastSaveTime>AQHOME_REACT_SAVE_INTERVAL) { + DBG_ERROR(NULL, "Writing var file"); + rv=AqHomeReact_WriteVarsFile(aqh); + if (rv<0) { + DBG_INFO(NULL, "Error writing runtime data"); + } + lastSaveTime=time(NULL); + } + if (timeout) { if ((now-startTime)>timeout) { DBG_ERROR(NULL, "Timeout, stopping service"); diff --git a/aqhome/data/vars_dbwrite.c b/aqhome/data/vars_dbwrite.c index 519e0d8..79b4af0 100644 --- a/aqhome/data/vars_dbwrite.c +++ b/aqhome/data/vars_dbwrite.c @@ -13,14 +13,17 @@ #include "./vars_p.h" -#include "./vars_dbread.h" +#include "./vars_dbwrite.h" #include "aqhome/data/path.h" #include #include +#include #include #include +#include +#include @@ -52,6 +55,48 @@ static void _appendEscapedString(const char *s, GWEN_BUFFER *dbuf); +int AQH_Vars_WriteDbFile(const AQH_VARS *vt, const char *filename) +{ + int rv; + GWEN_BUFFER *fbuf; + GWEN_BUFFER *dbuf; + + fbuf=GWEN_Buffer_new(0, 256, 0, 1); + GWEN_Buffer_AppendString(fbuf, filename); + GWEN_Buffer_AppendString(fbuf, ".tmp"); + + dbuf=GWEN_Buffer_new(0, 256, 0, 1); + rv=AQH_Vars_WriteDbFormat(vt, dbuf); + if (rv<0) { + DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv); + GWEN_Buffer_free(dbuf); + GWEN_Buffer_free(fbuf); + return rv; + } + + rv=GWEN_SyncIo_Helper_WriteFile(GWEN_Buffer_GetStart(fbuf), + (const uint8_t*) GWEN_Buffer_GetStart(dbuf), + GWEN_Buffer_GetUsedBytes(dbuf)); + if (rv<0) { + DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv); + GWEN_Buffer_free(dbuf); + GWEN_Buffer_free(fbuf); + return rv; + } + GWEN_Buffer_free(dbuf); + + if (rename(GWEN_Buffer_GetStart(fbuf), filename)) { + DBG_INFO(AQH_LOGDOMAIN, "here (%d: %s)", errno, strerror(errno)); + GWEN_Buffer_free(fbuf); + return GWEN_ERROR_IO; + } + GWEN_Buffer_free(fbuf); + + return 0; +} + + + int AQH_Vars_WriteDbFormat(const AQH_VARS *vt, GWEN_BUFFER *dbuf) { if (vt && dbuf) { diff --git a/aqhome/data/vars_dbwrite.h b/aqhome/data/vars_dbwrite.h index 1ff0d35..baff360 100644 --- a/aqhome/data/vars_dbwrite.h +++ b/aqhome/data/vars_dbwrite.h @@ -14,6 +14,7 @@ #include +AQHOME_API int AQH_Vars_WriteDbFile(const AQH_VARS *vt, const char *filename); AQHOME_API int AQH_Vars_WriteDbFormat(const AQH_VARS *vt, GWEN_BUFFER *dbuf);