aqhome-tool: use refMsgId when waiting for response.

This commit is contained in:
Martin Preuss
2024-10-01 23:45:15 +02:00
parent 1d08945ae5
commit 0926ba2381
3 changed files with 124 additions and 100 deletions

View File

@@ -36,7 +36,7 @@
static int _doGetDataPoints(GWEN_DB_NODE *dbArgs);
static int _awaitAndCalcAndPrintResponse(GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds, int printMean, int printDiff);
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);
static uint64_t _getTimeStampFromString(const char *s);
@@ -234,6 +234,7 @@ int _doGetDataPoints(GWEN_DB_NODE *dbArgs)
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);
@@ -257,13 +258,11 @@ int _doGetDataPoints(GWEN_DB_NODE *dbArgs)
return 2;
}
/*fprintf(stdout, "Sending GetDataPoints request\n");*/
msgOut=AQH_GetDataDataIpcMsg_new(AQH_MSGTYPE_IPC_DATA_GETDATA_REQ,
GWEN_MsgEndpoint_GetNextMessageId(epTcp), 0,
valueName, tsBegin, tsEnd, num);
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, timeoutInSeconds, printMean?1:0, printDiff?1:0);
rv=_awaitAndCalcAndPrintResponse(epTcp, msgId, timeoutInSeconds, printMean?1:0, printDiff?1:0);
if (rv!=0) {
GWEN_MsgEndpoint_free(epTcp);
return rv;
@@ -275,35 +274,38 @@ int _doGetDataPoints(GWEN_DB_NODE *dbArgs)
int _awaitAndCalcAndPrintResponse(GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds, int printMean, int printDiff)
int _awaitAndCalcAndPrintResponse(GWEN_MSG_ENDPOINT *epTcp, uint32_t msgId, int timeoutInSeconds, int printMean, int printDiff)
{
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) {
_handleDataResponse(msg, printMean, printDiff);
GWEN_Msg_free(msg);
return 0;
}
else if (code==AQH_MSGTYPE_IPC_DATA_RESULT) {
uint32_t resultCode;
msg=Utils_WaitForResponse(epTcp, msgId, timeoutInSeconds);
if (msg) {
uint16_t code;
resultCode=AQH_ResultIpcMsg_GetResultCode(msg);
fprintf(stderr, "ERROR: %d\n", resultCode);
GWEN_Msg_free(msg);
return 3;
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_INFO(NULL, "Unexpected message \"%d\"", code);
GWEN_Msg_free(msg);
return 3;
DBG_ERROR(NULL, "No response received");
return 2;
}
} /* for */
return 0;

View File

@@ -33,7 +33,8 @@
static int _doGetValues(GWEN_DB_NODE *dbArgs);
static void _sendCommand(GWEN_MSG_ENDPOINT *epTcp);
static uint32_t _sendRequest(GWEN_MSG_ENDPOINT *epTcp);
static int _handleResponses(GWEN_MSG_ENDPOINT *epTcp, uint32_t msgId, int timeoutInSeconds);
@@ -155,7 +156,8 @@ int _doGetValues(GWEN_DB_NODE *dbArgs)
{
GWEN_MSG_ENDPOINT *epTcp;
int timeoutInSeconds;
GWEN_MSG *msg;
uint32_t msgId;
int rv;
timeoutInSeconds=GWEN_DB_GetIntValue(dbArgs, "timeout", 0, 5);
@@ -165,85 +167,99 @@ int _doGetValues(GWEN_DB_NODE *dbArgs)
return 2;
}
/*fprintf(stdout, "Sending GetValues request\n");*/
msgId=_sendRequest(epTcp);
rv=_handleResponses(epTcp, msgId, timeoutInSeconds);
if (rv!=0) {
DBG_ERROR(NULL, "here (%d)", rv);
}
GWEN_MsgEndpoint_free(epTcp);
return rv;
}
_sendCommand(epTcp);
uint32_t _sendRequest(GWEN_MSG_ENDPOINT *epTcp)
{
GWEN_MSG *msgOut;
uint32_t msgId;
msgId=GWEN_MsgEndpoint_GetNextMessageId(epTcp);
msgOut=GWEN_IpcMsg_new(AQH_IPC_PROTOCOL_DATA_ID, AQH_IPC_PROTOCOL_DATA_VERSION, AQH_MSGTYPE_IPC_DATA_GETVALUES_REQ,
msgId, 0,
0, NULL);
GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut);
return msgId;
}
int _handleResponses(GWEN_MSG_ENDPOINT *epTcp, uint32_t msgId, int timeoutInSeconds)
{
for (;;) {
uint16_t code;
GWEN_MSG *msg;
msg=Utils_WaitForSpecificIpcMessage(epTcp, AQH_MSGTYPE_IPC_DATA_GETVALUES_RSP, timeoutInSeconds);
if (msg==NULL) {
msg=Utils_WaitForResponse(epTcp, msgId, timeoutInSeconds);
if (msg) {
uint16_t code;
code=GWEN_IpcMsg_GetCode(msg);
if (code==AQH_MSGTYPE_IPC_DATA_GETVALUES_RSP) {
AQH_VALUE_LIST *valueList;
AQH_ValuesDataIpcMsg_Parse(msg, 0);
valueList=AQH_ValuesDataIpcMsg_ReadValueList(msg);
if (valueList) {
AQH_VALUE *v;
v=AQH_Value_List_First(valueList);
while(v) {
uint64_t valueId;
const char *valueName;
const char *valueUnits;
valueId=AQH_Value_GetId(v);
valueName=AQH_Value_GetNameForSystem(v);
valueUnits=AQH_Value_GetValueUnits(v);
fprintf(stdout, "%lu\t%s\t%s\n",
(unsigned long int) valueId,
valueName?valueName:"",
valueUnits?valueUnits:"");
v=AQH_Value_List_Next(v);
}
AQH_Value_List_free(valueList);
}
if (AQH_ValuesDataIpcMsg_GetFlags(msg) & AQH_MSGDATA_VALUES_FLAGS_LASTMSG) {
DBG_INFO(NULL, "Last message received");
GWEN_Msg_free(msg);
break;
}
}
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;
}
} /* if msg */
else {
DBG_ERROR(NULL, "No response received");
return 2;
}
code=GWEN_IpcMsg_GetCode(msg);
if (code==AQH_MSGTYPE_IPC_DATA_GETVALUES_RSP) {
AQH_VALUE_LIST *valueList;
AQH_ValuesDataIpcMsg_Parse(msg, 0);
valueList=AQH_ValuesDataIpcMsg_ReadValueList(msg);
if (valueList) {
AQH_VALUE *v;
v=AQH_Value_List_First(valueList);
while(v) {
uint64_t valueId;
const char *valueName;
const char *valueUnits;
valueId=AQH_Value_GetId(v);
valueName=AQH_Value_GetNameForSystem(v);
valueUnits=AQH_Value_GetValueUnits(v);
fprintf(stdout, "%lu\t%s\t%s\n",
(unsigned long int) valueId,
valueName?valueName:"",
valueUnits?valueUnits:"");
v=AQH_Value_List_Next(v);
}
AQH_Value_List_free(valueList);
}
if (AQH_ValuesDataIpcMsg_GetFlags(msg) & AQH_MSGDATA_VALUES_FLAGS_LASTMSG) {
DBG_INFO(NULL, "Last message received");
break;
}
}
else if (code==AQH_MSGTYPE_IPC_DATA_RESULT) {
uint32_t resultCode;
resultCode=AQH_ResultIpcMsg_GetResultCode(msg);
fprintf(stderr, "ERROR: %d\n", resultCode);
GWEN_MsgEndpoint_free(epTcp);
return 3;
}
else {
DBG_INFO(NULL, "Unexpected message \"%d\"", code);
GWEN_MsgEndpoint_free(epTcp);
return 3;
}
} /* for */
GWEN_MsgEndpoint_free(epTcp);
return 0;
}
void _sendCommand(GWEN_MSG_ENDPOINT *epTcp)
{
GWEN_MSG *msgOut;
msgOut=GWEN_IpcMsg_new(AQH_IPC_PROTOCOL_DATA_ID, AQH_IPC_PROTOCOL_DATA_VERSION, AQH_MSGTYPE_IPC_DATA_GETVALUES_REQ,
GWEN_MsgEndpoint_GetNextMessageId(epTcp), 0,
0, NULL);
GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut);
}

View File

@@ -32,12 +32,21 @@
#define I18N(msg) GWEN_I18N_Translate(PACKAGE, msg)
/* ------------------------------------------------------------------------------------------------
* forward declarations
* ------------------------------------------------------------------------------------------------
*/
static int _doSetData(GWEN_DB_NODE *dbArgs);
static uint32_t _sendRequest(GWEN_MSG_ENDPOINT *epTcp, const char *valueName, const char *valueUnits, const char *valueData);
static int _awaitResponse(GWEN_MSG_ENDPOINT *epTcp, uint32_t msgId, int timeoutInSeconds);
/* ------------------------------------------------------------------------------------------------
* implementations
* ------------------------------------------------------------------------------------------------
*/
int AQH_Tool_SetData(GWEN_DB_NODE *dbGlobalArgs, int argc, char **argv)
{
@@ -209,9 +218,6 @@ int _doSetData(GWEN_DB_NODE *dbArgs)
return 1;
}
/*fprintf(stdout, "Sending SetData request\n");*/
epTcp=Utils_SetupBrokerClientEndpoint(dbArgs, 0);
if (epTcp==NULL) {
DBG_ERROR(NULL, "ERROR creating TCP connection");