Storage: make storage class virtual with default implementations.

This commit is contained in:
Martin Preuss
2024-02-11 20:07:30 +01:00
parent b6fe1775bd
commit 5888bc9068
3 changed files with 184 additions and 34 deletions

View File

@@ -39,16 +39,26 @@ static GWEN_BUFFER *_getDataFilePathForValueId(const AQH_STORAGE *sto, uint64_t
/* ------------------------------------------------------------------------------------------------ /* ------------------------------------------------------------------------------------------------
* implementations * GWEN macros
* ------------------------------------------------------------------------------------------------ * ------------------------------------------------------------------------------------------------
*/ */
GWEN_INHERIT_FUNCTIONS(AQH_STORAGE)
/* ------------------------------------------------------------------------------------------------
* implementations
* ------------------------------------------------------------------------------------------------
*/
AQH_STORAGE *AQH_Storage_new(void) AQH_STORAGE *AQH_Storage_new(void)
{ {
AQH_STORAGE *sto; AQH_STORAGE *sto;
GWEN_NEW_OBJECT(AQH_STORAGE, sto); GWEN_NEW_OBJECT(AQH_STORAGE, sto);
GWEN_INHERIT_INIT(AQH_STORAGE, sto);
sto->deviceList=AQH_Device_List_new(); sto->deviceList=AQH_Device_List_new();
sto->valueList=AQH_Value_List_new(); sto->valueList=AQH_Value_List_new();
sto->dataFileList=AQH_DataFile_List_new(); sto->dataFileList=AQH_DataFile_List_new();
@@ -61,6 +71,7 @@ AQH_STORAGE *AQH_Storage_new(void)
void AQH_Storage_free(AQH_STORAGE *sto) void AQH_Storage_free(AQH_STORAGE *sto)
{ {
if (sto) { if (sto) {
GWEN_INHERIT_FINI(AQH_STORAGE, sto);
AQH_DataFile_List_free(sto->dataFileList); AQH_DataFile_List_free(sto->dataFileList);
AQH_Value_List_free(sto->valueList); AQH_Value_List_free(sto->valueList);
AQH_Device_List_free(sto->deviceList); AQH_Device_List_free(sto->deviceList);
@@ -194,6 +205,116 @@ void AQH_Storage_SubRuntimeFlags(AQH_STORAGE *sto, uint32_t flags)
int AQH_Storage_WriteState(AQH_STORAGE *sto)
{
int rv;
rv=AQH_Storage_WriteStateFile(sto, sto->stateFile);
if (rv<0) {
DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
return rv;
}
sto->runtimeFlags&=~AQH_STORAGE_RTFLAGS_MODIFIED;
return 0;
}
const char *AQH_Storage_GetDataFileFolder(const AQH_STORAGE *sto)
{
return sto?sto->dataFileFolder:NULL;
}
void AQH_Storage_SetDataFileFolder(AQH_STORAGE *sto, const char *s)
{
if (sto) {
free(sto->dataFileFolder);
sto->dataFileFolder=s?strdup(s):NULL;
}
}
AQH_STORAGE_INIT_FN AQH_Storage_SetInitFn(AQH_STORAGE *sto, AQH_STORAGE_INIT_FN fn)
{
AQH_STORAGE_INIT_FN oldFn;
oldFn=sto->initFn;
sto->initFn=fn;
return oldFn;
}
AQH_STORAGE_FINI_FN AQH_Storage_SetFiniFn(AQH_STORAGE *sto, AQH_STORAGE_FINI_FN fn)
{
AQH_STORAGE_FINI_FN oldFn;
oldFn=sto->finiFn;
sto->finiFn=fn;
return oldFn;
}
AQH_STORAGE_ADDDATAPOINT_FN AQH_Storage_SetAddDatapointFn(AQH_STORAGE *sto, AQH_STORAGE_ADDDATAPOINT_FN fn)
{
AQH_STORAGE_ADDDATAPOINT_FN oldFn;
oldFn=sto->addDatapointFn;
sto->addDatapointFn=fn;
return oldFn;
}
AQH_STORAGE_GETDATAPOINTS_FN AQH_Storage_SetGetDatapointsFn(AQH_STORAGE *sto, AQH_STORAGE_GETDATAPOINTS_FN fn)
{
AQH_STORAGE_GETDATAPOINTS_FN oldFn;
oldFn=sto->getDatapointsFn;
sto->getDatapointsFn=fn;
return oldFn;
}
AQH_STORAGE_GETFIRSTDATAPOINT_FN AQH_Storage_SetGetFirstDatapointFn(AQH_STORAGE *sto, AQH_STORAGE_GETFIRSTDATAPOINT_FN fn)
{
AQH_STORAGE_GETFIRSTDATAPOINT_FN oldFn;
oldFn=sto->getFirstDatapointFn;
sto->getFirstDatapointFn=fn;
return oldFn;
}
AQH_STORAGE_GETLASTDATAPOINT_FN AQH_Storage_SetGetLastDatapointFn(AQH_STORAGE *sto, AQH_STORAGE_GETLASTDATAPOINT_FN fn)
{
AQH_STORAGE_GETLASTDATAPOINT_FN oldFn;
oldFn=sto->getLastDatapointFn;
sto->getLastDatapointFn=fn;
return oldFn;
}
AQH_STORAGE_GETLASTNDATAPOINTS_FN AQH_Storage_SetGetLastNDatapointsFn(AQH_STORAGE *sto, AQH_STORAGE_GETLASTNDATAPOINTS_FN fn)
{
AQH_STORAGE_GETLASTNDATAPOINTS_FN oldFn;
oldFn=sto->getLastNDatapointsFn;
sto->getLastNDatapointsFn=fn;
return oldFn;
}
int AQH_Storage_Init(AQH_STORAGE *sto) int AQH_Storage_Init(AQH_STORAGE *sto)
{ {
int rv; int rv;
@@ -244,39 +365,6 @@ int AQH_Storage_Fini(AQH_STORAGE *sto)
int AQH_Storage_WriteState(AQH_STORAGE *sto)
{
int rv;
rv=AQH_Storage_WriteStateFile(sto, sto->stateFile);
if (rv<0) {
DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
return rv;
}
sto->runtimeFlags&=~AQH_STORAGE_RTFLAGS_MODIFIED;
return 0;
}
const char *AQH_Storage_GetDataFileFolder(const AQH_STORAGE *sto)
{
return sto?sto->dataFileFolder:NULL;
}
void AQH_Storage_SetDataFileFolder(AQH_STORAGE *sto, const char *s)
{
if (sto) {
free(sto->dataFileFolder);
sto->dataFileFolder=s?strdup(s):NULL;
}
}
int AQH_Storage_AddDatapoint(AQH_STORAGE *sto, uint64_t valueId, uint64_t timestamp, double dataPoint) int AQH_Storage_AddDatapoint(AQH_STORAGE *sto, uint64_t valueId, uint64_t timestamp, double dataPoint)
{ {
AQH_DATAFILE *df; AQH_DATAFILE *df;
@@ -471,6 +559,34 @@ int AQH_Storage_GetLastDataPoint(AQH_STORAGE *sto, uint64_t valueId, uint64_t *p
int AQH_Storage_GetFirstDataPoint(AQH_STORAGE *sto, uint64_t valueId, uint64_t *pTimestamp, double *pValue)
{
AQH_DATAFILE *df;
uint64_t numEntries;
int rv;
df=_getDataFileByValueId(sto, valueId);
if (df==NULL) {
DBG_ERROR(AQH_LOGDOMAIN, "No file for value id %lu", (unsigned long int) valueId);
return GWEN_ERROR_INVALID;
}
numEntries=AQH_DataFile_GetNumberOfEntries(df);
if (numEntries<2) {
DBG_INFO(AQH_LOGDOMAIN, "No data entries in file");
return GWEN_ERROR_NO_DATA;
}
rv=AQH_DataFile_ReadRecord(df, 1, pTimestamp, pValue);
if (rv<0) {
DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
return rv;
}
return 0;
}
AQH_DATAFILE *_getDataFileByValueId(AQH_STORAGE *sto, uint64_t valueId) AQH_DATAFILE *_getDataFileByValueId(AQH_STORAGE *sto, uint64_t valueId)
{ {

View File

@@ -12,6 +12,8 @@
#include <aqhome/api.h> #include <aqhome/api.h>
#include <gwenhywfar/inherit.h>
#ifdef __cplusplus #ifdef __cplusplus
@@ -20,6 +22,8 @@ extern "C" {
typedef struct AQH_STORAGE AQH_STORAGE; typedef struct AQH_STORAGE AQH_STORAGE;
GWEN_INHERIT_FUNCTION_LIB_DEFS(AQH_STORAGE, AQHOME_API)
#ifdef __cplusplus #ifdef __cplusplus
@@ -40,6 +44,17 @@ typedef struct AQH_STORAGE AQH_STORAGE;
extern "C" { extern "C" {
#endif #endif
typedef int (*AQH_STORAGE_INIT_FN)(AQH_STORAGE *sto);
typedef int (*AQH_STORAGE_FINI_FN)(AQH_STORAGE *sto);
typedef int (*AQH_STORAGE_ADDDATAPOINT_FN)(AQH_STORAGE *sto, uint64_t valueId, uint64_t timestamp, double dataPoint);
typedef uint64_t *(*AQH_STORAGE_GETDATAPOINTS_FN)(AQH_STORAGE *sto, uint64_t valueId,
uint64_t fromTime, uint64_t toTime,
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_GETLASTNDATAPOINTS_FN)(AQH_STORAGE *sto, uint64_t valueId, uint64_t maxDataPointsRequested);
/** /**
@@ -74,14 +89,23 @@ AQHOME_API int AQH_Storage_Fini(AQH_STORAGE *sto);
AQHOME_API int AQH_Storage_WriteState(AQH_STORAGE *sto); AQHOME_API int AQH_Storage_WriteState(AQH_STORAGE *sto);
AQHOME_API int AQH_Storage_AddDatapoint(AQH_STORAGE *sto, uint64_t valueId, uint64_t timestamp, double dataPoint); AQHOME_API int AQH_Storage_AddDatapoint(AQH_STORAGE *sto, uint64_t valueId, uint64_t timestamp, double dataPoint);
AQHOME_API uint64_t *AQH_Storage_GetDataPoints(AQH_STORAGE *sto, uint64_t valueId, AQHOME_API uint64_t *AQH_Storage_GetDataPoints(AQH_STORAGE *sto, uint64_t valueId,
uint64_t fromTime, uint64_t toTime, uint64_t fromTime, uint64_t toTime,
uint64_t maxArrayLen); 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 int AQH_Storage_GetLastDataPoint(AQH_STORAGE *sto, uint64_t valueId, uint64_t *pTimestamp, double *pValue);
AQHOME_API uint64_t *AQH_Storage_GetLastNDataPoints(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);
AQHOME_API AQH_STORAGE_INIT_FN AQH_Storage_SetInitFn(AQH_STORAGE *sto, AQH_STORAGE_INIT_FN fn);
AQHOME_API AQH_STORAGE_FINI_FN AQH_Storage_SetFiniFn(AQH_STORAGE *sto, AQH_STORAGE_FINI_FN fn);
AQHOME_API AQH_STORAGE_ADDDATAPOINT_FN AQH_Storage_SetAddDatapointFn(AQH_STORAGE *sto, AQH_STORAGE_ADDDATAPOINT_FN fn);
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_GETLASTNDATAPOINTS_FN AQH_Storage_SetGetLastNDatapointsFn(AQH_STORAGE *sto, AQH_STORAGE_GETLASTNDATAPOINTS_FN fn);
#ifdef __cplusplus #ifdef __cplusplus

View File

@@ -25,6 +25,8 @@
struct AQH_STORAGE { struct AQH_STORAGE {
GWEN_INHERIT_ELEMENT(AQH_STORAGE)
AQH_VALUE_LIST *valueList; AQH_VALUE_LIST *valueList;
AQH_DEVICE_LIST *deviceList; AQH_DEVICE_LIST *deviceList;
@@ -37,6 +39,14 @@ struct AQH_STORAGE {
AQH_DATAFILE_LIST *dataFileList; AQH_DATAFILE_LIST *dataFileList;
uint32_t runtimeFlags; uint32_t runtimeFlags;
AQH_STORAGE_INIT_FN initFn;
AQH_STORAGE_FINI_FN finiFn;
AQH_STORAGE_ADDDATAPOINT_FN addDatapointFn;
AQH_STORAGE_GETDATAPOINTS_FN getDatapointsFn;
AQH_STORAGE_GETFIRSTDATAPOINT_FN getFirstDatapointFn;
AQH_STORAGE_GETLASTDATAPOINT_FN getLastDatapointFn;
AQH_STORAGE_GETLASTNDATAPOINTS_FN getLastNDatapointsFn;
}; };