Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tmrobert8
Copy link

Adding support for wake on lan magic packets

@tmrobert8 tmrobert8 force-pushed the NEEO-WOL branch 2 times, most recently from c990704 to 7a6dd60 Compare June 5, 2017 13:35
Copy link
Contributor

@neophob neophob left a 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.

const PORT_NBR = 9;
const DEFAULT_IPADDRESS = '255.255.255.255';

let wolMap = "wol.map";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

two comments here

  1. use const instead let (you wont change this name)

  2. 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

Copy link
Author

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)

Copy link
Author

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)

*/
module.exports.onButtonPressed = function (deviceid, name) {
console.log(`WOL button pressed for ${name}`);
var addr = parseAddr(name);
Copy link
Contributor

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.
#####################################################################################

Copy link
Contributor

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"
}

macAddress: addr.substring(0, idx),
ipAddress: addr.substring(idx + 1),
}
} else {
Copy link
Contributor

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

Copy link
Author

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 ???

Copy link
Contributor

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';


var max = lines.length;
for (var i = 0; i < max; i++) {
var line = lines[i].endsWith('\r') ? lines[i].slice(0, -1) : lines[i];
Copy link
Contributor

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

const net = require('net');
const dgram = require('dgram');

const PORT_NBR = 9;
Copy link
Contributor

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

@tmrobert8
Copy link
Author

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).

console.log(`WOL button pressed for ${name}`);

parseAddr(name)
.then(function (addr) {
Copy link
Contributor

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) => {

Copy link
Author

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!)

* @param {any} addr the address field to parse
*/
function parseAddr(addr) {
return new BluePromise(function (resolve, reject) {
Copy link
Contributor

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

Copy link
Author

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!)

@tmrobert8
Copy link
Author

Just committed with arrow functions - I'll be changing my other PR for this as well

* @param {any} macAddress the max address
*/
function getMacBuffer(macAddress) {
return new BluePromise((resolve, reject) => {
Copy link
Contributor

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

return Promise.all([addr, getMacBuffer(addr.macAddress)])
})
.then((results) => {
let magic = Buffer.alloc(6, 0xff);
Copy link
Contributor

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');

return sendPacket(results[0].ipAddress, results[1]);
})
.then((results) => {
console.log("WOL packet sent");
Copy link
Contributor

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

if (err === null || err === undefined) {
resolve(true);
} else {
reject("Exception writing the magic packet: " + err);
Copy link
Contributor

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();
}


readline.createInterface({ input: fs.createReadStream(wolMap) })
.on('line', (line) => {
if (!line.startsWith("#")) {
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants