pimatic-johnny-five
Version:
Pimatic Plugin for Johnny Five, a Robotics and IoT programming framework.
77 lines (64 loc) • 2.11 kB
text/coffeescript
module.exports = (env) ->
Promise = env.require 'bluebird'
_ = env.require 'lodash'
five = require('johnny-five')
commons = require('pimatic-plugin-commons')(env)
# Device class representing an Johnny Five digital output
class JohnnyFiveRelay extends env.devices.SwitchActuator
# Create a new JohnnyFiveRelay device
# [Object] config device configuration
# [JohnnyFivePlugin] plugin plugin instance
# [Object] lastState state information stored in database
constructor: (, , lastState) ->
= .id
= .name
= .config.debug || false
= commons.base @, .class
= lastState?.state?.value or off
= .boardManager.getBoard(.boardId)
super()
.boardReady()
.then( (board)=>
= new five.Relay {
pin: .pin
type: "NC"
board: board
}
)
.catch ((error) =>
.rejectWithError null, error
)
destroy: () ->
super()
_queryState: () ->
return new Promise( (resolve, reject) =>
.boardReady()
.then =>
try
resolve .isOn
catch e
.rejectWithError reject, e
.catch (error) =>
.rejectWithError reject, error
)
changeStateTo: (newState) ->
.debug "state change requested to: #{newState}"
return new Promise( (resolve, reject) =>
.boardReady()
.then =>
if newState
.on()
else
.off()
newState
resolve newState
.catch (error) =>
.rejectWithError reject, error
)
getState: () ->
return
.then (state) =>
return Promise.resolve state
.catch (error) =>
return .rejectWithError Promise.reject