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.js38
1 files changed, 33 insertions, 5 deletions
diff --git a/src/web/DillerWeb.js b/src/web/DillerWeb.js
index 396c2f8..b42e47e 100644
--- a/src/web/DillerWeb.js
+++ b/src/web/DillerWeb.js
@@ -1,8 +1,11 @@
var express = require('express');
+var moment = require('moment');
var bodyParser = require('body-parser');
var _ = require('lodash');
var di = require('di');
+var isoFormat = 'YYYY-MM-DDTHH:mm:ss';
+
/**
* @param {DillerConfig} config
* @param {DillerMqttClient} mqttClient
@@ -105,14 +108,33 @@ function DillerWeb(config, mqttClient, tx) {
* @param {HttpRes} res
*/
function getValues(req, res) {
+ var from = req.query.from && moment(req.query.from, isoFormat);
+ var to = req.query.to && moment(req.query.to, isoFormat);
+ var limit = req.query.limit || 10;
+
tx(function (tx, dao) {
- var propertyId = req.params.propertyId;
- return dao.valuesByPropertyId(propertyId, 10);
+ var propertyId = Number(req.params.propertyId);
+
+ if (from || to) {
+ from = (from || moment()).startOf('hour');
+ to = (to || moment()).startOf('hour');
+
+ if (typeof propertyId !== 'number' || !from.isValid() || !to.isValid()) {
+ log.info('getValues: Invalid parameters: propertyId', propertyId, 'from', from.toISOString(), 'to', to.toISOString());
+ return Promise.reject('Invalid parameters: ' + typeof propertyId);
+ } else {
+ log.info('getValues: propertyId', propertyId, 'from', from.toISOString(), 'to', to.toISOString());
+
+ return dao.aggregateValuesByPropertyId(propertyId, 'hour', from.toDate(), to.toDate());
+ }
+ } else {
+ return dao.valuesByPropertyId(propertyId, limit);
+ }
}).then(function (values) {
res.json({values: values});
}, function (err) {
log.warn('fail', err);
- res.status(500).json({message: 'fail'});
+ res.status(500).json({message: typeof err === 'string' ? err : 'fail'});
});
}
@@ -254,7 +276,7 @@ function DillerWeb(config, mqttClient, tx) {
s += _.map(calls, function (call) {
- var s = ' function ' + call.name + '(' + call.keys.join(', ') + ') {\n' +
+ var s = ' function ' + call.name + 'Req(' + call.keys.join(', ') + ') {\n' +
' var req = {};\n' +
' req.method = \'' + call.method + '\';\n' +
' req.url = baseUrl + \'/api' + call.path + '\';\n';
@@ -268,6 +290,12 @@ function DillerWeb(config, mqttClient, tx) {
}
s +=
+ ' return req;\n' +
+ ' }\n' +
+ '\n';
+
+ s += ' function ' + call.name + '(' + call.keys.join(', ') + ') {\n' +
+ ' var req = ' + call.name + 'Req(' + call.keys.join(', ') + ');\n' +
' return $http(req);\n' +
' }\n';
@@ -276,7 +304,7 @@ function DillerWeb(config, mqttClient, tx) {
s += '\n';
s += ' return {\n';
s += _.map(calls, function (call) {
- return ' ' + call.name + ': ' + call.name
+ return ' ' + call.name + 'Req: ' + call.name + 'Req,\n ' + call.name + ': ' + call.name
}).join(',\n');
s += '\n';
s += ' };\n';