Skip to content

Commit e46e3ed

Browse files
committed
fix serialnode & Buffer deprecations
1 parent 3aaca30 commit e46e3ed

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

gateway.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ port.on('close', function serialCloseHandler(error) {
6363
});
6464

6565
parser.on("data", function(data) { processSerialData(data); });
66-
port.open();
6766

6867
require("console-stamp")(console, settings.general.consoleLogDateFormat.value); //timestamp logs - https://github.com/starak/node-console-stamp
6968

logUtil.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ exports.getData = function(filename, start, end, dpcount) {
3535
fd = fs.openSync(filename, 'r');
3636

3737
//truncate start/end to log time limits if necessary - this ensures good data resolution when time limits are out of bounds
38-
var buff = new Buffer(9);
38+
var buff = Buffer.alloc(9);
3939
fs.readSync(fd, buff, 0, 9, 0);
4040
var firstLogTimestamp = buff.readUInt32BE(1);
4141
fs.readSync(fd, buff, 0, 9, filesize-9);
@@ -113,7 +113,7 @@ exports.postData = function post(filename, timestamp, value, duplicateInterval)
113113
if (logsize % 9 > 0) throw 'File ' + filename +' is not multiple of 9bytes, post aborted';
114114

115115
var fd;
116-
var buff = new Buffer(9);
116+
var buff = Buffer.alloc(9);
117117
var lastTime = 0, lastValue = 0, pos = 0;
118118
value=Math.round(value*10000); //round to make an exactly even integer
119119

@@ -126,7 +126,7 @@ exports.postData = function post(filename, timestamp, value, duplicateInterval)
126126
if (logsize>=9) {
127127
// read the last value appended to the file
128128
fd = fs.openSync(filename, 'r');
129-
var buf8 = new Buffer(8);
129+
var buf8 = Buffer.alloc(8);
130130

131131
fs.readSync(fd, buf8, 0, 8, logsize-8);
132132
lastTime = buf8.readUInt32BE(0); //read timestamp (bytes 0-3 in buffer)
@@ -172,7 +172,7 @@ exports.postData = function post(filename, timestamp, value, duplicateInterval)
172172
exports.binarySearch = function(fileDescriptor, timestamp, filesize) {
173173
start = 0;
174174
end = filesize-9;
175-
var buff = new Buffer(4);
175+
var buff = Buffer.alloc(4);
176176
var time = 0;
177177

178178
fs.readSync(fileDescriptor, buff, 0, 4, end+1);
@@ -201,7 +201,7 @@ exports.binarySearch = function(fileDescriptor, timestamp, filesize) {
201201
exports.binarySearchExact = function(fileDescriptor, timestamp, filesize) {
202202
if (filesize==0) return -1;
203203
start = 0; end = filesize-9;
204-
var buff = new Buffer(4);
204+
var buff = Buffer.alloc(4);
205205
var tmp = 0;
206206
for (i=0; i<30; i++)
207207
{

metrics.js

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// **********************************************************************************
2-
// Websocket backend for the Moteino IoT Framework
2+
// Websocket backend for the Moteino IoT Gateway
33
// http://lowpowerlab.com/gateway
44
// **********************************************************************************
55
// This is the metrics definitions file containing the definitions of token matches
@@ -10,18 +10,32 @@
1010
// This is a work in progress and updates and fixes will be added as they come up
1111
// and time permits. Contributions are encouraged.
1212
// ********************************************************************************************
13-
// Copyright Felix Rusu, Low Power Lab LLC (2015), http://lowpowerlab.com/contact
13+
// Copyright Felix Rusu, Low Power Lab LLC (2018), http://lowpowerlab.com/contact
1414
// ********************************************************************************************
1515
// Great reference on Javascript Arrays: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
1616
// Great reference on Javascript Objects: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects
1717
// Great reference on Javascript Regular Expressions: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
1818
// Great sandbox to test your Regular Expressions: http://regexr.com/
1919
// JqueryMobile generic icons: http://api.jquerymobile.com/icons/
2020
// FLOT graphs customizations: http://www.jqueryflottutorial.com/jquery-flot-customizing-data-series-format.html
21+
var request = require('request');
22+
var config = require('nconf');
23+
var JSON5 = require('json5');
24+
var suncalc = require('suncalc'); //https://github.com/mourner/suncalc
25+
config.argv().file({ file: require('path').resolve(__dirname, 'settings.json5'), format: JSON5 });
26+
var settings = config.get('settings'); //these are local to avoid runtime errors but in events they will reference the global settings declared in gateway.js
27+
2128
// ******************************************************************************************************************************************
22-
// SAMPLE EVENTS/ALERTS
29+
// SAMPLE METRICS DEFINITIONS
2330
// ******************************************************************************************************************************************
24-
// These metrics definitions consist of a regular expression that will be attempted to be matched to any incoming tokens from the gateway Moteino serial port
31+
// The metrics definitions use [regular expressions] to match an incoming metric token
32+
// The metrics tokens have a pattern that must be followed:
33+
// - a packet received from a node can contain multiple metrics (ie temperature, humidity, motion etc.)
34+
// - metrics are separated by space (not by comma, or other characters, ex: "T:42 H:50")
35+
// - each metric that has a name/value pair is defined as NAME:VALUE (metric name, colon, metric value)
36+
// - each metric that simply defines a status can be standalone without a value (ex: "MOTION")
37+
// ******************************************************************************************************************************************
38+
// These metrics definitions consist of a regular expression that will be attempted to be matched to any incoming tokens from the gateway (ie. Moteino, MightyHat, etc.) serial port
2539
// If one matches you should see a new node/metric show up in the UI or be updated if previously matched
2640
// Other parameters:
2741
// - value - this can be hardcoded, or if left blank the value will be the first captured parentheses from the regex expression
@@ -34,18 +48,13 @@
3448
// - it should only be specified one per each metric - the first one (ie one for each set of metrics that have multiple entries with same 'name') - ex: GarageMote 'Status' metric
3549
// - this object is overlapped over the default 'graphOptions' defined in index.html
3650
// - for more details how to customize FLOT graphs see this: http://www.jqueryflottutorial.com/jquery-flot-customizing-data-series-format.html
51+
// ******************************************************************************************************************************************
3752
// Important Notes:
38-
// - the same node can have any number of metrics
53+
// - the same node can have any number of metrics (only limited by the packet max length - ex. 61 chars in the RFM69 library)
3954
// - each related metric should have the same name - for instance look at GarageMote - all the regex expressions actually update the same metric specified by name='Status'
4055
// so when garage goes through different states it will update a single metric called 'Status'
4156
// Another good example is SwitchMote where we have 6 different metric definitions here but only 3 resultant actual metrics (Button1, Button2 and Button3)
42-
var request = require('request');
43-
var config = require('nconf');
44-
var JSON5 = require('json5');
45-
var suncalc = require('suncalc'); //https://github.com/mourner/suncalc
46-
config.argv().file({ file: require('path').resolve(__dirname, 'settings.json5'), format: JSON5 });
47-
var settings = config.get('settings'); //these are local to avoid runtime errors but in events they will reference the global settings declared in gateway.js
48-
57+
// ******************************************************************************************************************************************
4958
exports.metrics = {
5059
//GarageMote
5160
//NOTE the \b word boundary is used to avoid matching "OPENING" (ie OPEN must be followed by word boundary/end of word)
@@ -123,6 +132,7 @@ exports.metrics = {
123132
// serverExecute is an action meant to be executed only at the server side (ex sending an email when a condition is met), must be defined as a function
124133
// Server side execution for events is recommended since you can have multiple clients and you don't want to trigger SMS messages from each one, instead only one SMS message should be sent when an event happens
125134
// default out-of-box jquery mobile icons are listed here: https://api.jquerymobile.com/icons/
135+
// ******************************************************************************************************************************************
126136
exports.events = {
127137
motionAlert : { label:'Motion : Alert', icon:'audio', descr:'Alert sound when MOTION is detected', serverExecute:function(node) { if (node.metrics['M'] && node.metrics['M'].value == 'MOTION' && (Date.now() - new Date(node.metrics['M'].updated).getTime() < 2000)) { io.sockets.emit('PLAYSOUND', 'sounds/alert.wav'); }; } },
128138
mailboxAlert : { label:'Mailbox Open Alert!', icon:'audio', descr:'Message sound when mailbox is opened', serverExecute:function(node) { if (node.metrics['M'] && node.metrics['M'].value == 'MOTION' && (Date.now() - new Date(node.metrics['M'].updated).getTime() < 2000)) { io.sockets.emit('PLAYSOUND', 'sounds/incomingmessage.wav'); }; } },
@@ -301,6 +311,7 @@ exports.events = {
301311
// The 'action' property is a string message that will be sent to that node when the control is clicked
302312
// The 'serverExecute' property is a server side function that if defined, will be called when the control is clicked (ie it can do anything like triggering an HTTP request like in the case of an IP thermostat)
303313
// The 'breakAfter' property, if set to 'true', will insert a page break after the control it's specified for. This is useful for nodes that have many of controls, to break them apart on the page
314+
// ******************************************************************************************************************************************
304315
exports.motes = {
305316
DoorBellMote: {
306317
label : 'DoorBell',
@@ -633,9 +644,9 @@ exports.millisToFutureDate = function(futureDate, failSafe) {
633644
}
634645

635646
// ******************************************************************************************************************************************
636-
// RADIO THERMOSTAT SPECIFIC HELPER FUNCTIONS
647+
// RADIO THERMOSTAT SPECIFIC HELPER FUNCTIONS
637648
// ******************************************************************************************************************************************
638-
// *** these are implemented for Radio Thermostat CT50
649+
// *** these are implemented for Radio Thermostat model CT50
639650
// ******************************************************************************************************************************************
640651
//this function sends an HTTP GET request to the thermostat to refresh metrics like current temperature, target temp, mode (heat/cool), hold etc.
641652
exports.tstatPoll = function(nodeId) {

0 commit comments

Comments
 (0)