aboutsummaryrefslogtreecommitdiff
path: root/src/DillerDao.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/DillerDao.js')
-rw-r--r--src/DillerDao.js9
1 files changed, 6 insertions, 3 deletions
diff --git a/src/DillerDao.js b/src/DillerDao.js
index a47f181..fa27aa1 100644
--- a/src/DillerDao.js
+++ b/src/DillerDao.js
@@ -3,7 +3,7 @@ var _ = require('lodash');
function DillerDao(tx) {
var deviceColumns = 'id, created_timestamp, key, name, description';
- var propertyColumns = 'id, device, key, created_timestamp';
+ var propertyColumns = 'id, created_timestamp, device, key, last_value, last_timestamp';
var valueColumns = 'property, timestamp, value_text, value_numeric';
// -------------------------------------------------------------------------------------------------------------------
@@ -85,11 +85,14 @@ function DillerDao(tx) {
return tx.manyOrNone('SELECT timestamp, coalesce(value_numeric::text, value_text) AS value FROM value WHERE property=$1 ORDER BY timestamp DESC LIMIT $2', [propertyId, limit]);
}
- function insertValue(propertyId, value) {
+ function insertValue(propertyId, timestamp, value) {
var value_numeric = parseFloat(value) || undefined,
value_text = value_numeric ? null : value;
- return tx.one('INSERT INTO value(property, timestamp, value_text, value_numeric) VALUES($1, CURRENT_TIMESTAMP, $2, $3::NUMERIC) RETURNING timestamp', [propertyId, value_text, value_numeric]);
+ return tx.none('UPDATE device_property SET last_value = $2, last_timestamp = $3 WHERE id = $1', [propertyId, value, timestamp])
+ .then(function () {
+ return tx.one('INSERT INTO value(property, timestamp, value_text, value_numeric) VALUES($1, $2, $3, $4::NUMERIC) RETURNING timestamp', [propertyId, timestamp, value_text, value_numeric]);
+ });
}
function updateHourAggregatesForProperty(propertyId, timestamp) {