aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-11-01 15:28:40 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2015-11-01 15:28:40 +0100
commit85474565b0a0d92a09fecb14e8bc0afaad7fbc64 (patch)
tree66fc12f1fdfcd19e4a5d0fc01666a191bc3cdcb0 /src
parentf7ff8a837bed336a14aa0442ed8286f3ef78f577 (diff)
downloaddiller-server-85474565b0a0d92a09fecb14e8bc0afaad7fbc64.tar.gz
diller-server-85474565b0a0d92a09fecb14e8bc0afaad7fbc64.tar.bz2
diller-server-85474565b0a0d92a09fecb14e8bc0afaad7fbc64.tar.xz
diller-server-85474565b0a0d92a09fecb14e8bc0afaad7fbc64.zip
misc:
o Playing around with type annotations.
Diffstat (limited to 'src')
-rw-r--r--src/Diller.js13
-rw-r--r--src/DillerConfig.js6
-rw-r--r--src/DillerDao.js21
-rw-r--r--src/DillerTx.js29
-rw-r--r--src/mqtt/DillerMqtt.js6
-rw-r--r--src/types.js95
-rw-r--r--src/web/DillerWeb.js49
7 files changed, 188 insertions, 31 deletions
diff --git a/src/Diller.js b/src/Diller.js
index dc4c5e7..e2dd47d 100644
--- a/src/Diller.js
+++ b/src/Diller.js
@@ -1,5 +1,11 @@
var _ = require('lodash');
+/**
+ * @param {DillerConfig} config
+ * @param {PgTx} pg
+ * @param {DillerDao} dao
+ * @constructor
+ */
function Diller(config, pg, dao) {
var log = config.log();
@@ -46,6 +52,10 @@ function Diller(config, pg, dao) {
});
}
+ /**
+ * @param deviceId int
+ * @param attributes object
+ */
function updateDeviceAttributes(deviceId, attributes) {
var x = _.clone(attributes);
x.deviceId = deviceId;
@@ -127,11 +137,12 @@ function Diller(config, pg, dao) {
});
}
+ /** @lends Diller.prototype */
return {
onMessage: onMessage,
updateDeviceAttributes: updateDeviceAttributes,
updatePropertyAttributes: updatePropertyAttributes
- }
+ };
}
module.exports = Diller;
diff --git a/src/DillerConfig.js b/src/DillerConfig.js
index 440aadb..f7755a0 100644
--- a/src/DillerConfig.js
+++ b/src/DillerConfig.js
@@ -52,9 +52,15 @@ function configureLogging(app) {
log = bunyan.createLogger(cfg);
}
+/**
+ * @constructor
+ */
function DillerConfig() {
var httpPort = process.env.HTTP_PORT || 8080;
+ /**
+ * @lends DillerConfig.prototype
+ */
return {
isProd: isProd,
updateDillerRpc: !isProd(),
diff --git a/src/DillerDao.js b/src/DillerDao.js
index 63ad3c1..d81b418 100644
--- a/src/DillerDao.js
+++ b/src/DillerDao.js
@@ -1,15 +1,21 @@
var _ = require('lodash');
+/**
+ * @param tx {PgTx}
+ * @class
+ */
function DillerDao(tx) {
var deviceColumns = 'id, created_timestamp, key, name, description';
var propertyColumns = 'id, created_timestamp, device, key, name, description, last_value, last_timestamp';
- var valueColumns = 'property, timestamp, value_text, value_numeric';
// -------------------------------------------------------------------------------------------------------------------
// Device
// -------------------------------------------------------------------------------------------------------------------
+ /**
+ * @returns {Promise}
+ */
function devices() {
return tx.manyOrNone("SELECT " + deviceColumns + " FROM device");
}
@@ -122,6 +128,9 @@ function DillerDao(tx) {
});
}
+ /**
+ * @returns {Promise}
+ */
function updateMinuteAggregatesForProperty(propertyId, timestamp) {
return tx.none('DELETE FROM value_by_minute WHERE property=$1 AND timestamp=DATE_TRUNC(\'minute\', $2::TIMESTAMPTZ)', [propertyId, timestamp])
.then(function () {
@@ -134,6 +143,7 @@ function DillerDao(tx) {
});
}
+ /** @lends DillerDao.prototype */
return {
devices: devices,
deviceById: deviceById,
@@ -151,7 +161,14 @@ function DillerDao(tx) {
insertValue: insertValue,
updateHourAggregatesForProperty: updateHourAggregatesForProperty,
updateMinuteAggregatesForProperty: updateMinuteAggregatesForProperty
- }
+ };
}
+/**
+ * @type DillerDao
+ */
+var x;
+
+x.deviceById(123)
+
module.exports = DillerDao;
diff --git a/src/DillerTx.js b/src/DillerTx.js
index 85daee5..0f6b2ea 100644
--- a/src/DillerTx.js
+++ b/src/DillerTx.js
@@ -1,19 +1,14 @@
var di = require('di');
var _ = require('lodash');
-var DillerConfig = require('./DillerConfig');
-var DillerDao = require('./DillerDao');
-var Diller = require('./Diller');
+var _DillerDao = require('./DillerDao');
+var _Diller = require('./Diller');
+var _DillerConfig = require('./DillerConfig');
var pgpConstructor = require('pg-promise');
var pgp;
-/**
- * @param config DillerConfig
- * @returns {Function}
- * @constructor
- */
-function DillerTx(config) {
+function _DillerTx(config) {
var log = config.log();
function queryLogger(e) {
@@ -30,19 +25,19 @@ function DillerTx(config) {
}
function connectLogger(client) {
- log.info('PGP: connect');
+ //log.info('PGP: connect');
}
function disconnectLogger(client) {
- log.info('PGP: disconnect');
+ //log.info('PGP: disconnect');
}
if (!pgp) {
var pgpOptions = {};
pgpOptions.query = (config.logQueries && queryLogger) || undefined;
- //pgpOptions.connect = (config.logQueries && connectLogger) || undefined;
- //pgpOptions.disconnect = (config.logQueries && disconnectLogger) || undefined;
+ pgpOptions.connect = (config.logQueries && connectLogger) || undefined;
+ pgpOptions.disconnect = (config.logQueries && disconnectLogger) || undefined;
pgp = pgpConstructor(pgpOptions);
}
@@ -51,13 +46,13 @@ function DillerTx(config) {
var con = pgp(config.postgresqlConfig);
return con.tx(function (pg) {
- var dao = new DillerDao(con);
- var diller = new Diller(config, con, dao);
+ var dao = new _DillerDao(con);
+ var diller = new _Diller(config, con, dao);
return action(pg, dao, diller)
});
};
}
-di.annotate(DillerTx, new di.Inject(DillerConfig));
+di.annotate(_DillerTx, new di.Inject(_DillerConfig));
-module.exports = DillerTx;
+module.exports = _DillerTx;
diff --git a/src/mqtt/DillerMqtt.js b/src/mqtt/DillerMqtt.js
index e991b40..0bd8ed9 100644
--- a/src/mqtt/DillerMqtt.js
+++ b/src/mqtt/DillerMqtt.js
@@ -32,9 +32,9 @@ function DillerMqtt(config, tx) {
run: run
};
}
-var DillerTx = require('../DillerTx');
-var DillerConfig = require('../DillerConfig');
+var _DillerTx = require('../DillerTx');
+var _DillerConfig = require('../DillerConfig');
-di.annotate(DillerMqtt, new di.Inject(DillerConfig, DillerTx));
+di.annotate(DillerMqtt, new di.Inject(_DillerConfig, _DillerTx));
module.exports = DillerMqtt;
diff --git a/src/types.js b/src/types.js
new file mode 100644
index 0000000..8c6300c
--- /dev/null
+++ b/src/types.js
@@ -0,0 +1,95 @@
+var Promise = {};
+
+Promise.resolve = function (value) {
+};
+
+Promise.reject = function (reason) {
+};
+
+/**
+ * @param {Function} resolved
+ * @param {Function=} rejected
+ * @return {Promise}
+ */
+Promise.prototype.then = function (resolved, rejected) {
+};
+
+var _ = function (value) {
+};
+
+/**
+ * @returns {_}
+ */
+_.chain = function () {
+};
+
+var PgTx = function () {
+ /** @lends {PgTx.prototype} */
+ return {
+ /**
+ * @param {Promise[]} promises
+ * @return {Promise}
+ */
+ batch: function (promises) {
+ },
+ /**
+ * @param {String} sql
+ * @param {Array=} values
+ * @return {Promise}
+ */
+ many: function (sql, values) {
+ },
+ /**
+ * @param {String} sql
+ * @param {Array=} values
+ * @return {Promise}
+ */
+ manyOrNone: function (sql, values) {
+ },
+ /**
+ * @param {String} sql
+ * @param {Array=} values
+ * @return {Promise}
+ */
+ one: function (sql, values) {
+ },
+ /**
+ * @param {String} sql
+ * @param {Array=} values
+ * @return {Promise}
+ */
+ oneOrNone: function (sql, values) {
+ },
+ /**
+ * @param {String} sql
+ * @param {Array=} values
+ * @return {Promise}
+ */
+ none: function (sql, values) {
+ }
+ };
+};
+
+var HttpReq = {};
+HttpReq.prototype.body = {};
+HttpReq.prototype.headers = {};
+HttpReq.prototype.params = {};
+
+var HttpRes = {};
+/**
+ * @returns {HttpRes}
+ */
+HttpRes.prototype.status = function (status) {
+};
+/**
+ * @returns {HttpRes}
+ */
+HttpRes.prototype.json = function (object) {
+};
+/**
+ * @param {string} view
+ * @param {object} params
+ * @returns {HttpRes}
+ */
+HttpRes.prototype.render = function (view, params) {
+};
diff --git a/src/web/DillerWeb.js b/src/web/DillerWeb.js
index 03daca7..42aa7bf 100644
--- a/src/web/DillerWeb.js
+++ b/src/web/DillerWeb.js
@@ -3,13 +3,12 @@ var bodyParser = require('body-parser');
var _ = require('lodash');
var di = require('di');
-var DillerConfig = require('../DillerConfig');
-var DillerTx = require('../DillerTx');
+var _DillerConfig = require('../DillerConfig');
+var _DillerTx = require('../DillerTx');
/**
- * @param tx DillerTx
- * @param config DillerConfig
- * @returns {{init: init, listen: listen, generateRpc: generateRpc}}
+ * @param {function(function(PgTx, DillerDao, Diller))} tx
+ * @param {DillerConfig} config
* @constructor
*/
function DillerWeb(tx, config) {
@@ -18,14 +17,17 @@ function DillerWeb(tx, config) {
var calls = [];
var app;
+ /**
+ * @param {HttpRes} res
+ */
function genericErrorHandler(res) {
return function (data) {
if (typeof data === 'string') {
- data = {message: data}
+ data = {message: data};
}
log.warn('fail', data);
return res.status(500).json(data);
- }
+ };
}
function deviceResponse(data) {
@@ -34,6 +36,10 @@ function DillerWeb(tx, config) {
return {device: device};
}
+ /**
+ * @param {HttpReq} req
+ * @param {HttpRes} res
+ */
function getDevices(req, res) {
tx(function (pg, dao) {
return dao.devices();
@@ -42,6 +48,10 @@ function DillerWeb(tx, config) {
}, genericErrorHandler(res));
}
+ /**
+ * @param {HttpReq} req
+ * @param {HttpRes} res
+ */
function getDevice(req, res) {
tx(function (pg, dao) {
var deviceId = req.params.deviceId;
@@ -55,6 +65,10 @@ function DillerWeb(tx, config) {
}, genericErrorHandler(res));
}
+ /**
+ * @param {HttpReq} req
+ * @param {HttpRes} res
+ */
function patchDevice(req, res) {
tx(function (pg, dao, diller) {
var deviceId = req.params.deviceId;
@@ -86,6 +100,10 @@ function DillerWeb(tx, config) {
}, genericErrorHandler(res));
}
+ /**
+ * @param {HttpReq} req
+ * @param {HttpRes} res
+ */
function getValues(req, res) {
tx(function (tx, dao) {
var propertyId = req.params.propertyId;
@@ -98,6 +116,10 @@ function DillerWeb(tx, config) {
});
}
+ /**
+ * @param {HttpReq} req
+ * @param {HttpRes} res
+ */
function patchProperty(req, res) {
tx(function (pg, dao, diller) {
var propertyId = req.params.propertyId;
@@ -129,6 +151,10 @@ function DillerWeb(tx, config) {
}, genericErrorHandler(res));
}
+ /**
+ * @param {HttpReq} req
+ * @param {HttpRes} res
+ */
function getIndex(req, res) {
var baseUrl = (req.headers['trygvis-prefix'] || '').replace(/\/$/, '');
res.render('index', {baseUrl: baseUrl + '/'});
@@ -142,6 +168,12 @@ function DillerWeb(tx, config) {
var api = express.Router();
+ /**
+ * @param name
+ * @param method
+ * @param path
+ * @param {function(HttpReq, HttpRes)} callback
+ */
function addApi(name, method, path, callback) {
api[method](path, callback);
var layer = _.last(api.stack);
@@ -192,6 +224,7 @@ function DillerWeb(tx, config) {
}
function listen() {
+ log.info('Starting HTTP listener on ' + config.httpPort);
app.listen(config.httpPort);
}
@@ -260,6 +293,6 @@ function DillerWeb(tx, config) {
}
}
-di.annotate(DillerWeb, new di.Inject(DillerTx, DillerConfig));
+di.annotate(DillerWeb, new di.Inject(_DillerTx, _DillerConfig));
module.exports = DillerWeb;