Skip to content

Commit 384c089

Browse files
committed
v8.8 release
- user metrics folder! - (total log disk space / visible data disk space) in graph status - server uptime on terminal page - new dependency: merge package (get latest packages.json and run `npm update` to get it) - updated nedb to 1.8, json5 to 0.5, request to 2.75, console-stamp to 0.2.4 - removed old neDB upgrade script (gateway_NeDB-2-Binary.js)
1 parent b3e2f21 commit 384c089

File tree

8 files changed

+77
-86
lines changed

8 files changed

+77
-86
lines changed

gateway.js

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ var JSON5 = require('json5'); //https://github
3232
var path = require('path');
3333
var dbDir = 'data/db';
3434
var metricsFile = 'metrics.js';
35+
var userMetricsDir = 'userMetrics';
3536
nconf.argv().file({ file: path.resolve(__dirname, 'settings.json5'), format: JSON5 });
3637
settings = nconf.get('settings');
3738
var dbLog = require(path.resolve(__dirname,'logUtil.js'));
@@ -58,12 +59,38 @@ serial.on('close', function serialCloseHandler(error) {
5859
});
5960

6061
serial.on("data", function(data) { processSerialData(data); });
61-
6262
serial.open();
6363

64+
require("console-stamp")(console, settings.general.consoleLogDateFormat.value); //timestamp logs - https://github.com/starak/node-console-stamp
65+
66+
//LOAD METRICS:
67+
// - First load main metrics.js definitions (metrics, motes, events etc)
6468
metricsDef = require(path.resolve(__dirname, metricsFile));
69+
// - Then load user metrics which can override default metrics
70+
try {
71+
console.info('LOADING USER METRICS...');
72+
fs = require('fs');
73+
merge = require('merge');
74+
if (fs.existsSync(__dirname + '/' + userMetricsDir))
75+
{
76+
fs.readdirSync(__dirname + '/' + userMetricsDir).forEach(function(file) {
77+
if (file.match(/\.js$/) !== null) {
78+
var name = file.replace('.js', '');
79+
console.info('LOADING USER METRICS MODULE [' + name + ']');
80+
try {
81+
var tmp = require(__dirname + '/' + userMetricsDir + '/' + file);
82+
metricsDef = merge(true, metricsDef, tmp);
83+
} catch (ex) {
84+
console.error('FAIL LOADING USER METRICS MODULE ['+ name + ']: ' + ex.message);
85+
}
86+
}
87+
});
88+
} else console.info('NO USER METRICS DEFINED (dir: /' + userMetricsDir + '), SKIPPING');
89+
}
90+
catch (ex) {
91+
console.error('FAIL ACCESSING USER METRICS: '+ ex.message);
92+
}
6593

66-
require("console-stamp")(console, settings.general.consoleLogDateFormat.value); //timestamp logs - https://github.com/starak/node-console-stamp
6794
db.persistence.setAutocompactionInterval(settings.database.compactDBInterval.value); //compact the database every 24hrs
6895

