aqhome-cgi: more code sharing, adding page handling
allows to define your own pages with graphs, sensors and actors.
This commit is contained in:
@@ -26,10 +26,6 @@
|
||||
#include <gwenhywfar/timestamp.h>
|
||||
#include <gwenhywfar/text.h>
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
@@ -42,17 +38,6 @@
|
||||
|
||||
|
||||
|
||||
enum {
|
||||
VALUEGRAPH_PERIOD_4H=1,
|
||||
VALUEGRAPH_PERIOD_1D,
|
||||
VALUEGRAPH_PERIOD_1W,
|
||||
VALUEGRAPH_PERIOD_1M,
|
||||
VALUEGRAPH_PERIOD_6M,
|
||||
VALUEGRAPH_PERIOD_12M,
|
||||
};
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* vars
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
@@ -97,12 +82,8 @@ static void _createGraph(AQH_DATACLIENT *dc,
|
||||
const MY_GRAPH_PARAMS *graphParams,
|
||||
const char *graphTitle, int precision, const char *curveLabel,
|
||||
const char *sImgFile, int imgWidth, int imgHeight, uint64_t numDataPoints);
|
||||
static int _fileIsCurrent(const char *sPath, int seconds);
|
||||
static AQDG_GRAPH *_mkGraphObjectWithTitle(const char *graphTitle, const MY_GRAPH_PARAMS *graphParams, int precision);
|
||||
static void _mkPathForValueAndPeriod(AQH_MODULE *m, const AQH_VALUE *v, const MY_GRAPH_PARAMS *graphParams, GWEN_BUFFER *dbuf);
|
||||
static AQDG_GRAPH_DATAPAIR_LIST *_requestDataPairList(AQH_DATACLIENT *dc, const char *valueName,
|
||||
uint64_t tsBegin, uint64_t tsEnd, uint64_t num);
|
||||
static AQDG_GRAPH_DATAPAIR_LIST *_createDataPairListFromDataPoints(const uint64_t *dataPoints, uint64_t numValues);
|
||||
static const MY_GRAPH_PARAMS *_getParamsByName(const char *s);
|
||||
|
||||
|
||||
@@ -167,11 +148,10 @@ void _runGraphValueWithArgs(AQH_MODULE *m,
|
||||
value=AQH_ModDevices_GetValueForDevice(dc, sDeviceName, sValueName);
|
||||
if (value) {
|
||||
GWEN_BUFFER *fbuf;
|
||||
int rv;
|
||||
|
||||
fbuf=GWEN_Buffer_new(0, 256, 0, 1);
|
||||
_mkPathForValueAndPeriod(m, value, graphParams, fbuf);
|
||||
if (!_fileIsCurrent(GWEN_Buffer_GetStart(fbuf), graphParams->acceptedAgeInSeconds)) {
|
||||
if (!AQH_ModService_FileIsCurrent(GWEN_Buffer_GetStart(fbuf), graphParams->acceptedAgeInSeconds)) {
|
||||
DBG_DEBUG(NULL, "Creating graph");
|
||||
_createGraph(dc,
|
||||
value,
|
||||
@@ -184,23 +164,7 @@ void _runGraphValueWithArgs(AQH_MODULE *m,
|
||||
100000);
|
||||
}
|
||||
|
||||
if (1) {
|
||||
GWEN_BUFFER *ibuf;
|
||||
|
||||
ibuf=GWEN_Buffer_new(0, 1024, 0, 1);
|
||||
// return file
|
||||
rv=GWEN_SyncIo_Helper_ReadFile(GWEN_Buffer_GetStart(fbuf), ibuf);
|
||||
if (rv<0) {
|
||||
DBG_ERROR(NULL, "Error reading \"%s\" (%d)", GWEN_Buffer_GetStart(fbuf), rv);
|
||||
}
|
||||
else {
|
||||
GWEN_Buffer_Reset(dbuf);
|
||||
GWEN_Buffer_AppendBytes(dbuf, GWEN_Buffer_GetStart(ibuf), GWEN_Buffer_GetUsedBytes(ibuf));
|
||||
AQCGI_Request_AddResponseHeaderData(rq, "Content-type: image/png");
|
||||
AQCGI_Request_AddFlags(rq, AQH_MODSERVICE_RQFLAGS_RAWFILE);
|
||||
}
|
||||
GWEN_Buffer_free(ibuf);
|
||||
}
|
||||
AQH_ModService_RespondWithMimeFile(rq, GWEN_Buffer_GetStart(fbuf), "image/png", dbuf);
|
||||
|
||||
GWEN_Buffer_free(fbuf);
|
||||
AQH_Value_free(value);
|
||||
@@ -212,25 +176,6 @@ void _runGraphValueWithArgs(AQH_MODULE *m,
|
||||
|
||||
|
||||
|
||||
int _fileIsCurrent(const char *sPath, int seconds)
|
||||
{
|
||||
struct stat sb;
|
||||
time_t t1;
|
||||
|
||||
if (lstat(sPath, &sb)==-1) {
|
||||
DBG_ERROR(NULL, "Error on lstat(%s): %s (%d)", sPath, strerror(errno), errno);
|
||||
return 0;
|
||||
}
|
||||
t1=time(0);
|
||||
if ((t1-sb.st_mtime)<(time_t) seconds) {
|
||||
DBG_DEBUG(NULL, "File %s is current", sPath);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void _createGraph(AQH_DATACLIENT *dc,
|
||||
const AQH_VALUE *v,
|
||||
@@ -252,7 +197,7 @@ void _createGraph(AQH_DATACLIENT *dc,
|
||||
tsBegin=time(0)-(graphParams->startTimeDiff);
|
||||
g=_mkGraphObjectWithTitle(graphTitle, graphParams, precision);
|
||||
|
||||
dpList=_requestDataPairList(dc, sValue, tsBegin, tsEnd, numDataPoints);
|
||||
dpList=AQH_ModDevices_RequestDataPairList(dc, sValue, tsBegin, tsEnd, numDataPoints);
|
||||
if (dpList) {
|
||||
DBG_DEBUG(NULL, "Adding data for %s", sValue);
|
||||
AQDG_TimeGraph_ModifyDataAndAddCurve(g, curveLabel?curveLabel:sValue, graphParams->modifiers, dpList);
|
||||
@@ -309,63 +254,13 @@ void _mkPathForValueAndPeriod(AQH_MODULE *m, const AQH_VALUE *v, const MY_GRAPH_
|
||||
|
||||
/* var name */
|
||||
s=AQH_Value_GetNameForSystem(v);
|
||||
GWEN_Text_EscapeToBuffer(s, dbuf);
|
||||
AQH_ModService_EscapeToBuffer(s, dbuf);
|
||||
|
||||
GBAA(dbuf, "-%s.png", graphParams->name);
|
||||
}
|
||||
|
||||
|
||||
|
||||
AQDG_GRAPH_DATAPAIR_LIST *_requestDataPairList(AQH_DATACLIENT *dc, const char *valueName,
|
||||
uint64_t tsBegin, uint64_t tsEnd, uint64_t num)
|
||||
{
|
||||
uint64_t *dataPoints;
|
||||
uint64_t recvdNum;
|
||||
|
||||
dataPoints=malloc(num*sizeof(uint64_t)*2);
|
||||
|
||||
recvdNum=AQH_DataClient_GetPeriodData(dc, valueName, dataPoints, num, tsBegin, tsEnd);
|
||||
if (recvdNum>0) {
|
||||
AQDG_GRAPH_DATAPAIR_LIST *dpList;
|
||||
|
||||
dpList=_createDataPairListFromDataPoints(dataPoints, recvdNum);
|
||||
free(dataPoints);
|
||||
return dpList;
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(NULL, "No data received for %s", valueName);
|
||||
free(dataPoints);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
AQDG_GRAPH_DATAPAIR_LIST *_createDataPairListFromDataPoints(const uint64_t *dataPoints, uint64_t numValues)
|
||||
{
|
||||
AQDG_GRAPH_DATAPAIR_LIST *dpList;
|
||||
uint64_t i;
|
||||
|
||||
DBG_DEBUG(NULL, "Got %d datapoints", (int) numValues);
|
||||
dpList=AQDG_Graph_DataPair_List_new();
|
||||
for(i=0; i<numValues; i++) {
|
||||
AQDG_GRAPH_DATAPAIR *dp;
|
||||
double timestamp;
|
||||
union {double f; uint64_t i;} u;
|
||||
|
||||
timestamp=(double)(*(dataPoints++));
|
||||
u.i=*(dataPoints++);
|
||||
dp=AQDG_Graph_DataPair_new();
|
||||
AQDG_Graph_DataPair_SetValueX(dp, timestamp);
|
||||
AQDG_Graph_DataPair_SetValueY(dp, u.f);
|
||||
AQDG_Graph_DataPair_List_Add(dp, dpList);
|
||||
}
|
||||
AQDG_Graph_DataPair_List_SortByValueX(dpList, 1);
|
||||
return dpList;
|
||||
}
|
||||
|
||||
|
||||
|
||||
const MY_GRAPH_PARAMS *_getParamsByName(const char *s)
|
||||
{
|
||||
const MY_GRAPH_PARAMS *p;
|
||||
|
||||
Reference in New Issue
Block a user