aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Diller.js3
-rw-r--r--src/DillerConfig.js1
-rw-r--r--src/DillerDao.js8
-rw-r--r--src/web/DillerWeb.js83
4 files changed, 58 insertions, 37 deletions
diff --git a/src/Diller.js b/src/Diller.js
index a057054..fa2e308 100644
--- a/src/Diller.js
+++ b/src/Diller.js
@@ -81,7 +81,7 @@ function Diller(config, db) {
}
if (!f) {
- log.warn('Unknown message topic:', topic);
+ log.warn('Unknown topic:', topic);
return;
}
@@ -107,6 +107,7 @@ function Diller(config, db) {
});
})
.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) {
diff --git a/src/DillerConfig.js b/src/DillerConfig.js
index 84afe42..652561a 100644
--- a/src/DillerConfig.js
+++ b/src/DillerConfig.js
@@ -55,6 +55,7 @@ function configureLogging(app) {
function DillerConfig() {
return {
isProd: isProd,
+ updateDillerRpc: !isProd(),
mqttUrl: mqttUrl,
postgresqlConfig: postgresqlConfig,
configureLogging: configureLogging,
diff --git a/src/DillerDao.js b/src/DillerDao.js
index c4d0e67..b46e734 100644
--- a/src/DillerDao.js
+++ b/src/DillerDao.js
@@ -33,7 +33,7 @@ function DillerDao(tx) {
}
function devicePropertyByDeviceIdAndKey(deviceId, key) {
- return tx.oneOrNone('SELECT id FROM device_property WHERE device=$1 AND key=$2', [deviceId, key]);
+ return tx.oneOrNone('SELECT ' + propertyColumns + ' FROM device_property WHERE device=$1 AND key=$2', [deviceId, key]);
}
function devicePropertiesByDeviceId(deviceId) {
@@ -41,11 +41,11 @@ function DillerDao(tx) {
}
function insertDeviceProperty(deviceId, key) {
- return tx.oneOrNone('INSERT INTO device_property(id, device, key, created_timestamp) VALUES(DEFAULT, $1, $2, CURRENT_TIMESTAMP) RETURNING ' + propertyColumns, [deviceId, key]);
+ return tx.one('INSERT INTO device_property(id, device, key, created_timestamp) VALUES(DEFAULT, $1, $2, CURRENT_TIMESTAMP) RETURNING ' + propertyColumns, [deviceId, key]);
}
function updatePropertyName(id, name) {
- return tx.none('UPDATE device_property SET name=$1 WHERE id=$2', name, id);
+ return tx.none('UPDATE device_property SET name=$1 WHERE id=$2', [name, id]);
}
function updatePropertyDescription(id, description) {
@@ -58,7 +58,7 @@ function DillerDao(tx) {
function valuesByPropertyId(propertyId, limit) {
limit = limit || 10;
- return tx.many('SELECT timestamp, value FROM value WHERE property=$1 LIMIT $2', [propertyId, limit]);
+ return tx.many('SELECT timestamp, value FROM value WHERE property=$1 ORDER BY timestamp DESC LIMIT $2', [propertyId, limit]);
}
function insertValue(propertyId, value) {
diff --git a/src/web/DillerWeb.js b/src/web/DillerWeb.js
index 40fbc3d..a5bdf6b 100644
--- a/src/web/DillerWeb.js
+++ b/src/web/DillerWeb.js
@@ -59,17 +59,22 @@ function DillerWeb(diller, db, config) {
});
}
+ function getIndex(req, res) {
+ var baseUrl = (req.headers['trygvis-prefix'] || '').replace(/\/$/, '');
+ res.render('index', {baseUrl: baseUrl + '/'});
+ }
+
function init() {
app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
- var router = express.Router();
+ var api = express.Router();
- function addRoute(name, method, path, callback) {
- router[method](path, callback);
- var layer = _.last(router.stack);
+ function addApi(name, method, path, callback) {
+ api[method](path, callback);
+ var layer = _.last(api.stack);
calls.push({
name: name,
@@ -82,12 +87,21 @@ function DillerWeb(diller, db, config) {
});
}
- addRoute('getDevices', 'get', '/device', getDevices);
- addRoute('getDevice', 'get', '/device/:deviceId', getDevice);
- addRoute('getValues', 'get', '/property/:propertyId/values', getValues);
+ addApi('getDevices', 'get', '/device', getDevices);
+ addApi('getDevice', 'get', '/device/:deviceId', getDevice);
+ addApi('getValues', 'get', '/property/:propertyId/values', getValues);
+
+ var templates = express.Router();
- app.use('/api', router);
- app.use(express.static('web'));
+ templates.get('/', getIndex);
+
+ app.set('views', 'web/templates');
+ app.set('view engine', 'jade');
+ app.set('view cache', false);
+
+ app.use('/api', api);
+ app.use('/', templates);
+ app.use(express.static('web/static'));
}
function listen() {
@@ -96,54 +110,59 @@ function DillerWeb(diller, db, config) {
}
function generateRpc() {
- console.log('function DillerRpc($http) {');
+ var s =
+ 'function DillerRpc($http, DillerConfig) {\n' +
+ ' var baseUrl = DillerConfig.baseUrl;\n' +
+ '\n';
- var s = _.map(calls, function (call) {
+ s += _.map(calls, function (call) {
//console.error(call);
- console.error('call.layer', call.layer);
+ //console.error('call.layer', call.layer);
var s = ' function ' + call.name + '(' + call.keys.join(', ') + ') {\n' +
' var req = {};\n' +
' req.method = \'' + call.method + '\';\n' +
- ' req.url = \'/api' + call.path + '\';\n';
+ ' req.url = baseUrl + \'/api' + call.path + '\';\n';
s += _.map(call.layer.keys, function (key) {
return ' req.url = req.url.replace(/:' + key.name + '/, ' + key.name + ');\n'
- }).join('');
+ }).join('\n');
s +=
' return $http(req);\n' +
' }\n';
return s;
- });
- _.each(s, function (x) {
- console.log(x);
- });
-
- console.log(' return {');
- console.log(_.map(calls, function (call) {
+ }).join('\n');
+ s += '\n';
+ s += ' return {\n';
+ s += _.map(calls, function (call) {
return ' ' + call.name + ': ' + call.name
- }).join(',\n'));
- console.log(' };');
- console.log('}');
- console.log('');
-
- console.log('DillerRpcResolve = {};');
- _.each(calls, function (call) {
+ }).join(',\n');
+ s += '\n';
+ s += ' };\n';
+ s += '}\n';
+ s += '\n';
+
+ s += 'DillerRpcResolve = {};\n';
+ s += _.map(calls, function (call) {
+ var s = '';
var args = ['DillerRpc'];
if (call.keys.length > 0) {
args.push('$route');
}
- console.log('DillerRpcResolve.' + call.name + ' = function(' + args.join(', ') + ') {');
+ s += 'DillerRpcResolve.' + call.name + ' = function(' + args.join(', ') + ') {\n';
args = _.map(call.keys, function (key) {
return '$route.current.params.' + key;
});
- console.log(' return DillerRpc.' + call.name + '(' + args.join(', ') + ');');
- console.log('};');
- });
+ s += ' return DillerRpc.' + call.name + '(' + args.join(', ') + ');\n';
+ s += '};\n';
+
+ return s;
+ }).join('');
+ return s;
}
return {