Skip to content

Instantly share code, notes, and snippets.

@Fjuxx
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fjuxx/4e295f2734088569d0c8 to your computer and use it in GitHub Desktop.
Save Fjuxx/4e295f2734088569d0c8 to your computer and use it in GitHub Desktop.
Predicate Provider problem
class PlayingPredicateProvider extends env.predicates.PredicateProvider
constructor: (@framework) ->
parsePredicate: (input, context) ->
kodiDevices = _(@framework.deviceManager.devices).values()
.filter((device) => device.hasAttribute( 'state')).value()
device = null
state = null
negated = null
match = null
stateAcFilter = (v) => v.trim() isnt 'not playing'
M(input, context)
.matchDevice(kodiDevices, (next, d) =>
next.match([' is', ' reports', ' signals'])
.match([' playing', ' stopped',' paused', ' not playing'], {acFilter: stateAcFilter}, (m, s) =>
if device? and device.id isnt d.id
context?.addError(""""#{input.trim()}" is ambiguous.""")
return
device = d
negated = (s.trim() isnt "playing")
match = m.getFullMatch()
)
)
if match?
assert device?
assert negated?
assert typeof match is "string"
return {
token: match
nextInput: input.substring(match.length)
predicateHandler: new PlayingPredicateHandler(device, negated)
}
else
return null
class PlayingPredicateHandler extends env.predicates.PredicateHandler
constructor: (@device, @negated) ->
setup: ->
@playingListener = (p) =>
env.logger.debug (if @negated then not p else p)
@emit 'change', (if @negated then not p else p)
@device.on 'state', @playingListener
super()
getValue: ->
return @device.getUpdatedAttributeValue('state').then(
(p) => (if @negated then not p else p)
)
destroy: ->
@device.removeListener "state", @playingListener
super()
getType: -> 'state'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment