-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontroller710.py
228 lines (217 loc) · 8.87 KB
/
controller710.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import sys
import serial
import argparse
from time import time
from queue import Queue
from threading import Thread
from ptt710 import Ptt
from cat710 import Cat
from common710 import stamp
from common710 import UpdateDisplayException
from xmlrpc710 import RigXMLRPC
from copy import deepcopy
__author__ = "Steve Magnuson AG7GN"
__copyright__ = "Copyright 2023, Steve Magnuson"
__credits__ = ["Steve Magnuson"]
__license__ = "GPL v3.0"
__version__ = "1.0.4"
__maintainer__ = "Steve Magnuson"
__email__ = "ag7gn@arrl.net"
__status__ = "Production"
class Controller(object):
"""
Object Manager object for the Kenwood TM-D710G and TM-V71A controller
"""
def __init__(self, o_serial: serial, args: argparse, **kwargs):
"""
Instantiate Controller and Cat classes.
:param: o_serial: Serial port object from Serial module
:param: args: Command line arguments from argparse
:param: kwargs: 'version', the program version for inclusion
on the GUI title bar
"""
self.version = kwargs.get('version', '')
self.o_serial = o_serial
self.serial_port = args.port
self.baudrate = args.baudrate
self.ptt_method = args.ptt
self.ptt = None
self.xmlrpc_port = args.xmlport
self.loc = args.location
if args.small:
self.size = 'small'
else:
self.size = 'normal'
self.root = None
self.msg_queue = None
self.gui = None
self.xmlrpc_server = None
self.title = None
self.cmd_queue = Queue()
self.cat = Cat(self.o_serial, job_queue=self.cmd_queue)
self.controller_thread = None
self.controller_running = False
self.previous_rig_dictionary = None
self.xmlrpc_thread = None
self.ptt_handler = None
def _start_xmlrpc_server(self):
time_current = time()
# Wait until radio parameters have been read and state
# dictionary populated before starting the XML-RPC server
# so that we have something to serve.
while self.cat.get_dictionary()['speed'] is None:
# Exit if we receive no data from radio in 5 seconds
if time() >= time_current + 5:
self.print_error(f"{stamp()}: No data received from "
f"radio in 5 seconds")
# sys.exit(1)
self.stop()
print(f"{stamp()}: Starting XML-RPC server...", file=sys.stderr)
self.xmlrpc_thread.start()
print(f"{stamp()}: XML-RPC server listening on port "
f"{self.xmlrpc_port}.", file=sys.stderr)
def start_gui(self, root):
self.root = root
self.msg_queue = Queue()
if self.loc is not None:
loc = self.loc.split(':')
try:
x_loc = int(loc[0])
y_loc = int(loc[1])
except (ValueError, IndexError):
print(f"{stamp()}: '{self.loc}' is an invalid "
f"screen position. Using defaults instead.",
file=sys.stderr)
loc = None
else:
y_max = self.root.winfo_screenheight()
x_max = self.root.winfo_screenwidth()
if 0 <= x_loc < x_max - 100 and 0 <= y_loc < y_max - 100:
loc = (x_loc, y_loc)
else:
print(f"{stamp()}: '{self.loc}' is an invalid "
f"screen position. Using defaults instead.",
file=sys.stderr)
loc = None
else:
loc = None
model = self.cat.info['model']
self.title = f"Kenwood {model} Controller"
from gui710 import Display
self.gui = Display(root=self.root,
title=self.title,
version=self.version,
cmd_queue=self.cmd_queue,
msg_queue=self.msg_queue,
size=self.size,
initial_location=loc,
info=self.cat.info)
self.msg_queue.put(['INFO', f"{stamp()}: Found {model} on "
f"{self.serial_port} @ {self.baudrate}"])
self.controller_running = True
self.controller_thread = Thread(target=self.controller)
self.controller_thread.daemon = True
print(f"{stamp()}: Starting controller...", file=sys.stderr)
self.controller_thread.start()
print(f"{stamp()}: Controller running.", file=sys.stderr)
self.ptt_handler = Ptt(self.ptt_method, default_port=self.o_serial,
msg_queue=self.msg_queue)
try:
self.xmlrpc_server = RigXMLRPC(self.xmlrpc_port, self.cat,
self.cmd_queue,
ptt_handler=self.ptt_handler)
except OSError as _:
self.print_error(f"XML-RPC port {self.xmlrpc_port} is already in use.\n\n"
f"Is this program or Flrig already running?\n"
f"Close it before running this program.")
self.stop()
else:
self.xmlrpc_thread = Thread(target=self.xmlrpc_server.start)
# Kill this thread when the main app terminates
self.xmlrpc_thread.daemon = True
self._start_xmlrpc_server()
self.msg_queue.put(['INFO', f"{stamp()}: XML-RPC server "
f"listening on port {self.xmlrpc_port}"])
self.cat.gui_root = self.root
def controller(self):
"""
Manages the job queue.
"""
while self.controller_running:
if self.cmd_queue.empty():
try:
rig_dictionary = self.cat.update_dictionary()
except IndexError as _:
self.print_error(f"Error communicating with radio on "
f"{self.serial_port}")
break
else:
# Only update the GUI display if something has changed
if rig_dictionary != self.previous_rig_dictionary:
try:
self.gui.update_display(rig_dictionary)
except UpdateDisplayException as _:
self.print_error(f"Error communicating with "
f"radio on {self.serial_port}")
break
else:
if self.root:
self.root.update()
self.previous_rig_dictionary = deepcopy(rig_dictionary)
else:
job = self.cmd_queue.get() # Get job from queue
if job[0] == 'quit':
break
self.msg_queue.put(['INFO', f"{stamp()}: Queued {job}"])
if self.cat.run_job(job, self.msg_queue):
self.msg_queue.put(['INFO', f"{stamp()}: Finished {job}"])
else:
break
self.cmd_queue.task_done()
self.stop()
def print_error(self, err: str):
"""
If GUI, pop up a window with the message, otherwise print
message to stderr.
:param: err: String containing message
:return: None
"""
if self.root is None:
# No GUI. Print to stderr.
print(f"{stamp()}: {err}", file=sys.stderr)
else:
# GUI exists.
from tkinter import messagebox
messagebox.showerror(title=f"Controller ERROR!",
message=err,
parent=self.root)
def send_command(self, cmd: str) -> list:
# This is not managed by the job queue!
return self.cat.handle_query(cmd)
def set_info(self, info: list):
self.cat.info = info
def stop(self):
print(f"{stamp()}: Stopping controller...",
file=sys.stderr)
self.controller_running = False
if self.controller_thread and self.controller_thread.is_alive():
try:
self.controller_thread.join(timeout=2)
except RuntimeError as _:
print(f"{stamp()}: Controller thread stopped",
file=sys.stderr)
if self.xmlrpc_server:
self.xmlrpc_server.stop()
if self.controller_thread.is_alive():
self.xmlrpc_thread.join(timeout=1)
print(f"{stamp()}: XML-RPC server stopped",
file=sys.stderr)
print(f"{stamp()}: Controller stopped.", file=sys.stderr)
if self.root:
self.root.quit()
self.root.update()
try:
self.o_serial.close()
except OSError:
# Serial port already closed
pass