From 62abfd56e9ad83a0a259b6013657cd8dbce95561 Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Sat, 21 Oct 2023 02:19:16 +0200 Subject: [PATCH] Added functionality to print a value difference. Also call GWEN_MsgEndpoint_IoLoop() only after checking all messages in the endpoint's list. --- apps/aqhome-tool/data/getdatapoints.c | 52 ++++++++++++++++++++------- apps/aqhome-tool/utils.c | 43 +++++++++++++++++++--- apps/aqhome-tool/utils.h | 1 + 3 files changed, 80 insertions(+), 16 deletions(-) diff --git a/apps/aqhome-tool/data/getdatapoints.c b/apps/aqhome-tool/data/getdatapoints.c index 0df7be5..7599091 100644 --- a/apps/aqhome-tool/data/getdatapoints.c +++ b/apps/aqhome-tool/data/getdatapoints.c @@ -28,6 +28,7 @@ #include #include +#include #define I18S(msg) msg @@ -35,8 +36,8 @@ static int _doGetDataPoints(GWEN_DB_NODE *dbArgs); -static int _awaitAndCalcAndPrintResponse(GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds, int printMean); -static void _handleDataResponse(GWEN_MSG *msg, int printMean); +static int _awaitAndCalcAndPrintResponse(GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds, int printMean, int printDiff); +static void _handleDataResponse(GWEN_MSG *msg, int printMean, int printDiff); static uint64_t _getTimeStampFromString(const char *s); @@ -135,6 +136,17 @@ int AQH_Tool_GetDataPoints(GWEN_DB_NODE *dbGlobalArgs, int argc, char **argv) I18S("Print mean value of data received"), I18S("Print mean value of data received") }, + { + 0, /* flags */ + GWEN_ArgsType_Int, /* type */ + "printDiff", /* name */ + 0, /* minnum */ + 1, /* maxnum */ + "D", /* short option */ + "diff", /* long option */ + I18S("Print difference between last and first datapoint received"), + I18S("Print difference between last and first datapoint received") + }, { GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */ GWEN_ArgsType_Char, /* type */ @@ -220,9 +232,11 @@ int _doGetDataPoints(GWEN_DB_NODE *dbArgs) uint64_t num; GWEN_MSG *msgOut; int printMean; + int printDiff; int rv; printMean=GWEN_DB_GetIntValue(dbArgs, "printMean", 0, 0); + printDiff=GWEN_DB_GetIntValue(dbArgs, "printDiff", 0, 0); timeoutInSeconds=GWEN_DB_GetIntValue(dbArgs, "timeout", 0, 5); valueName=GWEN_DB_GetCharValue(dbArgs, "valueName", 0, NULL); num=GWEN_DB_GetIntValue(dbArgs, "numOfLastDatapoints", 0, 0); @@ -247,7 +261,7 @@ int _doGetDataPoints(GWEN_DB_NODE *dbArgs) msgOut=AQH_GetDataDataIpcMsg_new(AQH_MSGTYPE_IPC_DATA_GETDATA_REQ, valueName, tsBegin, tsEnd, num); GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut); - rv=_awaitAndCalcAndPrintResponse(epTcp, timeoutInSeconds, printMean?1:0); + rv=_awaitAndCalcAndPrintResponse(epTcp, timeoutInSeconds, printMean?1:0, printDiff?1:0); if (rv!=0) { GWEN_MsgEndpoint_free(epTcp); return rv; @@ -259,7 +273,7 @@ int _doGetDataPoints(GWEN_DB_NODE *dbArgs) -int _awaitAndCalcAndPrintResponse(GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds, int printMean) +int _awaitAndCalcAndPrintResponse(GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds, int printMean, int printDiff) { for (;;) { GWEN_MSG *msg; @@ -272,7 +286,7 @@ int _awaitAndCalcAndPrintResponse(GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds } code=GWEN_IpcMsg_GetCode(msg); if (code==AQH_MSGTYPE_IPC_DATA_GETDATA_RSP) { - _handleDataResponse(msg, printMean); + _handleDataResponse(msg, printMean, printDiff); GWEN_Msg_free(msg); return 0; } @@ -295,7 +309,7 @@ int _awaitAndCalcAndPrintResponse(GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds -void _handleDataResponse(GWEN_MSG *msg, int printMean) +void _handleDataResponse(GWEN_MSG *msg, int printMean, int printDiff) { AQH_VALUE *value; const GWEN_TAG16 *tag; @@ -313,6 +327,8 @@ void _handleDataResponse(GWEN_MSG *msg, int printMean) if (numberOfPoints>0 && dataPoints) { if (printMean) Utils_PrintMeanData(dataPoints, numberOfPoints, valueUnits); + else if (printDiff) + Utils_PrintDiffData(dataPoints, numberOfPoints, valueUnits); else Utils_PrintDataPoints(dataPoints, numberOfPoints, valueUnits); } @@ -326,15 +342,27 @@ uint64_t _getTimeStampFromString(const char *s) { if (s && *s) { if (*s=='-') { - unsigned long int x; - unsigned long int now=time(NULL); + uint64_t x=0; + uint64_t now=time(NULL); s++; - if (1!=sscanf(s, "%lu", &x)) { - DBG_ERROR(NULL, "ERROR: Invalid timestamp"); - return (uint64_t) (-1); + while(*s && isdigit(*s)) { + unsigned int i; + + i=*(s++)-'0'; + x*=10; + x+=i; } - return (uint64_t) (now-x); + if (*s) { + switch(*s) { + case 0: + case 'm': x*=60; break; + case 'h': x*=(60*60); break; + case 'd': x*=(60*60*24); break; + default: break; + } + } + return (now-x); } else { unsigned long int x; diff --git a/apps/aqhome-tool/utils.c b/apps/aqhome-tool/utils.c index 07e74d7..b4f7cc0 100644 --- a/apps/aqhome-tool/utils.c +++ b/apps/aqhome-tool/utils.c @@ -137,9 +137,7 @@ GWEN_MSG *Utils_WaitForSpecificNodeMessage(GWEN_MSG_ENDPOINT *epTcp, GWEN_MSG *msg; time_t now; - GWEN_MsgEndpoint_IoLoop(epTcp, 2000); /* 2000 ms */ - msg=GWEN_MsgEndpoint_TakeFirstReceivedMessage(epTcp); - if (msg) { + while( (msg=GWEN_MsgEndpoint_TakeFirstReceivedMessage(epTcp)) ) { if (GWEN_IpcMsg_GetCode(msg)==AQH_MSGTYPE_IPC_NODES_FORWARD) { GWEN_MSG *nodeMsg; @@ -160,12 +158,13 @@ GWEN_MSG *Utils_WaitForSpecificNodeMessage(GWEN_MSG_ENDPOINT *epTcp, DBG_INFO(NULL, "Received IPC message %d, ignoring", GWEN_IpcMsg_GetCode(msg)); } GWEN_Msg_free(msg); - } + } /* while */ now=time(NULL); if (now-startTime>timeoutInSeconds) { DBG_INFO(NULL, "Timeout"); break; } + GWEN_MsgEndpoint_IoLoop(epTcp, 2000); /* 2000 ms */ } return NULL; @@ -415,6 +414,42 @@ void Utils_PrintMeanData(const uint64_t *dataPoints, uint32_t numValues, const c +void Utils_PrintDiffData(const uint64_t *dataPoints, uint32_t numValues, const char *valueUnits) +{ + if (numValues) { + if (numValues>1) { + uint64_t timestamp=0; + double valueFirst=0.0; + double valueLast=0.0; + double valueDiff=0.0; + union {double f; uint64_t i;} u; + + /* ignore timestamp of first datapoint */ + u.i=dataPoints[1]; + valueFirst=u.f; + + timestamp=dataPoints[(numValues-1)*2]; + u.i=dataPoints[((numValues-1)*2)+1]; + valueLast=u.f; + + valueDiff=valueLast-valueFirst; + + Utils_PrintSingleDataPoint(timestamp, valueDiff, valueUnits); + } + else { + uint64_t timestamp; + union {double f; uint64_t i;} u; + + timestamp=dataPoints[0]; + u.i=dataPoints[1]; + + Utils_PrintSingleDataPoint(timestamp, u.f, valueUnits); + } + } +} + + + void Utils_PrintDevice(const AQH_DEVICE *device, int printHeader) { GWEN_TIMESTAMP *ts; diff --git a/apps/aqhome-tool/utils.h b/apps/aqhome-tool/utils.h index d737963..e1451b4 100644 --- a/apps/aqhome-tool/utils.h +++ b/apps/aqhome-tool/utils.h @@ -40,6 +40,7 @@ GWEN_MSG_ENDPOINT *Utils_OpenBrokerConnection(GWEN_DB_NODE *dbArgs, uint32_t fla void Utils_PrintDataPoints(const uint64_t *dataPoints, uint32_t numValues, const char *valueUnits); void Utils_PrintSingleDataPoint(uint64_t timestamp, double data, const char *valueUnits); void Utils_PrintMeanData(const uint64_t *dataPoints, uint32_t numValues, const char *valueUnits); +void Utils_PrintDiffData(const uint64_t *dataPoints, uint32_t numValues, const char *valueUnits); void Utils_PrintFormattedSingleDataPoint(const AQH_VALUE *v, uint64_t timestamp, double data, const char *tmpl); void Utils_PrintDevice(const AQH_DEVICE *device, int printHeader);