test-tutorial-framework/solvable/src/custom_handlers.py

46 lines
1.6 KiB
Python
Raw Normal View History

2019-07-05 13:37:54 +00:00
import logging
from ast import literal_eval
2019-07-24 13:52:38 +00:00
from tfw.components.frontend import MessageSender
from tfw.components.terminal import TerminalCommandsHandler
2019-07-12 21:26:57 +00:00
from tfw.main import TFWUplinkConnector
2019-07-05 13:37:54 +00:00
LOG = logging.getLogger(__name__)
2019-07-24 13:52:38 +00:00
class CenatorHandler:
2019-07-12 21:26:57 +00:00
keys = ['history.bash']
2019-07-15 11:43:34 +00:00
2019-07-30 13:17:47 +00:00
def handle_event(self, message, connector): # pylint: disable=no-self-use
2019-07-05 13:37:54 +00:00
command = message['value']
LOG.debug('User executed command: "%s"', command)
MessageSender(connector).send(f'You\'ve executed "{command}"', originator='JOHN CENA')
2019-07-05 13:37:54 +00:00
2019-07-24 13:52:38 +00:00
class TestCommandsHandler(TerminalCommandsHandler):
2019-07-15 11:43:34 +00:00
# pylint: disable=unused-argument,attribute-defined-outside-init,no-self-use
2019-07-05 13:37:54 +00:00
def command_sendmessage(self, *args):
if not args:
message_template = """'{"key": "", "data": {"command": ""}}'"""
2019-07-12 21:26:57 +00:00
TFWUplinkConnector().send_message({
2019-07-05 13:37:54 +00:00
'key': 'shell',
'data': {
'command': 'write',
'value': f'sendmessage {message_template}'
}
})
else:
2019-07-12 21:26:57 +00:00
TFWUplinkConnector().send_message(literal_eval(args[0]))
2019-07-05 13:37:54 +00:00
2019-07-30 13:17:47 +00:00
def messageFSMStepsHandler(message, connector):
2019-07-24 13:52:38 +00:00
"""
When the FSM steps this method is invoked.
Receives a 'data' field from an fsm_update message as kwargs.
"""
2019-07-30 13:17:47 +00:00
MessageSender(connector).send(
2019-07-24 13:52:38 +00:00
f'FSM has stepped from state "{message["last_event"]["from_state"]}" '
f'to state "{message["current_state"]}" in response to trigger "{message["last_event"]["trigger"]}"',
originator='FSM info'
2019-07-24 13:52:38 +00:00
)