From 2adefc4b7957c82d131452dc83b6d2c59ae57bd0 Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Wed, 20 Sep 2023 17:49:38 +0200 Subject: [PATCH] Decreased verbosity. --- apps/aqhome-data/loop.c | 2 +- apps/aqhome-tool/data/adddata.c | 2 +- apps/aqhome-tool/data/getdatapoints.c | 127 ++++++++++++++++++----- apps/aqhome-tool/data/getlastdatapoint.c | 2 +- apps/aqhome-tool/data/getvalues.c | 2 +- apps/aqhome-tool/data/setdata.c | 2 +- apps/aqhome-tool/main.c | 2 +- apps/aqhome-tool/utils.c | 21 ++++ apps/aqhome-tool/utils.h | 1 + aqhome-tool.sh | 2 +- 10 files changed, 132 insertions(+), 31 deletions(-) diff --git a/apps/aqhome-data/loop.c b/apps/aqhome-data/loop.c index 1e0542a..1734184 100644 --- a/apps/aqhome-data/loop.c +++ b/apps/aqhome-data/loop.c @@ -176,7 +176,7 @@ void _handleIpcMsg(AQHOME_DATA *aqh, GWEN_MSG_ENDPOINT *ep, const GWEN_MSG *msg) /* exec IPC message */ code=GWEN_IpcMsg_GetCode(msg); - DBG_ERROR(AQH_LOGDOMAIN, "Received IPC packet %d (%x)", (int) code, code); + DBG_DEBUG(AQH_LOGDOMAIN, "Received IPC packet %d (%x)", (int) code, code); switch(code) { case AQH_MSGTYPE_IPC_DATA_CONNECT_REQ: AqHomeData_HandleConnect(aqh, ep, msg); break; case AQH_MSGTYPE_IPC_DATA_UPDATEDATA: AqHomeData_HandleUpdateData(aqh, ep, msg); break; diff --git a/apps/aqhome-tool/data/adddata.c b/apps/aqhome-tool/data/adddata.c index c171099..89b9cb9 100644 --- a/apps/aqhome-tool/data/adddata.c +++ b/apps/aqhome-tool/data/adddata.c @@ -239,7 +239,7 @@ int _doAddData(GWEN_DB_NODE *dbArgs) return 1; } - fprintf(stdout, "Sending AddData request\n"); + /*fprintf(stdout, "Sending AddData request\n");*/ epTcp=Utils_OpenConnection(dbArgs, 0, timeoutInSeconds); diff --git a/apps/aqhome-tool/data/getdatapoints.c b/apps/aqhome-tool/data/getdatapoints.c index 9f9538d..efe6b8a 100644 --- a/apps/aqhome-tool/data/getdatapoints.c +++ b/apps/aqhome-tool/data/getdatapoints.c @@ -33,7 +33,8 @@ static int _doGetDataPoints(GWEN_DB_NODE *dbArgs); -static void _sendCommand(GWEN_MSG_ENDPOINT *epTcp, const char *valueName, uint64_t tsBegin, uint64_t tsEnd); +static int _awaitAndPrintResponse(GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds); +static int _awaitAndCalcAndPrintResponse(GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds); static uint64_t _getTimeStampFromString(const char *s); @@ -110,6 +111,17 @@ int AQH_Tool_GetDataPoints(GWEN_DB_NODE *dbGlobalArgs, int argc, char **argv) I18S("Get data up until this timestamp (latest timestamp if omitted)"), I18S("Get data up until this timestamp (latest timestamp if omitted)") }, + { + 0, /* flags */ + GWEN_ArgsType_Int, /* type */ + "printMean", /* name */ + 0, /* minnum */ + 1, /* maxnum */ + "M", /* short option */ + "mean", /* long option */ + I18S("Print mean value of data received"), + I18S("Print mean value of data received") + }, { GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */ GWEN_ArgsType_Char, /* type */ @@ -187,11 +199,14 @@ int _doGetDataPoints(GWEN_DB_NODE *dbArgs) { GWEN_MSG_ENDPOINT *epTcp; int timeoutInSeconds; - GWEN_MSG *msg; const char *valueName; uint64_t tsBegin; uint64_t tsEnd; + GWEN_MSG *msgOut; + int printMean; + int rv; + printMean=GWEN_DB_GetIntValue(dbArgs, "printMean", 0, 0); timeoutInSeconds=GWEN_DB_GetIntValue(dbArgs, "timeout", 0, 5); valueName=GWEN_DB_GetCharValue(dbArgs, "valueName", 0, NULL); tsBegin=_getTimeStampFromString(GWEN_DB_GetCharValue(dbArgs, "tsBegin", 0, NULL)); @@ -211,11 +226,27 @@ int _doGetDataPoints(GWEN_DB_NODE *dbArgs) return 2; } - fprintf(stdout, "Sending GetDataPoints request\n"); + /*fprintf(stdout, "Sending GetDataPoints request\n");*/ - _sendCommand(epTcp, valueName, tsBegin, tsEnd); + msgOut=AQH_GetDataDataIpcMsg_new(AQH_MSGTYPE_IPC_DATA_GETDATA_REQ, valueName, tsBegin, tsEnd); + GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut); + rv=printMean?_awaitAndCalcAndPrintResponse(epTcp, timeoutInSeconds):_awaitAndPrintResponse(epTcp, timeoutInSeconds); + if (rv!=0) { + GWEN_MsgEndpoint_free(epTcp); + return rv; + } + + GWEN_MsgEndpoint_free(epTcp); + return 0; +} + + + +int _awaitAndPrintResponse(GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds) +{ for (;;) { + GWEN_MSG *msg; uint16_t code; msg=Utils_WaitForSpecificIpcMessage(epTcp, AQH_MSGTYPE_IPC_DATA_GETDATA_RSP, timeoutInSeconds); @@ -231,12 +262,59 @@ int _doGetDataPoints(GWEN_DB_NODE *dbArgs) AQH_DataPointsDataIpcMsg_GetUnits(msg)); if (AQH_DataPointsDataIpcMsg_GetFlags(msg) & AQH_MSGDATA_DATAPOINTS_FLAGS_LASTMSG) { DBG_INFO(NULL, "Last message received"); + GWEN_Msg_free(msg); break; } } else { DBG_ERROR(NULL, "Invalid message received"); - GWEN_MsgEndpoint_free(epTcp); + GWEN_Msg_free(msg); + return 3; + } + GWEN_Msg_free(msg); + } + else if (code==AQH_MSGTYPE_IPC_DATA_RESULT) { + uint32_t resultCode; + + resultCode=AQH_ResultIpcMsg_GetResultCode(msg); + fprintf(stderr, "ERROR: %d\n", resultCode); + GWEN_Msg_free(msg); + return 3; + } + else { + DBG_INFO(NULL, "Unexpected message \"%d\"", code); + GWEN_Msg_free(msg); + return 3; + } + } /* for */ + return 0; +} + + + +int _awaitAndCalcAndPrintResponse(GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds) +{ + for (;;) { + GWEN_MSG *msg; + uint16_t code; + + msg=Utils_WaitForSpecificIpcMessage(epTcp, AQH_MSGTYPE_IPC_DATA_GETDATA_RSP, timeoutInSeconds); + if (msg==NULL) { + DBG_ERROR(NULL, "No response received"); + return 2; + } + code=GWEN_IpcMsg_GetCode(msg); + if (code==AQH_MSGTYPE_IPC_DATA_GETDATA_RSP) { + if (AQH_DataPointsDataIpcMsg_IsValid(msg)) { + Utils_PrintMeanData(AQH_DataPointsDataIpcMsg_GetDataPoints(msg), + AQH_DataPointsDataIpcMsg_GetNumValues(msg), + AQH_DataPointsDataIpcMsg_GetUnits(msg)); + GWEN_Msg_free(msg); + break; + } + else { + DBG_ERROR(NULL, "Invalid message received"); + GWEN_Msg_free(msg); return 3; } } @@ -245,42 +323,43 @@ int _doGetDataPoints(GWEN_DB_NODE *dbArgs) resultCode=AQH_ResultIpcMsg_GetResultCode(msg); fprintf(stderr, "ERROR: %d\n", resultCode); - GWEN_MsgEndpoint_free(epTcp); + GWEN_Msg_free(msg); return 3; } else { DBG_INFO(NULL, "Unexpected message \"%d\"", code); - GWEN_MsgEndpoint_free(epTcp); + GWEN_Msg_free(msg); return 3; } } /* for */ - - GWEN_MsgEndpoint_free(epTcp); return 0; } -void _sendCommand(GWEN_MSG_ENDPOINT *epTcp, const char *valueName, uint64_t tsBegin, uint64_t tsEnd) -{ - GWEN_MSG *msgOut; - - msgOut=AQH_GetDataDataIpcMsg_new(AQH_MSGTYPE_IPC_DATA_GETDATA_REQ, valueName, tsBegin, tsEnd); - GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut); -} - - - uint64_t _getTimeStampFromString(const char *s) { if (s && *s) { - unsigned long int x; + if (*s=='-') { + unsigned long int x; + unsigned long int now=time(NULL); - if (1!=sscanf("%lu", s, &x)) { - DBG_ERROR(NULL, "ERROR: Invalid timestamp"); - return (uint64_t) (-1); + s++; + if (1!=sscanf(s, "%lu", &x)) { + DBG_ERROR(NULL, "ERROR: Invalid timestamp"); + return (uint64_t) (-1); + } + return (uint64_t) (now-x); + } + else { + unsigned long int x; + + if (1!=sscanf(s, "%lu", &x)) { + DBG_ERROR(NULL, "ERROR: Invalid timestamp"); + return (uint64_t) (-1); + } + return (uint64_t) x; } - return (uint64_t) x; } return 0; } diff --git a/apps/aqhome-tool/data/getlastdatapoint.c b/apps/aqhome-tool/data/getlastdatapoint.c index 0abbef2..2c78442 100644 --- a/apps/aqhome-tool/data/getlastdatapoint.c +++ b/apps/aqhome-tool/data/getlastdatapoint.c @@ -188,7 +188,7 @@ int _doGetLastDataPoint(GWEN_DB_NODE *dbArgs) return 2; } - fprintf(stdout, "Sending GetLastDataPoint request (%s)\n", valueName); + /*fprintf(stdout, "Sending GetLastDataPoint request (%s)\n", valueName);*/ _sendCommand(epTcp, valueName); diff --git a/apps/aqhome-tool/data/getvalues.c b/apps/aqhome-tool/data/getvalues.c index 65f3d2c..a18d1c6 100644 --- a/apps/aqhome-tool/data/getvalues.c +++ b/apps/aqhome-tool/data/getvalues.c @@ -162,7 +162,7 @@ int _doGetValues(GWEN_DB_NODE *dbArgs) return 2; } - fprintf(stdout, "Sending GetValues request\n"); + /*fprintf(stdout, "Sending GetValues request\n");*/ _sendCommand(epTcp); diff --git a/apps/aqhome-tool/data/setdata.c b/apps/aqhome-tool/data/setdata.c index 6d404c1..40e158f 100644 --- a/apps/aqhome-tool/data/setdata.c +++ b/apps/aqhome-tool/data/setdata.c @@ -212,7 +212,7 @@ int _doSetData(GWEN_DB_NODE *dbArgs) return 1; } - fprintf(stdout, "Sending SetData request\n"); + /*fprintf(stdout, "Sending SetData request\n");*/ epTcp=Utils_OpenConnection(dbArgs, 0, timeoutInSeconds); diff --git a/apps/aqhome-tool/main.c b/apps/aqhome-tool/main.c index f175540..f323116 100644 --- a/apps/aqhome-tool/main.c +++ b/apps/aqhome-tool/main.c @@ -95,7 +95,7 @@ int main(int argc, char **argv) GWEN_Logger_Open(0, "aqhome-tool", 0, GWEN_LoggerType_Console, GWEN_LoggerFacility_User); GWEN_Logger_SetLevel(0, GWEN_LoggerLevel_Notice); - GWEN_Logger_SetLevel(0, GWEN_LoggerLevel_Info); + /*GWEN_Logger_SetLevel(0, GWEN_LoggerLevel_Info);*/ rv=AQH_Init(); if (rv<0) { diff --git a/apps/aqhome-tool/utils.c b/apps/aqhome-tool/utils.c index b15cbc8..287885c 100644 --- a/apps/aqhome-tool/utils.c +++ b/apps/aqhome-tool/utils.c @@ -262,5 +262,26 @@ void Utils_PrintSingleDataPoint(uint64_t timestamp, double data, const char *val +void Utils_PrintMeanData(const uint64_t *dataPoints, uint32_t numValues, const char *valueUnits) +{ + if (numValues) { + uint32_t i; + uint64_t timestamp=0; + double total=0.0; + + for(i=0; i