aqhome-data, aqhome-tool: more work on new protocol.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/****************************************************************************
|
||||
* This file is part of the project AqHome.
|
||||
* AqHome (c) by 2023 Martin Preuss, all rights reserved.
|
||||
* AqHome (c) by 2025 Martin Preuss, all rights reserved.
|
||||
*
|
||||
* The license for this file can be found in the file COPYING which you
|
||||
* should have received along with this file.
|
||||
@@ -10,334 +10,169 @@
|
||||
# include <config.h>
|
||||
#endif
|
||||
|
||||
#include "./getdatapoints.h"
|
||||
#include "./getvalues.h"
|
||||
#include "./client.h"
|
||||
#include "../utils.h"
|
||||
|
||||
#include "aqhome/aqhome.h"
|
||||
#include "aqhome/msg/msg_node.h"
|
||||
#include "aqhome/ipc/msg_ipc_result.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 "aqhome/msg/ipc/m_ipc.h"
|
||||
#include "aqhome/msg/ipc/m_ipc_result.h"
|
||||
#include "aqhome/msg/ipc/data/m_ipcd.h"
|
||||
#include "aqhome/msg/ipc/data/m_ipcd_getdata.h"
|
||||
#include "aqhome/msg/ipc/data/m_ipcd_multidata.h"
|
||||
|
||||
#include <gwenhywfar/args.h>
|
||||
#include <gwenhywfar/i18n.h>
|
||||
#include <gwenhywfar/debug.h>
|
||||
#include <gwenhywfar/text.h>
|
||||
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <ctype.h>
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* defs
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#define I18S(msg) msg
|
||||
#define I18N(msg) GWEN_I18N_Translate(PACKAGE, msg)
|
||||
|
||||
#define A_ARG GWEN_ARGS_FLAGS_HAS_ARGUMENT
|
||||
#define A_END (GWEN_ARGS_FLAGS_HELP | GWEN_ARGS_FLAGS_LAST)
|
||||
#define A_CHAR GWEN_ArgsType_Char
|
||||
#define A_INT GWEN_ArgsType_Int
|
||||
|
||||
static int _doGetDataPoints(GWEN_DB_NODE *dbArgs);
|
||||
static int _awaitAndCalcAndPrintResponse(GWEN_MSG_ENDPOINT *epTcp, uint32_t msgId, int timeoutInSeconds, int printMean, int printDiff);
|
||||
static void _handleDataResponse(GWEN_MSG *msg, int printMean, int printDiff);
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* forward declarations
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
static AQH_MESSAGE *_createRequestMessage(AQH_OBJECT *o, uint32_t msgId);
|
||||
static int _handleResponseMessage(AQH_OBJECT *o, const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList, int first);
|
||||
static void _handleDataResponse(const GWEN_TAG16_LIST *tagList, int printMean, int printDiff);
|
||||
static uint64_t _getTimeStampFromString(const char *s);
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* code
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
int AQH_Tool_GetDataPoints(GWEN_DB_NODE *dbGlobalArgs, int argc, char **argv)
|
||||
{
|
||||
GWEN_DB_NODE *dbLocalArgs;
|
||||
AQH_EVENT_LOOP *eventLoop;
|
||||
AQH_OBJECT *o;
|
||||
int rv;
|
||||
const GWEN_ARGS args[]= {
|
||||
{
|
||||
GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
|
||||
GWEN_ArgsType_Char, /* type */
|
||||
"brokerAddress", /* name */
|
||||
0, /* minnum */
|
||||
1, /* maxnum */
|
||||
"t", /* short option */
|
||||
"tcpaddress", /* long option */
|
||||
I18S("Specify TCP address to connect to (defaults to 127.0.0.1)"),
|
||||
I18S("Specify TCP address to connect to (defaults to 127.0.0.1)")
|
||||
},
|
||||
{
|
||||
GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
|
||||
GWEN_ArgsType_Int, /* type */
|
||||
"brokerPort", /* name */
|
||||
0, /* minnum */
|
||||
1, /* maxnum */
|
||||
"P", /* short option */
|
||||
"tcpport", /* long option */
|
||||
I18S("Specify the TCP port to listen on"),
|
||||
I18S("Specify the TCP port to listen on")
|
||||
},
|
||||
{
|
||||
GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
|
||||
GWEN_ArgsType_Int, /* type */
|
||||
"timeout", /* name */
|
||||
0, /* minnum */
|
||||
1, /* maxnum */
|
||||
"T", /* short option */
|
||||
NULL, /* long option */
|
||||
I18S("Specify timeout in seconds for response"),
|
||||
I18S("Specify timeout in seconds for response")
|
||||
},
|
||||
{
|
||||
GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
|
||||
GWEN_ArgsType_Char, /* type */
|
||||
"valueName", /* name */
|
||||
1, /* minnum */
|
||||
1, /* maxnum */
|
||||
"N", /* short option */
|
||||
"valuename", /* long option */
|
||||
I18S("Name/path of the value to add (e.g. server/temp/system)"),
|
||||
I18S("Name/path of the value to add (e.g. server/temp/system)")
|
||||
},
|
||||
{
|
||||
GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
|
||||
GWEN_ArgsType_Char, /* type */
|
||||
"tsBegin", /* name */
|
||||
0, /* minnum */
|
||||
1, /* maxnum */
|
||||
"tb", /* short option */
|
||||
"tsbegin", /* long option */
|
||||
I18S("Get data from this timestamp on (earliest timestamp if omitted)"),
|
||||
I18S("Get data from this timestamp on (earliest timestamp if omitted)")
|
||||
},
|
||||
{
|
||||
GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
|
||||
GWEN_ArgsType_Char, /* type */
|
||||
"tsEnd", /* name */
|
||||
0, /* minnum */
|
||||
1, /* maxnum */
|
||||
"te", /* short option */
|
||||
"tsend", /* long option */
|
||||
I18S("Get data up until this timestamp (latest timestamp if omitted)"),
|
||||
I18S("Get data up until this timestamp (latest timestamp if omitted)")
|
||||
},
|
||||
{
|
||||
GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
|
||||
GWEN_ArgsType_Int, /* type */
|
||||
"numOfLastDatapoints", /* name */
|
||||
0, /* minnum */
|
||||
1, /* maxnum */
|
||||
"n", /* short option */
|
||||
NULL, /* long option */
|
||||
I18S("Get last n datapoints"),
|
||||
I18S("Get last n datapoints")
|
||||
},
|
||||
{
|
||||
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")
|
||||
},
|
||||
{
|
||||
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 */
|
||||
"brokerClientId", /* name */
|
||||
0, /* minnum */
|
||||
1, /* maxnum */
|
||||
"c", /* short option */
|
||||
"clientid", /* long option */
|
||||
I18S("Specify CLIENTID"),
|
||||
I18S("Specify CLIENTID")
|
||||
},
|
||||
{
|
||||
GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
|
||||
GWEN_ArgsType_Char, /* type */
|
||||
"userId", /* name */
|
||||
0, /* minnum */
|
||||
1, /* maxnum */
|
||||
"u", /* short option */
|
||||
"userid", /* long option */
|
||||
I18S("Specify user id"),
|
||||
I18S("Specify user id")
|
||||
},
|
||||
{
|
||||
GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
|
||||
GWEN_ArgsType_Char, /* type */
|
||||
"password", /* name */
|
||||
0, /* minnum */
|
||||
1, /* maxnum */
|
||||
"p", /* short option */
|
||||
"password", /* long option */
|
||||
I18S("Specify service password"),
|
||||
I18S("Specify service password")
|
||||
},
|
||||
{
|
||||
GWEN_ARGS_FLAGS_HELP | GWEN_ARGS_FLAGS_LAST, /* flags */
|
||||
GWEN_ArgsType_Int, /* type */
|
||||
"help", /* name */
|
||||
0, /* minnum */
|
||||
0, /* maxnum */
|
||||
"h", /* short option */
|
||||
"help", /* long option */
|
||||
"Show this help screen", /* short description */
|
||||
"Show this help screen" /* long description */
|
||||
}
|
||||
/* flags type name min max s long short_descr, long_descr */
|
||||
{ A_ARG, A_CHAR, "brokerAddress", 0, 1, "t", "tcpaddress", I18S("TCP address to connect to [127.0.0.1]"), NULL},
|
||||
{ A_ARG, A_INT, "brokerPort", 0, 1, "P", "tcpport", I18S("Specify the TCP port to listen on"), NULL},
|
||||
{ A_ARG, A_INT, "timeout", 0, 1, "T", NULL, I18S("Specify timeout in seconds for response"), NULL},
|
||||
{ A_ARG, A_CHAR, "brokerClientId", 0, 1, "c", "clientid", I18S("Specify CLIENTID"), NULL},
|
||||
{ A_ARG, A_CHAR, "userId", 0, 1, "u", "userid", I18S("Specify user id"), NULL},
|
||||
{ A_ARG, A_CHAR, "password", 0, 1, "p", "password", I18S("Specify service password"), NULL},
|
||||
{ A_ARG, A_CHAR, "valueName", 1, 1, "N", "valuename", I18S("Value name (e.g. server/temp/system)"), NULL},
|
||||
{ A_ARG, A_CHAR, "tsBegin", 0, 1, "tb", "tsbegin", I18S("Timestamp range begin"), NULL},
|
||||
{ A_ARG, A_CHAR, "tsEnd", 0, 1, "te", "tsend", I18S("Timestamp range end"), NULL},
|
||||
{ A_ARG, A_INT, "numOfLastDatapoints", 0, 1, "n", NULL, I18S("Get last n datapoints"), NULL},
|
||||
{ 0, A_INT, "printMean", 0, 1, "M", "mean", I18S("Print mean value of data received"), NULL},
|
||||
{ 0, A_INT, "printDiff", 0, 1, "D", "diff", I18S("Print diff last-first value"), NULL},
|
||||
{ A_END, A_INT, "help", 0, 0, "h", "help", I18S("Show this help screen"), NULL}
|
||||
};
|
||||
|
||||
dbLocalArgs=GWEN_DB_GetGroup(dbGlobalArgs, GWEN_DB_FLAGS_DEFAULT, "local");
|
||||
rv=GWEN_Args_Check(argc, argv, 1,
|
||||
GWEN_ARGS_MODE_ALLOW_FREEPARAM,
|
||||
args,
|
||||
dbLocalArgs);
|
||||
if (rv==GWEN_ARGS_RESULT_ERROR) {
|
||||
fprintf(stderr, "ERROR: Could not parse arguments\n");
|
||||
return 1;
|
||||
}
|
||||
else if (rv==GWEN_ARGS_RESULT_HELP) {
|
||||
GWEN_BUFFER *ubuf;
|
||||
|
||||
ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
|
||||
if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) {
|
||||
fprintf(stderr, "ERROR: Could not create help string\n");
|
||||
return 1;
|
||||
}
|
||||
fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf));
|
||||
GWEN_Buffer_free(ubuf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
AQH_MergeConfigFileIntoConfig(dbLocalArgs, "ConfigFile");
|
||||
|
||||
return _doGetDataPoints(dbLocalArgs);
|
||||
eventLoop=AQH_EventLoop_new();
|
||||
o=AQH_ToolClient_new(eventLoop, dbGlobalArgs, args);
|
||||
AQH_ToolClient_SetCreateRequestMessageFn(o, _createRequestMessage);
|
||||
AQH_ToolClient_SetHandleResponseMessageFn(o, _handleResponseMessage);
|
||||
rv=AQH_ToolClient_ReadLocalArgs(o, argc, argv);
|
||||
if (rv!=0)
|
||||
return rv;
|
||||
rv=AQH_ToolClient_Run(o);
|
||||
AQH_Object_free(o);
|
||||
AQH_EventLoop_free(eventLoop);
|
||||
return rv;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _doGetDataPoints(GWEN_DB_NODE *dbArgs)
|
||||
AQH_MESSAGE *_createRequestMessage(GWEN_UNUSED AQH_OBJECT *o, uint32_t msgId)
|
||||
{
|
||||
GWEN_MSG_ENDPOINT *epTcp;
|
||||
int timeoutInSeconds;
|
||||
GWEN_DB_NODE *dbArgs;
|
||||
const char *valueName;
|
||||
uint64_t tsBegin;
|
||||
uint64_t tsEnd;
|
||||
uint64_t num;
|
||||
GWEN_MSG *msgOut;
|
||||
int printMean;
|
||||
int printDiff;
|
||||
int rv;
|
||||
uint32_t msgId;
|
||||
|
||||
printMean=GWEN_DB_GetIntValue(dbArgs, "printMean", 0, 0);
|
||||
printDiff=GWEN_DB_GetIntValue(dbArgs, "printDiff", 0, 0);
|
||||
timeoutInSeconds=GWEN_DB_GetIntValue(dbArgs, "timeout", 0, 5);
|
||||
dbArgs=AQH_ToolClient_GetDbLocalArgs(o);
|
||||
valueName=GWEN_DB_GetCharValue(dbArgs, "valueName", 0, NULL);
|
||||
num=GWEN_DB_GetIntValue(dbArgs, "numOfLastDatapoints", 0, 0);
|
||||
tsBegin=_getTimeStampFromString(GWEN_DB_GetCharValue(dbArgs, "tsBegin", 0, NULL));
|
||||
if (tsBegin==(uint64_t) (-1)) {
|
||||
DBG_ERROR(NULL, "Bad begin timestamp");
|
||||
return 2;
|
||||
return NULL;
|
||||
}
|
||||
tsEnd=_getTimeStampFromString(GWEN_DB_GetCharValue(dbArgs, "tsEnd", 0, NULL));
|
||||
if (tsEnd==(uint64_t) (-1)) {
|
||||
DBG_ERROR(NULL, "Bad end timestamp");
|
||||
return 2;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
epTcp=Utils_SetupBrokerClientEndpoint(dbArgs, 0);
|
||||
if (epTcp==NULL) {
|
||||
DBG_ERROR(NULL, "ERROR creating TCP connection");
|
||||
return 2;
|
||||
}
|
||||
|
||||
msgId=GWEN_MsgEndpoint_GetNextMessageId(epTcp);
|
||||
msgOut=AQH_GetDataDataIpcMsg_new(AQH_MSGTYPE_IPC_DATA_GETDATA_REQ, msgId, 0, valueName, tsBegin, tsEnd, num);
|
||||
GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut);
|
||||
|
||||
rv=_awaitAndCalcAndPrintResponse(epTcp, msgId, timeoutInSeconds, printMean?1:0, printDiff?1:0);
|
||||
if (rv!=0) {
|
||||
GWEN_MsgEndpoint_free(epTcp);
|
||||
return rv;
|
||||
}
|
||||
|
||||
GWEN_MsgEndpoint_free(epTcp);
|
||||
return 0;
|
||||
return AQH_IpcdMessageGetData_new(AQH_MSGTYPE_IPC_DATA_GETDATA_REQ, msgId, 0, valueName, tsBegin, tsEnd, num);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _awaitAndCalcAndPrintResponse(GWEN_MSG_ENDPOINT *epTcp, uint32_t msgId, int timeoutInSeconds, int printMean, int printDiff)
|
||||
int _handleResponseMessage(AQH_OBJECT *o, const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList, int first)
|
||||
{
|
||||
for (;;) {
|
||||
GWEN_MSG *msg;
|
||||
GWEN_DB_NODE *dbArgs;
|
||||
int printMean;
|
||||
int printDiff;
|
||||
uint16_t code;
|
||||
|
||||
msg=Utils_WaitForResponse(epTcp, msgId, timeoutInSeconds);
|
||||
if (msg) {
|
||||
uint16_t code;
|
||||
dbArgs=AQH_ToolClient_GetDbLocalArgs(o);
|
||||
printMean=GWEN_DB_GetIntValue(dbArgs, "printMean", 0, 0);
|
||||
printDiff=GWEN_DB_GetIntValue(dbArgs, "printDiff", 0, 0);
|
||||
|
||||
code=GWEN_IpcMsg_GetCode(msg);
|
||||
if (code==AQH_MSGTYPE_IPC_DATA_GETDATA_RSP) {
|
||||
_handleDataResponse(msg, printMean, printDiff);
|
||||
GWEN_Msg_free(msg);
|
||||
return 0;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(NULL, "No response received");
|
||||
return 2;
|
||||
}
|
||||
} /* for */
|
||||
return 0;
|
||||
code=AQH_IpcMessage_GetCode(msg);
|
||||
if (code==AQH_MSGTYPE_IPC_DATA_GETDATA_RSP) {
|
||||
_handleDataResponse(tagList, printMean, printDiff);
|
||||
return 1;
|
||||
}
|
||||
else if (code==AQH_MSGTYPE_IPC_DATA_RESULT)
|
||||
return AQH_ToolClient_HandleResultMsg(msg, tagList, first);
|
||||
else {
|
||||
DBG_INFO(NULL, "Unexpected message \"%d\"", code);
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void _handleDataResponse(GWEN_MSG *msg, int printMean, int printDiff)
|
||||
void _handleDataResponse(const GWEN_TAG16_LIST *tagList, int printMean, int printDiff)
|
||||
{
|
||||
AQH_VALUE *value;
|
||||
const GWEN_TAG16 *tag;
|
||||
const char *valueUnits;
|
||||
unsigned int numberOfPoints;
|
||||
const uint64_t *dataPoints;
|
||||
|
||||
AQH_MultiDataDataIpcMsg_Parse(msg, 0);
|
||||
value=AQH_MultiDataDataIpcMsg_ReadValue(msg);
|
||||
valueUnits=value?AQH_Value_GetValueUnits(value):NULL;
|
||||
value=AQH_IpcdMessageMultiData_ReadValue(tagList);
|
||||
if (value) {
|
||||
const char *valueUnits;
|
||||
uint64_t numberOfPoints;
|
||||
const uint64_t *dataPoints;
|
||||
|
||||
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 if (printDiff)
|
||||
Utils_PrintDiffData(dataPoints, numberOfPoints, valueUnits);
|
||||
else
|
||||
Utils_PrintDataPoints(dataPoints, numberOfPoints, valueUnits);
|
||||
valueUnits=AQH_Value_GetValueUnits(value);
|
||||
AQH_IpcdMessageMultiData_ReadDatapoints(tagList, &dataPoints, &numberOfPoints);
|
||||
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);
|
||||
}
|
||||
AQH_Value_free(value);
|
||||
}
|
||||
|
||||
AQH_Value_free(value);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user