Skip to content

Commit

Permalink
fix serialnode & Buffer deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
LowPowerLab committed Aug 1, 2018
1 parent 3aaca30 commit e46e3ed
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
1 change: 0 additions & 1 deletion gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ port.on('close', function serialCloseHandler(error) {
});

parser.on("data", function(data) { processSerialData(data); });
port.open();

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

Expand Down
10 changes: 5 additions & 5 deletions logUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ exports.getData = function(filename, start, end, dpcount) {
fd = fs.openSync(filename, 'r');

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

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

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

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

fs.readSync(fileDescriptor, buff, 0, 4, end+1);
Expand Down Expand Up @@ -201,7 +201,7 @@ exports.binarySearch = function(fileDescriptor, timestamp, filesize) {
exports.binarySearchExact = function(fileDescriptor, timestamp, filesize) {
if (filesize==0) return -1;
start = 0; end = filesize-9;
var buff = new Buffer(4);
var buff = Buffer.alloc(4);
var tmp = 0;
for (i=0; i<30; i++)
{
Expand Down
39 changes: 25 additions & 14 deletions metrics.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// **********************************************************************************
// Websocket backend for the Moteino IoT Framework
// Websocket backend for the Moteino IoT Gateway
// http://lowpowerlab.com/gateway
// **********************************************************************************
// This is the metrics definitions file containing the definitions of token matches
Expand All @@ -10,18 +10,32 @@
// This is a work in progress and updates and fixes will be added as they come up
// and time permits. Contributions are encouraged.
// ********************************************************************************************
// Copyright Felix Rusu, Low Power Lab LLC (2015), http://lowpowerlab.com/contact
// Copyright Felix Rusu, Low Power Lab LLC (2018), http://lowpowerlab.com/contact
// ********************************************************************************************
// Great reference on Javascript Arrays: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
// Great reference on Javascript Objects: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects
// Great reference on Javascript Regular Expressions: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
// Great sandbox to test your Regular Expressions: http://regexr.com/
// JqueryMobile generic icons: http://api.jquerymobile.com/icons/
// FLOT graphs customizations: http://www.jqueryflottutorial.com/jquery-flot-customizing-data-series-format.html
var request = require('request');
var config = require('nconf');
var JSON5 = require('json5');
var suncalc = require('suncalc'); //https://github.com/mourner/suncalc
config.argv().file({ file: require('path').resolve(__dirname, 'settings.json5'), format: JSON5 });
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

// ******************************************************************************************************************************************
// SAMPLE EVENTS/ALERTS
// SAMPLE METRICS DEFINITIONS
// ******************************************************************************************************************************************
// 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
// The metrics definitions use [regular expressions] to match an incoming metric token
// The metrics tokens have a pattern that must be followed:
// - a packet received from a node can contain multiple metrics (ie temperature, humidity, motion etc.)
// - metrics are separated by space (not by comma, or other characters, ex: "T:42 H:50")
// - each metric that has a name/value pair is defined as NAME:VALUE (metric name, colon, metric value)
// - each metric that simply defines a status can be standalone without a value (ex: "MOTION")
// ******************************************************************************************************************************************
// 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
// If one matches you should see a new node/metric show up in the UI or be updated if previously matched
// Other parameters:
// - value - this can be hardcoded, or if left blank the value will be the first captured parentheses from the regex expression
Expand All @@ -34,18 +48,13 @@
// - 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
// - this object is overlapped over the default 'graphOptions' defined in index.html
// - for more details how to customize FLOT graphs see this: http://www.jqueryflottutorial.com/jquery-flot-customizing-data-series-format.html
// ******************************************************************************************************************************************
// Important Notes:
// - the same node can have any number of metrics
// - the same node can have any number of metrics (only limited by the packet max length - ex. 61 chars in the RFM69 library)
// - 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'
// so when garage goes through different states it will update a single metric called 'Status'
// Another good example is SwitchMote where we have 6 different metric definitions here but only 3 resultant actual metrics (Button1, Button2 and Button3)
var request = require('request');
var config = require('nconf');
var JSON5 = require('json5');
var suncalc = require('suncalc'); //https://github.com/mourner/suncalc
config.argv().file({ file: require('path').resolve(__dirname, 'settings.json5'), format: JSON5 });
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

// ******************************************************************************************************************************************
exports.metrics = {
//GarageMote
//NOTE the \b word boundary is used to avoid matching "OPENING" (ie OPEN must be followed by word boundary/end of word)
Expand Down Expand Up @@ -123,6 +132,7 @@ exports.metrics = {
// 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
// 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
// default out-of-box jquery mobile icons are listed here: https://api.jquerymobile.com/icons/
// ******************************************************************************************************************************************
exports.events = {
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'); }; } },
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'); }; } },
Expand Down Expand Up @@ -301,6 +311,7 @@ exports.events = {
// The 'action' property is a string message that will be sent to that node when the control is clicked
// 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)
// 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
// ******************************************************************************************************************************************
exports.motes = {
DoorBellMote: {
label : 'DoorBell',
Expand Down Expand Up @@ -633,9 +644,9 @@ exports.millisToFutureDate = function(futureDate, failSafe) {
}

// ******************************************************************************************************************************************
// RADIO THERMOSTAT SPECIFIC HELPER FUNCTIONS
// RADIO THERMOSTAT SPECIFIC HELPER FUNCTIONS
// ******************************************************************************************************************************************
// *** these are implemented for Radio Thermostat CT50
// *** these are implemented for Radio Thermostat model CT50
// ******************************************************************************************************************************************
//this function sends an HTTP GET request to the thermostat to refresh metrics like current temperature, target temp, mode (heat/cool), hold etc.
exports.tstatPoll = function(nodeId) {
Expand Down

0 comments on commit e46e3ed

Please sign in to comment.