Skip to content

Commit

Permalink
Add relaysSwitch ability
Browse files Browse the repository at this point in the history
  • Loading branch information
javierbrea committed Mar 5, 2019
1 parent 5cd79cf commit e8ef0a7
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
8 changes: 7 additions & 1 deletion lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const {
RELAY_GPIO_2_OPTION,
SENSOR_GPIO_OPTION,
DEBOUNCE_OPTION,
REVERSE_STATE_OPTION
REVERSE_STATE_OPTION,
INVERT_RELAYS_OPTION
} = require('./statics')

module.exports = {
Expand Down Expand Up @@ -40,5 +41,10 @@ module.exports = {
type: 'boolean',
describe: 'Invert value of sensor',
default: false
},
[INVERT_RELAYS_OPTION]: {
type: 'boolean',
describe: 'Invert value of relays',
default: false
}
}
6 changes: 5 additions & 1 deletion lib/statics.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
'use strict'

const ABILITY_NAME = 'switch'
const RELAYS_ABILITY_NAME = 'relaysSwitch'
const WAYS_OPTION = 'ways'
const RELAY_GPIO_1_OPTION = 'relayGpio1'
const RELAY_GPIO_2_OPTION = 'relayGpio2'
const SENSOR_GPIO_OPTION = 'sensorGpio'
const DEBOUNCE_OPTION = 'debounce'
const REVERSE_STATE_OPTION = 'reverse'
const INVERT_RELAYS_OPTION = 'invertRelays'

module.exports = {
ABILITY_NAME,
RELAYS_ABILITY_NAME,
WAYS_OPTION,
RELAY_GPIO_1_OPTION,
RELAY_GPIO_2_OPTION,
SENSOR_GPIO_OPTION,
DEBOUNCE_OPTION,
REVERSE_STATE_OPTION
REVERSE_STATE_OPTION,
INVERT_RELAYS_OPTION
}
35 changes: 32 additions & 3 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ const pluginConfigs = require('./lib/plugins.json')
const options = require('./lib/options')
const {
ABILITY_NAME,
RELAYS_ABILITY_NAME,
WAYS_OPTION,
RELAY_GPIO_1_OPTION,
RELAY_GPIO_2_OPTION,
SENSOR_GPIO_OPTION,
DEBOUNCE_OPTION,
REVERSE_STATE_OPTION
REVERSE_STATE_OPTION,
INVERT_RELAYS_OPTION
} = require('./lib/statics')

domapic.createModule({
Expand All @@ -36,7 +38,8 @@ domapic.createModule({
initialStatus: true,
rememberLastStatus: true
}, {
gpio: RELAY_GPIO_1_OPTION
gpio: RELAY_GPIO_1_OPTION,
invert: INVERT_RELAYS_OPTION
})
let relay2 = null
if (ways === 4) {
Expand All @@ -47,7 +50,8 @@ domapic.createModule({
initialStatus: true,
rememberLastStatus: true
}, {
gpio: RELAY_GPIO_2_OPTION
gpio: RELAY_GPIO_2_OPTION,
invert: INVERT_RELAYS_OPTION
})
}

Expand All @@ -59,6 +63,7 @@ domapic.createModule({
if (relay2) {
await relay2.setStatus(targetStatus)
}
dmpcModule.events.emit(RELAYS_ABILITY_NAME, targetStatus)
}

await dmpcModule.register({
Expand All @@ -83,6 +88,30 @@ domapic.createModule({
event: {
description: 'Sensor has changed status'
}
},
[RELAYS_ABILITY_NAME]: {
description: 'Relays status',
data: {
type: 'boolean'
},
action: {
description: 'Change relays status',
handler: async newState => {
await relay1.setStatus(newState)
if (relay2) {
await relay2.setStatus(newState)
}
dmpcModule.events.emit(RELAYS_ABILITY_NAME, newState)
return newState
}
},
state: {
description: 'Current relays status',
handler: () => relay1.status
},
event: {
description: 'Relays have changed status'
}
}
})

Expand Down
22 changes: 22 additions & 0 deletions test/unit/server.specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ test.describe('server', () => {
test.expect(domapic.stubs.module.events.emit).to.have.been.calledWith('switch', true)
})
})

test.describe('relays state handler', () => {
test.it('should return current relays status', () => {
gpioOut.stubs.instance.status = true
test.expect(abilities.relaysSwitch.state.handler()).to.equal(true)
})
})

test.describe('relays action handler', () => {
test.it('should change relay 1 status', async () => {
await abilities.relaysSwitch.action.handler(false)
test.expect(gpioOut.stubs.instance.setStatus).to.have.been.calledWith(false)
})
})
})

test.describe('when reverse option is true', () => {
Expand Down Expand Up @@ -140,6 +154,14 @@ test.describe('server', () => {
test.expect(gpioOut.stubs.instance.setStatus).to.have.been.calledTwice()
})
})

test.describe('relays action handler', () => {
test.it('should change relay 2 status', async () => {
await abilities.relaysSwitch.action.handler(false)
test.expect(gpioOut.stubs.instance.setStatus.callCount).to.equal(4)
test.expect(gpioOut.stubs.instance.setStatus).to.have.been.calledWith(false)
})
})
})

test.describe('when ways option is 4, and no second gpio is defined', () => {
Expand Down

0 comments on commit e8ef0a7

Please sign in to comment.