From 3b8ee9437c876c2a58a4de940dd51c8ecbbe059f Mon Sep 17 00:00:00 2001 From: Rick Waldron Date: Sat, 14 Nov 2015 19:15:43 -0500 Subject: [PATCH] Correct and simplify channel validation. Closes gh-10 --- examples/relay.js | 2 +- index.js | 29 ++++++++++++----------------- test/test.js | 40 +++++++++++++++++++++++++++++++++------- 3 files changed, 46 insertions(+), 25 deletions(-) diff --git a/examples/relay.js b/examples/relay.js index 30c2e1c..5f09fdb 100644 --- a/examples/relay.js +++ b/examples/relay.js @@ -10,7 +10,7 @@ values to the console upon latching. var tessel = require('tessel'); var relaylib = require('../'); // Replace '../' with 'relay-mono' in your own code -var relay = relaylib.use(tessel.port['A']); +var relay = relaylib.use(tessel.port['A']); // Wait for the module to connect relay.on('ready', function relayReady () { diff --git a/index.js b/index.js index b39b6a2..8636d64 100644 --- a/index.js +++ b/index.js @@ -10,10 +10,16 @@ var util = require('util'); var events = require('events'); +var invalidChannel = 'Invalid relay channel. Expected 1 or 2.'; + +function isInvalidChannel(channel) { + return channel !== 1 && channel !== 2; +} + function Relay(hardware, callback) { // Save the port this.hardware = hardware; - + // Set the gpios as output this.hardware.digital[0].output(false); this.hardware.digital[1].output(false); @@ -32,9 +38,9 @@ function Relay(hardware, callback) { util.inherits(Relay, events.EventEmitter); Relay.prototype._setValue = function(channel, value, callback) { - var err; - if ((err = this._validChannel(channel))) { - return callback && callback(err); + + if (isInvalidChannel(channel)) { + return callback && callback(new Error(invalidChannel)); } else { // Get the relay @@ -56,21 +62,10 @@ Relay.prototype._setValue = function(channel, value, callback) { } }; -Relay.prototype._validChannel = function(channel) { - var self = this; - if (!(channel > 0 || channel <= this.currentStates.length)) { - var err = new Error("Invalid relay channel. Must be 1 or 2."); - self.emit('error', err); - return err; - } - return null; -}; - // Gets the state of the specified relay channel Relay.prototype.getState = function(channel, callback) { - var err; - if ((err = this._validChannel(channel))) { - return callback && callback(err); + if (isInvalidChannel(channel)) { + return callback && callback(new Error(invalidChannel)); } else { callback && callback(null, this.currentStates[channel - 1]); diff --git a/test/test.js b/test/test.js index d7c1b10..f176a9b 100644 --- a/test/test.js +++ b/test/test.js @@ -1,32 +1,58 @@ var tessel = require('tessel'); var relayDriver = require('../'); var relayPort = tessel.port.A; -var testPort = tessel.port['B']; +var testPort = tessel.port.B; var pinout = testPort.digital[0]; var pinin = testPort.digital[1]; var relay = relayDriver.use(relayPort); -console.log('1..5'); +console.log('1..11'); relay.on('ready', function () { console.log('# ready'); console.log('ok'); var channel = 1; + var boundOff = relay.turnOff.bind(relay); + var boundOn = relay.turnOn.bind(relay); setup(function() { - testHelper(relay.turnOff.bind(relay), channel, 1, function() { - testHelper(relay.turnOn.bind(relay), channel, 0, function() { - testHelper(relay.turnOff.bind(relay), channel, 1, function() { + testHelper(boundOff, channel, 1, function() { + testHelper(boundOn, channel, 0, function() { + testHelper(boundOff, channel, 1, function() { pinout.write(1, function() { - testHelper(relay.turnOn.bind(relay), channel, 1); + testHelper(boundOn, channel, 1); }); }); }); }); }); + + relay._setValue(0, 1, function(error) { + console.log(error !== null ? 'ok' : 'not ok'); + }); + + relay._setValue(3, 1, function(error) { + console.log(error !== null ? 'ok' : 'not ok'); + }); + + relay.getState(0, function(error) { + console.log(error !== null ? 'ok' : 'not ok'); + }); + + relay.getState(3, function(error) { + console.log(error !== null ? 'ok' : 'not ok'); + }); + + relay.getState(1, function(error) { + console.log(error === null ? 'ok' : 'not ok'); + }); + + relay.getState(2, function(error) { + console.log(error === null ? 'ok' : 'not ok'); + }); }); function setup(cb) { @@ -61,4 +87,4 @@ function testHelper(relayFunc, channel, expectedValue, cb) { relay.on('latch', function(channel, value) { console.log('# latch on channel ' + channel + ' switched to', value); -}); \ No newline at end of file +});