Files
aqhomecontrol/apps/aqhome-cgi/modules/devices/mdevices_pagectx.c
2026-08-31 12:18:46 +02:00

218 lines
5.2 KiB
C

/****************************************************************************
* This file is part of the project AqHome.
* AqHome (c) by 2026 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 "./mdevices_pagectx_p.h"
#include <gwenhywfar/misc.h>
#include <gwenhywfar/text.h>
#include <gwenhywfar/debug.h>
/* ------------------------------------------------------------------------------------------------
* global vars
* ------------------------------------------------------------------------------------------------
*/
GWEN_TREE2_FUNCTIONS(AQH_MD_PAGECTX, AQH_MDPageCtx);
/* ------------------------------------------------------------------------------------------------
* forward declarations
* ------------------------------------------------------------------------------------------------
*/
static int _valueMatches(const AQH_VALUE *value, int varType, const char *room, int modality, const char *vname, uint64_t lastChange);
/* ------------------------------------------------------------------------------------------------
* code
* ------------------------------------------------------------------------------------------------
*/
AQH_MD_PAGECTX *AQH_MDPageCtx_new(AQCGI_MODULE *module, AQH_DATACLIENT *dataClient)
{
AQH_MD_PAGECTX *ctx;
GWEN_NEW_OBJECT(AQH_MD_PAGECTX, ctx);
if (ctx) {
GWEN_TREE2_INIT(AQH_MD_PAGECTX, ctx, AQH_MDPageCtx);
ctx->module=module;
ctx->dataClient=dataClient;
ctx->dbVars=GWEN_DB_Group_new("vars");
}
return ctx;
}
void AQH_MDPageCtx_free(AQH_MD_PAGECTX *ctx)
{
if (ctx) {
GWEN_DB_Group_free(ctx->dbVars);
AQH_Value_List_free(ctx->valueList);
GWEN_TREE2_FINI(AQH_MD_PAGECTX, ctx, AQH_MDPageCtx);
GWEN_FREE_OBJECT(ctx);
}
}
AQH_MD_PAGECTX *AQH_MDPageCtx_CopyWithMatchingVars(const AQH_MD_PAGECTX *octx,
int varType, const char *room, int modality, const char *vname, uint64_t lastChange)
{
AQH_MD_PAGECTX *ctx=NULL;
if (octx) {
ctx=AQH_MDPageCtx_new(octx->module, octx->dataClient);
if (ctx) {
/* copy dbVars */
GWEN_DB_Group_free(ctx->dbVars);
ctx->dbVars=GWEN_DB_Group_dup(octx->dbVars);
/* copy device list */
ctx->deviceList=octx->deviceList;
/* copy matching values from value list */
if (octx->valueList) {
AQH_VALUE_LIST *valueList;
const AQH_VALUE *vSrc;
DBG_ERROR(NULL, "Copying values");
valueList=AQH_Value_List_new();
vSrc=AQH_Value_List_First(octx->valueList);
while(vSrc) {
if (_valueMatches(vSrc, varType, room, modality, vname, lastChange)) {
AQH_VALUE *vDest;
DBG_ERROR(NULL, "+ %s", AQH_Value_GetNameForSystem(vSrc));
vDest=AQH_Value_dup(vSrc);
AQH_Value_List_Add(vDest, valueList);
}
vSrc=AQH_Value_List_Next(vSrc);
}
AQH_Value_List_free(ctx->valueList);
if (AQH_Value_List_GetCount(valueList))
ctx->valueList=valueList;
else
AQH_Value_List_free(valueList);
}
}
}
return ctx;
}
int _valueMatches(const AQH_VALUE *value, int varType, const char *room, int modality, const char *vname, uint64_t lastChange)
{
int matches=1;
/* fast compares first */
if ((varType!=AQH_ValueType_Unknown) && (AQH_Value_GetValueType(value)!=varType))
matches=0;
if (matches && modality && modality!=AQH_Value_GetModality(value))
matches=0;
/* then slower compares */
if (matches && lastChange && AQH_Value_GetTimestampLastChange(value)<lastChange)
matches=0;
if (matches && vname) {
const char *sName;
sName=AQH_Value_GetNameForSystem(value);
if (GWEN_Text_ComparePattern(sName, vname, 0)==-1)
matches=0;
}
if (matches && room) {
const AQH_DEVICE *device;
device=AQH_Value_GetDevicePtr(value);
if (device) {
const char *s;
s=AQH_Device_GetRoomName(device);
if (s && strcasecmp(s, room)!=0)
matches=0;
}
}
return matches?1:0;
}
const AQH_DEVICE_LIST *AQH_MDPageCtx_GetDevices(const AQH_MD_PAGECTX *ctx)
{
return ctx?ctx->deviceList:NULL;
}
void AQH_MDPageCtx_SetDevices(AQH_MD_PAGECTX *ctx, const AQH_DEVICE_LIST *dList)
{
if (ctx)
ctx->deviceList=dList;
}
AQH_VALUE_LIST *AQH_MDPageCtx_GetValues(const AQH_MD_PAGECTX *ctx)
{
return ctx?ctx->valueList:NULL;
}
void AQH_MDPageCtx_SetValues(AQH_MD_PAGECTX *ctx, AQH_VALUE_LIST *vList)
{
if (ctx)
ctx->valueList=vList;
}
GWEN_DB_NODE *AQH_MDPageCtx_GetDbVars(const AQH_MD_PAGECTX *ctx)
{
return ctx?ctx->dbVars:NULL;
}
AQCGI_MODULE *AQH_MDPageCtx_GetModule(const AQH_MD_PAGECTX *ctx)
{
return ctx?ctx->module:NULL;
}
AQH_DATACLIENT *AQH_MDPageCtx_GetDataClient(const AQH_MD_PAGECTX *ctx)
{
return ctx?ctx->dataClient:NULL;
}
AQH_VALUE *AQH_MDPageCtx_GetValue(const AQH_MD_PAGECTX *ctx, const char *vName)
{
return (ctx && ctx->valueList && vName)?AQH_Value_List_GetByNameForSystem(ctx->valueList, vName):NULL;
}