-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial Commit for Wake On Lan #4
base: master
Are you sure you want to change the base?
Conversation
c990704
to
7a6dd60
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @tmrobert8 for this preview.
I did a review and gave you feedback about some minor issues.
device/WakeOnLan/controller.js
Outdated
const PORT_NBR = 9; | ||
const DEFAULT_IPADDRESS = '255.255.255.255'; | ||
|
||
let wolMap = "wol.map"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
two comments here
-
use const instead let (you wont change this name)
-
why don't you use a JSON file to parse (much easier to parse) and/or support passing parameters via command line or env variable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although I'd prefer JSON myself, I've dealt with enough users to know that non-programmers hate json/xml formats and to keep setup/config type files as simple as possible (like a simple x=y type of lines). Since a majority will try this stuff without knowing how to program - I chose that format for ease. If you feel strongly that it should be json - I'll switch to json (pretty easy)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also - let is appropriate here since I can potentially change the name in the setMapPath function (although that reminds me I need to change the name of the variable since it's a path to the map file)
device/WakeOnLan/controller.js
Outdated
*/ | ||
module.exports.onButtonPressed = function (deviceid, name) { | ||
console.log(`WOL button pressed for ${name}`); | ||
var addr = parseAddr(name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
try to use const/let instead var. this makes code easier to read
# Please note that if the MACAddress appears twice (or MAC Address/IP Address combo), | ||
# the NEEO app will complain of a duplicate when trying to add a device. | ||
##################################################################################### | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
json example
{
"name": "devicename",
"mac": "macaddress",
"ip": "optional ip address"
}
device/WakeOnLan/controller.js
Outdated
macAddress: addr.substring(0, idx), | ||
ipAddress: addr.substring(idx + 1), | ||
} | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
obsolete else {}
- as you already returned
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
??? not sure what you mean by this ???
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
your code:
if (something()) {
return 'a';
} else {
return 'b';
}
Easier to read version:
if (something()) {
return 'a';
}
return 'b';
device/WakeOnLan/controller.js
Outdated
|
||
var max = lines.length; | ||
for (var i = 0; i < max; i++) { | ||
var line = lines[i].endsWith('\r') ? lines[i].slice(0, -1) : lines[i]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
json would render this code obsolete, as you could access the attributes directly
device/WakeOnLan/controller.js
Outdated
const net = require('net'); | ||
const dgram = require('dgram'); | ||
|
||
const PORT_NBR = 9; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dont use abbreviation in const names
Just pushed a new version. Made most the changes (except the json one) and added a few more (mainly implemented Promises now that I'm more familiar with them). |
device/WakeOnLan/controller.js
Outdated
console.log(`WOL button pressed for ${name}`); | ||
|
||
parseAddr(name) | ||
.then(function (addr) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hint use the es6 arrow functions (shorter to write and this is attached to the function scope), instead .then(function (addr) {
you can use .then((addr) => {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice - about time they implemented something like that (I haven't even looked at the ES6 spec in detail - so hints like this are great!)
device/WakeOnLan/controller.js
Outdated
* @param {any} addr the address field to parse | ||
*/ | ||
function parseAddr(addr) { | ||
return new BluePromise(function (resolve, reject) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you don't need to wrap all functions in promise
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah - just exploring using promises (haven't used them before - too old school still!)
Just committed with arrow functions - I'll be changing my other PR for this as well |
device/WakeOnLan/controller.js
Outdated
* @param {any} macAddress the max address | ||
*/ | ||
function getMacBuffer(macAddress) { | ||
return new BluePromise((resolve, reject) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need to return a promise here, just return the plain result
device/WakeOnLan/controller.js
Outdated
return Promise.all([addr, getMacBuffer(addr.macAddress)]) | ||
}) | ||
.then((results) => { | ||
let magic = Buffer.alloc(6, 0xff); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do you allocate a buffer with 6 bytes and the concatinate it 16 times. just init a buffer with the correct size.
and you can also create a buffer object from a string: Buffer.from(string, 'utf8');
device/WakeOnLan/controller.js
Outdated
return sendPacket(results[0].ipAddress, results[1]); | ||
}) | ||
.then((results) => { | ||
console.log("WOL packet sent"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would use the debug
package to output debug information, see https://github.com/visionmedia/debug.
I would add this output before the return sendPacket...
so you can omit the additional then branch
device/WakeOnLan/controller.js
Outdated
if (err === null || err === undefined) { | ||
resolve(true); | ||
} else { | ||
reject("Exception writing the magic packet: " + err); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reject should wrap the error message in a error object, like reject(new Error('TEXT'))
, in your case you could just do a reject(err)
.
to make it more readable you can
if (err) {
reject(err);
} else {
resolve();
}
device/WakeOnLan/controller.js
Outdated
|
||
readline.createInterface({ input: fs.createReadStream(wolMap) }) | ||
.on('line', (line) => { | ||
if (!line.startsWith("#")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i would extract the text parsing in its own function, to make the code more readable
Adding support for wake on lan magic packets