Skip to content

Commit 0d0dd03

Browse files
committed
v1.3: reset_times, logfile size limit
1 parent 02537b1 commit 0d0dd03

File tree

140 files changed

+730
-343
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

140 files changed

+730
-343
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 1.3 (2025-07-13)
4+
+ New config option for logbook columns: `reset_times` to reset data at specific times of day.
5+
+ New config option: `logfile_max_lines` to limit logfile size (defaults to 10000 lines).
6+
+ Bugfix: crash when reading big integers from JSON structures.
7+
38
## 1.2 (2022-10-03)
49
+ Added sensor retry time (`retry_time` and `default_retry_time`): time for another attempt after a failed sensor reading.
510
+ New config options for `general`: `http_timeout`, `default_rest_period`, `default_retry_time`, `loglevel`.

README.md

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ In the following listing you see a very simple example for a logbook file. It ke
1919
2021-01-31 18:00:00 -0.926 89.06
2020
2021-01-31 18:30:00 -1.14067 89.1933
2121

22-
## Data accumu­lation and statisti­cal summary
22+
## Data accumulation and statistical summary
2323

2424
Sensorlogger is able to read values from Tinkerforge sensors and JSON structures in user-defined **time intervals.** After reading, these values can be forwarded immediately to MQTT brokers or to a HomeMatic CCU (the XML-API is used to set system variables using their ISE-ID). The values are also stored internally until they become irrelevant for any further statistical analysis.
2525

@@ -68,7 +68,7 @@ The virtual package `libcurl-dev` offers different implementations. All of them
6868

6969
### MQTT Support
7070

