diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-11-01 16:13:45 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-11-01 16:13:45 +0100 |
commit | 478a69d834904f62cc4fb5ff52d5ed345e6859af (patch) | |
tree | efa4839e878f1debf3db6a11f0636405538af761 /src/web | |
parent | 85474565b0a0d92a09fecb14e8bc0afaad7fbc64 (diff) | |
download | diller-server-478a69d834904f62cc4fb5ff52d5ed345e6859af.tar.gz diller-server-478a69d834904f62cc4fb5ff52d5ed345e6859af.tar.bz2 diller-server-478a69d834904f62cc4fb5ff52d5ed345e6859af.tar.xz diller-server-478a69d834904f62cc4fb5ff52d5ed345e6859af.zip |
web:
o Publishing messages when the name/description is changed from the web.
Diffstat (limited to 'src/web')
-rw-r--r-- | src/web/DillerWeb.js | 73 |
1 files changed, 47 insertions, 26 deletions
diff --git a/src/web/DillerWeb.js b/src/web/DillerWeb.js index 42aa7bf..5f52bde 100644 --- a/src/web/DillerWeb.js +++ b/src/web/DillerWeb.js @@ -3,20 +3,20 @@ var bodyParser = require('body-parser'); var _ = require('lodash'); var di = require('di'); -var _DillerConfig = require('../DillerConfig'); -var _DillerTx = require('../DillerTx'); - /** - * @param {function(function(PgTx, DillerDao, Diller))} tx * @param {DillerConfig} config + * @param {DillerMqttClient} mqttClient + * @param {function(function(PgTx, DillerDao, Diller))} tx * @constructor */ -function DillerWeb(tx, config) { +function DillerWeb(config, mqttClient, tx) { var log = config.log(); var calls = []; var app; + mqttClient.run('web'); + /** * @param {HttpRes} res */ @@ -124,26 +124,44 @@ function DillerWeb(tx, config) { tx(function (pg, dao, diller) { var propertyId = req.params.propertyId; - var body = req.body; - - var p; - if (!body.attribute) { - res.status(400).json({message: 'Required keys: "attribute" and "value".'}); - } else if (body.attribute == 'name' || body.attribute == 'description') { - var attributes = {}; - attributes[body.attribute] = body.value; - - p = diller.updatePropertyAttributes(propertyId, attributes); - } else { - p = Promise.reject('Unsupported attribute: ' + body.attribute); - } - - return p.then(function (property) { - return pg.batch([ - dao.deviceById(property.device), - dao.devicePropertiesByDeviceId(property.device)] - ); - }); + return dao.devicePropertyById(propertyId) + .then(function (property) { + return dao.deviceById(property.device) + .then(function (device) { + return {property: property, device: device}; + }); + }) + .then(function (data) { + var property = data.property; + var device = data.device; + var body = req.body; + + var p; + if (!body.attribute) { + res.status(400).json({message: 'Required keys: "attribute" and "value".'}); + } else if (body.attribute == 'name' || body.attribute == 'description') { + var attributes = {}; + attributes[body.attribute] = body.value; + + p = diller.updatePropertyAttributes(propertyId, attributes); + + var topic = '/diller/' + device.key + '/sensors/' + property.key + '/' + body.attribute; + var opts = { + retain: true + }; + mqttClient.publish(topic, body.value, opts); + + } else { + p = Promise.reject('Unsupported attribute: ' + body.attribute); + } + + return p.then(function () { + return pg.batch([ + dao.deviceById(property.device), + dao.devicePropertiesByDeviceId(property.device)] + ); + }); + }); }).then(function (data) { var device = data[0]; device.properties = data[1]; @@ -293,6 +311,9 @@ function DillerWeb(tx, config) { } } -di.annotate(DillerWeb, new di.Inject(_DillerTx, _DillerConfig)); +di.annotate(DillerWeb, new di.Inject( + require('../DillerConfig'), + require('../mqtt/DillerMqttClient'), + require('../DillerTx'))); module.exports = DillerWeb; |