|
|
@ -5,10 +5,18 @@ |
|
|
|
#include <termios.h> |
|
|
|
#include <termios.h> |
|
|
|
#include <stdlib.h> |
|
|
|
#include <stdlib.h> |
|
|
|
#include <signal.h> |
|
|
|
#include <signal.h> |
|
|
|
|
|
|
|
#include <time.h> |
|
|
|
|
|
|
|
|
|
|
|
#include <sqlite3.h> |
|
|
|
#include <sqlite3.h> |
|
|
|
#include <stdbool.h> |
|
|
|
#include <stdbool.h> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define WTHR_STATION_ID 0 |
|
|
|
|
|
|
|
#define TIMESTAMP_FIELD 1 |
|
|
|
|
|
|
|
#define DATA_VAL_FIELD 2 |
|
|
|
|
|
|
|
#define STATION_ID_FIELD 3 |
|
|
|
|
|
|
|
#define INSERT_HUM_TEMPL "INSERT INTO humidity(timestamp, value, station) VALUES(?, ?, ?)" |
|
|
|
|
|
|
|
#define INSERT_TEMP_TEMPL "INSERT INTO temperature(timestamp, value, station) VALUES(?, ?, ?)" |
|
|
|
|
|
|
|
|
|
|
|
sqlite3* get_db(void) { |
|
|
|
sqlite3* get_db(void) { |
|
|
|
sqlite3 *db; |
|
|
|
sqlite3 *db; |
|
|
|
char *zErrMsg = 0; |
|
|
|
char *zErrMsg = 0; |
|
|
@ -52,6 +60,7 @@ void ccleanup(sqlite3 *db) { |
|
|
|
sqlite3_close(db); |
|
|
|
sqlite3_close(db); |
|
|
|
printf("Closed DB.\n"); |
|
|
|
printf("Closed DB.\n"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void call_cleanup(int signo) { |
|
|
|
static void call_cleanup(int signo) { |
|
|
@ -88,10 +97,10 @@ static int callback(void *retval, int argc, char **argv, char **azColName) { |
|
|
|
|
|
|
|
|
|
|
|
void initialise_db(sqlite3* db) { |
|
|
|
void initialise_db(sqlite3* db) { |
|
|
|
char *query = "SELECT name from sqlite_master WHERE name = 'temperature'"; |
|
|
|
char *query = "SELECT name from sqlite_master WHERE name = 'temperature'"; |
|
|
|
|
|
|
|
|
|
|
|
char *zErrMsg = 0; |
|
|
|
char *zErrMsg = 0; |
|
|
|
int rc; |
|
|
|
int rc; |
|
|
|
struct query_result r; |
|
|
|
struct query_result r; |
|
|
|
|
|
|
|
|
|
|
|
rc = sqlite3_exec(db, query, save_qry, &r, &zErrMsg); |
|
|
|
rc = sqlite3_exec(db, query, save_qry, &r, &zErrMsg); |
|
|
|
|
|
|
|
|
|
|
|
if (rc != SQLITE_OK) { |
|
|
|
if (rc != SQLITE_OK) { |
|
|
@ -113,15 +122,18 @@ void initialise_db(sqlite3* db) { |
|
|
|
call_cleanup(1); |
|
|
|
call_cleanup(1); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
query = "SELECT name from sqlite_master WHERE name = 'humidity'"; |
|
|
|
query = "SELECT name from sqlite_master WHERE name = 'humidity'"; |
|
|
|
rc = sqlite3_exec(db, query, save_qry, &r, &zErrMsg); |
|
|
|
|
|
|
|
|
|
|
|
struct query_result re; |
|
|
|
|
|
|
|
rc = sqlite3_exec(db, query, save_qry, &re, &zErrMsg); |
|
|
|
|
|
|
|
|
|
|
|
if (rc != SQLITE_OK) { |
|
|
|
if (rc != SQLITE_OK) { |
|
|
|
fprintf(stderr, "SQL: %s\n", zErrMsg); |
|
|
|
fprintf(stderr, "SQL: %s\n", zErrMsg); |
|
|
|
sqlite3_free(zErrMsg); |
|
|
|
sqlite3_free(zErrMsg); |
|
|
|
call_cleanup(1); |
|
|
|
call_cleanup(1); |
|
|
|
} |
|
|
|
} |
|
|
|
if (r.argc == 0) { |
|
|
|
|
|
|
|
|
|
|
|
if (re.argc == 0) { |
|
|
|
query = "CREATE TABLE humidity(" \
|
|
|
|
query = "CREATE TABLE humidity(" \
|
|
|
|
"timestamp INT PRIMARY KEY NOT NULL," \
|
|
|
|
"timestamp INT PRIMARY KEY NOT NULL," \
|
|
|
|
"value INT NOT NULL," \
|
|
|
|
"value INT NOT NULL," \
|
|
|
@ -137,6 +149,34 @@ void initialise_db(sqlite3* db) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int check_error(int rc) { |
|
|
|
|
|
|
|
if (rc != SQLITE_OK) { |
|
|
|
|
|
|
|
fprintf(stderr, "SQL Error: %d\n", rc); |
|
|
|
|
|
|
|
call_cleanup(1); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return 0; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int insert_rec(sqlite3 *db, char* sqlt, int value, int station, int timestamp) { |
|
|
|
|
|
|
|
sqlite3_stmt * statement; |
|
|
|
|
|
|
|
const char *tail = 0; |
|
|
|
|
|
|
|
int rc = sqlite3_prepare_v2(db, sqlt, -1, &statement, &tail);
|
|
|
|
|
|
|
|
check_error(rc); |
|
|
|
|
|
|
|
rc = sqlite3_bind_int(statement, DATA_VAL_FIELD, value); |
|
|
|
|
|
|
|
check_error(rc); |
|
|
|
|
|
|
|
rc = sqlite3_bind_int(statement, STATION_ID_FIELD, station); |
|
|
|
|
|
|
|
check_error(rc); |
|
|
|
|
|
|
|
rc = sqlite3_bind_int(statement, TIMESTAMP_FIELD, timestamp); |
|
|
|
|
|
|
|
check_error(rc); |
|
|
|
|
|
|
|
rc = sqlite3_step(statement); |
|
|
|
|
|
|
|
if (rc != SQLITE_DONE) { |
|
|
|
|
|
|
|
check_error(rc); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
rc = sqlite3_finalize(statement); |
|
|
|
|
|
|
|
check_error(rc); |
|
|
|
|
|
|
|
return 0; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int main (int argc, char **arv) { |
|
|
|
int main (int argc, char **arv) { |
|
|
|
sqlite3 *db = get_db(); |
|
|
|
sqlite3 *db = get_db(); |
|
|
|
ccleanup(db); |
|
|
|
ccleanup(db); |
|
|
@ -149,14 +189,24 @@ int main (int argc, char **arv) { |
|
|
|
char string[200]; |
|
|
|
char string[200]; |
|
|
|
int val; |
|
|
|
int val; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int time_now = 0; |
|
|
|
|
|
|
|
int last_time = 0; |
|
|
|
while(1) { |
|
|
|
while(1) { |
|
|
|
|
|
|
|
last_time = time_now; |
|
|
|
|
|
|
|
time_now = time(NULL); |
|
|
|
memset(string, 0, sizeof(char) * 200); |
|
|
|
memset(string, 0, sizeof(char) * 200); |
|
|
|
fscanf(serial, "%s%d", string, &val); |
|
|
|
fscanf(serial, "%s%d", string, &val); |
|
|
|
if (!strcmp(string, "temp:")) { |
|
|
|
if (!strcmp(string, "temp:")) { |
|
|
|
printf("Temperature: %d\n", val); |
|
|
|
printf("Temperature: %d\n", val); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (last_time != time_now) { |
|
|
|
|
|
|
|
insert_rec(db, INSERT_TEMP_TEMPL, val, WTHR_STATION_ID, time_now); |
|
|
|
|
|
|
|
} |
|
|
|
if (!strcmp(string, "humi:")) { |
|
|
|
if (!strcmp(string, "humi:")) { |
|
|
|
printf("Relative Humidity: %d\n", val); |
|
|
|
printf("Relative Humidity: %d\n", val); |
|
|
|
|
|
|
|
if (last_time != time_now) { |
|
|
|
|
|
|
|
insert_rec(db, INSERT_HUM_TEMPL, val, WTHR_STATION_ID, time_now); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|