aboutsummaryrefslogtreecommitdiff
path: root/apps/device.js
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-10-18 18:10:20 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2015-10-18 18:10:20 +0200
commit52eb8072664a61ea61dbdbef7485d6c81dbbcfe9 (patch)
treef4218bc276a0188da26da472d1233c264ff31dfe /apps/device.js
parent9339e6269d6f5a5d0421c23c5e9ba3c8500fb535 (diff)
downloaddiller-server-52eb8072664a61ea61dbdbef7485d6c81dbbcfe9.tar.gz
diller-server-52eb8072664a61ea61dbdbef7485d6c81dbbcfe9.tar.bz2
diller-server-52eb8072664a61ea61dbdbef7485d6c81dbbcfe9.tar.xz
diller-server-52eb8072664a61ea61dbdbef7485d6c81dbbcfe9.zip
o Dropping the app concept.
o Switching to pg-promise to not get overloaded by callbacks.
Diffstat (limited to 'apps/device.js')
-rw-r--r--apps/device.js55
1 files changed, 0 insertions, 55 deletions
diff --git a/apps/device.js b/apps/device.js
deleted file mode 100644
index 1b8e169..0000000
--- a/apps/device.js
+++ /dev/null
@@ -1,55 +0,0 @@
-var pg, log;
-
-function init(config) {
- pg = config.pg;
- log = config.log;
-}
-exports.init = init;
-
-var deviceR = /^\/diller\/([0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5})\//;
-
-function onMessage(topic, message, payload) {
- var matches = deviceR.exec(topic)
-
- if (!matches) {
- log.warn('no match: ', topic);
- return;
- }
-
- var mac = matches[1];
-
- pg.connect(function (err, client, done) {
- if (err) {
- done();
- log.error('Could not connect to postgres', err);
- return;
- }
-
- client.query('select count(*) as count from device where key=$1', [mac], function (err, result) {
- if (err) {
- done();
- log.error('error running query', err);
- return;
- }
-
- if (result.rows[0].count > 0) {
- done();
- return;
- }
-
- log.info('New device:', mac);
-
- client.query('insert into device(id, key, created_timestamp) values(default, $1, current_timestamp) returning id', [mac], function (err, result) {
- if (err) {
- done();
- log.error('error inserting new device', err);
- }
-
- log.info('New device created', result.rows[0].id);
- done();
- });
- });
- });
-}
-
-exports.onMessage = onMessage;