diff --git a/config.c b/config.c index de59a81..b1091bc 100644 --- a/config.c +++ b/config.c @@ -21,6 +21,9 @@ */ #include "includes.h" +#include "database.h" +#include "jsonbot.h" +#include "spacestate.h" /* My global state */ configuration *conf = NULL; @@ -160,6 +163,8 @@ STATUS read_configuration_file(TALLOC_CTX *mem_ctx) database_init(); } else if (strcmp(ptr, "jsonbot") == 0) { jsonbot_init(); + } else if (strcmp(ptr, "spacestate") == 0) { + spacestate_init(); } } while((ptr = strtok(NULL, " ")) != NULL); } diff --git a/config.h b/config.h index 041b7ad..afb8bec 100644 --- a/config.h +++ b/config.h @@ -48,6 +48,13 @@ typedef struct { char *jsonbot_password; char *jsonbot_privmsg_to; + /* Space state database client configuration */ + char *spacestate_host; + char *spacestate_username; + char *spacestate_password; + char *spacestate_name; + char *spacestate_driver; + /* Global configuration based state */ GKeyFile *keyfile; uint8_t event_handler_cnt; diff --git a/includes.h b/includes.h index d9ed54f..27271c5 100644 --- a/includes.h +++ b/includes.h @@ -43,5 +43,3 @@ #include "talloc.h" #include "sia.h" #include "config.h" -#include "database.h" -#include "jsonbot.h" diff --git a/spacestate.c b/spacestate.c new file mode 100644 index 0000000..4f03fca --- /dev/null +++ b/spacestate.c @@ -0,0 +1,118 @@ +/* + SIA-HS Alarm Monitoring Service + Copyright (C) Wilco Baan Hofman 2013 + + This program 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 + (at your option) any later version. + + This program 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 this program. If not, see . +*/ +#include "includes.h" + + +static dbi_conn conn; + +STATUS spacestate_update(TALLOC_CTX *mem_ctx, const char *prom, const char *code, const char *description) { + bool must_close = 0; + bool must_open = 0; + + DEBUG(6, "Got event for spacestate: %s %s %s -- %s: %s\n", prom, code, description, sia_code_str(code), sia_code_desc(code)); + + + if (strncmp(code, "CL", 2) == 0 || + strncmp(code, "CA", 2) == 0 || + strncmp(code, "CF", 2) == 0 || + strncmp(code, "CJ", 2) == 0 || + strncmp(code, "CK", 2) == 0 || + strncmp(code, "CQ", 2) == 0 || + strncmp(code, "CS", 2) == 0) { + must_close = 1; + } + + if (strncmp(code, "OP", 2) == 0 || + strncmp(code, "OA", 2) == 0 || + strncmp(code, "OJ", 2) == 0 || + strncmp(code, "OK", 2) == 0 || + strncmp(code, "OQ", 2) == 0 || + strncmp(code, "OS", 2) == 0) { + must_open = 1; + } + + if (must_open) { + dbi_conn_queryf(conn, "UPDATE space_state set override=0, override_state='open'"); + } else if (must_close) { + dbi_conn_queryf(conn, "UPDATE space_state set override=1, override_state='close'"); + } + + return ST_OK; +} + + + +STATUS spacestate_init(void) +{ + configuration *conf = get_modifiable_conf(); + GError *error = NULL; + + conf->spacestate_host = g_key_file_get_string(conf->keyfile, "spacestate", + "host", &error); + if (error) { + fprintf(stderr, "No spacestate host supplied in the configuration.\n"); + return ST_CONFIGURATION_ERROR; + } + conf->spacestate_name = g_key_file_get_string(conf->keyfile, "spacestate", + "name", &error); + if (error) { + fprintf(stderr, "No spacestate name supplied in the configuration.\n"); + return ST_CONFIGURATION_ERROR; + } + conf->spacestate_driver = g_key_file_get_string(conf->keyfile, "spacestate", + "driver", &error); + if (error) { + fprintf(stderr, "No spacestate driver supplied in the configuration.\n"); + return ST_CONFIGURATION_ERROR; + } + conf->spacestate_username = g_key_file_get_string(conf->keyfile, "spacestate", + "username", &error); + if (error) { + fprintf(stderr, "No spacestate username supplied in the configuration.\n"); + return ST_CONFIGURATION_ERROR; + } + conf->spacestate_password = g_key_file_get_string(conf->keyfile, "spacestate", + "password", &error); + if (error) { + fprintf(stderr, "No spacestate password supplied in the configuration.\n"); + return ST_CONFIGURATION_ERROR; + } + + conf->event_handlers = talloc_realloc(conf, conf->event_handlers, event_function, conf->event_handler_cnt+1); + conf->event_handlers[conf->event_handler_cnt] = spacestate_update; + conf->event_handler_cnt++; + + DEBUG(1, "Connecting to %s space state database %s at %s as user %s", conf->spacestate_driver, + conf->spacestate_name, conf->spacestate_host, conf->spacestate_username); + + dbi_initialize(NULL); + conn = dbi_conn_new(conf->spacestate_driver); + dbi_conn_set_option(conn, "host", conf->spacestate_host); + dbi_conn_set_option(conn, "username", conf->spacestate_username); + dbi_conn_set_option(conn, "password", conf->spacestate_password); + dbi_conn_set_option(conn, "dbname", conf->spacestate_name); + dbi_conn_set_option(conn, "encoding", "UTF-8"); + + if (dbi_conn_connect(conn) < 0) { + DEBUG(0, "Could not connect to the space state database"); + return ST_DATABASE_FAILURE; + } + + return ST_OK; +} + diff --git a/spacestate.h b/spacestate.h new file mode 100644 index 0000000..3043d81 --- /dev/null +++ b/spacestate.h @@ -0,0 +1,20 @@ +/* + SIA-HS Alarm Monitoring Service + Copyright (C) Wilco Baan Hofman 2012 + + This program 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 + (at your option) any later version. + + This program 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 this program. If not, see . +*/ + +STATUS spacestate_update(TALLOC_CTX *mem_ctx, const char *prom, const char *code, const char *description); +STATUS spacestate_init(void); diff --git a/wscript b/wscript index d3e4c67..6377ba0 100644 --- a/wscript +++ b/wscript @@ -97,7 +97,8 @@ def configure(conf): def build(bld): bld.stlib(source="database.c", target="database", use='glib-2.0') bld.stlib(source="status.c", target="status", use='glib-2.0') - bld.stlib(source="config.c", target="config", use='glib-2.0 database jsonbot') + bld.stlib(source="spacestate.c", target="spacestate", use='glib-2.0') + bld.stlib(source="config.c", target="config", use='glib-2.0 database jsonbot spacestate') bld.stlib(source="sia.c", target="sia", use='glib-2.0') bld.stlib(source="siahs.c", target="siahs", use='glib-2.0') bld.stlib(source="jsonbot.c", target="jsonbot", use='glib-2.0') @@ -105,12 +106,12 @@ def build(bld): bld.program( source = 'siahsd.c', target = 'siahsd', - use = [ 'database', 'config', 'status', 'sia', 'siahs', 'jsonbot', 'dbi', 'talloc', 'glib-2.0', 'nettle' ]) + use = [ 'database', 'config', 'status', 'sia', 'siahs', 'jsonbot', 'spacestate', 'dbi', 'talloc', 'glib-2.0', 'nettle' ]) bld.program( source = 'secip.idl secipd.c crc16.c', target = 'secipd', - use = [ 'database', 'config', 'status', 'sia', 'siahs', 'jsonbot', 'dbi', 'samba', 'glib-2.0', 'nettle', 'ndr' ]) + use = [ 'database', 'config', 'status', 'sia', 'siahs', 'jsonbot', 'spacestate', 'dbi', 'samba', 'glib-2.0', 'nettle', 'ndr' ]) pass def clean(ctx):