-
Notifications
You must be signed in to change notification settings - Fork 6
/
dumper.py
executable file
·90 lines (71 loc) · 2.42 KB
/
dumper.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3
import argparse
import logging
import re
import threading
import time
from ynca import YncaConnection, YncaProtocolStatus
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Execute YNCA commands from a file.")
parser.add_argument(
"serial_url",
help="Can be a devicename like /dev/ttyUSB0 or COM3 for serial or use socket://<ip-or-host>:50000 IP based connections.",
)
parser.add_argument(
"commandfile",
help="File with a command per line.",
)
parser.add_argument(
"--outputfile",
help="Optional name of output file.",
)
args = parser.parse_args()
logging.basicConfig(
level="DEBUG", format="%(message)s", filename=args.outputfile, filemode="w"
)
print("Setup connection")
sem = threading.Semaphore(0)
def on_disconnect():
sem.release()
def message_received(
status: YncaProtocolStatus, subunit: str, function_: str, value: str
):
if function_ == "VERSION":
sem.release()
connection = YncaConnection.create_from_serial_url(args.serial_url)
try:
connection.connect(on_disconnect)
except Exception as e:
print(f"** Connection error: {e}")
exit(1)
connection.register_message_callback(message_received)
time.sleep(1)
print("")
print("*" * 30)
print("Submitting commands")
print(
"Note that there is 100ms inbetween commands, with lots of commands it can take a while"
)
print("*" * 30)
print("")
time.sleep(1)
commands_sent = 0
with open(args.commandfile) as commandfile:
for line in commandfile:
line = re.sub(r"#.*", "", line)
line = line.strip()
match = re.match(r"@(?P<subunit>.+?):(?P<function>.+?)=(?P<value>.*)", line)
if match is not None:
subunit = match.group("subunit")
function = match.group("function")
value = match.group("value")
if function == "VERSION":
logging.info("Skipping VERSION command as it is used as end marker")
continue
connection.raw(f"@{subunit}:{function}={value}")
commands_sent += 1
# Send command with guarenteed response as done indication
connection.raw("@SYS:VERSION=?")
sem.acquire()
print("Done")
connection.close()