Skip to content

Commit 3c39c16

Browse files
committed
power cumulative values now stored in redis and use associative arrays
1 parent 0e1fccf commit 3c39c16

File tree

3 files changed

+83
-34
lines changed

3 files changed

+83
-34
lines changed

mqtt/server-stats.js

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,34 @@ function log(type) {
1616
}
1717
}
1818

19-
20-
var powerlasttime = new Date(); // UNIX time in ms
19+
// var powerlasttime = new Date(); // UNIX time in ms
2120

2221
// get last stored values from Redis
22+
// var execSync = require('execSync');
23+
// var result = execSync.exec('redis-cli get powercumulativeHour');
24+
// var powercumulativeHour = parseFloat(result.stdout);
25+
// var result = execSync.exec('redis-cli get powercumulativeToday');
26+
// var powercumulativeToday = parseFloat(result.stdout);
27+
28+
// var records_hourly = new Array();
29+
// var records_daily = new Array();
30+
// var records_lasttime = new Array();
31+
// var records_hourly = {};
32+
var records_hourly = {};
33+
var records_daily = {};
34+
var records_lasttime = {};
35+
2336
var execSync = require('execSync');
24-
var result = execSync.exec('redis-cli get powercumulativeHour');
25-
var powercumulativeHour = parseFloat(result.stdout);
26-
var result = execSync.exec('redis-cli get powercumulativeToday');
27-
var powercumulativeToday = parseFloat(result.stdout);
37+
var result = execSync.exec('redis-cli get records_hourly');
38+
records_hourly = JSON.parse(result.stdout);
39+
console.log("Loading hourly records from redis...");
2840

29-
console.log("loaded powercumulativeHour: " + powercumulativeHour + " (" + typeof powercumulativeHour + ")");
30-
console.log("loaded powercumulativeToday: " + powercumulativeToday + " (" + typeof powercumulativeToday + ")");
41+
result = execSync.exec('redis-cli get records_daily');
42+
records_daily = JSON.parse(result.stdout);
43+
console.log("Loading daily records from redis...");
3144

32-
// subscribe to MQTT
45+
46+
// connect to to MQTT
3347
var mqtt = require('mqtt');
3448
var mqttclient = mqtt.createClient(1883, config.mqtt.host, function(err, client) {
3549
keepalive: 1000
@@ -42,27 +56,43 @@ mqttclient.on('connect', function() {
4256
console.log('subscribing to sensors/power/+ on ' + config.mqtt.host + '(' + config.mqtt.port + ')');
4357

4458
mqttclient.on('message', function(topic, message) {
45-
// console.log('topic: ' + topic + ' payload: ' + message);
46-
if (topic == "sensors/power/0") {
47-
var powercurrenttime = new Date();
48-
// Is it now a different day from the last time this block ran?
49-
if (powerlasttime.getDate() != powercurrenttime.getDate()) {
50-
powercumulativeToday = 0;
51-
}
52-
if (powerlasttime.getHours() != powercurrenttime.getHours()) {
53-
powercumulativeHour = 0;
54-
}
55-
// caluclate cumlative power used in KWh
56-
var duration = (powercurrenttime - powerlasttime) / 1000.0;
57-
var powerused = parseInt(message, 10) * (duration / 3600.0) / 1000.0; // convert to KWh
58-
powercumulativeToday += powerused;
59-
powercumulativeHour += powerused;
60-
redisClient.set("powercumulativeToday", powercumulativeToday);
61-
redisClient.set("powercumulativeHour", powercumulativeHour);
62-
// console.log("duration ", duration, "period ", powerused, "today ", powercumulativeToday, "hour ", powercumulativeHour);
63-
mqttclient.publish("sensors/power/0/cumulative/today", powercumulativeToday.toFixed(2));
64-
mqttclient.publish("sensors/power/0/cumulative/hour", powercumulativeHour.toFixed(2));
65-
powerlasttime = powercurrenttime;
59+
var powercurrenttime = new Date();
60+
61+
// have we seen this mesage before?
62+
if(records_lasttime[topic] == undefined) {
63+
console.log("initialising lasttime" + topic);
64+
records_lasttime[topic] = new Date();
65+
}
66+
if(records_hourly[topic] == undefined) {
67+
console.log("initialising records_hourly " + topic);
68+
records_hourly[topic] = 0;
6669
}
70+
if(records_daily[topic] == undefined) {
71+
console.log("initialising records_daily " + topic);
72+
records_daily[topic] = 0;
73+
}
74+
75+
// different hour?
76+
if (records_lasttime[topic].getHours() != powercurrenttime.getHours()) {
77+
powercumulativeHour = 0;
78+
}
79+
// different day?
80+
if (records_lasttime[topic].getDate() != powercurrenttime.getDate()) {
81+
powercumulativeDay = 0;
82+
}
83+
// calculate cumulative power used in KWh
84+
var duration = (powercurrenttime - records_lasttime[topic]) / 1000.0;
85+
var powerused = parseInt(message, 10) * (duration / 3600.0) / 1000.0; // convert to KWh
86+
records_lasttime[topic] = powercurrenttime;
87+
records_hourly[topic] += powerused;
88+
records_daily[topic] += powerused;
89+
console.log("topic:", topic, " duration ", duration, " period ", powerused, " hour ", records_hourly[topic], "daily ", records_daily[topic]);
90+
91+
mqttclient.publish(topic + "/cumulative/hour", records_hourly[topic].toFixed(2));
92+
mqttclient.publish(topic + "/cumulative/daily", records_daily[topic].toFixed(2));
93+
94+
redisClient.set("records_hourly", JSON.stringify(records_hourly));
95+
redisClient.set("records_daily", JSON.stringify(records_daily));
96+
// console.log( JSON.stringify(records_hourly));
6797
});
6898
});

mqtt/server.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ console.log('listening on port 8500');
3131

3232

3333

34+
35+
36+
3437
io.of('/sensors').on('connection', function (socket) {
3538
// subscribe to MQTT
3639
var mqtt = require('mqtt');
3740
var mqttclient = mqtt.createClient(1883, config.mqtt.host, function(err, client) {
38-
keepalive: 1000
41+
keepalive: 1000
3942
});
40-
4143
mqttclient.on('connect', function() {
4244
mqttclient.subscribe('sensors/+/+');
4345
mqttclient.subscribe('sensors/power/+/cumulative/+');
@@ -54,9 +56,8 @@ io.of('/mqtt').on('connection', function (socket) {
5456
// subscribe to MQTT
5557
var mqtt = require('mqtt');
5658
var mqttclient = mqtt.createClient(1883, config.mqtt.host, function(err, client) {
57-
keepalive: 1000
59+
keepalive: 1000
5860
});
59-
6061
mqttclient.on('connect', function() {
6162
mqttclient.subscribe('#');
6263
// console.log('subscribing to everything on ' + config.mqtt.host + '(' + config.mqtt.port + ')');

mqtt/test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var records_hourly = new Array();
2+
3+
records_hourly['first'] = 1;
4+
console.log(records_hourly['first']);
5+
console.log(records_hourly['second']);
6+
7+
8+
if(records_hourly['three'] == undefined) {
9+
console.log("three undefined");
10+
} else {
11+
console.log("three defined");
12+
}
13+
records_hourly['three'] = 1;
14+
if(records_hourly['three'] == undefined) {
15+
console.log("three undefined");
16+
} else {
17+
console.log("three defined");
18+
}

0 commit comments

Comments
 (0)