-
Notifications
You must be signed in to change notification settings - Fork 24
/
log_playback.py
242 lines (194 loc) · 7.78 KB
/
log_playback.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
#!/usr/bin/env python
#
# ChaseMapper - Log File Playback
#
# Copyright (C) 2019 Mark Jessop <[email protected]>
# Released under GNU GPL v3 or later
#
#
# TODO:
# [x] Playback of basic log entries (car position, balloon telemetry, bearings)
# [ ] Playback and display of balloon prediction data
# [ ] Skip forward / back through file
#
import json
import socket
import sys
import time
import datetime
import traceback
from dateutil.parser import *
def send_bearing(json_data, udp_port=55672, hostname='<broadcast>'):
"""
Grab bearing data out of a json log entry and send it via UDP.
Example bearing line:
{"bearing": 112.0,
"confidence": 47.24745556875042,
"power": 25.694795608520508,
"raw_bearing_angles": [0.0, 1.0, ... 359.0, 360.0],
"raw_doa": [-4.722, -4.719, ... -4.724, -4.722],
"source": "kerberos-sdr",
"bearing_type": "relative",
"log_time": "2019-08-19T11:21:51.714657+00:00",
"type": "BEARING",
"log_type": "BEARING"}
"""
# Also get bearings of form:
# {"type": "BEARING", "bearing_type": "absolute", "source": "EasyBearing", "latitude": -34.9016115,
#"longitude": 138.58986819999998, "bearing": 0, "log_type": "BEARING", "log_time": "2021-12-10T07:33:14.156227+00:00"}
packet = json_data
packet['replay_time'] = json_data['log_time']
if 'kerberos' in json_data['source']:
# Log data from the kerberos has been flipped in bearing already. Need to make sure this isn't done twice.
packet['source'] = 'replay'
# Set up our UDP socket
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.settimeout(1)
# Set up socket for broadcast, and allow re-use of the address
s.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST,1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
except:
pass
s.bind(('',udp_port))
try:
s.sendto(json.dumps(packet).encode('ascii'), (hostname, udp_port))
except socket.error as e:
s.sendto(json.dumps(packet).encode('ascii'), ('127.0.0.1', udp_port))
if hostname != '<broadcast>':
s.sendto(json.dumps(packet).encode('ascii'), ('127.0.0.1', udp_port))
def send_car_position(json_data, udp_port=55672):
"""
Grab car position data from a json log entry and emit it
{"comment": "CAR",
"log_time": "2019-08-19T11:21:52.303204+00:00",
"lon": 138.68833,
"log_type": "CAR POSITION",
"time": "2019-08-19T11:21:52.300687+00:00",
"lat": -34.71413666666667,
"alt": 69.3,
"speed": 17.118923473141397,
"heading": 27.53170956683383}
"""
packet = {
'type' : 'GPS',
'latitude' : json_data['lat'],
'longitude' : json_data['lon'],
'altitude': json_data['alt'],
'speed': json_data['speed'],
'valid': True,
'replay_time': json_data['log_time']
}
if 'heading' in json_data:
packet['heading'] = json_data['heading']
# Set up our UDP socket
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.settimeout(1)
# Set up socket for broadcast, and allow re-use of the address
s.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST,1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
except:
pass
s.bind(('',udp_port))
try:
s.sendto(json.dumps(packet).encode('ascii'), ('<broadcast>', udp_port))
except socket.error:
s.sendto(json.dumps(packet).encode('ascii'), ('127.0.0.1', udp_port))
def send_balloon_telemetry(json_data, udp_port=55672):
""" Grab balloon telemetry data from a JSON log entry and emit it
{"sats": -1,
"log_time": "2019-08-21T11:02:25.596045+00:00",
"temp": -1,
"lon": 138.000000,
"callsign": "HORUS",
"time": "2019-08-21T11:02:16+00:00",
"lat": -34.000000,
"alt": 100,
"log_type": "BALLOON TELEMETRY"}
"""
packet = {
'type' : 'PAYLOAD_SUMMARY',
'latitude' : json_data['lat'],
'longitude' : json_data['lon'],
'altitude': json_data['alt'],
'callsign': json_data['callsign'],
'time': parse(json_data['time']).strftime("%H:%M:%S"),
'comment': "Log Playback",
'replay_time': json_data['log_time']
}
# Set up our UDP socket
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.settimeout(1)
# Set up socket for broadcast, and allow re-use of the address
s.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST,1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
except:
pass
s.bind(('',udp_port))
try:
s.sendto(json.dumps(packet).encode('ascii'), ('<broadcast>', udp_port))
except socket.error:
s.sendto(json.dumps(packet).encode('ascii'), ('127.0.0.1', udp_port))
def playback_json(filename, udp_port=55672, speed=1.0, start_time = 0, hostname='<broadcast>'):
""" Read in a JSON log file and play it back in real-time, or with a speed factor """
with open(filename, 'r') as _log_file:
try:
_first_line = _log_file.readline()
_log_data = json.loads(_first_line)
_previous_time = parse(_log_data['log_time'])
_first_time = _previous_time
except Exception as e:
print("First line of file must be a valid log entry - %s" % str(e))
return
for _line in _log_file:
try:
_log_data = json.loads(_line)
_new_time = parse(_log_data['log_time'])
_time_delta = (_new_time - _previous_time).total_seconds()
_previous_time = _new_time
# Running timer
_run_time = (_new_time - _first_time).total_seconds()
if _run_time < start_time:
continue
_time_min = int(_run_time)//60
_time_sec = _run_time%60.0
if (_time_delta < 100):
time.sleep(_time_delta/speed)
if _log_data['log_type'] == 'CAR POSITION':
send_car_position(_log_data, udp_port)
print("%s - %02d:%.2f - Car Position" % (_log_data['log_time'], _time_min, _time_sec))
elif _log_data['log_type'] == 'BEARING':
send_bearing(_log_data, udp_port, hostname=hostname)
print("%s - %02d:%.2f - Bearing Data" % (_log_data['log_time'], _time_min, _time_sec))
elif _log_data['log_type'] == 'BALLOON TELEMETRY':
send_balloon_telemetry(_log_data, udp_port)
print("%02d:%.2f - Balloon Telemetry (%s)" % (_time_min, _time_sec, _log_data['callsign']))
elif _log_data['log_type'] == 'PREDICTION':
print("%02d:%.2f - Prediction (Not re-played)" % (_time_min, _time_sec))
else:
print("%02d:%.2f - Unknown: %s" % (_time_min, _time_sec, _log_data['log_type']))
except Exception as e:
print("Invalid log entry: %s" % str(e))
if __name__ == '__main__':
filename = ""
speed = 1.0
start_time = 0
hostname = 'localhost'
udp_port = 55672
if len(sys.argv) == 2:
filename = sys.argv[1]
elif len(sys.argv) == 3:
filename = sys.argv[1]
speed = float(sys.argv[2])
elif len(sys.argv) == 4:
filename = sys.argv[1]
speed = float(sys.argv[2])
start_time = float(sys.argv[3])*60
else:
print("USAGE: python log_playback.py filename.log <speed_multiplier> <start_time>")
playback_json(filename, udp_port, speed, start_time, hostname=hostname)