Decreased verbosity.

This commit is contained in:
Martin Preuss
2023-09-20 17:49:38 +02:00
parent f03c078606
commit 2adefc4b79
10 changed files with 132 additions and 31 deletions

View File

@@ -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;
}