6996
var transporter = nodemailer.createTransport({
@@ -165,7 +192,8 @@ io.sockets.on('connection', function (socket) {
165192
socket.emit('METRICSDEF', metricsDef.metrics);
166193
socket.emit('EVENTSDEF', metricsDef.events);
167194
socket.emit('SETTINGSDEF', settings);
168-
socket.emit('SERVERTIME', new Date().getTime());
195+
socket.emit('SERVERTIME', Date.now());
196+
socket.emit('SERVERSTARTTIME', Date.now() - process.uptime()*1000);
169197

170198
//pull all nodes from the database and send them to client
171199
db.find({ _id : { $exists: true } }, function (err, entries) {
@@ -538,7 +566,7 @@ global.processSerialData = function (data) {
538566
else
539567
{
540568
//console.log('no match: ' + data);
541-
dbunmatched.insert({_id:(new Date().getTime()), data:data});
569+
dbunmatched.insert({_id:Date.now(), data:data});
542570
}
543571
}
544572

gateway_NeDB-2-Binary.js

Lines changed: 0 additions & 69 deletions
This file was deleted.

metrics.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ exports.metrics = {
6262
//SwitchMote buttons
6363
SMB0_OFF : { name:'B0', regexp:/BTN0\:0/i, value:'OFF'},
6464
SMB0_ON : { name:'B0', regexp:/BTN0\:1/i, value:'ON'},
65-
SMB1_OFF : { name:'B1', regexp:/(BTN1|SSR|RLY)\:0/i, value:'OFF', pin:1, graph:1, logValue:0, graphOptions:{ yaxis: {ticks:0}, colors:['#4a0']}},
65+
SMB1_OFF : { name:'B1', regexp:/(BTN1|SSR|RLY)\:0/i, value:'OFF', pin:1, graph:1, logValue:0, graphOptions:{ yaxis: {ticks:0, min:0, autoscaleMargin:0.5 }, colors:['#4a0']}},
6666
SMB1_ON : { name:'B1', regexp:/(BTN1|SSR|RLY)\:1/i, value:'ON', pin:1, graph:1, logValue:1, graphOptions: { /* already defined above for 'B1', no need to repeat */ }},
6767
SMB2_OFF : { name:'B2', regexp:/BTN2\:0/i, value:'OFF'},
6868
SMB2_ON : { name:'B2', regexp:/BTN2\:1/i, value:'ON'},

package.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
{
22
"name": "RaspberryPi-Gateway",
3-
"version": "8.6",
4-
"description": "RaspberryPi Home Automation Gateway",
3+
"version": "8.8.0",
4+
"description": "Moteino IoT Gateway",
55
"main": "gateway.js",
66
"repository": "https://github.com/LowPowerLab/RaspberryPi-Gateway.git",
77
"author": "Felix Rusu (https://github.com/LowPowerLab)",
88
"homepage": "http://lowpowerlab.com/gateway",
99
"bugs": {
1010
"url": "https://github.com/LowPowerLab/RaspberryPi-Gateway/issues"
1111
},
12-
"license": "SEE LICENSE IN license.txt",
12+
"license": "SEE LICENSE IN LICENSE FILE",
1313
"contributors": "Felix Rusu (https://github.com/LowPowerLab)",
1414
"dependencies": {
1515
"serialport": "~2.0",
1616
"socket.io": "~1.3",
17-
"nedb": "~1.5",
18-
"json5": "~0.4.0",
17+
"nedb": "~1.8",
18+
"json5": "~0.5.0",
1919
"nconf": "~0.8.2",
2020
"nodemailer": "~1.10",
21-
"console-stamp": "~0.2.0",
21+
"console-stamp": "~0.2.4",
2222
"colors": "~1.1.2",
23-
"request": "~2.67"
23+
"request": "~2.75",
24+
"merge": "~1.2.0"
2425
},
2526
"scripts": {
2627
"preinstall": "",

settings.json5

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
editable: true,
6969
exposed: true,
7070
ip: {
71-
value: "192.168.0.100"
71+
value: "192.168.1.100"
7272
}
7373
}
7474
}

userMetrics/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
User Metrics Folder
2+
----------------
3+
<br/>
4+
5+
The purpose of this folder is to have a place to put custom user definitions for metrics, motes, events, custom functions and variables etc., without the need to directly edit the main metrics.js which might get overwritten during an upgrade.
6+
7+
###How to use:
8+
- place your custom metrics/motes/events in this folder
9+
- whatever objects you define here will be merged with the contents of metrics.js
10+
- if a matching metric/mote/event is redefined in a file this folder, it will override the default from metrics.js, that also means that if duplicates are found, the last one in the last file (alphabetically) will be the winner
11+
- follow the same definition/syntax pattern as in main metrics.js definition file
12+
- each metric could be broken into its own separate file or everything could be in a single file just like in metrics.js, it all gets merged into the main `metricsDef` object
13+
- a basic example is provided as a starting point in _example.js

userMetrics/_example.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/*this sample metric will override that which is already defined in main metrics.js*/
2+
/*you can redefine defaults or write your own new custom metrics in 1 or more files in this folder, separate them as you'd like - they all get merged together when the app loads*/
3+
exports.metrics = {
4+
V : { name:'V', regexp:/(?:V?BAT|VOLTS|V)\:([\d\.]+)v?/i, value:'', unit:'v', graph:1, graphOptions:{ legendLbl:'VOLTZ!', lines: { fill:true, lineWidth:1 }, yaxis: { min: 0, autoscaleMargin: 0.5 }}}
5+
};

www/index.html

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
.btn-text, #loadingsocket { font-size: 32px; }
6969
#loadingsocket, #loadinggraph {padding:15px;}
7070
.labelbold { font-weight:bold !important; overflow:hidden; text-overflow:ellipsis;white-space:nowrap; }
71-
#wrap { padding: 12px; }
71+
.wrap { padding: 12px; }
7272
.ui-listview>li.hiddenNode { display:none; }
7373
.ui-listview>li.hiddenNodeShow { display:block; }
7474
.ui-li-count18 { font-size:18px; }
@@ -438,9 +438,12 @@ <h1>Terminal</h1>
438438
</span>
439439
</div>
440440

441-
<div id="wrap">
441+
<div class="wrap">
442442
<textarea name="log" id="log" rows="10" style="font-size:10px;"></textarea>
443443
</div>
444+
<div style="font-weight:bold;padding:0 12px;">
445+
<span>Server uptime: </span><span id="status-uptime" data-time="147676078" class="nodeAgo">X</span>
446+
</div>
444447
</div>
445448

446449
<div data-role="page" id="settingspage">
@@ -590,9 +593,13 @@ <h1>Settings</h1>
590593
});
591594

592595
socket.on('SERVERTIME', function(serverMillisSinceEpoch) {
593-
serverTimeOffset = new Date().getTime() - serverMillisSinceEpoch;
596+
serverTimeOffset = Date.now() - serverMillisSinceEpoch;
594597
LOG('SERVERTIME OFFSET: ' + serverTimeOffset + 'ms');
595598
});
599+
600+
socket.on('SERVERSTARTTIME', function(serverMillisSinceProcessStart) {
601+
$('#status-uptime').attr('data-time', serverMillisSinceProcessStart + serverTimeOffset);
602+
});
596603

597604
$(document).on("pagecreate", "#eventAdd", function(){ if ($('addEventType').val()) $('#addEvent_OK').show(); else $('#addEvent_OK').hide(); });
598605

@@ -732,7 +739,7 @@ <h1>Settings</h1>
732739
graphOptions.yaxis.max = max;
733740

734741
graphOptions = $.extend(true, graphOptions, rawData.options); //http://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically
735-
$(graphStat).html(rawData.graphData.msg != undefined ? rawData.graphData.msg : (rawData.graphData.data.length + (rawData.graphData.totalIntervalDatapoints != rawData.graphData.data.length ? ' / '+rawData.graphData.totalIntervalDatapoints : '') +'pts ('+ rawData.graphData.queryTime+'ms)'));
742+
$(graphStat).html(rawData.graphData.msg != undefined ? rawData.graphData.msg : (rawData.graphData.data.length + (rawData.graphData.totalIntervalDatapoints != rawData.graphData.data.length ? ' / '+rawData.graphData.totalIntervalDatapoints : '') +'pts ('+ rawData.graphData.queryTime+'ms) ('+humanFileSize(rawData.graphData.totalIntervalDatapoints*9)+'/'+humanFileSize(rawData.graphData.logSize)+')'));
736743
//need to defer plotting until after pageshow is finished rendering, otherwise the wrapper will return an incorrect width of "100"
737744
if (metricGraphWrapper.width()==100)
738745
$(document).on("pageshow", "#metricdetails", renderAndCloneStat);
@@ -894,6 +901,12 @@ <h1>Settings</h1>
894901
$('#nodeList').listview('refresh'); //re-render the listview
895902
}
896903

904+
/// http://stackoverflow.com/a/20732091
905+
function humanFileSize(size) {
906+
var i = Math.floor( Math.log(size) / Math.log(1024) );
907+
return ( size / Math.pow(1024, i) ).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i];
908+
};
909+
897910
/// calculated a style for an "ago" label based on a given timestamp in the past
898911
function ago(time, agoPrefix)
899912
{

0 commit comments

Comments
 (0)