aqhome-tool: added command to watch values changed on the server.

This commit is contained in:
Martin Preuss
2023-10-04 23:33:40 +02:00
parent f1753eeea7
commit b66f3d2ef4
8 changed files with 401 additions and 69 deletions

View File

@@ -16,9 +16,10 @@
#include "aqhome/aqhome.h"
#include "aqhome/msg/msg_node.h"
#include "aqhome/ipc/msg_ipc_result.h"
#include "aqhome/ipc/data/msg_data_datapoints.h"
#include "aqhome/ipc/data/msg_data_multidata.h"
#include "aqhome/ipc/data/msg_data_getdata.h"
#include "aqhome/ipc/data/ipc_data.h"
#include "aqhome/ipc/msg_ipc_tag16.h"
#include <gwenhywfar/args.h>
#include <gwenhywfar/i18n.h>
@@ -34,8 +35,8 @@
static int _doGetDataPoints(GWEN_DB_NODE *dbArgs);
static int _awaitAndPrintResponse(GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds);
static int _awaitAndCalcAndPrintResponse(GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds);
static int _awaitAndCalcAndPrintResponse(GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds, int printMean);
static void _handleDataResponse(GWEN_MSG *msg, int printMean);
static uint64_t _getTimeStampFromString(const char *s);
@@ -234,7 +235,7 @@ int _doGetDataPoints(GWEN_DB_NODE *dbArgs)
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);
rv=_awaitAndCalcAndPrintResponse(epTcp, timeoutInSeconds, printMean?1:0);
if (rv!=0) {
GWEN_MsgEndpoint_free(epTcp);
return rv;
@@ -246,7 +247,7 @@ int _doGetDataPoints(GWEN_DB_NODE *dbArgs)
int _awaitAndPrintResponse(GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds)
int _awaitAndCalcAndPrintResponse(GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds, int printMean)
{
for (;;) {
GWEN_MSG *msg;
@@ -259,22 +260,9 @@ int _awaitAndPrintResponse(GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds)
}
code=GWEN_IpcMsg_GetCode(msg);
if (code==AQH_MSGTYPE_IPC_DATA_GETDATA_RSP) {
if (AQH_DataPointsDataIpcMsg_IsValid(msg)) {
Utils_PrintDataPoints(AQH_DataPointsDataIpcMsg_GetDataPoints(msg),
AQH_DataPointsDataIpcMsg_GetNumValues(msg),
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_Msg_free(msg);
return 3;
}
_handleDataResponse(msg, printMean);
GWEN_Msg_free(msg);
return 0;
}
else if (code==AQH_MSGTYPE_IPC_DATA_RESULT) {
uint32_t resultCode;
@@ -295,47 +283,29 @@ int _awaitAndPrintResponse(GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds)
int _awaitAndCalcAndPrintResponse(GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds)
void _handleDataResponse(GWEN_MSG *msg, int printMean)
{
for (;;) {
GWEN_MSG *msg;
uint16_t code;
AQH_VALUE *value;
const GWEN_TAG16 *tag;
const char *valueUnits;
unsigned int numberOfPoints;
const uint64_t *dataPoints;
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;
}
}
else if (code==AQH_MSGTYPE_IPC_DATA_RESULT) {
uint32_t resultCode;
AQH_MultiDataDataIpcMsg_Parse(msg, 0);
value=AQH_MultiDataDataIpcMsg_ReadValue(msg);
valueUnits=value?AQH_Value_GetValueUnits(value):NULL;
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;
tag=AQH_Tag16IpcMsg_FindFirstTagByType(msg, AQH_MSGDATA_MULTIDATA_TAGS_DATA);
numberOfPoints=(tag?GWEN_Tag16_GetTagLength(tag):0)/(2*sizeof(uint64_t));
dataPoints=tag?((const uint64_t*) GWEN_Tag16_GetTagData(tag)):NULL;
if (numberOfPoints>0 && dataPoints) {
if (printMean)
Utils_PrintMeanData(dataPoints, numberOfPoints, valueUnits);
else
Utils_PrintDataPoints(dataPoints, numberOfPoints, valueUnits);
}
AQH_Value_free(value);
}