aboutsummaryrefslogtreecommitdiff
path: root/src/Diller.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/Diller.js')
-rw-r--r--src/Diller.js109
1 files changed, 42 insertions, 67 deletions
diff --git a/src/Diller.js b/src/Diller.js
index d52e3f1..edb501d 100644
--- a/src/Diller.js
+++ b/src/Diller.js
@@ -1,12 +1,6 @@
-var di = require('di');
-
-/**
- * @param config DillerConfig
- * @param db DillerDb
- * @returns {{onMessage: onMessage, updateDeviceName: updateDeviceName}}
- * @constructor
- */
-function Diller(config, db) {
+var _ = require('lodash');
+
+function Diller(config, pg, dao) {
var log = config.log();
function newValue(dao, device, property, value) {
@@ -38,39 +32,29 @@ function Diller(config, db) {
function updateAggregates(propertyId, timestamp) {
log.info('Updating aggregates', {propertyId: propertyId, timestamp: timestamp});
- return db()
- .tx(function (pg) {
- var dao = new DillerDao(pg);
-
- return dao.updateMinuteAggregatesForProperty(propertyId, timestamp).then(function (minute) {
- return dao.updateHourAggregatesForProperty(propertyId, timestamp).then(function (hour) {
- return {
- minute: minute,
- hour: hour
- };
- });
- });
- }).then(function (res) {
- log.info('updateAggregates: ok', {propertyId: propertyId, aggregate: res});
- }, function (res) {
- log.warn('updateAggregates: failed', {res: res, propertyId: propertyId});
+ return dao.updateMinuteAggregatesForProperty(propertyId, timestamp).then(function (minute) {
+ return dao.updateHourAggregatesForProperty(propertyId, timestamp).then(function (hour) {
+ return {
+ minute: minute,
+ hour: hour
+ };
});
+ }).then(function (res) {
+ log.info('updateAggregates: ok', {propertyId: propertyId, aggregate: res});
+ }, function (res) {
+ log.warn('updateAggregates: failed', {res: res, propertyId: propertyId});
+ });
}
- function updateDeviceName(deviceId, name) {
- log.info('Updating device name', {deviceId: deviceId, name: name});
- return db()
- .tx(function (tx) {
- var dao = new DillerDao(tx);
+ function updateDeviceAttributes(deviceId, attributes) {
+ var x = _.clone(attributes);
+ x.deviceId = deviceId;
+ log.info('Updating device attributes', x);
- return dao.updateDevice(deviceId, {name: name})
- .then(function (res) {
- log.info('Device name updated', {deviceId: deviceId, name: name});
- return res;
- });
- })
+ return dao.updateDevice(deviceId, attributes);
}
+ //noinspection JSUnusedLocalSymbols
function onMessage(topic, message, payload) {
var parts = topic.split(/\//);
@@ -105,32 +89,28 @@ function Diller(config, db) {
return;
}
- return db()
- .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) {
- // log.info('data.device', data.device, 'data.property', data.property);
- return f(dao, data.device, data.property, message.toString());
+ 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 (res) {
+ })
+ .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) {
+ // log.info('data.device', data.device, 'data.property', data.property);
+ return f(dao, data.device, data.property, message.toString());
+ })
+ .then(function (res) {
log.warn('success', res);
}, function (res) {
log.warn('fail', res);
@@ -139,13 +119,8 @@ function Diller(config, db) {
return {
onMessage: onMessage,
- updateDeviceName: updateDeviceName
+ updateDeviceAttributes: updateDeviceAttributes
}
}
-var DillerConfig = require('./DillerConfig');
-var DillerDao = require('./DillerDao');
-var DillerDb = require('./DillerDb');
-di.annotate(Diller, new di.Inject(DillerConfig, DillerDb));
-
module.exports = Diller;