forked from treban/pimatic-raspbee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
predicate.coffee
66 lines (53 loc) · 1.72 KB
/
predicate.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
module.exports = (env) ->
_ = env.require 'lodash'
M = env.matcher
Promise = env.require 'bluebird'
assert = env.require 'cassert'
class RaspBeePredicateProvider extends env.predicates.PredicateProvider
constructor: (@framework, @config) ->
super()
parsePredicate: (input, context) ->
fullMatch = null
nextInput = null
recCommand = null
device = null
setCommand = (m, tokens) => recCommand = tokens
RaspBeeDevices = _(@framework.deviceManager.devices).values().filter(
(device) => device.hasAttribute('switch')
).value()
m = M(input, context)
.match('received from ')
.matchDevice(RaspBeeDevices, (next, d) =>
next.match(' event ')
.matchString( (next, val) =>
recCommand = val
if device? and device.id isnt d.id
context?.addError(""""#{input.trim()}" is ambiguous.""")
return
device = d
fullMatch = next.getFullMatch()
)
)
if fullMatch?
assert typeof recCommand is "string"
return {
token: fullMatch
nextInput: input.substring(fullMatch.length)
predicateHandler: new RaspBeePredicateHandler(@framework, device, recCommand)
}
return null
class RaspBeePredicateHandler extends env.predicates.PredicateHandler
constructor: (framework, @device, @command) ->
@device.registerPredicate this
super()
setup: ->
super()
getValue: -> Promise.resolve false
getType: -> 'event'
getCommand: -> "#{@command}"
destroy: ->
@device.deregisterPredicate this
super()
return exports = {
RaspBeePredicateProvider
}