aboutsummaryrefslogtreecommitdiff
path: root/src/web/DillerWeb.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/web/DillerWeb.js')
-rw-r--r--src/web/DillerWeb.js73
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;