Files
aqhomecontrol/aqhome/msg/msg_value3.c
2024-09-07 14:57:40 +02:00

183 lines
5.2 KiB
C

/****************************************************************************
* This file is part of the project AqHome.
* AqHome (c) by 2023 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 "aqhome/msg/msg_value3.h"
#include <gwenhywfar/misc.h>
#include <gwenhywfar/list.h>
#include <gwenhywfar/error.h>
#include <gwenhywfar/debug.h>
#define AQH_MSG_OFFS_VALUE3_UID 0 /* 4 bytes */
#define AQH_MSG_OFFS_VALUE3_MSGID 4 /* 2 bytes */
#define AQH_MSG_OFFS_VALUE3_VALUEID 6 /* 1 byte */
#define AQH_MSG_OFFS_VALUE3_VALUETYPE 7 /* 1 byte */
#define AQH_MSG_OFFS_VALUE3_VALUE 8 /* 2 bytes */
#define AQH_MSG_OFFS_VALUE3_DENOM 10 /* 2 bytes */
#define AQH_MSG_VALUE3_MINSIZE (AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE3_DENOM+2)
uint32_t AQH_Value3Msg_GetUid(const GWEN_MSG *msg)
{
return GWEN_Msg_GetUint32At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE3_UID, 0);
}
int16_t AQH_Value3Msg_GetMsgId(const GWEN_MSG *msg)
{
return (int16_t) GWEN_Msg_GetUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE3_MSGID, 0);
}
uint8_t AQH_Value3Msg_GetValueId(const GWEN_MSG *msg)
{
return GWEN_Msg_GetUint8At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE3_VALUEID, 0);
}
uint8_t AQH_Value3Msg_GetValueType(const GWEN_MSG *msg)
{
return GWEN_Msg_GetUint8At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE3_VALUETYPE, 0);
}
int16_t AQH_Value3Msg_GetValueNom(const GWEN_MSG *msg)
{
return (int16_t) GWEN_Msg_GetUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE3_VALUE, 0);
}
int16_t AQH_Value3Msg_GetValueDenom(const GWEN_MSG *msg)
{
return (int16_t) GWEN_Msg_GetUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE3_DENOM, 0);
}
const char *AQH_Value3Msg_GetValueTypeName(const GWEN_MSG *msg)
{
uint8_t t;
t=AQH_Value3Msg_GetValueType(msg);
switch(t) {
case AQH_MSG_VALUE3_TYPE_TEMP: return "temperature";
case AQH_MSG_VALUE3_TYPE_HUMIDITY: return "humidity";
case AQH_MSG_VALUE3_TYPE_DOOR: return "door_window";
default: break;
}
return "unknown";
}
const char *AQH_Value3Msg_GetValueAsWindowStateString(const GWEN_MSG *msg)
{
switch(AQH_Value3Msg_GetValueNom(msg)) {
case 0: return "closed";
case 128: return "tilted";
case 255: return "open";
default: break;
}
return "unknown";
}
const char *AQH_Value3Msg_GetValueTypeUnits(const GWEN_MSG *msg)
{
uint8_t t;
t=AQH_Value3Msg_GetValueType(msg);
switch(t) {
case AQH_MSG_VALUE3_TYPE_TEMP: return "Celsius";
case AQH_MSG_VALUE3_TYPE_HUMIDITY: return "%";
case AQH_MSG_VALUE3_TYPE_DOOR: return NULL;
default: break;
}
return NULL;
}
double AQH_Value3Msg_GetValue(const GWEN_MSG *msg)
{
if (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_VALUE3_MINSIZE) {
const uint8_t *ptr;
double value;
double denom;
uint16_t intDenom;
ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN;
value=(double)((ptr[AQH_MSG_OFFS_VALUE3_VALUE])+(ptr[AQH_MSG_OFFS_VALUE3_VALUE+1]<<8));
intDenom=(ptr[AQH_MSG_OFFS_VALUE3_DENOM])+(ptr[AQH_MSG_OFFS_VALUE3_DENOM+1]<<8);
denom=(double)(intDenom);
if (intDenom==0)
denom=1.0;
return (double)(value/denom);
}
return 0.0;
}
void AQH_Value3Msg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText)
{
if (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_VALUE3_MINSIZE) {
const char *sCmd;
switch(AQH_NodeMsg_GetMsgType(msg)) {
case AQH_MSG_TYPE_VALUE_REPORT: sCmd="report"; break;
case AQH_MSG_TYPE_VALUE_SET: sCmd="set"; break;
case AQH_MSG_TYPE_VALUE_SET_ACK: sCmd="ack"; break;
case AQH_MSG_TYPE_VALUE_SET_NACK: sCmd="nack"; break;
default: sCmd="unknown"; break;
}
if (AQH_Value3Msg_GetValueType(msg)==AQH_MSG_VALUE3_TYPE_DOOR)
GWEN_Buffer_AppendArgs(dbuf, "0x%02x->0x%02x: VALUE3(%s) %s (uid=0x%08x, msgId=%u, value_id=0x%02x type=%s value=%s)\n",
AQH_NodeMsg_GetSourceAddress(msg),
AQH_NodeMsg_GetDestAddress(msg),
sCmd,
sText,
(unsigned int) AQH_Value3Msg_GetUid(msg),
(unsigned int)AQH_Value3Msg_GetMsgId(msg),
AQH_Value3Msg_GetValueId(msg),
AQH_Value3Msg_GetValueTypeName(msg),
AQH_Value3Msg_GetValueAsWindowStateString(msg));
else
GWEN_Buffer_AppendArgs(dbuf, "0x%02x->0x%02x: VALUE3(%s) %s (uid=0x%08x, msgId=%u, value_id=0x%02x type=%s value=%f)\n",
AQH_NodeMsg_GetSourceAddress(msg),
AQH_NodeMsg_GetDestAddress(msg),
sCmd,
sText,
(unsigned int) AQH_Value3Msg_GetUid(msg),
(unsigned int)AQH_Value3Msg_GetMsgId(msg),
AQH_Value3Msg_GetValueId(msg),
AQH_Value3Msg_GetValueTypeName(msg),
AQH_Value3Msg_GetValue(msg));
}
}