From b2802a37aaa132a6e31c0859df122f0eef3a6f9e Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Wed, 25 Jun 2025 00:00:39 +0200 Subject: [PATCH] added AQH_Storage_GetFirstNDataPoints() --- aqhome/data/storage.c | 72 +++++++++++++++++++++++++++++++++++++++++ aqhome/data/storage.h | 6 ++++ aqhome/data/storage_p.h | 1 + 3 files changed, 79 insertions(+) diff --git a/aqhome/data/storage.c b/aqhome/data/storage.c index 74a8a65..70faf46 100644 --- a/aqhome/data/storage.c +++ b/aqhome/data/storage.c @@ -305,6 +305,17 @@ AQH_STORAGE_GETLASTDATAPOINT_FN AQH_Storage_SetGetLastDatapointFn(AQH_STORAGE *s +AQH_STORAGE_GETFIRSTNDATAPOINTS_FN AQH_Storage_SetGetFirstNDatapointsFn(AQH_STORAGE *sto, AQH_STORAGE_GETFIRSTNDATAPOINTS_FN fn) +{ + AQH_STORAGE_GETFIRSTNDATAPOINTS_FN oldFn; + + oldFn=sto->getFirstNDatapointsFn; + sto->getFirstNDatapointsFn=fn; + return oldFn; +} + + + AQH_STORAGE_GETLASTNDATAPOINTS_FN AQH_Storage_SetGetLastNDatapointsFn(AQH_STORAGE *sto, AQH_STORAGE_GETLASTNDATAPOINTS_FN fn) { AQH_STORAGE_GETLASTNDATAPOINTS_FN oldFn; @@ -468,6 +479,67 @@ uint64_t *AQH_Storage_GetDataPoints(AQH_STORAGE *sto, uint64_t valueId, uint64_t +uint64_t *AQH_Storage_GetFirstNDataPoints(AQH_STORAGE *sto, uint64_t valueId, uint64_t maxDataPointsRequested) +{ + AQH_DATAFILE *df; + uint64_t numEntries; + uint64_t numOfDataEntries; + uint64_t arrayLen; + uint64_t arrayPos; + uint64_t *arrayPtr; + uint64_t firstRecord; + uint64_t i; + + df=_getDataFileByValueId(sto, valueId); + if (df==NULL) { + DBG_ERROR(AQH_LOGDOMAIN, "No file for value id %lu", (unsigned long int) valueId); + return NULL; + } + numEntries=AQH_DataFile_GetNumberOfEntries(df); + numOfDataEntries=numEntries-1; /* first entry is reserved, don't count it here */ + if (numOfDataEntries<1) { + DBG_INFO(AQH_LOGDOMAIN, "No data records for value id %lu", (unsigned long int) valueId); + return NULL; + } + firstRecord=1; + if (numOfDataEntries>maxDataPointsRequested) /* more entries in file than requested */ + arrayLen=(maxDataPointsRequested*2)+1; /* +1 because the first array entry contains the number of entries */ + else + arrayLen=(numOfDataEntries*2)+1; + + arrayPtr=(uint64_t*) malloc(arrayLen*sizeof(uint64_t)); + if (arrayPtr==NULL) { + DBG_ERROR(AQH_LOGDOMAIN, "Not enough memory for %lu entries", (unsigned long int) arrayLen); + return NULL; + } + arrayPos=1; + + for (i=firstRecord; i=arrayLen) { + DBG_INFO(AQH_LOGDOMAIN, "Requested number of entries reached"); + break; + } + arrayPtr[arrayPos++]=ts; + arrayPtr[arrayPos++]=u.i; + } /* for */ + + arrayPtr[0]=arrayPos-1; + return arrayPtr; +} + + + uint64_t *AQH_Storage_GetLastNDataPoints(AQH_STORAGE *sto, uint64_t valueId, uint64_t maxDataPointsRequested) { AQH_DATAFILE *df; diff --git a/aqhome/data/storage.h b/aqhome/data/storage.h index c23152f..d30b257 100644 --- a/aqhome/data/storage.h +++ b/aqhome/data/storage.h @@ -53,6 +53,8 @@ typedef uint64_t *(*AQH_STORAGE_GETDATAPOINTS_FN)(AQH_STORAGE *sto, uint64_t val uint64_t maxArrayLen); typedef int (*AQH_STORAGE_GETFIRSTDATAPOINT_FN)(AQH_STORAGE *sto, uint64_t valueId, uint64_t *pTimestamp, double *pValue); typedef int (*AQH_STORAGE_GETLASTDATAPOINT_FN)(AQH_STORAGE *sto, uint64_t valueId, uint64_t *pTimestamp, double *pValue); + +typedef uint64_t *(*AQH_STORAGE_GETFIRSTNDATAPOINTS_FN)(AQH_STORAGE *sto, uint64_t valueId, uint64_t maxDataPointsRequested); typedef uint64_t *(*AQH_STORAGE_GETLASTNDATAPOINTS_FN)(AQH_STORAGE *sto, uint64_t valueId, uint64_t maxDataPointsRequested); @@ -96,6 +98,8 @@ AQHOME_API uint64_t *AQH_Storage_GetDataPoints(AQH_STORAGE *sto, uint64_t valueI uint64_t maxArrayLen); AQHOME_API int AQH_Storage_GetFirstDataPoint(AQH_STORAGE *sto, uint64_t valueId, uint64_t *pTimestamp, double *pValue); AQHOME_API int AQH_Storage_GetLastDataPoint(AQH_STORAGE *sto, uint64_t valueId, uint64_t *pTimestamp, double *pValue); + +AQHOME_API uint64_t *AQH_Storage_GetFirstNDataPoints(AQH_STORAGE *sto, uint64_t valueId, uint64_t maxDataPointsRequested); AQHOME_API uint64_t *AQH_Storage_GetLastNDataPoints(AQH_STORAGE *sto, uint64_t valueId, uint64_t maxDataPointsRequested); @@ -105,6 +109,8 @@ AQHOME_API AQH_STORAGE_ADDDATAPOINT_FN AQH_Storage_SetAddDatapointFn(AQH_STORAGE AQHOME_API AQH_STORAGE_GETDATAPOINTS_FN AQH_Storage_SetGetDatapointsFn(AQH_STORAGE *sto, AQH_STORAGE_GETDATAPOINTS_FN fn); AQHOME_API AQH_STORAGE_GETFIRSTDATAPOINT_FN AQH_Storage_SetGetFirstDatapointFn(AQH_STORAGE *sto, AQH_STORAGE_GETFIRSTDATAPOINT_FN fn); AQHOME_API AQH_STORAGE_GETLASTDATAPOINT_FN AQH_Storage_SetGetLastDatapointFn(AQH_STORAGE *sto, AQH_STORAGE_GETLASTDATAPOINT_FN fn); + +AQHOME_API AQH_STORAGE_GETFIRSTNDATAPOINTS_FN AQH_Storage_SetGetFirstNDatapointsFn(AQH_STORAGE *sto, AQH_STORAGE_GETFIRSTNDATAPOINTS_FN fn); AQHOME_API AQH_STORAGE_GETLASTNDATAPOINTS_FN AQH_Storage_SetGetLastNDatapointsFn(AQH_STORAGE *sto, AQH_STORAGE_GETLASTNDATAPOINTS_FN fn); diff --git a/aqhome/data/storage_p.h b/aqhome/data/storage_p.h index 0cc5701..c8cad6b 100644 --- a/aqhome/data/storage_p.h +++ b/aqhome/data/storage_p.h @@ -47,6 +47,7 @@ struct AQH_STORAGE { AQH_STORAGE_GETFIRSTDATAPOINT_FN getFirstDatapointFn; AQH_STORAGE_GETLASTDATAPOINT_FN getLastDatapointFn; AQH_STORAGE_GETLASTNDATAPOINTS_FN getLastNDatapointsFn; + AQH_STORAGE_GETFIRSTNDATAPOINTS_FN getFirstNDatapointsFn; };