pimatic-johnny-five
Version:
Pimatic Plugin for Johnny Five, a Robotics and IoT programming framework.
148 lines (129 loc) • 4.28 kB
text/coffeescript
module.exports = (env) ->
types = env.require('decl-api').types
Promise = env.require 'bluebird'
_ = env.require 'lodash'
five = require('johnny-five')
commons = require('pimatic-plugin-commons')(env)
# Device class representing an Johnny Five digital PWM output
class JohnnyFiveRgbLed extends env.devices.DimmerActuator
=
color:
description: 'RGB hex string fo the LED color value'
type: types.string
unit: 'hex color'
acronym: 'RGB'
=
setColor:
description: 'set a light color'
params:
colorCode:
type: types.string
# Create a new JohnnyFiveRgbLed device
# [Object] config device configuration
# [JohnnyFivePlugin] plugin plugin instance
# [Object] lastState state information stored in database
constructor: (, , lastState) ->
= .id
= .name
= .config.debug || false
= 0
= off
= commons.base @, .class
= .boardManager.getBoard(.boardId)
= _.merge {}, , JohnnyFiveRgbLed.attributes
= _.merge {}, , JohnnyFiveRgbLed.actions
super()
.boardReady()
.then( (board)=>
= new five.Led.RGB {
pins: .pins
isAnode: .isAnode
board: board
}
.catch (error) =>
.rejectWithError null, error
)
.catch ((error) =>
.rejectWithError null, error
)
destroy: () ->
super()
_queryLevel: () ->
return new Promise( (resolve, reject) =>
.boardReady()
.then =>
try
resolve if .isOn then .intensity() else 0
catch e
.rejectWithError reject, e
.catch (error) =>
.rejectWithError reject, error
)
_componentToHex: (c) ->
hex = c.toString 16
if hex.length is 1 then '0' + hex else hex
_rgbToHex: (r, g, b) ->
'#' + + +
_queryColor: () ->
return new Promise( (resolve, reject) =>
.boardReady()
.then =>
try
color = .color()
resolve color.red, color.green, color.blue
catch e
.rejectWithError reject, e
.catch (error) =>
.rejectWithError reject, error
)
changeDimlevelTo: (newLevelPerCent) ->
.debug "dimlevel change requested to (per cent): #{newLevelPerCent}"
return new Promise( (resolve, reject) =>
.boardReady()
.then =>
try
.intensity newLevelPerCent
catch e
.rejectWithError reject, e
.then (level) =>
level
resolve level
.catch (error) =>
.rejectWithError reject, error
)
getState: () ->
.then (level) =>
return Promise.resolve level > 0
.catch (error) =>
return .rejectWithError Promise.reject, error
getDimlevel: () ->
.then (level) =>
return Promise.resolve level
.catch (error) =>
return .rejectWithError Promise.reject, error
getColor: () ->
.then (color) =>
return Promise.resolve color
.catch (error) =>
return .rejectWithError Promise.reject, error
setColor: (color) ->
.debug "color change requested to: #{color}"
return new Promise( (resolve, reject) =>
.boardReady()
.then =>
try
.color color
catch e
.rejectWithError reject, e
.then (c) =>
.setAttribute 'color', c
resolve c
.catch (error) =>
.rejectWithError reject, error
)