From cface0d4b215c4247fb101dd4e01fd7e96dda3c6 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Mon, 15 Mar 2021 17:16:19 +0100 Subject: [PATCH] lower and upper bound --- sink/sink20169.c | 60 ++++++++++++++++++++++++++++------------------ sink/sink20169.cfg | 3 +++ sink/sinkStruct.h | 5 ++-- 3 files changed, 42 insertions(+), 26 deletions(-) diff --git a/sink/sink20169.c b/sink/sink20169.c index 8fc4720..b6db22c 100644 --- a/sink/sink20169.c +++ b/sink/sink20169.c @@ -41,6 +41,8 @@ typedef struct { typedef struct { t_configHandle *configHandle; + uint32_t lowerBound; + uint32_t upperBound; const char *influxUser; const char *influxPass; const char *influxServer; @@ -236,6 +238,14 @@ int initForwarder(t_configHandle *configHandle, t_forwarderHandle *handle) { } logmsg(LOG_INFO, "influxUrl is %s", handle->influxUrl); + uint32_t lowerBound = 45000; + config_lookup_int(&(configHandle->cfg), "lowerBound", &lowerBound); + handle->lowerBound = lowerBound; + uint32_t upperBound = 55000; + config_lookup_int(&(configHandle->cfg), "upperBound", &upperBound); + handle->upperBound = upperBound; + logmsg(LOG_INFO, "lowerBound: %u, upperBound: %u", lowerBound, upperBound); + return 0; } @@ -280,30 +290,34 @@ int forwardMinuteBuffer(t_forwarderHandle *handle, t_minuteBuffer *buf) { for (uint8_t j = 0; j < SECONDS_PER_MINUTE; j++) { uint64_t timestamp = buf->s.timestamp + j; logmsg(LOG_DEBUG, "Time: %lu, Frequency: %u", timestamp, buf->s.frequency[j]); + + if ((buf->s.frequency[j] >= handle->lowerBound) && (buf->s.frequency[j] <= handle->upperBound)) { + int frequency_before_point = buf->s.frequency[j] / 1000; + int frequency_behind_point = buf->s.frequency[j] - (frequency_before_point * 1000); - int frequency_before_point = buf->s.frequency[j] / 1000; - int frequency_behind_point = buf->s.frequency[j] - (frequency_before_point * 1000); - - char payload[256]; - int res = snprintf(payload, sizeof(payload), - "%s,valid=1,location=%s,host=%s freq=%d.%03d" -#ifdef OpenBSD - " %llu" -#else - " %lu" -#endif - "", - handle->influxMeasurement, location, buf->s.deviceId, - frequency_before_point, frequency_behind_point, - timestamp); - if (res > sizeof(payload)) { - logmsg(LOG_ERR, "payload buffer to small"); - return -1; - } - logmsg(LOG_DEBUG, "Payload: %s", payload); - res = httpPostRequest(handle->influxUrl, handle->influxUser, handle->influxPass, payload); - if (res == 0) { - logmsg(LOG_DEBUG, "Successfully sent to InfluxDB"); + char payload[256]; + int res = snprintf(payload, sizeof(payload), + "%s,valid=1,location=%s,host=%s freq=%d.%03d" + #ifdef OpenBSD + " %llu" + #else + " %lu" + #endif + "", + handle->influxMeasurement, location, buf->s.deviceId, + frequency_before_point, frequency_behind_point, + timestamp); + if (res > sizeof(payload)) { + logmsg(LOG_ERR, "payload buffer to small"); + return -1; + } + logmsg(LOG_DEBUG, "Payload: %s", payload); + res = httpPostRequest(handle->influxUrl, handle->influxUser, handle->influxPass, payload); + if (res == 0) { + logmsg(LOG_DEBUG, "Successfully sent to InfluxDB"); + } + } else { + logmsg(LOG_ERR, "%u out of bound", buf->s.frequency[j]); } } diff --git a/sink/sink20169.cfg b/sink/sink20169.cfg index 3130de4..8fd56f5 100644 --- a/sink/sink20169.cfg +++ b/sink/sink20169.cfg @@ -5,6 +5,9 @@ influxPort = 8086; influxDatabase = "smarthome2"; influxMeasurement = "mainsfrequency"; +lowerBound = 45000; +upperBound = 55000; + receivePort = 20169; devices = ( diff --git a/sink/sinkStruct.h b/sink/sinkStruct.h index d2f8f61..b2b6757 100644 --- a/sink/sinkStruct.h +++ b/sink/sinkStruct.h @@ -3,11 +3,10 @@ #include #include -#include #define SECONDS_PER_MINUTE 60 typedef struct __attribute__((__packed__)) { - char deviceId[sizeof(((t_configBlock*)0)->deviceId)]; + char deviceId[16]; uint8_t hash[SHA256_BLOCK_SIZE]; uint32_t totalRunningHours; uint32_t totalPowercycles; @@ -22,4 +21,4 @@ typedef union { uint8_t b[sizeof(t_minuteStruct)]; } t_minuteBuffer; -#endif // _SINKSTRUCT_H_ \ No newline at end of file +#endif // _SINKSTRUCT_H_