Skip to content

Commit

Permalink
Merge branch 'volkszaehler:master' into flush
Browse files Browse the repository at this point in the history
  • Loading branch information
maxberger authored Apr 8, 2023
2 parents f6d542d + 7ab8642 commit 100b9a6
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 24 deletions.
12 changes: 12 additions & 0 deletions README.InfluxDB.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,15 @@ Set `"api"` to`"influxdb"` to use the InfluxDB API.

For optional parameters such as `username` or `password` have a look at the
example config file is available at [`etc/vzlogger.conf.InfluxDB`](https://github.com/volkszaehler/vzlogger/blob/master/etc/vzlogger.conf.InfluxDB)

Message Format
---------------------------

Messages are sent in the following format:

<measurement_name>,uuid=<uuid>,<tags> value=<value> <time[ms]>

In the above, measurement_name, tags and uuid are from the configuration
while value and time[ms] are value and time of the measurement.

Details about this can be found in the [InfluxDB line protocol tutorial](https://docs.influxdata.com/influxdb/v1.8/write_protocols/line_protocol_tutorial/)
4 changes: 4 additions & 0 deletions debian/vzlogger.examples
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
etc/vzlogger.conf.InfluxDB
etc/vzlogger.conf.meterOCR
etc/vzlogger.conf.meterOMS
etc/vzlogger.conf.mySmartGrid
1 change: 1 addition & 0 deletions debian/vzlogger.install
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
etc/vzlogger.conf etc
etc/logrotate.d/vzlogger etc/logrotate.d
8 changes: 8 additions & 0 deletions etc/logrotate.d/vzlogger
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/var/log/vzlogger.log {
rotate 1
size=100k
copytruncate
missingok
notifempty
create 0664 vzlogger root
}
20 changes: 19 additions & 1 deletion include/threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,27 @@
#ifndef _THREADS_H_
#define _THREADS_H_

void logging_thread_cleanup(void *arg);
#include <pthread.h>
#include <unistd.h>

void *logging_thread(void *arg);
void *reading_thread(void *arg);

// vzlogger uses pthread_cancel() to stop threads, which is not safe for C++ code that might invoke
// destructors, this macro is to be placed around any code that your meter spends significant
// amounts of time in, but which may not contain C++ code that might destroy objects.
// See https://blog.memzero.de/pthread-cancel-noexcept/ for details.

#define CANCELLABLE(...) \
do { \
int oldstate; \
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); \
__VA_ARGS__; \
pthread_setcancelstate(oldstate, NULL); \
} while (0)

inline void _safe_to_cancel() { CANCELLABLE(pthread_testcancel()); }

inline void _cancellable_sleep(int seconds) { CANCELLABLE(sleep(seconds)); }

#endif /* _THREADS_H_ */
2 changes: 1 addition & 1 deletion modules/FindJson.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ FIND_PATH(JSON-C_INCLUDE_DIR json-c/json.h
IF(WIN32)
SET(JSON_LIBRARY_NAMES ${JSON_LIBRARY_NAMES} libjson.lib)
ELSE(WIN32)
SET(JSON_LIBRARY_NAMES ${JSON_LIBRARY_NAMES} libjson-c.a)
SET(JSON_LIBRARY_NAMES ${JSON_LIBRARY_NAMES} libjson-c.so)
SET(JSON_LIBRARY_NAMES ${JSON_LIBRARY_NAMES} libjson-c.a)
ENDIF(WIN32)
FIND_LIBRARY(JSON_LIBRARY NAMES ${JSON_LIBRARY_NAMES}
HINTS
Expand Down
5 changes: 2 additions & 3 deletions modules/FindMicroHttpd.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
# Usage of this module as follows:
# This file defines:
# * MICROHTTPD_FOUND if protoc was found
# * MICROHTTPD_LIBRARY The lib to link to (currently only a static unix lib, not
# portable)
# * MICROHTTPD_LIBRARY The lib to link to
# * MICROHTTPD_INCLUDE The include directories for libmicrohttpd.

message(STATUS "FindMicrohttpd check")
Expand Down Expand Up @@ -81,8 +80,8 @@ FIND_PATH(MICROHTTPD_INCLUDE_DIR microhttpd.h
IF(WIN32)
SET(MICROHTTPD_LIBRARY_NAMES ${MICROHTTPD_LIBRARY_NAMES} libmicrohttpd.lib)
ELSE(WIN32)
SET(MICROHTTPD_LIBRARY_NAMES ${MICROHTTPD_LIBRARY_NAMES} libmicrohttpd.a)
SET(MICROHTTPD_LIBRARY_NAMES ${MICROHTTPD_LIBRARY_NAMES} libmicrohttpd.so)
SET(MICROHTTPD_LIBRARY_NAMES ${MICROHTTPD_LIBRARY_NAMES} libmicrohttpd.a)
ENDIF(WIN32)
FIND_LIBRARY(MICROHTTPD_LIBRARY NAMES ${MICROHTTPD_LIBRARY_NAMES}
HINTS
Expand Down
3 changes: 3 additions & 0 deletions src/protocols/MeterD0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
#include <netdb.h>
#include <sys/socket.h>

#include "threads.h"

#include "protocols/MeterD0.hpp"
#include <VZException.hpp>

Expand Down Expand Up @@ -470,6 +472,7 @@ ssize_t MeterD0::read(std::vector<Reading> &rds, size_t max_readings) {
}

while (1) {
_safe_to_cancel();
// check for timeout
time(&end_time);
if (difftime(end_time, start_time) > _read_timeout_s) {
Expand Down
3 changes: 3 additions & 0 deletions src/protocols/MeterFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include <sys/time.h>
#include <unistd.h>

#include "threads.h"

#include "Options.hpp"
#include "protocols/MeterFile.hpp"
#include <VZException.hpp>
Expand Down Expand Up @@ -237,6 +239,7 @@ ssize_t MeterFile::read(std::vector<Reading> &rds, size_t n) {
print(log_debug, "MeterFile::read: %d, %d", "", rds.size(), n);

while (i < n && fgets(line, 256, _fd)) {
_safe_to_cancel();
char *nl;
if ((nl = strrchr(line, '\n')))
*nl = '\0'; // remove trailing newlines
Expand Down
3 changes: 3 additions & 0 deletions src/protocols/MeterFluksoV2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include <sys/types.h>
#include <unistd.h>

#include "threads.h"

#include "Options.hpp"
#include "protocols/MeterFluksoV2.hpp"
#include <VZException.hpp>
Expand Down Expand Up @@ -89,6 +91,7 @@ ssize_t MeterFluksoV2::read(std::vector<Reading> &rds, size_t n) {
time.tv_usec = 0; /* no millisecond resolution available */

while (cursor) {
_safe_to_cancel();
int channel =
atoi(strsep(&cursor, " \t")) + 1; /* increment by 1 to distinguish between +0 and -0 */

Expand Down
12 changes: 9 additions & 3 deletions src/protocols/MeterS0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#include <time.h>
#include <unistd.h>

#include "threads.h"

#include "Options.hpp"
#include "protocols/MeterS0.hpp"
#include <VZException.hpp>
Expand Down Expand Up @@ -262,6 +264,11 @@ void MeterS0::counter_thread() {
else // main hardware interface caused the event
++_impulses;
}
} else {
if (!timeout) {
print(log_warning, "Reading from hardwareinterface failed with %s.",
name().c_str(), strerror(errno));
}
}
} else { // non-blocking case:
int state = _hwif->status();
Expand Down Expand Up @@ -357,8 +364,7 @@ ssize_t MeterS0::read(std::vector<Reading> &rds, size_t n) {
bool is_zero = true;
do {
req.tv_sec += 1;
while (EINTR == clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &req, NULL))
;
CANCELLABLE(while (EINTR == clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &req, NULL)));
// check from counter_thread the current impulses:
t_imp = _impulses;
t_imp_neg = _impulses_neg;
Expand Down Expand Up @@ -518,7 +524,7 @@ bool MeterS0::HWIF_UART::waitForImpulse(bool &timeout) {
// blocking until one character/pulse is read
ssize_t ret;
ret = ::read(_fd, buf, 8);
if (ret < 1) {
if (ret < 0) {
timeout = false;
return false;
} else if (ret == 0) {
Expand Down
8 changes: 5 additions & 3 deletions src/protocols/MeterSML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
#include <netdb.h>
#include <sys/socket.h>

#include "threads.h"

/* sml stuff */
#include <sml/sml_file.h>
#include <sml/sml_transport.h>
Expand Down Expand Up @@ -248,7 +250,7 @@ ssize_t MeterSML::read(std::vector<Reading> &rds, size_t n) {
if (_fd < 0) {
if (!reopen()) {
// sleep a little bit to prevent busy looping
sleep(1);
_cancellable_sleep(1);
return 0;
}
}
Expand All @@ -260,12 +262,12 @@ ssize_t MeterSML::read(std::vector<Reading> &rds, size_t n) {
}

/* wait until we receive a new datagram from the meter (blocking read) */
bytes = sml_transport_read(_fd, buffer, SML_BUFFER_LEN);
CANCELLABLE(bytes = sml_transport_read(_fd, buffer, SML_BUFFER_LEN));

if (0 == bytes) {
// try to reopen. see issue #362
if (reopen()) {
bytes = sml_transport_read(_fd, buffer, SML_BUFFER_LEN);
CANCELLABLE(bytes = sml_transport_read(_fd, buffer, SML_BUFFER_LEN));
print(log_info, "sml_transport_read returned len=%d after reopen", name().c_str(),
bytes);
}
Expand Down
3 changes: 3 additions & 0 deletions src/protocols/MeterW1therm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* @author Matthias Behr <mbehr (a) mcbehr.de>
* */

#include "threads.h"

#include "protocols/MeterW1therm.hpp"
#include <glob.h>

Expand Down Expand Up @@ -138,6 +140,7 @@ ssize_t MeterW1therm::read(std::vector<Reading> &rds, size_t n) {

for (std::list<std::string>::const_iterator it = list.cbegin();
it != list.cend() && static_cast<size_t>(ret) < n; ++it) {
_safe_to_cancel();
double value;
if (_hwif->readTemp(*it, value)) {
print(log_finest, "reading w1 device %s returned %f", name().c_str(), (*it).c_str(),
Expand Down
13 changes: 0 additions & 13 deletions src/threads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,6 @@

extern Config_Options options;

inline void _safe_to_cancel() {
// see https://blog.memzero.de/pthread-cancel-noexcept/
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_testcancel();
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
}

inline void _cancellable_sleep(int seconds) {
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
sleep(seconds);
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
}

void *reading_thread(void *arg) {
MeterMap *mapping = static_cast<MeterMap *>(arg);
Meter::Ptr mtr = mapping->meter();
Expand Down

0 comments on commit 100b9a6

Please sign in to comment.