aqhome-react: added statistics modules (average, min, max)
This commit is contained in:
@@ -22,7 +22,8 @@
|
||||
#include "aqhome-react/units/u_zeroposnegstring.h"
|
||||
#include "aqhome-react/units/u_suntime.h"
|
||||
#include "aqhome-react/units/u_varchanges.h"
|
||||
|
||||
#include "aqhome-react/units/u_timeprogram.h"
|
||||
#include "aqhome-react/units/u_statfns.h"
|
||||
|
||||
#include <gwenhywfar/misc.h>
|
||||
#include <gwenhywfar/debug.h>
|
||||
@@ -302,6 +303,14 @@ AQHREACT_UNIT *AqHomeReact_CreateUnitByName(AQHOME_REACT *aqh, const char *unitT
|
||||
return AqHomeReact_UnitZeroPosNegString_new(aqh);
|
||||
else if (strcasecmp(unitType, "suntime")==0)
|
||||
return AqHomeReact_UnitSuntime_new(aqh);
|
||||
else if (strcasecmp(unitType, "timeraction")==0)
|
||||
return AqHomeReact_UnitTimeProgram_new(aqh);
|
||||
else if (strcasecmp(unitType, "average")==0)
|
||||
return AqHomeReact_UnitAverage_new(aqh);
|
||||
else if (strcasecmp(unitType, "minvalue")==0)
|
||||
return AqHomeReact_UnitMinValue_new(aqh);
|
||||
else if (strcasecmp(unitType, "maxvalue")==0)
|
||||
return AqHomeReact_UnitMaxValue_new(aqh);
|
||||
else {
|
||||
AQHREACT_UNIT *unit;
|
||||
|
||||
|
||||
@@ -54,6 +54,7 @@
|
||||
u_suntime_p.h
|
||||
u_timeprogram.h
|
||||
u_timeprogram_p.h
|
||||
u_statfns.h
|
||||
</headers>
|
||||
|
||||
<sources>
|
||||
@@ -72,6 +73,7 @@
|
||||
u_module.c
|
||||
u_suntime.c
|
||||
u_timeprogram.c
|
||||
u_statfns.c
|
||||
</sources>
|
||||
|
||||
<useTargets>
|
||||
|
||||
277
apps/aqhome-react/units/u_statfns.c
Normal file
277
apps/aqhome-react/units/u_statfns.c
Normal file
@@ -0,0 +1,277 @@
|
||||
/****************************************************************************
|
||||
* This file is part of the project AqHome.
|
||||
* AqHome (c) by 2024 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 "./u_statfns.h"
|
||||
|
||||
#include <gwenhywfar/debug.h>
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* defines
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#define AQHOMEREACT_UNIT_STATFNS_INSLOT_INPUT 100
|
||||
#define AQHOMEREACT_UNIT_STATFNS_INSLOT_AUTOINPUT 101
|
||||
|
||||
#define AQHOMEREACT_UNIT_STATFNS_OUTSLOT_RESULT 0
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* forward declarations
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
static AQHREACT_UNIT *_unitStatFns_new(AQHOME_REACT *aqh);
|
||||
static int _outputResult(AQHREACT_UNIT *unit, AQHREACT_PORT *port, double result);
|
||||
static int _cbProcessAvg(AQHREACT_UNIT *unit);
|
||||
static int _cbProcessMin(AQHREACT_UNIT *unit);
|
||||
static int _cbProcessMax(AQHREACT_UNIT *unit);
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* implementations
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
|
||||
AQHREACT_UNIT *AqHomeReact_UnitAverage_new(AQHOME_REACT *aqh)
|
||||
{
|
||||
AQHREACT_UNIT *unit;
|
||||
|
||||
unit=_unitStatFns_new(aqh);
|
||||
AQHREACT_Unit_SetTypeName(unit, "average");
|
||||
AQHREACT_Unit_SetDescription(unit, "Average over inputs");
|
||||
AQHREACT_Unit_SetProcessFn(unit, _cbProcessAvg);
|
||||
return unit;
|
||||
}
|
||||
|
||||
|
||||
|
||||
AQHREACT_UNIT *AqHomeReact_UnitMinValue_new(AQHOME_REACT *aqh)
|
||||
{
|
||||
AQHREACT_UNIT *unit;
|
||||
|
||||
unit=_unitStatFns_new(aqh);
|
||||
AQHREACT_Unit_SetTypeName(unit, "minvalue");
|
||||
AQHREACT_Unit_SetDescription(unit, "Smallest value from all inputs");
|
||||
AQHREACT_Unit_SetProcessFn(unit, _cbProcessMin);
|
||||
return unit;
|
||||
}
|
||||
|
||||
|
||||
|
||||
AQHREACT_UNIT *AqHomeReact_UnitMaxValue_new(AQHOME_REACT *aqh)
|
||||
{
|
||||
AQHREACT_UNIT *unit;
|
||||
|
||||
unit=_unitStatFns_new(aqh);
|
||||
AQHREACT_Unit_SetTypeName(unit, "maxvalue");
|
||||
AQHREACT_Unit_SetDescription(unit, "Highest value from all inputs");
|
||||
AQHREACT_Unit_SetProcessFn(unit, _cbProcessMax);
|
||||
return unit;
|
||||
}
|
||||
|
||||
|
||||
|
||||
AQHREACT_UNIT *_unitStatFns_new(AQHOME_REACT *aqh)
|
||||
{
|
||||
AQHREACT_UNIT *unit;
|
||||
AQHREACT_PORT *port;
|
||||
|
||||
unit=AQHREACT_Unit_new(aqh);
|
||||
|
||||
AQHREACT_Unit_SetNextInputPortId(unit, AQHOMEREACT_UNIT_STATFNS_INSLOT_AUTOINPUT); /* for auto-gen multi-slots */
|
||||
|
||||
port=AQHREACT_Port_new();
|
||||
AQHREACT_Port_SetName(port, "output");
|
||||
AQHREACT_Port_SetIdForUnit(port, AQHOMEREACT_UNIT_STATFNS_OUTSLOT_RESULT);
|
||||
AQHREACT_Port_SetDataType(port, AQHREACT_DATAOBJECTTYPE_DOUBLE);
|
||||
AQHREACT_Unit_AddOutputPort(unit, port);
|
||||
|
||||
port=AQHREACT_Port_new();
|
||||
AQHREACT_Port_SetName(port, "input");
|
||||
AQHREACT_Port_SetIdForUnit(port, AQHOMEREACT_UNIT_STATFNS_INSLOT_INPUT);
|
||||
AQHREACT_Port_SetDataType(port, AQHREACT_DATAOBJECTTYPE_DOUBLE);
|
||||
AQHREACT_Port_AddFlags(port, AQHREACT_UNIT_FLAGS_MULTI);
|
||||
AQHREACT_Unit_AddInputPort(unit, port);
|
||||
|
||||
return unit;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _outputResult(AQHREACT_UNIT *unit, AQHREACT_PORT *port, double result)
|
||||
{
|
||||
DBG_INFO(NULL, "%s: Sending result %f", AQHREACT_Unit_GetId(unit), result);
|
||||
AQHREACT_Unit_OutputDoubleData(unit, port, result);
|
||||
return 1; /* we changed something */
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _cbProcessAvg(AQHREACT_UNIT *unit)
|
||||
{
|
||||
if (unit && AQHREACT_Unit_InputHasChanged(unit)) {
|
||||
AQHREACT_PORT *outputPort;
|
||||
|
||||
outputPort=AQHREACT_Unit_GetOutputPortByIdForUnit(unit, AQHOMEREACT_UNIT_STATFNS_OUTSLOT_RESULT);
|
||||
if (outputPort) {
|
||||
const AQHREACT_PORT *port;
|
||||
double sum=0.0;
|
||||
int numHandledInputs=0;
|
||||
|
||||
port=AQHREACT_Port_List_First(AQHREACT_Unit_GetInputPortList(unit));
|
||||
while(port) {
|
||||
const char *portName;
|
||||
const AQHREACT_DATAOBJECT *dataObject;
|
||||
|
||||
portName=AQHREACT_Port_GetName(port);
|
||||
dataObject=AQHREACT_Port_GetCurrentDataObject(port);
|
||||
if (dataObject) {
|
||||
if (AQHREACT_DataObject_GetDataType(dataObject)==AQHREACT_DATAOBJECTTYPE_DOUBLE) {
|
||||
double d;
|
||||
|
||||
d=AQHREACT_DataObject_GetDoubleData(dataObject);
|
||||
sum+=d;
|
||||
numHandledInputs++;
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(NULL, "Data at input slot \"%s\" isn't DOUBLE, ignoring", portName?portName:"<unnamed>");
|
||||
}
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(NULL, "Data at input slot \"%s\" has no current data, ignoring", portName?portName:"<unnamed>");
|
||||
}
|
||||
port=AQHREACT_Port_List_Next(port);
|
||||
}
|
||||
|
||||
AQHREACT_Unit_ClearChangeFlagsInUnitAndInputPorts(unit);
|
||||
if (numHandledInputs) {
|
||||
double result;
|
||||
|
||||
result=sum/numHandledInputs;
|
||||
return _outputResult(unit, outputPort, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _cbProcessMin(AQHREACT_UNIT *unit)
|
||||
{
|
||||
if (unit && AQHREACT_Unit_InputHasChanged(unit)) {
|
||||
AQHREACT_PORT *outputPort;
|
||||
|
||||
outputPort=AQHREACT_Unit_GetOutputPortByIdForUnit(unit, AQHOMEREACT_UNIT_STATFNS_OUTSLOT_RESULT);
|
||||
if (outputPort) {
|
||||
const AQHREACT_PORT *port;
|
||||
double value=0.0;
|
||||
int numHandledInputs=0;
|
||||
|
||||
port=AQHREACT_Port_List_First(AQHREACT_Unit_GetInputPortList(unit));
|
||||
while(port) {
|
||||
const char *portName;
|
||||
const AQHREACT_DATAOBJECT *dataObject;
|
||||
|
||||
portName=AQHREACT_Port_GetName(port);
|
||||
dataObject=AQHREACT_Port_GetCurrentDataObject(port);
|
||||
if (dataObject) {
|
||||
if (AQHREACT_DataObject_GetDataType(dataObject)==AQHREACT_DATAOBJECTTYPE_DOUBLE) {
|
||||
double d;
|
||||
|
||||
d=AQHREACT_DataObject_GetDoubleData(dataObject);
|
||||
if (numHandledInputs==0)
|
||||
value=d;
|
||||
else
|
||||
value=(d<value)?d:value;
|
||||
numHandledInputs++;
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(NULL, "Data at input slot \"%s\" isn't DOUBLE, ignoring", portName?portName:"<unnamed>");
|
||||
}
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(NULL, "Data at input slot \"%s\" has no current data, ignoring", portName?portName:"<unnamed>");
|
||||
}
|
||||
port=AQHREACT_Port_List_Next(port);
|
||||
}
|
||||
|
||||
AQHREACT_Unit_ClearChangeFlagsInUnitAndInputPorts(unit);
|
||||
if (numHandledInputs) {
|
||||
return _outputResult(unit, outputPort, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _cbProcessMax(AQHREACT_UNIT *unit)
|
||||
{
|
||||
if (unit && AQHREACT_Unit_InputHasChanged(unit)) {
|
||||
AQHREACT_PORT *outputPort;
|
||||
|
||||
outputPort=AQHREACT_Unit_GetOutputPortByIdForUnit(unit, AQHOMEREACT_UNIT_STATFNS_OUTSLOT_RESULT);
|
||||
if (outputPort) {
|
||||
const AQHREACT_PORT *port;
|
||||
double value=0.0;
|
||||
int numHandledInputs=0;
|
||||
|
||||
port=AQHREACT_Port_List_First(AQHREACT_Unit_GetInputPortList(unit));
|
||||
while(port) {
|
||||
const char *portName;
|
||||
const AQHREACT_DATAOBJECT *dataObject;
|
||||
|
||||
portName=AQHREACT_Port_GetName(port);
|
||||
dataObject=AQHREACT_Port_GetCurrentDataObject(port);
|
||||
if (dataObject) {
|
||||
if (AQHREACT_DataObject_GetDataType(dataObject)==AQHREACT_DATAOBJECTTYPE_DOUBLE) {
|
||||
double d;
|
||||
|
||||
d=AQHREACT_DataObject_GetDoubleData(dataObject);
|
||||
if (numHandledInputs==0)
|
||||
value=d;
|
||||
else
|
||||
value=(d>value)?d:value;
|
||||
numHandledInputs++;
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(NULL, "Data at input slot \"%s\" isn't DOUBLE, ignoring", portName?portName:"<unnamed>");
|
||||
}
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(NULL, "Data at input slot \"%s\" has no current data, ignoring", portName?portName:"<unnamed>");
|
||||
}
|
||||
port=AQHREACT_Port_List_Next(port);
|
||||
}
|
||||
|
||||
AQHREACT_Unit_ClearChangeFlagsInUnitAndInputPorts(unit);
|
||||
if (numHandledInputs) {
|
||||
return _outputResult(unit, outputPort, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
26
apps/aqhome-react/units/u_statfns.h
Normal file
26
apps/aqhome-react/units/u_statfns.h
Normal file
@@ -0,0 +1,26 @@
|
||||
/****************************************************************************
|
||||
* This file is part of the project AqHome.
|
||||
* AqHome (c) by 2024 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 AQHOMEREACT_U_STATFNS_H
|
||||
#define AQHOMEREACT_U_STATFNS_H
|
||||
|
||||
|
||||
#include "aqhome-react/aqhome_react.h"
|
||||
#include "aqhome-react/types/unit.h"
|
||||
|
||||
|
||||
AQHREACT_UNIT *AqHomeReact_UnitAverage_new(AQHOME_REACT *aqh);
|
||||
AQHREACT_UNIT *AqHomeReact_UnitMinValue_new(AQHOME_REACT *aqh);
|
||||
AQHREACT_UNIT *AqHomeReact_UnitMaxValue_new(AQHOME_REACT *aqh);
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user