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

Code readability Improvement #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 19 additions & 23 deletions nodejs_rpi/app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var http = require("http");
var gpio = require("pi-gpio");
var pin = 11;
var relayOpen = false;
let relayOpen = false; // A global flag for relay
var openTime = 2000;

Copy link

Choose a reason for hiding this comment

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

we might consider var pin = 11 and var openTime = 2000 can be const instead of var if they're not gonna change.

// Creation of a simple Webserver to handle requests from slack
Expand Down Expand Up @@ -33,29 +33,25 @@ console.log('Server running at http://127.0.0.1:80/');

// Method to open and close the relay
function openDoor(doorTimeout) {
doorTimeout = doorTimeout || 1;
if (!relayOpen) {
relayOpen = true;
console.log('open called');
gpio.open(pin, "output", function(err) { // Open pin 11 for output
gpio.write(pin, 1, function() { // Set pin 11 high (1)
setTimeout(function() {
closeRelay(); // Close pin 11
}, (doorTimeout));
});


});
}

return "success";
doorTimeout = doorTimeout || 1;

if (!relayOpen) {
relayOpen = true;
console.log('open called');
gpio.open(pin, 'output', (err) => { // Open pin 11 for output
gpio.write(pin, 1, () => { // Set pin 11 high (1)
setTimeout(=> () {
Copy link

Choose a reason for hiding this comment

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

setTimeout(=> () { seems like there's a syntax error here. Should have been like setTimeout( () => {

closeRelay(); // Close pin 11
}, doorTimeout);
});
});
}
}

function closeRelay() {
gpio.write(pin, 0, function() {
console.log('closed!');
relayOpen = false;
gpio.close(pin);
});

gpio.write(pin, 0, () => {
console.log('closed!');
relayOpen = false;
gpio.close(pin);
});
}