forked from donadigo/TMInterfaceClientPython
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcustom_command.py
More file actions
47 lines (37 loc) · 1.29 KB
/
custom_command.py
File metadata and controls
47 lines (37 loc) · 1.29 KB
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
from tminterface.interface import TMInterface
from tminterface.client import Client
import sys
import signal
import time
class MainClient(Client):
def __init__(self) -> None:
pass
def on_registered(self, iface: TMInterface) -> None:
print(f'Registered to {iface.server_name}')
iface.register_custom_command('echo')
def on_custom_command(self, iface, time_from: int, time_to: int, command: str, args: list):
# Usage: echo [message] [severity]
# echo "Something like this"
# echo "An error message" error
if command == 'echo':
if len(args) > 0:
severity = 'log' if len(args) == 1 else args[1]
iface.log(args[0], severity)
else:
iface.log('echo takes at least one argument', 'error')
def main():
server_name = 'TMInterface0'
if len(sys.argv) > 1:
server_name = 'TMInterface' + str(sys.argv[1])
print(f'Connecting to {server_name}...')
iface = TMInterface(server_name)
def handler(signum, frame):
iface.close()
signal.signal(signal.SIGBREAK, handler)
signal.signal(signal.SIGINT, handler)
client = MainClient()
iface.register(client)
while iface.running:
time.sleep(0)
if __name__ == '__main__':
main()