aqhome-tool: visualize data points using aqdiagram.

This commit is contained in:
Martin Preuss
2025-10-01 23:21:12 +02:00
parent 5040ccc065
commit 5bd1e06850
6 changed files with 335 additions and 1 deletions

View File

@@ -6,6 +6,7 @@
<includes type="c" >
$(gwenhywfar_cflags)
$(aqdiagram_cflags)
-I$(topsrcdir)
-I$(topbuilddir)
</includes>
@@ -54,6 +55,7 @@
<libraries>
$(gwenhywfar_libs)
$(aqdiagram_libs)
</libraries>
<subdirs>

View File

@@ -44,6 +44,7 @@
moddevice.h
watch.h
devicestate.h
imgperioddata.h
</headers>
<sources>
@@ -60,6 +61,7 @@
moddevice.c
watch.c
devicestate.c
imgperioddata.c
</sources>
<useTargets>

View File

@@ -0,0 +1,306 @@
/****************************************************************************
* This file is part of the project AqHome.
* 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.
****************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "./imgperioddata.h"
#include "../utils.h"
#include "aqhome/dataclient/client.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 "aqhome/dataclient/client.h"
#include <aqdiagram/graph/timegraph.h>
#include <aqdiagram/graph/w_graph.h>
#include <aqdiagram/draw/context_cairo.h>
#include <aqdiagram/data/date.h>
#include <aqdiagram/data/floatingavg.h>
#include <gwenhywfar/args.h>
#include <gwenhywfar/i18n.h>
#include <gwenhywfar/debug.h>
#include <gwenhywfar/text.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
/* ------------------------------------------------------------------------------------------------
* forward declarations
* ------------------------------------------------------------------------------------------------
*/
static int _runCommand(AQH_DATACLIENT *dc);
static int _createImageFromDataPoints(AQH_DATACLIENT *dc, const char *valueName, const uint64_t *dataPoints, uint64_t recvdNum);
static AQDG_GRAPH_DATAPAIR_LIST *_createDataPairListFromDataPoints(const uint64_t *dataPoints, uint64_t numValues);
/* ------------------------------------------------------------------------------------------------
* code
* ------------------------------------------------------------------------------------------------
*/
int AQH_Tool_ImgPeriodData(GWEN_DB_NODE *dbGlobalArgs, int argc, char **argv)
{
AQH_EVENT_LOOP *eventLoop;
AQH_DATACLIENT *dc;
int rv;
const GWEN_ARGS args[]= {
/* 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_INT, "numOfDatapoints", 0, 1, "n", NULL, I18S("Get up to n datapoints"), 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_CHAR, "imgFile", 1, 1, "F", NULL, I18S("Image path"), NULL},
{ A_ARG, A_INT, "imgWidth", 0, 1, "W", NULL, I18S("Image width"), NULL},
{ A_ARG, A_INT, "imgHeight", 0, 1, "H", NULL, I18S("Image height"), NULL},
{ A_ARG, A_CHAR, "upperLimit", 0, 1, "U", NULL, I18S("Upper limit for y axis"), NULL},
{ A_ARG, A_CHAR, "lowerLimit", 0, 1, "L", NULL, I18S("Lower limit for y axis"), NULL},
{ 0, A_INT, "dayAverage", 0, 1, "da", NULL, I18S("Average data over days"), NULL},
{ A_ARG, A_INT, "minuteAverage", 0, 1, "ma", NULL, I18S("Average data over units of (NUM) minutes"), NULL},
{ A_ARG, A_INT, "floatingAverage", 0, 1, "fa", NULL, I18S("Floating average over data (num)"), NULL},
{ A_ARG, A_INT, "precision", 0, 1, "yp", NULL, I18S("Y axis Data Precision (default: 2)"), NULL},
{ A_END, A_INT, "help", 0, 0, "h", "help", I18S("Show this help screen"), NULL}
};
eventLoop=AQH_EventLoop_new();
dc=AQH_DataClient_new(eventLoop, AQH_IPC_PROTOCOL_DATA_ID, AQH_IPC_PROTOCOL_DATA_VERSION);
rv=AQH_DataClient_ReadLocalArgs(dc, dbGlobalArgs, args, argc, argv);
if (rv<0) {
DBG_ERROR(NULL, "here (%d)", rv);
AQH_DataClient_free(dc);
AQH_EventLoop_free(eventLoop);
return 2;
}
rv=AQH_DataClient_ConnectWithArgs(dc, 0);
if (rv<0) {
DBG_ERROR(NULL, "Error connecting (%d)", rv);
AQH_DataClient_free(dc);
AQH_EventLoop_free(eventLoop);
return 2;
}
rv=_runCommand(dc);
if (rv<0) {
DBG_ERROR(NULL, "Error running (%d)", rv);
AQH_DataClient_free(dc);
AQH_EventLoop_free(eventLoop);
return 2;
}
AQH_DataClient_free(dc);
AQH_EventLoop_free(eventLoop);
return 0;
}
int _runCommand(AQH_DATACLIENT *dc)
{
GWEN_DB_NODE *dbLocalArgs;
const char *valueName;
uint64_t num;
uint64_t tsBegin;
uint64_t tsEnd;
dbLocalArgs=AQH_DataClient_GetDbLocalArgs(dc);
valueName=GWEN_DB_GetCharValue(dbLocalArgs, "valueName", 0, NULL);
num=GWEN_DB_GetIntValue(dbLocalArgs, "numOfDatapoints", 0, 10000);
tsBegin=Utils_GetTimeStampFromString(GWEN_DB_GetCharValue(dbLocalArgs, "tsBegin", 0, NULL));
if (tsBegin==(uint64_t) (-1)) {
DBG_ERROR(NULL, "Bad begin timestamp");
return 1;
}
tsEnd=Utils_GetTimeStampFromString(GWEN_DB_GetCharValue(dbLocalArgs, "tsEnd", 0, NULL));
if (tsEnd==(uint64_t) (-1)) {
DBG_ERROR(NULL, "Bad end timestamp");
return 1;
}
if (num>0) {
uint64_t *dataPoints;
uint64_t recvdNum;
dataPoints=malloc(num*sizeof(uint64_t)*2);
recvdNum=AQH_DataClient_GetPeriodData(dc, valueName, dataPoints, num, tsBegin, tsEnd);
if (recvdNum>0) {
int rv;
rv=_createImageFromDataPoints(dc, valueName, dataPoints, recvdNum);
if (rv!=0) {
DBG_INFO(NULL, "here (%d)", rv);
return rv;
}
}
free(dataPoints);
}
return 0;
}
int _createImageFromDataPoints(AQH_DATACLIENT *dc, const char *valueName, const uint64_t *dataPoints, uint64_t recvdNum)
{
GWEN_DB_NODE *dbLocalArgs;
const char *imgFile;
int imgWidth;
int imgHeight;
int dayAverage;
int minuteAverage;
int floatingAvg;
int precision;
const char *s;
uint32_t tickFlags=0;
double upperLimit;
double lowerLimit;
AQDG_GRAPH *g;
AQDG_GRAPH_DATAPAIR_LIST *dpList;
AQDG_DRAW_CONTEXT *drawContext;
AQDG_OBJECT *graphObject;
dbLocalArgs=AQH_DataClient_GetDbLocalArgs(dc);
imgFile=GWEN_DB_GetCharValue(dbLocalArgs, "imgFile", 0, NULL);
imgWidth=GWEN_DB_GetIntValue(dbLocalArgs, "imgWidth", 0, 800);
imgHeight=GWEN_DB_GetIntValue(dbLocalArgs, "imgHeight", 0, 600);
dayAverage=GWEN_DB_GetIntValue(dbLocalArgs, "dayAverage", 0, 0);
minuteAverage=GWEN_DB_GetIntValue(dbLocalArgs, "minuteAverage", 0, 0);
floatingAvg=GWEN_DB_GetIntValue(dbLocalArgs, "floatingAverage", 0, 0);
precision=GWEN_DB_GetIntValue(dbLocalArgs, "precision", 0, 2);
s=GWEN_DB_GetCharValue(dbLocalArgs, "upperLimit", 0, NULL);
if (s && *s) {
if (1==sscanf(s, "%lf", &upperLimit))
tickFlags|=AQDG_TIMEGRAPH_SETUPTICKS_FLAGS_MAXY;
else {
fprintf(stderr, "Invalid upperLimit (%s)\n", s);
return 1;
}
}
s=GWEN_DB_GetCharValue(dbLocalArgs, "lowerLimit", 0, NULL);
if (s && *s) {
if (1==sscanf(s, "%lf", &lowerLimit))
tickFlags|=AQDG_TIMEGRAPH_SETUPTICKS_FLAGS_MINY;
else {
fprintf(stderr, "Invalid lowerLimit (%s)\n", s);
return 1;
}
}
g=AQDG_TimeGraph_new(valueName, NULL, "Value", NULL, precision);
dpList=_createDataPairListFromDataPoints(dataPoints, recvdNum);
if (dpList && dayAverage) {
AQDG_GRAPH_DATAPAIR_LIST *newDp;
newDp=AQDG_Data_DayAverage(dpList);
AQDG_Graph_DataPair_List_free(dpList);
dpList=newDp;
AQDG_Graph_DataPair_List_SortByValueX(dpList, 1);
DBG_ERROR(NULL, "DataPair count now: %d", AQDG_Graph_DataPair_List_GetCount(dpList));
}
if (dpList && floatingAvg) {
AQDG_GRAPH_DATAPAIR_LIST *newDp;
newDp=AQDG_Data_FloatingAverage(dpList, floatingAvg);
AQDG_Graph_DataPair_List_free(dpList);
dpList=newDp;
AQDG_Graph_DataPair_List_SortByValueX(dpList, 1);
DBG_ERROR(NULL, "DataPair count now: %d", AQDG_Graph_DataPair_List_GetCount(dpList));
}
if (dpList && minuteAverage){
AQDG_GRAPH_DATAPAIR_LIST *newDp;
newDp=AQDG_Data_MinutesAverage(dpList, minuteAverage);
AQDG_Graph_DataPair_List_free(dpList);
dpList=newDp;
AQDG_Graph_DataPair_List_SortByValueX(dpList, 1);
DBG_ERROR(NULL, "DataPair count now: %d", AQDG_Graph_DataPair_List_GetCount(dpList));
}
AQDG_TimeGraph_AddCurve(g, "Testdata", AQDG_GRAPH_TYPE_LINE, dpList);
AQDG_TimeGraph_SetupTicks(g, tickFlags, lowerLimit, upperLimit);
drawContext=AQDG_Draw_ContextCairo_Png_new(imgFile, imgWidth, imgHeight);
graphObject=AQDG_GraphWidget_new(NULL, AQDG_OBJECT_OPTIONS_STRETCHX | AQDG_OBJECT_OPTIONS_STRETCHY, drawContext);
AQDG_Object_SetWidth(graphObject, imgWidth);
AQDG_Object_SetHeight(graphObject, imgHeight);
AQDG_GraphWidget_SetupDefaultPens(graphObject);
AQDG_GraphWidget_SetupDefaultFonts(graphObject);
AQDG_GraphWidget_FinishWithGraph(graphObject, g);
return 0;
}
AQDG_GRAPH_DATAPAIR_LIST *_createDataPairListFromDataPoints(const uint64_t *dataPoints, uint64_t numValues)
{
AQDG_GRAPH_DATAPAIR_LIST *dpList;
uint64_t i;
DBG_ERROR(NULL, "Got %d datapoints", (int) numValues);
dpList=AQDG_Graph_DataPair_List_new();
for(i=0; i<numValues; i++) {
AQDG_GRAPH_DATAPAIR *dp;
double timestamp;
union {double f; uint64_t i;} u;
timestamp=(double)(*(dataPoints++));
u.i=*(dataPoints++);
dp=AQDG_Graph_DataPair_new();
AQDG_Graph_DataPair_SetValueX(dp, timestamp);
AQDG_Graph_DataPair_SetValueY(dp, u.f);
AQDG_Graph_DataPair_List_Add(dp, dpList);
}
AQDG_Graph_DataPair_List_SortByValueX(dpList, 1);
return dpList;
}

View File

@@ -0,0 +1,21 @@
/****************************************************************************
* This file is part of the project AqHome.
* 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.
****************************************************************************/
#ifndef AQHOME_TOOL_IMGPERIODDATA_H
#define AQHOME_TOOL_IMGPERIODDATA_H
#include <gwenhywfar/db.h>
int AQH_Tool_ImgPeriodData(GWEN_DB_NODE *dbGlobalArgs, int argc, char **argv);
#endif

View File

@@ -24,6 +24,7 @@
#include "./data/moddevice.h"
#include "./data/watch.h"
#include "./data/devicestate.h"
#include "./data/imgperioddata.h"
#include <aqhome/api.h>
#include <aqhome/aqhome.h>
@@ -103,6 +104,7 @@ int main(int argc, char **argv)
GWEN_FE_DAH("moddevice", AQH_Tool_ModDevice, I18N("Modify a device on the data server")),
GWEN_FE_DAH("watch", AQH_Tool_Watch, I18N("Watch and print changes of values on the data server")),
GWEN_FE_DAH("devicestate", AQH_Tool_DeviceState, I18N("Show state of devices")),
GWEN_FE_DAH("imgperioddata", AQH_Tool_ImgPeriodData, I18N("Create diagram of datapoints from a date range")),
GWEN_FE_END(),
};
const GWEN_FUNCS *func;