aboutsummaryrefslogtreecommitdiff
path: root/src/web
diff options
context:
space:
mode:
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;