Ok, I just developed the needed ActionProvider and ActionHandler I had in mind. This would give every Device which has a sendCommand method a rule like
when <something> then send "<command>" to <device>
or
when <something> then send command "<command>" to <device>
_ = require 'lodash'
M = env.matcher
class SendCommandActionProvider extends env.actions.ActionProvider
constructor: (@framework) ->
parseAction: (input, context) =>
# Get all devices which have a send method
sendDevices = _(@framework.deviceManager.devices).values().filter(
(device) => device.hasAction('sendCommand')
).value()
device = null
command = null
match = null
# Match action
# send "<command>" to <device>
m = M(input, context)
.match('send ')
.match('command ', optional: yes)
.matchStringWithVars((m, _command) ->
m.match(' to ')
.matchDevice(sendDevices, (m, _device) ->
device = _device
command = _command
match = m.getFullMatch()
)
)
# Does the action match with our syntax?
if match?
assert device?
assert command?
assert typeof match is "string"
return {
token: match
nextInput: input.substring match.length
actionHandler: new SendCommandActionHandler @framework, device, command
}
return null
class SendCommandActionHandler extends env.actions.ActionHandler
constructor: (@framework, @device, @command) ->
executeAction: (simulate) ->
return (
@framework.variableManager.evaluateStringExpression(@command).then((command) =>
if simulate
Promise.resolve __('would send command %s to %s', command, @device.name)
else
@device.sendCommand command
Promise.resolve __('sended command %s to %s', command, @device.name)
)
)
I’m now working on a SerialDevice to send commands over the serial interface to a device I have which will use these ActionProvider and ActionHandler