From 64554f103e2b170380925e0c62fe754f4492bff4 Mon Sep 17 00:00:00 2001 From: Bernhard Tittelbach Date: Sat, 24 Apr 2010 02:29:08 +0000 Subject: [PATCH 1/1] improvements from sample_met --- openwrt-packages/usocket/Makefile | 2 +- serial_console/sample_sensors.c | 78 ++++++++++++++++++++++++------------- serial_console/sample_sensors.h | 2 + 3 files changed, 54 insertions(+), 28 deletions(-) diff --git a/openwrt-packages/usocket/Makefile b/openwrt-packages/usocket/Makefile index 5b927cd..1230cb3 100644 --- a/openwrt-packages/usocket/Makefile +++ b/openwrt-packages/usocket/Makefile @@ -1,7 +1,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=usocket -PKG_VERSION:=0.5 +PKG_VERSION:=0.6 PKG_RELEASE:=1 PKG_BUILD_DIR:=$(BUILD_DIR)/usocket diff --git a/serial_console/sample_sensors.c b/serial_console/sample_sensors.c index 01f4e88..0a21c3e 100644 --- a/serial_console/sample_sensors.c +++ b/serial_console/sample_sensors.c @@ -1,47 +1,71 @@ #include "sample_sensors.h" -unsigned int collect_data(char *buffer, unsigned int size) +int check_handle_line(char *buffer, unsigned int buflen, char *cmpstr, char *rrd_file, char *txt_file) { char *cmd; - if (size >= 8 && strncmp("movement", buffer, 8) == 0) - return 1; - else if (size > 15 && strncmp("temp0: Temp C:", buffer, 14) == 0) + unsigned int cmpstrlen = strlen(cmpstr); + unsigned int value = atoi(buffer + cmpstrlen); + if (buflen > cmpstrlen && strncmp(cmpstr,buffer,cmpstrlen) == 0) { - if (asprintf(&cmd, "rrdtool update %s -t temp N:%s", rrd_temp_, buffer + 15)) + if (asprintf(&cmd, "rrdtool update %s N:%d", rrd_file, value)) { - /*printf("%s\n",cmd);*/ + //printf("%s\n",cmd); system(cmd); free(cmd); } - } - else if (size > 7 && strncmp("temp0:", buffer, 6) == 0) - { - if (asprintf(&cmd, "rrdtool update %s -t temp N:%s", rrd_temp_, buffer + 7)) + int fd = open(txt_file, O_WRONLY | O_CREAT | O_TRUNC, 0666); + if (fd) { - /*printf("%s\n",cmd);*/ - system(cmd); - free(cmd); + if (asprintf(&cmd, "%d", value)) + { + + write(fd,cmd, strnlen(cmd,12)); //elim. newline + free(cmd); + } + close(fd); } - } - else if (size > 15 && strncmp("photo0: Photo:", buffer, 14) == 0) + return 1; + } + return 0; +} + +int check_handle_line_float(char *buffer, unsigned int buflen, char *cmpstr, char *rrd_file, char *txt_file) +{ + char *cmd; + unsigned int cmpstrlen = strlen(cmpstr); + float value = atof(buffer + cmpstrlen); + if (buflen > cmpstrlen && strncmp(cmpstr,buffer,cmpstrlen) == 0) { - if (asprintf(&cmd, "rrdtool update %s -t light N:%s", rrd_light_, buffer + 15)) + if (asprintf(&cmd, "rrdtool update %s N:%f", rrd_file, value)) { - /*printf("%s\n",cmd);*/ system(cmd); free(cmd); } - } - else if (size > 8 && strncmp("photo0:", buffer, 7) == 0) - { - if (asprintf(&cmd, "rrdtool update %s -t light N:%s", rrd_light_, buffer + 8)) + int fd = open(txt_file, O_WRONLY | O_CREAT | O_TRUNC, 0666); + if (fd) { - /*printf("%s\n",cmd);*/ - system(cmd); - free(cmd); - } + if (asprintf(&cmd, "%f", value)) + { + + write(fd,cmd, strnlen(cmd,12)); //elim. newline + free(cmd); + } + close(fd); + } + return 1; } - return 0; + return 0; +} + +unsigned int collect_data(char *buffer, unsigned int size) +{ + if (size >= 8 && strncmp("movement", buffer, 8) == 0) + return 1; + else if (check_handle_line(buffer, size, "temp0: ", rrd_temp_, txt_temp_)) + return 0; + else if (check_handle_line(buffer, size, "photo0: ", rrd_light_, txt_light_)) + return 0; + return 0; } void sample_sensors(int fd) @@ -66,7 +90,7 @@ void sample_sensors(int fd) timeout.tv_sec=1; timeout.tv_usec=0; last_sample_time=time(0); - while (select(fd+1,&fds_r,0,0,timeout) > -1) + while (select(fd+1,&fds_r,0,0,&timeout) > -1) { curr_time=time(0); if (FD_ISSET(fd,&fds_r)) diff --git a/serial_console/sample_sensors.h b/serial_console/sample_sensors.h index e27c32c..3031147 100644 --- a/serial_console/sample_sensors.h +++ b/serial_console/sample_sensors.h @@ -16,5 +16,7 @@ char *default_socket_file_="/var/run/powersensordaemon/cmd.sock"; char *rrd_temp_ = "/home/sensortemp.rrd"; char *rrd_light_ = "/home/sensorlight.rrd"; +char *txt_temp_ = "/home/sensortemp.txt"; +char *txt_light_ = "/home/sensorlight.txt"; char *rrd_movement_ = "/home/sensormovement.rrd"; const int sample_interval_s_ = 30; \ No newline at end of file -- 1.7.10.4