updated config

fixed some other bugs
This commit is contained in:
Steffen Vogel 2011-05-29 17:20:41 +02:00
parent 882de1172c
commit 3dfe790f66
8 changed files with 163 additions and 49 deletions

View file

@ -36,26 +36,30 @@ extern options_t opts;
/**
* Reformat CURLs debugging output
*/
int curl_custom_debug_callback(CURL *curl, curl_infotype type, char *data, size_t size, void *custom) {
int curl_custom_debug_callback(CURL *curl, curl_infotype type, char *data, size_t size, void *ch) {
char *end = strchr(data, '\n');
if (data == end) return 0; /* skip empty line */
switch (type) {
case CURLINFO_TEXT:
case CURLINFO_END:
print(4, "%.*s", NULL, (int) size, data);
if (end) *end = '\0'; /* terminate without \n */
print(3, "%.*s", (channel_t *) ch, (int) size, data);
break;
case CURLINFO_HEADER_IN:
case CURLINFO_HEADER_OUT:
//print(4, "Header: %.*s", NULL, size, data);
break;
case CURLINFO_SSL_DATA_IN:
case CURLINFO_DATA_IN:
print(4, "Received %lu bytes\n", NULL, (unsigned long) size);
print(6, "Received %lu bytes", (channel_t *) ch, (unsigned long) size);
break;
case CURLINFO_SSL_DATA_OUT:
case CURLINFO_DATA_OUT:
print(4, "Sent %lu bytes.. ", NULL, (unsigned long) size);
print(6, "Sent %lu bytes.. ", (channel_t *) ch, (unsigned long) size);
break;
case CURLINFO_HEADER_IN:
case CURLINFO_HEADER_OUT:
break;
}
@ -92,7 +96,7 @@ CURLcode api_log(channel_t *ch, reading_t rd) {
/* build request strings */
sprintf(url, "%s/data/%s.json", ch->middleware, ch->uuid); /* build url */
sprintf(useragent, "vzlogger/%s (%s)", VZ_VERSION, curl_version());
sprintf(useragent, "vzlogger/%s (%s)", "VZ_VERSION", curl_version());
sprintf(post, "?timestamp=%lu%lu&value=%f", rd.tv.tv_sec, rd.tv.tv_usec / 1000, rd.value);
curl = curl_easy_init();
@ -103,6 +107,7 @@ CURLcode api_log(channel_t *ch, reading_t rd) {
curl_easy_setopt(curl, CURLOPT_USERAGENT, useragent);
curl_easy_setopt(curl, CURLOPT_VERBOSE, (int) opts.verbose);
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, curl_custom_debug_callback);
curl_easy_setopt(curl, CURLOPT_DEBUGDATA, (void *) ch);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_custom_write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &chunk);

View file

@ -25,21 +25,21 @@
#include <json/json.h>
#include <string.h>
#include <stdio.h>
#include "main.h"
#include "local.h"
extern channel_t *chans;
extern channel_t chans[MAX_CHANNELS];
extern options_t opts;
int handle_request(void *cls, struct MHD_Connection *connection, const char *url, const char *method,
const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls) {
const char * json_str;
const char * uuid = url + 1;
int ret;
int num_chans = *(int *) cls;
print(2, "Local request reveived: %s %s %s", NULL, version, method, url);
print(2, "Local request received: %s %s %s", NULL, version, method, url);
struct MHD_Response *response;
@ -50,7 +50,7 @@ int handle_request(void *cls, struct MHD_Connection *connection, const char *url
channel_t *ch = &chans[i];
reading_t rd;
if (strcmp(ch->uuid, uuid) == 0) {
if (strcmp(url, "/") == 0 || strcmp(ch->uuid, url + 1) == 0) {
pthread_mutex_lock(&ch->mutex);
pthread_cond_wait(&ch->condition, &ch->mutex); /* wait for new data comet-like blocking of HTTP response */
pthread_mutex_unlock(&ch->mutex);
@ -86,6 +86,7 @@ int handle_request(void *cls, struct MHD_Connection *connection, const char *url
}
json_object_object_add(json_obj, "version", json_object_new_string(VZ_VERSION));
json_object_object_add(json_obj, "generator", json_object_new_string("vzlogger"));
json_object_object_add(json_obj, "data", json_data);
json_str = json_object_to_json_string(json_obj);

View file

@ -23,8 +23,6 @@
* along with volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
*/
#define VZ_VERSION "0.2"
#include <stdio.h>
#include <getopt.h>
#include <stdarg.h>
@ -169,7 +167,7 @@ void parse_options(int argc, char * argv[], options_t * opts) {
/* getopt_long stores the option index here. */
int option_index = 0;
int c = getopt_long(argc, argv, "i:c:p:lhdv", long_options, &option_index);
int c = getopt_long(argc, argv, "i:c:p:lhdv::", long_options, &option_index);
/* detect the end of the options. */
if (c == -1)
@ -218,35 +216,35 @@ int parse_channels(char * filename, channel_t * chans) {
int j = 0;
while (j < MAX_CHANNELS && fgets(line, sizeof line, file) != NULL) { /* read a line */
if (line[0] == '#' || line[0] == '\n') continue; /* skip comments */
if (line[0] == ';' || line[0] == '\n') continue; /* skip comments */
channel_t ch;
protocol_t *prot;
char *tok = strtok(line, ";");
char *tok = strtok(line, " \t");
for (int i = 0; i < 7 && tok != NULL; i++) {
size_t len = strlen(tok);
switch(i) {
case 0: /* middleware */
ch.middleware = (char *) malloc(len+1); /* including string termination */
strcpy(ch.middleware, tok);
break;
case 1: /* uuid */
ch.uuid = (char *) malloc(len+1); /* including string termination */
strcpy(ch.uuid, tok);
break;
case 2: /* protocol */
case 0: /* protocol */
prot = protocols; /* reset pointer */
while (prot->name && strcmp(prot->name, tok) != 0) prot++; /* linear search */
ch.prot = prot;
break;
case 3: /* interval */
case 1: /* interval */
ch.interval = atoi(tok);
break;
case 2: /* uuid */
ch.uuid = (char *) malloc(len+1); /* including string termination */
strcpy(ch.uuid, tok);
break;
case 3: /* middleware */
ch.middleware = (char *) malloc(len+1); /* including string termination */
strcpy(ch.middleware, tok);
break;
case 4: /* options */
ch.options = (char *) malloc(len);
@ -255,7 +253,7 @@ int parse_channels(char * filename, channel_t * chans) {
break;
}
tok = strtok(NULL, ";");
tok = strtok(NULL, " \t");
}
ch.id = j;
@ -291,7 +289,7 @@ void *log_thread(void *arg) {
while (!queue_is_empty(&ch->queue)) {
pthread_mutex_lock(&ch->mutex);
queue_first(&ch->queue, &rd);
queue_first(&ch->queue, &rd);
pthread_mutex_unlock(&ch->mutex);
rc = api_log(ch, rd); /* log reading */
@ -329,13 +327,12 @@ void *read_thread(void *arg) {
reading_t rd = ch->prot->read_func(ch->handle); /* aquire reading */
pthread_mutex_lock(&ch->mutex);
if (opts.verbose > 4) queue_print(&ch->queue); /* Debugging */
queue_enque(&ch->queue, rd);
pthread_cond_broadcast(&ch->condition);
pthread_mutex_unlock(&ch->mutex);
print(1, "Value read: %.3f (next reading in %i secs)", ch, rd.value, ch->interval);
if (opts.verbose > 5) queue_print(&ch->queue); /* Debugging */
pthread_testcancel(); /* test for cancelation request */
sleep(ch->interval); /* else sleep and restart aquisition */
@ -394,11 +391,11 @@ int main(int argc, char * argv[]) {
pthread_join(ch->reading_thread, NULL);
pthread_join(ch->logging_thread, NULL);
free(ch->middleware);
/*free(ch->middleware);
free(ch->uuid);
free(ch->options);
queue_free(&ch->queue);
queue_free(&ch->queue);*/
pthread_cond_destroy(&ch->condition);
pthread_mutex_destroy(&ch->mutex);

View file

@ -1,15 +1,10 @@
/**
* Wrapper to read Dallas 1-wire Sensors via the 1-wire Filesystem (owfs)
*
* This is our example protocol. Use this skeleton to add your own
* protocols and meters.
*
* @package vzlogger
* @copyright Copyright (c) 2011, The volkszaehler.org project
* @license http://www.gnu.org/licenses/gpl.txt GNU Public License
* @author Steffen Vogel <info@steffenvogel.de>
* @author Mathias Dalheimer <md@gonium.net>
* based heavily on libehz (https://github.com/gonium/libehz.git)
*/
/*
* This file is part of volkzaehler.org

View file

@ -1,9 +1,6 @@
/**
* Wrapper to read Dallas 1-wire Sensors via the 1-wire Filesystem (owfs)
*
* This is our example protocol. Use this skeleton to add your own
* protocols and meters.
*
* @package vzlogger
* @copyright Copyright (c) 2011, The volkszaehler.org project
* @license http://www.gnu.org/licenses/gpl.txt GNU Public License

View file

@ -0,0 +1,76 @@
/**
* OBIS protocol parser
*
*
*
* @package vzlogger
* @copyright Copyright (c) 2011, The volkszaehler.org project
* @license http://www.gnu.org/licenses/gpl.txt GNU Public License
* @author Steffen Vogel <info@steffenvogel.de>
*/
/*
* This file is part of volkzaehler.org
*
* volkzaehler.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* volkzaehler.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
*/
#include <fcntl.h>
#include <termios.h>
#include "../main.h"
#include "../protocol.h"
#include "rawS0.h"
typedef struct {
int fd; /* file descriptor of port */
struct termios old_tio;
struct termios tio;
} rawS0_state_t;
void * rawS0_init(char * port) {
struct termios tio;
int *fd = malloc(sizeof(int));
memset(&tio, 0, sizeof(tio));
tio.c_iflag = 0;
tio.c_oflag = 0;
tio.c_cflag = CS7|CREAD|CLOCAL; // 7n1, see termios.h for more information
tio.c_lflag = 0;
tio.c_cc[VMIN] = 1;
tio.c_cc[VTIME] = 5;
*fd = open(port, O_RDWR | O_NONBLOCK);
cfsetospeed(&tio, B9600); // 9600 baud
cfsetispeed(&tio, B9600); // 9600 baud
return (void *) fd;
}
void obis_close(void *handle) {
// TODO close serial port
free(handle);
}
reading_t obis_get(void *handle) {
reading_t rd;
read();
gettimeofday(&rd.tv, NULL);
return rd;
}

View file

@ -0,0 +1,38 @@
/**
* OBIS protocol parser
*
* This is our example protocol. Use this skeleton to add your own
* protocols and meters.
*
* @package vzlogger
* @copyright Copyright (c) 2011, The volkszaehler.org project
* @license http://www.gnu.org/licenses/gpl.txt GNU Public License
* @author Steffen Vogel <info@steffenvogel.de>
*/
/*
* This file is part of volkzaehler.org
*
* volkzaehler.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* volkzaehler.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _RAWS0_H_
#define _RAWS0_H_
#include "../protocol.h"
void * rawS0_init(char * port);
void rawS0_close(void *handle);
reading_t rawS0_get(void *handle);
#endif /* _RAWS0_H_ */

View file

@ -1,3 +1,8 @@
#middleware;uuid;type;interval;options
http://localhost/workspace/volkszaehler.org/htdocs/middleware;52960fe0-8882-11e0-b356-85eba28c1922;obis;10;/mnt/1wire/10.12E6D3000800/temperature
#http://volkszaehler.org/demo/middleware.php;ef0e9adf-cd9e-4d9a-92c5-b4fb4c89ff98;obis;10;/dev/ttyS0
; vzlogger configuration
; use tabs, spaces as delimiter
; use ; to introduce a comment
;prot intval uuid middleware options
;1wire 3 52960fe0-8882-11e0-b356-85eba28c1922 http://localhost/workspace/volkszaehler.org/htdocs/middleware /mnt/1wire/10.12E6D3000800/temperature
;obis 10 ef0e9adf-cd9e-4d9a-92c5-b4fb4c89ff98 http://volkszaehler.org/demo/middleware.php /dev/ttyS0
obis 10 ef0e9adf-cd9e-4d9a-92c5-b4fb4c89ff98 http://volkszaehler.org/demo/middleware.php /dev/ttyS1