From f2975055fd741b6cafaf71b2701fec0d8e24d335 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Thu, 29 Apr 2021 16:13:27 +0200 Subject: [PATCH] findDevice tuned --- schema/createSchema.sql | 8 +++++ sink/sink20169.c | 76 +++++++++++++++++++++++++++++------------ 2 files changed, 63 insertions(+), 21 deletions(-) create mode 100644 schema/createSchema.sql diff --git a/schema/createSchema.sql b/schema/createSchema.sql new file mode 100644 index 0000000..68f8bc4 --- /dev/null +++ b/schema/createSchema.sql @@ -0,0 +1,8 @@ +CREATE SEQUENCE device_s START WITH 1 INCREMENT BY 1; + +CREATE TABLE device_t ( + id integer PRIMARY KEY DEFAULT NEXTVAL('device_s'), + deviceid varchar(16) NOT NULL, + sharedsecret varchar(31) NOT NULL, + active boolean NOT NULL DEFAULT false +); \ No newline at end of file diff --git a/sink/sink20169.c b/sink/sink20169.c index 40ec1b7..282e47b 100644 --- a/sink/sink20169.c +++ b/sink/sink20169.c @@ -53,6 +53,26 @@ typedef struct { bool verbose = false; + +int openDatabaseConnection(t_commonHandle *handle) { + int res = 0; + + if (! handle->conn) { + logmsg(LOG_DEBUG, "Opening connection to database"); + handle->conn = PQconnectdb(handle->postgresqlConnInfo); + } else if (PQstatus(handle->conn) != CONNECTION_OK) { + logmsg(LOG_DEBUG, "Resetting connection to database"); + PQreset(handle->conn); + } + + if (PQstatus(handle->conn) != CONNECTION_OK) { + logmsg(LOG_ERR, "Connection to database failed: %s", PQerrorMessage(handle->conn)); + res = -1; + } + + return res; +} + int initConfig(const char *configFilename, t_configHandle *configHandle) { configHandle->numOfDevices = 0; configHandle->devices = NULL; @@ -114,13 +134,46 @@ void deinitConfig(t_configHandle *configHandle) { } t_device *findDevice(t_commonHandle *handle, char *deviceId) { + t_device *foundDevice = NULL; + if (0 == openDatabaseConnection(handle)) { + char stmt[256]; + int res1 = snprintf(stmt, sizeof(stmt), + "SELECT sharedsecret, active " + " FROM device_t " + " WHERE deviceid = '%s'", + deviceId); + if (res1 > sizeof(stmt)) { + logmsg(LOG_ERR, "stmt buffer to small"); + } else { + logmsg(LOG_DEBUG, "Statement: %s", stmt); + PGresult *res2 = PQexec(handle->conn, stmt); + ExecStatusType execStatus = PQresultStatus(res2); + switch(PQresultStatus(execStatus)) { + case PGRES_COMMAND_OK: + logmsg(LOG_INFO, "findDevice select returns PGRES_COMMAND_OK"); + break; + case PGRES_TUPLES_OK: + logmsg(LOG_INFO, "findDevice select returns PGRES_TUPLES_OK"); + break; + default: + logmsg(LOG_INFO, "findDevice select returns something else: %s", PQresStatus(execStatus)); + } + PQclear(res2); + } + } else { + logmsg(LOG_ERR, "No database connection available, data lost"); + } + + + t_configHandle *configHandle = handle->configHandle; for (uint16_t i = 0; i < configHandle->numOfDevices; i++) { if (! strcmp(configHandle->devices[i].deviceId, deviceId)) { - return &(configHandle->devices[i]); + foundDevice = &(configHandle->devices[i]); + break; } } - return NULL; + return foundDevice; } int initReceiver(t_configHandle *configHandle, t_commonHandle *handle) { @@ -226,25 +279,6 @@ void deinitForwarder(t_commonHandle *handle) { PQfinish(handle->conn); } -int openDatabaseConnection(t_commonHandle *handle) { - int res = 0; - - if (! handle->conn) { - logmsg(LOG_DEBUG, "Opening connection to database"); - handle->conn = PQconnectdb(handle->postgresqlConnInfo); - } else if (PQstatus(handle->conn) != CONNECTION_OK) { - logmsg(LOG_DEBUG, "Resetting connection to database"); - PQreset(handle->conn); - } - - if (PQstatus(handle->conn) != CONNECTION_OK) { - logmsg(LOG_ERR, "Connection to database failed: %s", PQerrorMessage(handle->conn)); - res = -1; - } - - return res; -} - int sendToDB(t_commonHandle *handle, const char *location, const char *deviceId, uint32_t frequency, uint64_t timestamp) { int retcode = 0;