-
Notifications
You must be signed in to change notification settings - Fork 2
/
board-manager.coffee
233 lines (209 loc) · 7.93 KB
/
board-manager.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# Class UniPiUpdateManager
module.exports = (env) ->
Promise = env.require 'bluebird'
_ = env.require 'lodash'
events = require 'events'
util = require 'util'
five = require('johnny-five')
commons = require('pimatic-plugin-commons')(env)
class ExpanderBoardMapper
constructor: (@opts) ->
@boardIsReady = false
@debug = @opts.debug || false
@id = @opts.id
@_base = commons.base @, "ExpanderBoard"
if not @opts.controller?
throw new Error "Missing controller property for expander board"
@boardInit = new Promise((resolve, reject) =>
@_boardReadyListener = @_boardReadyHandler(resolve, reject)
@_boardNotReadyListener = @_boardNotReadyHandler(resolve, reject)
if @opts.board.isReady
@_boardReadyListener()
else
@opts.board.once "ready", @_boardReadyListener
@opts.board.once "error", @_boardNotReadyListener
)
.catch (error) =>
@_base.error error
_boardReadyHandler: (resolve, reject) ->
return () =>
expanderOptions =
controller: @opts.controller
board: @opts.board
if @opts.address?
expanderOptions.address = parseInt @opts.address
try
@virtual = new five.Board.Virtual({
io: new five.Expander(expanderOptions),
board: @opts.board
})
catch error
return reject new Error "Expander board initialization failed: #{error}"
@opts.board.removeListener "error", @_boardNotReadyListener if @_boardNotReadyListener?
@virtual.remote = false
@_base.debug "Board Ready"
@boardIsReady = true
resolve @virtual
_boardNotReadyHandler: (resolve, reject) ->
return (error) =>
@opts.board.removeListener "ready", @_boardReadyListener if @_boardReadyListener?
@_base.rejectWithErrorString(reject, error)
boardReady: () ->
return new Promise( (resolve, reject) =>
Promise.settle([@boardInit])
.then () =>
if @boardIsReady
resolve @virtual
else
@_base.rejectWithErrorString(reject, new Error "Board not ready")
.catch (error) =>
@_base.rejectWithErrorString(reject, error)
)
releasePin: (pin, controller) ->
# nothing to do
class BoardWrapper extends five.Board
constructor: (opts) ->
super(opts)
@boardIsReady = false
@debug = opts.debug || false
@_base = commons.base @, "Board"
@boardInit = new Promise((resolve, reject) =>
@_boardReadyListener = @_boardReadyHandler(resolve, reject)
@_boardNotReadyListener = @_boardNotReadyHandler(resolve, reject)
if @isReady
@_boardReadyListener()
else
@once "ready", @_boardReadyListener
@once "error", @_boardNotReadyListener
@on "message", (event) =>
@_base.debug "Message received:", event.message
@on "error", (error) =>
@_base.error "Board not ready:", error.message.replace("\n", "")
@on "ready", =>
@_base.debug "Board Ready"
)
_boardReadyHandler: (resolve, reject) ->
return () =>
@boardIsReady = true
@removeListener "error", @_boardNotReadyListener if @_boardNotReadyListener?
resolve @board
_boardNotReadyHandler: (resolve, reject) ->
return (error) =>
@removeListener "ready", @_boardReadyListener if @_boardReadyListener?
@_base.rejectWithError(reject, error)
boardReady: () ->
return new Promise( (resolve, reject) =>
Promise.settle([@boardInit])
.then () =>
if @boardIsReady
resolve @
else
@_base.rejectWithError(reject, new Error "Board not ready")
.catch (error) =>
@_base.rejectWithError(reject, error)
)
releasePin: (pin, controller) ->
if not _.isEmpty pin
checkController = not _.isEmpty controller
for index, slot of @occupied
if checkController
match = slot.controller? and slot.controller is controller
else
match = true
if slot.value is pin and slot.type is 'pin' and match
@occupied.splice index, 1
break
class BoardManager extends events.EventEmitter
constructor: (@config, plugin) ->
@boards = {}
@debug = plugin.config.debug || false
@_base = commons.base @, "BoardManager"
@piGpioInitialized = false
super()
boardConfigs = @config.boards
if boardConfigs? and boardConfigs.length isnt 0
defaultBoardConfig =
debug: @debug
repl: false
timeout: 40000
for boardConfig in @config.boards
if boardConfig.id?
try
@boards[boardConfig.id] = @createBoard(_.assign {}, defaultBoardConfig, boardConfig)
@_base.debug "Created board #{boardConfig.id}"
catch e
@_base.error "Creation of board #{boardConfig.id} raised exception:" + e
else
@_base.error "Invalid plugin configuration. Missing board id"
else
@_base.error "Invalid plugin configuration. No boards configured"
plugin.framework.once 'destroy', (context) =>
promise = new Promise( (resolve, reject) =>
@_base.info "pimatic is shutting down"
if @piGpioInitialized
(require('pigpio').terminate)()
@_base.info "pigpio terminated"
@piGpioInitialized = false
resolve()
)
context.waitForIt promise
createBoard: (options) ->
switch options.boardType || 'arduino'
when 'arduino' then (
if options.port? and options.baudrate?
fiveModule = require.cache[require.resolve 'johnny-five']
SerialPort = fiveModule.require 'serialport'
options.port = new SerialPort(options.port, {baudrate: options.baudrate})
@board = new BoardWrapper options
)
when 'raspi-io' then (
unless @piGpioInitialized
@piGpioInitialized = true
@_base.info "pigpio hardwareRevision #{(require('pigpio')).hardwareRevision()}"
(require('pigpio').initialize)()
@_base.info "pigpio initialized"
raspi = require 'raspi-io'
raspiOptions =
enableSoftPwm: true
if options.address?
try
raspiOptions = _.assign({}, JSON.parse(options.address), raspiOptions)
catch e
env.logger.error "Property address does not contain stringified JSON options for raspi-io - ignored."
@board = new BoardWrapper(_.assign({}, options, {io: new raspi(raspiOptions)}))
)
when 'particle-io' then (
Particle = require 'particle-io'
@board = new BoardWrapper(_.assign({}, options, {io: new Particle({
token: options.token, deviceId: options.deviceId })}))
)
when 'etherport' then (
EtherPort = require 'etherport'
@board = new BoardWrapper(_.assign({}, options, {port: new EtherPort({
port: options.port, reset: options.port || false})}))
)
when 'etherport-client', 'esp8266' then (
EtherPortClient = require('etherport-client').EtherPortClient
@board = new BoardWrapper(_.assign({}, options, {
port: new EtherPortClient({
port: options.port,
host: options.address
})
}))
)
when 'expander' then (
parentBoard = @getBoard options.port
@board = new ExpanderBoardMapper(_.assign({}, options, {board: parentBoard}))
)
else
throw new Error "Unsupported boardType #{options.boardType}"
return @board
getBoard: (id) ->
if @boards[id]
board=@boards[id]
if board?
return board
else
error = new Error "Board not found"
@_base.error error
throw error