forked from bjorgan/rotctld-web-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotatorgui.py
executable file
·279 lines (210 loc) · 7.73 KB
/
rotatorgui.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env python3
#
# ROTCTLD Basic Web GUI
#
# Copyright (C) 2018 Mark Jessop <[email protected]>
# Released under GNU GPL v3 or later
#
import json
import flask
from flask_socketio import SocketIO, emit
from flask import request
import time
import socket
import sys
import datetime
import logging
# Define Flask Application, and allow automatic reloading of templates for dev
app = flask.Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
app.config['TEMPLATES_AUTO_RELOAD'] = True
app.jinja_env.auto_reload = True
# SocketIO instance
socketio = SocketIO(app)
current_setpoints = {}
current_positions = {}
class ROTCTLD(object):
""" rotctld (hamlib) communication class """
# Note: This is a massive hack.
def __init__(self, hostname, port=4533, poll_rate=5, timeout=5, az_180 = False):
""" Open a connection to rotctld, and test it for validity """
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.settimeout(timeout)
self.hostname = hostname
self.port = port
def connect(self):
""" Connect to rotctld instance """
self.sock.connect((self.hostname,self.port))
model = self.get_model()
if model == None:
# Timeout!
self.close()
raise Exception("Timeout!")
else:
return model
def close(self):
self.sock.close()
def send_command(self, command):
""" Send a command to the connected rotctld instance,
and return the return value.
"""
self.sock.sendall((command+'\n').encode())
try:
recv_msg = self.sock.recv(1024).decode()
except:
recv_msg = None
timestring = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
text = f'{timestring} {command} --> {recv_msg}'
try:
emit('log_event', text, namespace='/update_status')
except RuntimeError:
pass
if recv_msg.startswith('RPRT'):
r, n = recv_msg.split()
num = int(n)
if num < 0:
return None
else:
return num
return recv_msg
def get_model(self):
""" Get the rotator model from rotctld """
model = self.send_command('_')
return model
def set_azel(self,azimuth,elevation):
""" Command rotator to a particular azimuth/elevation """
# Sanity check inputs.
if elevation > 90.0:
elevation = 90.0
elif elevation < 0.0:
elevation = 0.0
if azimuth > 360.0:
azimuth = azimuth % 360.0
command = "P %3.1f %2.1f" % (azimuth,elevation)
response = self.send_command(command)
if "RPRT 0" in response:
return True
else:
return False
def get_azel(self):
""" Poll rotctld for azimuth and elevation """
# Send poll command and read in response.
response = self.send_command('p')
# Attempt to split response by \n (az and el are on separate lines)
try:
lines = response.split('\n')
az = float(lines[0])
el = float(lines[1])
return (az, el)
except:
logging.error("Could not parse position: %s" % response)
return (None,None)
def halt(self):
""" Immediately halt rotator movement, if it support it """
self.send_command('S')
def limit(num, minimum, maximum):
"""Limits input 'num' between minimum and maximum values."""
return max(min(num, maximum), minimum)
# Rotator map.
rotators = {}
# Map over rotator resolutions.
increments = {}
#
# Flask Routes
#
@app.route("/")
def flask_index():
""" Render main index page """
rotator_names = list(rotators)
return flask.render_template('index.html', chosen_rotor_name=rotator_names[0], rotor_increment=increments[rotator_names[0]], rotator_names=rotator_names)
@app.route("/<rotorname>")
def flask_show_rotor(rotorname):
rotator_names = list(rotators)
if rotorname not in rotator_names:
return flask.render_template('404.html')
rotor_increment = increments[rotorname]
return flask.render_template('index.html', chosen_rotor_name=rotorname, rotor_increment=rotor_increment, rotator_names=rotator_names)
#
# SocketIO Handlers
#
@socketio.on('client_connected', namespace='/update_status')
def client_connected(data):
#display current position
read_position(data)
#and setpoint
emit('setpoint_event', current_setpoints[data['rotator_key']])
@socketio.on('update_setpoint', namespace='/update_status')
def update_setpoint(data):
rotator = data['rotator_key']
motor = data['motor']
setpoint = current_setpoints[rotator]
position = current_positions[rotator]
#set new setpoints
if 'delta' in data:
setpoint[motor] = position[motor] + data['delta']
else:
try:
setpoint[motor] = float(data['val'])
except ValueError:
# bogus input, just ignore it and quit
return
#limit azi and ele to 0-360 and 0-180 for setpoint display purposes,
#though rotctld will take care of this automatically
az = setpoint['azimuth'] = setpoint['azimuth'] % 360
el = setpoint['elevation'] = limit(setpoint['elevation'], 0, 180.0)
#set rotctld to current setpoint
rotators[rotator].set_azel(az, el)
#update client display
emit('setpoint_event', setpoint)
@socketio.on('halt_rotator', namespace='/update_status')
def halt_rotator(data):
rotator = data['rotator_key']
rotators[rotator].halt()
#update setpoint to current position
(az, el) = rotators[rotator].get_azel()
current_setpoints[rotator] = {'azimuth': az, 'elevation': el}
emit('setpoint_event', current_setpoints[rotator])
@socketio.on('get_position', namespace='/update_status')
def read_position(data):
rotator = data['rotator_key']
position = current_positions[rotator]
(az, el) = rotators[rotator].get_azel()
if (az == None):
return
else:
# ensure we are updating the dict and not replacing it!
position.update({'azimuth': az, 'elevation': el})
#display current position
emit('position_event', position)
if __name__ == "__main__":
import argparse
from configparser import ConfigParser
parser = argparse.ArgumentParser()
parser.add_argument("-l", "--listen_port",default=5001,help="Port to run Web Server on. (Default: 5001)")
parser.add_argument("--config-file", default='rotors.conf', help="Path to config file specifying rotor ports and names.")
args = parser.parse_args()
# Parse config file.
default_hostname = 'localhost'
config = ConfigParser()
config.read_file(open(args.config_file, 'r'))
# Connect to rotctld instances specified in config file.
for rotor in config.sections():
hostname = config.get(rotor, 'rotctld_host', fallback=default_hostname)
port = int(config.get(rotor, 'rotctld_port'))
name = rotor
#add rotor resolution to rotor increments
DEFAULT_INCREMENT = 1
resolution = float(config.get(rotor, 'resolution', fallback=DEFAULT_INCREMENT))
increments[name] = resolution
#connect to rotctld
rotator = ROTCTLD(hostname=hostname, port=port)
rotator.connect()
rotators[name] = rotator
(az, el) = rotators[name].get_azel()
current_setpoints[name] = {'azimuth': az, 'elevation': el}
current_positions[name] = current_setpoints[name]
# Run the Flask app, which will block until CTRL-C'd.
socketio.run(app, host='0.0.0.0', port=args.listen_port, allow_unsafe_werkzeug=True)
# Close the rotator connection.
for rotator in rotators.values():
rotator.close()