aboutsummaryrefslogtreecommitdiff
path: root/src/web
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-10-20 23:18:16 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2015-10-20 23:18:16 +0200
commit0266bdd60cb9cccf20a5ded3eba72ea833bee72d (patch)
treed727bad80aeaef673f48bbbc171fb4e9297b72fc /src/web
parent73d272ffe8954b3169901eda74428bad3d2740fe (diff)
downloaddiller-server-0266bdd60cb9cccf20a5ded3eba72ea833bee72d.tar.gz
diller-server-0266bdd60cb9cccf20a5ded3eba72ea833bee72d.tar.bz2
diller-server-0266bdd60cb9cccf20a5ded3eba72ea833bee72d.tar.xz
diller-server-0266bdd60cb9cccf20a5ded3eba72ea833bee72d.zip
o Adding a webapp.
o Using di.js as dependency injection framework.
Diffstat (limited to 'src/web')
-rw-r--r--src/web/DillerWeb.js158
1 files changed, 158 insertions, 0 deletions
diff --git a/src/web/DillerWeb.js b/src/web/DillerWeb.js
new file mode 100644
index 0000000..40fbc3d
--- /dev/null
+++ b/src/web/DillerWeb.js
@@ -0,0 +1,158 @@
+var express = require('express');
+var bodyParser = require('body-parser');
+var _ = require('lodash');
+var di = require('di');
+
+var DillerConfig = require('../DillerConfig');
+var Diller = require('../Diller');
+var DillerDb = require('../DillerDb');
+var DillerDao = require('../DillerDao');
+
+function DillerWeb(diller, db, config) {
+ var log = config.log();
+
+ var calls = [];
+ var app;
+
+ function getDevices(req, res) {
+ db().tx(function (pg) {
+ var dao = new DillerDao(pg);
+ return dao.devices();
+ }).then(function (devices) {
+ res.json({devices: devices});
+ }, function (err) {
+ log.warn('fail', err);
+ res.status(500).json({message: 'fail'});
+ });
+ }
+
+ function getDevice(req, res) {
+ db().tx(function (tx) {
+ var deviceId = req.params.deviceId;
+
+ var dao = new DillerDao(tx);
+ return tx.batch([
+ dao.deviceById(deviceId),
+ dao.devicePropertiesByDeviceId(deviceId)]
+ );
+ }).then(function (data) {
+ var device = data[0];
+ device.properties = data[1];
+ res.json({device: device});
+ }, function (err) {
+ log.warn('fail', err);
+ res.status(500).json({message: 'fail'});
+ });
+ }
+
+ function getValues(req, res) {
+ db().tx(function (tx) {
+ var propertyId = req.params.propertyId;
+
+ var dao = new DillerDao(tx);
+ return dao.valuesByPropertyId(propertyId, 10);
+ }).then(function (values) {
+ res.json({values: values});
+ }, function (err) {
+ log.warn('fail', err);
+ res.status(500).json({message: 'fail'});
+ });
+ }
+
+ function init() {
+ app = express();
+
+ app.use(bodyParser.urlencoded({extended: true}));
+ app.use(bodyParser.json());
+
+ var router = express.Router();
+
+ function addRoute(name, method, path, callback) {
+ router[method](path, callback);
+ var layer = _.last(router.stack);
+
+ calls.push({
+ name: name,
+ method: method,
+ path: path,
+ layer: layer,
+ keys: _.map(layer.keys, function (key) {
+ return key.name;
+ })
+ });
+ }
+
+ addRoute('getDevices', 'get', '/device', getDevices);
+ addRoute('getDevice', 'get', '/device/:deviceId', getDevice);
+ addRoute('getValues', 'get', '/property/:propertyId/values', getValues);
+
+ app.use('/api', router);
+ app.use(express.static('web'));
+ }
+
+ function listen() {
+ var port = process.env.HTTP_PORT || 8080;
+ app.listen(port);
+ }
+
+ function generateRpc() {
+ console.log('function DillerRpc($http) {');
+
+ var s = _.map(calls, function (call) {
+
+ //console.error(call);
+ 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';
+
+ s += _.map(call.layer.keys, function (key) {
+ return ' req.url = req.url.replace(/:' + key.name + '/, ' + key.name + ');\n'
+ }).join('');
+
+ s +=
+ ' return $http(req);\n' +
+ ' }\n';
+
+ return s;
+ });
+ _.each(s, function (x) {
+ console.log(x);
+ });
+
+ console.log(' return {');
+ console.log(_.map(calls, function (call) {
+ return ' ' + call.name + ': ' + call.name
+ }).join(',\n'));
+ console.log(' };');
+ console.log('}');
+ console.log('');
+
+ console.log('DillerRpcResolve = {};');
+ _.each(calls, function (call) {
+ var args = ['DillerRpc'];
+
+ if (call.keys.length > 0) {
+ args.push('$route');
+ }
+ console.log('DillerRpcResolve.' + call.name + ' = function(' + args.join(', ') + ') {');
+
+ args = _.map(call.keys, function (key) {
+ return '$route.current.params.' + key;
+ });
+ console.log(' return DillerRpc.' + call.name + '(' + args.join(', ') + ');');
+ console.log('};');
+ });
+ }
+
+ return {
+ init: init,
+ listen: listen,
+ generateRpc: generateRpc
+ }
+}
+
+di.annotate(DillerWeb, new di.Inject(Diller, DillerDb, DillerConfig));
+
+module.exports = DillerWeb;