aboutsummaryrefslogtreecommitdiff
path: root/src/Diller.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 /src/Diller.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 'src/Diller.js')
-rw-r--r--src/Diller.js101
1 files changed, 101 insertions, 0 deletions
diff --git a/src/Diller.js b/src/Diller.js
new file mode 100644
index 0000000..9a1de23
--- /dev/null
+++ b/src/Diller.js
@@ -0,0 +1,101 @@
+var DillerDao = require('./DillerDao');
+var pgp = require('pg-promise')();
+
+function Diller(config, log) {
+
+ function newValue(dao, device, property, value) {
+ log.info('new value for device ' + device.key + '/' + property.key + ' = ' + value, {
+ deviceId: device.id,
+ propertyId: property.id,
+ value: value
+ });
+
+ return dao.insertValue(property.id, value);
+ }
+
+ function newName(dao, device, property, name) {
+ log.info('New name for property ', device.key + '/' + property.key + '.name = ' + name);
+
+ return dao.updatePropertyName(property.id, name);
+ }
+
+ function newDescription(dao, device, property, description) {
+ log.info('New description for property ', device.key + '/' + property.key + '.description = ' + description);
+
+ return dao.updatePropertyDescription(property.id, description);
+ }
+
+ function onMessage(topic, message, payload) {
+ var parts = topic.split(/\//);
+
+ log.info('Processing message to ' + topic);
+
+ // /diller/5c:cf:7f:06:59:a5/sensors/temp-0/value
+
+ if (parts[3] == 'sensors') {
+ parts[3] = 'property';
+ }
+
+ if (parts.length != 6 || parts[1] != 'diller' || parts[3] != 'property') {
+ log.warn('no match: ', topic, parts);
+ return;
+ }
+
+ var device_key = parts[2];
+ var property_key = parts[4];
+ var msg_type = parts[5];
+
+ var f;
+ if (msg_type == 'value') {
+ f = newValue
+ } else if (msg_type == 'name') {
+ f = newName
+ } else if (msg_type == 'description') {
+ f = newDescription
+ }
+
+ if (!f) {
+ log.warn('Unknown message topic:', topic);
+ return;
+ }
+
+ return pgp(config.postgresqlConfig)
+ .tx(function (pg) {
+ var dao = new DillerDao(pg);
+
+ return dao.deviceByKey(device_key)
+ .then(function (device) {
+ return device || dao.insertDevice(device_key).then(function (device) {
+ log.info('New device created', {device_key: device_key, id: device.id});
+ return device;
+ });
+ })
+ .then(function (device) {
+ return dao.devicePropertyByDeviceIdAndKey(device.id, property_key).then(function (property) {
+ var ret = {device: device, property: property};
+ return (property && ret) || dao.insertDeviceProperty(device.id, property_key).then(function (p) {
+ log.info('Created new device property', {id: p.id, key: p.key});
+ ret.property = p;
+ return ret;
+ });
+ });
+ })
+ .then(function (data) {
+ return f(dao, data.device, data.property, message.toString());
+ });
+ }).then(function (res) {
+ log.warn('success', res);
+ }, function (res) {
+ log.warn('fail', res);
+ });
+ }
+
+ return {
+ onMessage: onMessage
+ }
+}
+
+//noinspection JSUnresolvedVariable
+module.exports = {
+ Diller: Diller
+};