-
Notifications
You must be signed in to change notification settings - Fork 0
/
puara_serial_manager.py
196 lines (166 loc) · 6.87 KB
/
puara_serial_manager.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
"""Puara Serial Manager.
Usage:
puara_serial_manager.py <wifiSSID> <wifiPSK> <ipAddress> <port>
puara_serial_manager.py -h | --help
puara_serial_manager.py -v | --version
Options:
-h --help Show this screen.
-v --version Show version.
"""
# Standard libraries
from collections import namedtuple
from string import Template
from typing import NamedTuple, Optional
import json
import logging
import threading
from time import sleep
# Third-party libraries
from docopt import docopt
import serial
import serial.tools.list_ports
# Local libraries
from .wired import tstick, tstick_serial, tstick_osc
BAUDRATE = 115200
READ_TIMEOUT_SECS = 5.0
CONFIG_TEMPLATE = '''
{
"wifiSSID": "$ssid",
"wifiPSK": "$psk",
"oscPORT1": $osc_port,
"oscIP1": "$ip_addr"
}
'''
DATA_START = b'<<<'
DATA_END = b'>>>'
WifiNetwork = namedtuple("WifiNetwork", ['ssid', 'psk'])
class Config(NamedTuple):
ip_addr: str
port: int
class Device(NamedTuple):
device_id: str
ser: serial.Serial
name: Optional[str]=None
def __str__(self):
if self.name is None:
return self.device_id
else:
return self.name
def named_device(self, name):
return Device(self.device_id, self.ser, name)
class PuaraSerialException(Exception):
pass
class SerialManager:
def __init__(self, wifi: WifiNetwork, ip_address: Optional[str]=None, port: Optional[str]=None, config_delegate=None):
self.wifi = wifi
self.devices = {}
self.scanner = threading.Thread(target=self.scan_thread, daemon=True)
self.scanner.start()
self.config = Config(ip_address, port)
self.config_delegate = config_delegate
self.config_template = Template(CONFIG_TEMPLATE)
self.wired_tsticks = tstick.all_tsticks()
def scan_thread(self):
while True:
for port in serial.tools.list_ports.comports():
device_id = port.serial_number
if not device_id:
device_id = port.hwid
if device_id in self.devices:
pass
# TODO(p42ul): do something with these?
# Wired T-Stick
elif device_id in [ts.ser() for ts in self.wired_tsticks]:
tstick = [ts for ts in self.wired_tsticks if device_id == ts.ser()][0]
config_data = self.config_delegate(tstick.name())
lexer = tstick.lexer()
parser = tstick.parser()
baudrate = tstick.baudrate()
sender = tstick_osc.OSCSender(tstick.name(), '127.0.0.1', config_data.port)
lexer.subscribe(parser.parse)
parser.subscribe(sender.send)
self.devices[device_id] = tstick_serial.TStickSerial(port.device, baudrate, tstick_serial.READ_SIZE, lexer.enqueue, b's')
else:
ser = serial.Serial()
ser.port, ser.baudrate, ser.timeout = port.device, BAUDRATE, READ_TIMEOUT_SECS
device = Device(device_id, ser)
self.devices[device_id] = device
print(f'found new serial device {device}')
try:
ser.open()
except serial.SerialException as e:
print(f"Couldn't open serial device {device}, got error {e}")
threading.Thread(target=self.configure_device, args=[device]).start()
sleep(1)
def create_config(self, config):
return self.config_template.substitute(ip_addr=config.ip_addr, osc_port=config.port,
ssid=self.wifi.ssid, psk=self.wifi.psk)
def configure_device(self, device: Device):
print(f'waiting for {device} to become ready...')
self.wait_for_device_ready(device)
print(f'{device} ready.')
if device.name is None:
name = self.get_device_name(device)
device = device.named_device(name)
print(f'{device.device_id} is {device.name}')
print(f'getting config data from {device}')
config_data = self.get_config_data(device)
config_json = json.loads(config_data)
if self.config_delegate:
desired_data = self.create_config(self.config_delegate(device.name))
else:
desired_data = self.create_config(self.config)
desired_json = json.loads(desired_data)
needs_config = False
for k, desired_v in desired_json.items():
if not config_json[k] == desired_v:
needs_config = True
print(f'value of {k} does not match | desired: "{desired_v}" device: "{config_json[k]}"')
if needs_config:
print(f'configuring {device}')
device.ser.write(f'sendconfig {json.dumps(desired_json)}'.encode('utf-8'))
# TODO(p42ul): Replace this with an ACK from the device side.
sleep(3)
device.ser.write(b'writeconfig')
sleep(3)
print(f'{device} has been successfully configured. rebooting to initialize config')
device.ser.write(b'reboot')
device.ser.close()
def wait_for_device_ready(self, device: Device):
ser = device.ser
expected = b'pong'
while True:
ser.write(b'ping')
data = ser.read_until(expected=expected)
if expected in data:
break
sleep(READ_TIMEOUT_SECS)
ser.reset_output_buffer()
sleep(READ_TIMEOUT_SECS)
def get_response(self, device: Device, request: str) -> str:
ser = device.ser
ser.write(request.encode('utf-8'))
start_data = ser.read_until(expected=DATA_START)
if DATA_START not in start_data:
raise PuaraSerialException(f'received {start_data} in response to "{request}" from {device} on {ser.port}')
response_data = ser.read_until(expected=DATA_END)
if DATA_END not in response_data:
raise PuaraSerialException(f'"{DATA_END}" not received in time from {device} on {ser.port}')
response_data = response_data.decode('utf-8', 'backslashreplace')
response_data = response_data.strip(str(DATA_END, 'utf-8'))
return response_data
def get_config_data(self, device) -> str:
return self.get_response(device, 'readconfig')
def get_device_name(self, device) -> str:
return self.get_response(device, 'whatareyou')
def print_available_ports(self):
for port in serial.tools.list_ports.comports():
print(f'{port}: hwid {port.hwid} vid {port.vid} pid {port.pid}')
def main(args):
wifi = WifiNetwork(ssid=args['<wifiSSID>'], psk=args['<wifiPSK>'])
_ = SerialManager(wifi, args['<ipAddress>'], args['<port>'])
while True:
sleep(1)
if __name__ == '__main__':
args = docopt(__doc__, version="Puara Serial Manager 0.1")
main(args)