290 lines
8.8 KiB
C
290 lines
8.8 KiB
C
/****************************************************************************
|
|
* This file is part of the project AqHome.
|
|
* AqHome (c) by 2023 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.
|
|
****************************************************************************/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
# include <config.h>
|
|
#endif
|
|
|
|
#include "./getlastdatapoint.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_getdata.h"
|
|
#include "aqhome/ipc/data/msg_data_multidata.h"
|
|
#include "aqhome/ipc/data/ipc_data.h"
|
|
#include "aqhome/ipc/msg_ipc_tag16.h"
|
|
|
|
#include <gwenhywfar/args.h>
|
|
#include <gwenhywfar/i18n.h>
|
|
#include <gwenhywfar/debug.h>
|
|
#include <gwenhywfar/text.h>
|
|
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
|
|
#define I18S(msg) msg
|
|
#define I18N(msg) GWEN_I18N_Translate(PACKAGE, msg)
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
* forward declarations
|
|
* ------------------------------------------------------------------------------------------------
|
|
*/
|
|
|
|
static int _doGetLastDataPoint(GWEN_DB_NODE *dbArgs);
|
|
static void _sendCommand(GWEN_MSG_ENDPOINT *epTcp, const char *valueName);
|
|
static int _awaitAndHandleResponse(GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds);
|
|
static int _handleDataResponse(GWEN_MSG *msg);
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
* code
|
|
* ------------------------------------------------------------------------------------------------
|
|
*/
|
|
|
|
int AQH_Tool_GetLastDataPoint(GWEN_DB_NODE *dbGlobalArgs, int argc, char **argv)
|
|
{
|
|
GWEN_DB_NODE *dbLocalArgs;
|
|
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 */
|
|
"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 */
|
|
}
|
|
};
|
|
|
|
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 _doGetLastDataPoint(dbLocalArgs);
|
|
}
|
|
|
|
|
|
|
|
int _doGetLastDataPoint(GWEN_DB_NODE *dbArgs)
|
|
{
|
|
GWEN_MSG_ENDPOINT *epTcp;
|
|
int timeoutInSeconds;
|
|
const char *valueName;
|
|
int rv;
|
|
|
|
timeoutInSeconds=GWEN_DB_GetIntValue(dbArgs, "timeout", 0, 5);
|
|
valueName=GWEN_DB_GetCharValue(dbArgs, "valueName", 0, NULL);
|
|
|
|
epTcp=Utils_SetupBrokerClientEndpoint(dbArgs, 0);
|
|
if (epTcp==NULL) {
|
|
DBG_ERROR(NULL, "ERROR creating TCP connection");
|
|
return 2;
|
|
}
|
|
|
|
/*fprintf(stdout, "Sending GetLastDataPoint request (%s)\n", valueName);*/
|
|
|
|
_sendCommand(epTcp, valueName);
|
|
|
|
rv=_awaitAndHandleResponse(epTcp, timeoutInSeconds);
|
|
if (rv!=0) {
|
|
DBG_INFO(NULL, "here (%d)", rv);
|
|
GWEN_MsgEndpoint_free(epTcp);
|
|
return rv;
|
|
}
|
|
GWEN_MsgEndpoint_free(epTcp);
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
void _sendCommand(GWEN_MSG_ENDPOINT *epTcp, const char *valueName)
|
|
{
|
|
GWEN_MSG *msgOut;
|
|
|
|
msgOut=AQH_GetDataDataIpcMsg_new(AQH_MSGTYPE_IPC_DATA_GETLASTDATA_REQ, valueName, 0, 0, 1);
|
|
GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut);
|
|
}
|
|
|
|
|
|
|
|
int _awaitAndHandleResponse(GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds)
|
|
{
|
|
GWEN_MSG *msg;
|
|
uint16_t code;
|
|
|
|
msg=Utils_WaitForSpecificIpcMessage(epTcp, AQH_MSGTYPE_IPC_DATA_GETLASTDATA_RSP, timeoutInSeconds);
|
|
if (msg) {
|
|
code=GWEN_IpcMsg_GetCode(msg);
|
|
if (code==AQH_MSGTYPE_IPC_DATA_GETLASTDATA_RSP) {
|
|
int rv;
|
|
|
|
rv=_handleDataResponse(msg);
|
|
GWEN_Msg_free(msg);
|
|
return rv;
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
int _handleDataResponse(GWEN_MSG *msg)
|
|
{
|
|
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;
|
|
|
|
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) {
|
|
uint64_t timestamp;
|
|
union {double f; uint64_t i;} u;
|
|
|
|
timestamp=dataPoints[0];
|
|
u.i=dataPoints[1];
|
|
Utils_PrintSingleDataPoint(timestamp, u.f, valueUnits);
|
|
}
|
|
|
|
AQH_Value_free(value);
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|