71-
If you need support for MQTT, the Paho MQTT libraries for C and C++ must be installed. You can get packages for Arch-based distributions for the AUR: [paho-mqtt-c-git](https://aur.archlinux.org/packages/paho-mqtt-c-git/) and [paho-mqtt-cpp-git](https://aur.archlinux.org/packages/paho-mqtt-cpp-git/). For Debian-based systems (e.g. Raspberry Pi, Tinkerforge Red Brick) we have to compile and install the libraries on our own. To do this, we need to make sure that we already have all the required packages installed:
71+
If you need support for MQTT, the Paho MQTT libraries for C and C++ must be installed. You can get packages for Arch-based distributions from the AUR: [paho-mqtt-c](https://aur.archlinux.org/packages/paho-mqtt-c/) and [paho-mqtt-cpp](https://aur.archlinux.org/packages/paho-mqtt-cpp/). There also exist [Debian packages](https://packages.debian.org/search?suite=stable&section=all&arch=any&searchon=names&keywords=paho-mqtt) for newer distributions. If not, we have to compile and install the libraries on our own. To do this, we need to make sure that we already have all the required packages installed:
7272

7373
apt-get install libssl-dev build-essential gcc make
7474
apt-get install cmake cmake-gui cmake-curses-gui
@@ -168,6 +168,7 @@ The `general` section of the configuration file may contain the following settin
168168
```
169169
"general": {
170170
"logfile": "/home/username/sensorlogger.log",
171+
"logfile_max_lines": 10000,
171172
"loglevel": "info",
172173
"http_timeout": 20,
173174
"default_rest_period": {"value": 60, "unit": "s"},
@@ -179,6 +180,12 @@ The `general` section of the configuration file may contain the following settin
179180

180181
Standard value: `null`
181182

183+
+ `"logfile_max_lines":` Maximum number of lines in the log file. If the log file grows bigger than this limit, it will be trimmed by removing the oldest lines from the top. Note that for performance reasons, the log file is only trimmed when Sensorlogger starts and then every time after 100 lines have been written.
184+
185+
Set this value to `0` to deactivate trimming of the log file.
186+
187+
Standard value: `10000`
188+
182189
+ `"loglevel":` Type of events that are logged. Can be any of the following:
183190
- `"error"`: Only log error messages.
184191
- `"warning"`: Log errors and warnings.
@@ -746,12 +753,14 @@ The `"columns"` key (as a child node of a logbooks object) always hosts a JSON a
746753
"confidence_absolute": 10.0,
747754
},
748755
{
749-
"title": "Humidity",
756+
"title": "Maximum humidity today",
750757
"unit": "%rel",
751758
"sensor_id": "Weather/Humidity",
752-
"mqtt_publish": "House/Weather/Humidity/median",
759+
"mqtt_publish": "House/Weather/Humidity/max24h",
753760
"homematic_publish": "37856",
754-
"operation": "median",
761+
"operation": "max",
762+
"evaluation_period": {"value": 24, "unit": "h"},
763+
"reset_times": ["0:00"],
755764
"confidence_sigma": 3.0
756765
},
757766
{
@@ -803,6 +812,10 @@ The `"columns"` key (as a child node of a logbooks object) always hosts a JSON a
803812

804813
Standard value: `null`
805814

815+
+ `"reset_times":` Specific points in time when the column values are reset, i.e., the value cache for this column is cleared. For example, when an evaluation period is defined to calculate a moving average, the accumulated data can be cleared at midnight such that the previous day is not part of the moving average. Likewise, a daily maximum for a value can be computed.
816+
817+
The reset times are listed in an array. An arbitrary number of reset times can be defined. Each reset time is a string that contains hour and minute in 24-hour format, such as `"14:30"`. To repeat a reset at a certain minute every hour, the star notation can be used: `"*:00"` does a reset every full hour.
818+
806819
+ `"count_factor":` This factor can be used to weight the number of events of a pulse counter. It only affects the counter operations `"count"`, `"freq"`, `"freq_min"` and `"freq_max"`. In the example above, the wind sensor triggers two pulses per rotation, so we scale the pulse frequency by a factor of 0.5 to get the rotation frequency.
807820

808821
Standard value: `1`
@@ -832,7 +845,7 @@ The `"columns"` key (as a child node of a logbooks object) always hosts a JSON a
832845

833846
### Tinkerforge: Supported Bricklets
834847

835-
Currently, version 2.1.33 (2022-05-11) of the [Tinkerforge](https://www.tinkerforge.com/) C/C++ bindings are used. The following Tinkerforge Bricklets are supported. The table also lists the channel IDs if a Bricklet supports different types of measurements.
848+
Currently, version 2.1.34 (2024-02-27) of the [Tinkerforge](https://www.tinkerforge.com/) C/C++ bindings are used. The following Tinkerforge Bricklets are supported. The table also lists the channel IDs if a Bricklet supports different types of measurements.
836849

837850
| Bricklet (and versions) | Channels |
838851
| :---------------------------------- | :------------------------------------------------ |

include/column.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
#define _COLUMN_H
33

44
#define E_NO_VALUES_FOR_COLUMN 7002
5+
#define E_INVALID_RESET_TIME 7003
56

7+
#include <cstdint>
68
#include <string>
7-
#include <sstream>
89
#include <vector>
910

1011
enum operation {mean, median, max, min, sum, count, freq, freq_min, freq_max, stdDevMean, stdDevMedian};
1112

13+
class logger;
1214
class logbook;
1315
class sensor;
1416
class counter;
@@ -20,26 +22,30 @@ class column
2022
counter* _pulseCounter;
2123
std::string _title;
2224
std::string _unit;
23-
uint64_t _evaluationPeriod;
25+
uint64_t _evaluationPeriod; // in ms
26+
std::vector<uint64_t> _evaluationPeriod_resetHours;
27+
std::vector<uint64_t> _evaluationPeriod_resetMinutes;
2428
operation _op;
2529
double _confidenceSigma;
2630
double _confidenceAbsolute;
2731
std::string _mqttPublishTopic;
2832
std::string _homematicPublishISE;
2933
double _countFactor;
3034

35+
logger* _root;
3136
logbook* _rootLogbook;
3237

3338
// Calculated from evaluation period / logbook cycle time:
3439
size_t _nCycles; // How many logbook cycles does the evaluation period represent?
3540

3641
public:
37-
column(sensor* s, logbook* lb, const std::string &title, const std::string &unit, uint64_t evaluationPeriod, operation op, double confidenceAbsolute, double confidenceSigma, const std::string &mqttPublishTopic, const std::string &homematicPublishISE, double countFactor);
42+
column(logger* root, sensor* s, logbook* lb, const std::string &title, const std::string &unit, uint64_t evaluationPeriod, operation op, double confidenceAbsolute, double confidenceSigma, const std::string &mqttPublishTopic, const std::string &homematicPublishISE, double countFactor);
3843

3944
std::string getTitle() const;
4045
std::string getUnit() const;
4146
operation getOperation() const;
4247
uint64_t getEvaluationPeriod() const;
48+
uint64_t getEvaluationPeriod_consideringResetTimes(int current_hour, int current_minute, int current_second) const;
4349
double getConfidenceAbsolute() const;
4450
double getConfidenceSigma() const;
4551
std::string getMQTTPublishTopic() const;
@@ -53,6 +59,8 @@ class column
5359
void setUnit(const std::string &unit);
5460
void setOperation(operation op);
5561
void setEvaluationPeriod(uint64_t evaluationPeriod);
62+
void addEvaluationPeriodResetTime(std::string resetTime);
63+
void addEvaluationPeriodResetTime(uint64_t hour, uint64_t minute);
5664
void calculateColumnCycles();
5765
void setConfidenceAbsolute(double confidenceAbsolute);
5866
void setConfidenceSigma(double confidenceSigma);

include/homematic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class homematic
1212
private:
1313
std::string _xmlAPI_URL; // Full Homematic XML API URL, including http(s)
1414

15-
logger* _root;
15+
logger* _root;
1616

1717
public:
1818
homematic(logger* root);

include/json.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
#ifndef _JSON_H
22
#define _JSON_H
33

4+
#include <cstdint>
45
#include <string>
56
#include <cstring>
6-
#include <sstream>
7-
#include <fstream>
87
#include <vector>
9-
#include <iostream>
10-
#include <cmath>
118

129
#define E_VALUE_IS_NOT_BOOL 2001
1310
#define E_VALUE_IS_NOT_INT 2002
@@ -31,15 +28,15 @@
3128
enum jsonType {jsonNull, jsonBool, jsonInt, jsonDouble, jsonString};
3229

3330
bool isIn(const char& c, const char* chain);
34-
bool isIntInString(const std::string &s);
35-
bool isFloatInString(const std::string &s);
31+
bool isIntInString(const std::string *s);
32+
bool isFloatInString(const std::string *s);
3633

3734
class jsonValue
3835
{
3936
private:
4037
jsonType _type;
4138
bool _vBool;
42-
long _vInt;
39+
long long _vInt;
4340
double _vDouble;
4441
std::string _vString;
4542

@@ -49,13 +46,13 @@ class jsonValue
4946

5047
void setNull();
5148
void setBool(bool v);
52-
void setInt(long v);
49+
void setInt(long long v);
5350
void setDouble(double v);
5451
void setString(const std::string &v);
5552

5653
bool isNull() const;
5754
bool getBool() const;
58-
long getInt() const;
55+
long long getInt() const;
5956
double getDouble() const;
6057
std::string getString() const;
6158

include/logbook.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
#ifndef _LOG_H
22
#define _LOG_H
33

4+
#include <cstdint>
45
#include <cstdio>
56
#include <vector>
67
#include <string>
7-
#include <iostream>
88
#include <ctime>
9-
#include <chrono>
10-
#include <iomanip>
11-
#include <sstream>
12-
#include <fstream>
139

1410
class sensor;
1511
class counter;
1612
class mqttManager;
1713
class homematic;
1814
class logbook;
19-
class logger;
2015
class column;
16+
class logger;
2117

2218
#define E_COLUMN_DOES_NOT_EXIST 5001
2319

include/logger.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@
66
#define E_JSON_READ 6003
77
#define E_HTTP_REQUEST_FAILED 6004
88

9+
#include <cstdint>
910
#include <cstdio>
10-
#include <iostream>
11-
#include <iomanip>
12-
#include <sstream>
1311
#include <string>
1412
#include <vector>
15-
#include <thread>
1613

1714
#ifdef OPTION_CURL
18-
#include <curl/curl.h>
15+
#include <curl/curl.h>
1916
#endif
2017

2118
enum loglevel {
@@ -40,6 +37,10 @@ class logger
4037
std::string _lastLogfileMessage;
4138
int _logLevel;
4239

40+
// Trim logfile every
41+
size_t _logfile_max_lines;
42+
size_t _logfile_trim_counter;
43+
4344
uint64_t _default_rest_period;
4445
uint64_t _default_retry_time;
4546

@@ -80,6 +81,10 @@ class logger
8081
std::string logLevelString();
8182
void welcomeMessage(const std::string &configJSON);
8283

84+
static void removeWhiteSpace(std::string *s);
85+
86+
void trim_logfile() const;
87+
8388
void loadConfig(const std::string &configJSON);
8489

8590
std::string getLogFilename() const;

include/measurements.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
#include <cstdint>
77
#include <vector>
8-
#include <cmath>
9-
#include <algorithm>
108

119
uint64_t timeDiff(uint64_t timestamp1, uint64_t timestamp2);
1210

include/mqttbroker.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#ifdef OPTION_MQTT
55

6-
#include <sstream>
76
#include "mqtt/async_client.h"
87

98
class logger;

include/readoutbuffer.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@
66
#define BUFFERTIME 800 // milliseconds
77
#define N_TRIALS 2
88

9+
#include <cstdint>
910
#include <string>
10-
#include <sstream>
11-
#include <iostream>
12-
#include <fstream>
1311
#include <vector>
14-
#include <thread>
1512

1613
class logger;
1714

0 commit comments

Comments
 (0)