From b4c696032c60ed063b00286cdffc69db6c84e96f Mon Sep 17 00:00:00 2001 From: Wahib-Ul-Haq Date: Fri, 9 Feb 2018 10:02:39 +0100 Subject: [PATCH] replaced with the updated code --- nodejs_rpi/app.js | 42 +++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/nodejs_rpi/app.js b/nodejs_rpi/app.js index 9c20b41..16e38ec 100644 --- a/nodejs_rpi/app.js +++ b/nodejs_rpi/app.js @@ -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; // Creation of a simple Webserver to handle requests from slack @@ -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(=> () { + 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); + }); } \ No newline at end of file