-
Notifications
You must be signed in to change notification settings - Fork 2
/
johnny-five.coffee
62 lines (52 loc) · 2.17 KB
/
johnny-five.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Johnny Five plugin
module.exports = (env) ->
BoardManager = require('./board-manager')(env)
deviceTypes = {}
for device in [
# 'johnny-five-lcd-display'
'johnny-five-oled-display'
'johnny-five-switch'
'johnny-five-contact-sensor'
'johnny-five-presence-sensor'
'johnny-five-button'
'johnny-five-relay'
'johnny-five-pwm-output'
'johnny-five-temperature'
'johnny-five-temperature-humidity'
'johnny-five-temperature-pressure'
'johnny-five-rgb-led'
'johnny-five-servo'
]
# convert kebap-case to camel-case notation with first character capitalized
className = device.replace /(^[a-z])|(\-[a-z])/g, ($1) -> $1.toUpperCase().replace('-','')
deviceTypes[className] = require('./devices/' + device)(env)
actionProviders = {}
for provider in [
'johnny-five-rgb-color-action'
]
# convert kebap-case to camel-case notation with first character capitalized
className = provider.replace(/(^[a-z])|(\-[a-z])/g, ($1) -> $1.toUpperCase().replace('-','')) + 'Provider'
actionProviders[className] = require('./actions/' + provider)(env)
# ###JohnnyFivePlugin class
class JohnnyFivePlugin extends env.plugins.Plugin
init: (app, @framework, @config) =>
@boardManager = new BoardManager(@config, @)
# register devices
deviceConfigDef = require("./device-config-schema")
for className, classType of deviceTypes
env.logger.debug "Registering device class #{className}"
@framework.deviceManager.registerDeviceClass(className, {
configDef: deviceConfigDef[className],
createCallback: @callbackHandler(className, classType)
})
for className, classType of actionProviders
env.logger.debug "Registering action provider #{className}"
@framework.ruleManager.addActionProvider(new classType @framework)
callbackHandler: (className, classType) ->
# this closure is required to keep the className and classType context as part of the iteration
return (config, lastState) =>
return new classType(config, @, lastState)
# ###Finally
# Create a instance of my plugin
# and return it to the framework.
return new JohnnyFivePlugin