cofnig stuff
This commit is contained in:
parent
b0257f5888
commit
0a39c5c85c
@ -1,7 +1,7 @@
|
||||
CC=gcc
|
||||
|
||||
CFLAGS=-Wall
|
||||
LDFLAGS=-lwiringPi -lcurl
|
||||
LDFLAGS=-lwiringPi -lcurl -lconfig
|
||||
|
||||
counter: counter.o LS7366R.o influx.o ringbuffer.o
|
||||
$(CC) -o $@ $(LDFLAGS) $^
|
||||
|
@ -3,6 +3,7 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <libconfig.h>
|
||||
|
||||
#include "LS7366R.h"
|
||||
#include "influx.h"
|
||||
@ -13,6 +14,8 @@ const int INTR_IN = 19;
|
||||
const int SPI_CHAN = 0;
|
||||
const int SPI_SPEED = 1000000;
|
||||
|
||||
config_t cfg;
|
||||
|
||||
|
||||
void isr() {
|
||||
static uint32_t lastCounter = 0;
|
||||
@ -36,6 +39,16 @@ void init() {
|
||||
pinMode(INTR_IN, INPUT);
|
||||
}
|
||||
|
||||
void readConfig() {
|
||||
config_init(&cfg);
|
||||
if (! config_read_file(&cfg, "/opt/etc/counter.cfg")) {
|
||||
fprintf(stderr, "failed to read config file: %s:%d - %s\n",
|
||||
config_error_file(&cfg), config_error_line(&cfg),
|
||||
config_error_text(&cfg));
|
||||
config_destroy(&cfg);
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
void start() {
|
||||
wiringPiISR(INTR_IN, INT_EDGE_RISING, isr);
|
||||
}
|
||||
@ -43,7 +56,7 @@ void start() {
|
||||
int main (void) {
|
||||
init();
|
||||
ls7366rInit(SPI_CHAN);
|
||||
influxInit();
|
||||
influxInit(&cfg);
|
||||
start();
|
||||
|
||||
|
||||
@ -54,4 +67,7 @@ int main (void) {
|
||||
// printf("%f\n", f);
|
||||
influxAddFrequency(f);
|
||||
}
|
||||
|
||||
// will never be reached
|
||||
config_destroy(&cfg);
|
||||
}
|
||||
|
3
src/counter.cfg
Normal file
3
src/counter.cfg
Normal file
@ -0,0 +1,3 @@
|
||||
influxUser = "secundus"
|
||||
influxPass = "geheim"
|
||||
influxUrl = "http://172.16.3.15:8086/write?db=smarthome2&precision=ms"
|
51
src/influx.c
51
src/influx.c
@ -4,11 +4,19 @@
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <libconfig.h>
|
||||
|
||||
|
||||
const char INFLUXURL[] = "https://home.hottis.de/influx/write?db=smarthome2&precision=ms";
|
||||
const char INFLUXUSER[] = "secundus";
|
||||
const char INFLUXPASS[] = "geheim";
|
||||
const char INFLUXURL_KEY[] = "influxUrl";
|
||||
const char DEFAULT_INFLUXURL[] = "https://home.hottis.de/influx/write?db=smarthome2&precision=ms";
|
||||
const char *influxUrl;
|
||||
const char INFLUXUSER_KEY[] = "influxUser";
|
||||
const char *influxUser;
|
||||
const char INFLUXPASS_KEY[] = "influxPass";
|
||||
const char *influxPass;
|
||||
const char INFLUXTAG_KEY[] = "influxTag";
|
||||
const char *influxTag;
|
||||
|
||||
|
||||
// #define BUFSIZE 131070
|
||||
#define BUFSIZE 65535
|
||||
@ -19,19 +27,40 @@ char *bufferNextEntry;
|
||||
#define HOSTNAMESIZE 128
|
||||
char hostname[HOSTNAMESIZE];
|
||||
|
||||
void influxInit() {
|
||||
static void influxClearBuffer() {
|
||||
memset(influxBuffer, 0, BUFSIZE);
|
||||
bufferNextEntry = influxBuffer;
|
||||
gethostname(hostname, HOSTNAMESIZE);
|
||||
|
||||
}
|
||||
|
||||
void influxInit(config_t *pCfg) {
|
||||
if (! config_lookup_string(pCfg, INFLUXURL_KEY, &influxUrl)) {
|
||||
influxUrl = DEFAULT_INFLUXURL;
|
||||
}
|
||||
if (! config_lookup_string(pCfg, INFLUXUSER_KEY, &influxUser)) {
|
||||
influxUser = NULL;
|
||||
}
|
||||
if (! config_lookup_string(pCfg, INFLUXPASS_KEY, &influxPass)) {
|
||||
influxPass = NULL;
|
||||
}
|
||||
if (! config_lookup_string(pCfg, INFLUXTAG_KEY, &influxTag)) {
|
||||
gethostname(hostname, HOSTNAMESIZE);
|
||||
influxTag = hostname;
|
||||
}
|
||||
influxClearBuffer();
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void influxSendRequest() {
|
||||
CURL *curl = curl_easy_init();
|
||||
if(curl) {
|
||||
curl_easy_setopt(curl, CURLOPT_URL, INFLUXURL);
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
|
||||
curl_easy_setopt(curl, CURLOPT_USERNAME, INFLUXUSER);
|
||||
curl_easy_setopt(curl, CURLOPT_PASSWORD, INFLUXPASS);
|
||||
curl_easy_setopt(curl, CURLOPT_URL, influxUrl);
|
||||
if (influxUser && influxPass) {
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
|
||||
curl_easy_setopt(curl, CURLOPT_USERNAME, influxUser);
|
||||
curl_easy_setopt(curl, CURLOPT_PASSWORD, influxPass);
|
||||
}
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, influxBuffer);
|
||||
CURLcode res = curl_easy_perform(curl);
|
||||
if(res != CURLE_OK) {
|
||||
@ -49,11 +78,11 @@ void influxAddFrequency(double f) {
|
||||
|
||||
clock_gettime(CLOCK_REALTIME, &t);
|
||||
uint64_t tt = (((uint64_t)t.tv_sec) * 1000) + (((uint64_t)t.tv_nsec) / 1000000);
|
||||
int c = sprintf(tmpBuf, "mainsfrequency,host=%s freq=%f %llu\n", hostname, f, tt);
|
||||
int c = sprintf(tmpBuf, "mainsfrequency,host=%s freq=%f %llu\n", influxTag, f, tt);
|
||||
|
||||
if ((bufferNextEntry + c + 10) > (influxBuffer + BUFSIZE)) {
|
||||
influxSendRequest();
|
||||
influxInit();
|
||||
influxClearBuffer();
|
||||
totalEntries += entries;
|
||||
fprintf(stderr, "%u entries sent to database, in total %u\n", entries, totalEntries);
|
||||
entries = 0;
|
||||
|
@ -1,7 +1,9 @@
|
||||
#ifndef _INFLUX_H_
|
||||
#define _INFLUX_H_
|
||||
|
||||
#include <libconfig.h>
|
||||
|
||||
void influxAddFrequency(double f);
|
||||
void influxInit();
|
||||
void influxInit(config_t *pCfg);
|
||||
|
||||
#endif // _INFLUX_H_
|
Loading…
x
Reference in New Issue
Block a user