aprsd 1.0.0__py3-none-any.whl → 3.4.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (141) hide show
  1. aprsd/__init__.py +6 -4
  2. aprsd/cli_helper.py +151 -0
  3. aprsd/client/__init__.py +13 -0
  4. aprsd/client/aprsis.py +132 -0
  5. aprsd/client/base.py +105 -0
  6. aprsd/client/drivers/__init__.py +0 -0
  7. aprsd/client/drivers/aprsis.py +224 -0
  8. aprsd/client/drivers/fake.py +73 -0
  9. aprsd/client/drivers/kiss.py +119 -0
  10. aprsd/client/factory.py +88 -0
  11. aprsd/client/fake.py +48 -0
  12. aprsd/client/kiss.py +103 -0
  13. aprsd/client/stats.py +38 -0
  14. aprsd/cmds/__init__.py +0 -0
  15. aprsd/cmds/completion.py +22 -0
  16. aprsd/cmds/dev.py +162 -0
  17. aprsd/cmds/fetch_stats.py +156 -0
  18. aprsd/cmds/healthcheck.py +86 -0
  19. aprsd/cmds/list_plugins.py +319 -0
  20. aprsd/cmds/listen.py +230 -0
  21. aprsd/cmds/send_message.py +174 -0
  22. aprsd/cmds/server.py +142 -0
  23. aprsd/cmds/webchat.py +681 -0
  24. aprsd/conf/__init__.py +56 -0
  25. aprsd/conf/client.py +131 -0
  26. aprsd/conf/common.py +302 -0
  27. aprsd/conf/log.py +65 -0
  28. aprsd/conf/opts.py +80 -0
  29. aprsd/conf/plugin_common.py +191 -0
  30. aprsd/conf/plugin_email.py +105 -0
  31. aprsd/exception.py +13 -0
  32. aprsd/log/__init__.py +0 -0
  33. aprsd/log/log.py +138 -0
  34. aprsd/main.py +104 -867
  35. aprsd/messaging.py +4 -0
  36. aprsd/packets/__init__.py +12 -0
  37. aprsd/packets/collector.py +56 -0
  38. aprsd/packets/core.py +823 -0
  39. aprsd/packets/log.py +143 -0
  40. aprsd/packets/packet_list.py +116 -0
  41. aprsd/packets/seen_list.py +54 -0
  42. aprsd/packets/tracker.py +109 -0
  43. aprsd/packets/watch_list.py +122 -0
  44. aprsd/plugin.py +475 -284
  45. aprsd/plugin_utils.py +86 -0
  46. aprsd/plugins/__init__.py +0 -0
  47. aprsd/plugins/email.py +709 -0
  48. aprsd/plugins/fortune.py +61 -0
  49. aprsd/plugins/location.py +179 -0
  50. aprsd/plugins/notify.py +61 -0
  51. aprsd/plugins/ping.py +31 -0
  52. aprsd/plugins/time.py +115 -0
  53. aprsd/plugins/version.py +31 -0
  54. aprsd/plugins/weather.py +405 -0
  55. aprsd/stats/__init__.py +20 -0
  56. aprsd/stats/app.py +49 -0
  57. aprsd/stats/collector.py +38 -0
  58. aprsd/threads/__init__.py +11 -0
  59. aprsd/threads/aprsd.py +119 -0
  60. aprsd/threads/keep_alive.py +124 -0
  61. aprsd/threads/log_monitor.py +121 -0
  62. aprsd/threads/registry.py +56 -0
  63. aprsd/threads/rx.py +354 -0
  64. aprsd/threads/stats.py +44 -0
  65. aprsd/threads/tx.py +255 -0
  66. aprsd/utils/__init__.py +163 -0
  67. aprsd/utils/counter.py +51 -0
  68. aprsd/utils/json.py +80 -0
  69. aprsd/utils/objectstore.py +123 -0
  70. aprsd/utils/ring_buffer.py +40 -0
  71. aprsd/utils/trace.py +180 -0
  72. aprsd/web/__init__.py +0 -0
  73. aprsd/web/admin/__init__.py +0 -0
  74. aprsd/web/admin/static/css/index.css +84 -0
  75. aprsd/web/admin/static/css/prism.css +4 -0
  76. aprsd/web/admin/static/css/tabs.css +35 -0
  77. aprsd/web/admin/static/images/Untitled.png +0 -0
  78. aprsd/web/admin/static/images/aprs-symbols-16-0.png +0 -0
  79. aprsd/web/admin/static/images/aprs-symbols-16-1.png +0 -0
  80. aprsd/web/admin/static/images/aprs-symbols-64-0.png +0 -0
  81. aprsd/web/admin/static/images/aprs-symbols-64-1.png +0 -0
  82. aprsd/web/admin/static/images/aprs-symbols-64-2.png +0 -0
  83. aprsd/web/admin/static/js/charts.js +235 -0
  84. aprsd/web/admin/static/js/echarts.js +465 -0
  85. aprsd/web/admin/static/js/logs.js +26 -0
  86. aprsd/web/admin/static/js/main.js +231 -0
  87. aprsd/web/admin/static/js/prism.js +12 -0
  88. aprsd/web/admin/static/js/send-message.js +114 -0
  89. aprsd/web/admin/static/js/tabs.js +28 -0
  90. aprsd/web/admin/templates/index.html +196 -0
  91. aprsd/web/chat/static/css/chat.css +115 -0
  92. aprsd/web/chat/static/css/index.css +66 -0
  93. aprsd/web/chat/static/css/style.css.map +1 -0
  94. aprsd/web/chat/static/css/tabs.css +41 -0
  95. aprsd/web/chat/static/css/upstream/bootstrap.min.css +6 -0
  96. aprsd/web/chat/static/css/upstream/font.woff2 +0 -0
  97. aprsd/web/chat/static/css/upstream/google-fonts.css +23 -0
  98. aprsd/web/chat/static/css/upstream/jquery-ui.css +1311 -0
  99. aprsd/web/chat/static/css/upstream/jquery.toast.css +28 -0
  100. aprsd/web/chat/static/css/upstream/themes/default/assets/fonts/LatoLatin-Bold.woff +0 -0
  101. aprsd/web/chat/static/css/upstream/themes/default/assets/fonts/LatoLatin-Bold.woff2 +0 -0
  102. aprsd/web/chat/static/css/upstream/themes/default/assets/fonts/LatoLatin-Regular.woff +0 -0
  103. aprsd/web/chat/static/css/upstream/themes/default/assets/fonts/LatoLatin-Regular.woff2 +0 -0
  104. aprsd/web/chat/static/css/upstream/themes/default/assets/fonts/icons.woff +0 -0
  105. aprsd/web/chat/static/css/upstream/themes/default/assets/fonts/icons.woff2 +0 -0
  106. aprsd/web/chat/static/css/upstream/themes/default/assets/fonts/outline-icons.woff +0 -0
  107. aprsd/web/chat/static/css/upstream/themes/default/assets/fonts/outline-icons.woff2 +0 -0
  108. aprsd/web/chat/static/images/Untitled.png +0 -0
  109. aprsd/web/chat/static/images/aprs-symbols-16-0.png +0 -0
  110. aprsd/web/chat/static/images/aprs-symbols-16-1.png +0 -0
  111. aprsd/web/chat/static/images/aprs-symbols-64-0.png +0 -0
  112. aprsd/web/chat/static/images/aprs-symbols-64-1.png +0 -0
  113. aprsd/web/chat/static/images/aprs-symbols-64-2.png +0 -0
  114. aprsd/web/chat/static/images/globe.svg +3 -0
  115. aprsd/web/chat/static/js/gps.js +84 -0
  116. aprsd/web/chat/static/js/main.js +45 -0
  117. aprsd/web/chat/static/js/send-message.js +585 -0
  118. aprsd/web/chat/static/js/tabs.js +28 -0
  119. aprsd/web/chat/static/js/upstream/bootstrap.bundle.min.js +7 -0
  120. aprsd/web/chat/static/js/upstream/jquery-3.7.1.min.js +2 -0
  121. aprsd/web/chat/static/js/upstream/jquery-ui.min.js +13 -0
  122. aprsd/web/chat/static/js/upstream/jquery.toast.js +374 -0
  123. aprsd/web/chat/static/js/upstream/semantic.min.js +11 -0
  124. aprsd/web/chat/static/js/upstream/socket.io.min.js +7 -0
  125. aprsd/web/chat/templates/index.html +139 -0
  126. aprsd/wsgi.py +315 -0
  127. aprsd-3.4.1.dist-info/AUTHORS +13 -0
  128. aprsd-3.4.1.dist-info/LICENSE +175 -0
  129. aprsd-3.4.1.dist-info/METADATA +799 -0
  130. aprsd-3.4.1.dist-info/RECORD +134 -0
  131. {aprsd-1.0.0.dist-info → aprsd-3.4.1.dist-info}/WHEEL +1 -1
  132. aprsd-3.4.1.dist-info/entry_points.txt +8 -0
  133. aprsd/fake_aprs.py +0 -83
  134. aprsd/utils.py +0 -166
  135. aprsd-1.0.0.dist-info/AUTHORS +0 -6
  136. aprsd-1.0.0.dist-info/METADATA +0 -181
  137. aprsd-1.0.0.dist-info/RECORD +0 -13
  138. aprsd-1.0.0.dist-info/entry_points.txt +0 -4
  139. aprsd-1.0.0.dist-info/pbr.json +0 -1
  140. /aprsd/{fuzzyclock.py → utils/fuzzyclock.py} +0 -0
  141. {aprsd-1.0.0.dist-info → aprsd-3.4.1.dist-info}/top_level.txt +0 -0
aprsd/cmds/webchat.py ADDED
@@ -0,0 +1,681 @@
1
+ import datetime
2
+ import json
3
+ import logging
4
+ import math
5
+ import signal
6
+ import sys
7
+ import threading
8
+ import time
9
+
10
+ import click
11
+ import flask
12
+ from flask import request
13
+ from flask_httpauth import HTTPBasicAuth
14
+ from flask_socketio import Namespace, SocketIO
15
+ from geopy.distance import geodesic
16
+ from oslo_config import cfg
17
+ from werkzeug.security import check_password_hash, generate_password_hash
18
+ import wrapt
19
+
20
+ import aprsd
21
+ from aprsd import (
22
+ cli_helper, client, packets, plugin_utils, stats, threads, utils,
23
+ )
24
+ from aprsd.client import client_factory, kiss
25
+ from aprsd.main import cli
26
+ from aprsd.threads import aprsd as aprsd_threads
27
+ from aprsd.threads import keep_alive, rx, tx
28
+ from aprsd.utils import trace
29
+
30
+
31
+ CONF = cfg.CONF
32
+ LOG = logging.getLogger()
33
+ auth = HTTPBasicAuth()
34
+ users = {}
35
+ socketio = None
36
+
37
+ # List of callsigns that we don't want to track/fetch their location
38
+ callsign_no_track = [
39
+ "REPEAT", "WB4BOR-11", "APDW16", "WXNOW", "WXBOT", "BLN0", "BLN1", "BLN2",
40
+ "BLN3", "BLN4", "BLN5", "BLN6", "BLN7", "BLN8", "BLN9",
41
+ ]
42
+
43
+ # Callsign location information
44
+ # callsign: {lat: 0.0, long: 0.0, last_update: datetime}
45
+ callsign_locations = {}
46
+
47
+ flask_app = flask.Flask(
48
+ "aprsd",
49
+ static_url_path="/static",
50
+ static_folder="web/chat/static",
51
+ template_folder="web/chat/templates",
52
+ )
53
+
54
+
55
+ def signal_handler(sig, frame):
56
+
57
+ click.echo("signal_handler: called")
58
+ LOG.info(
59
+ f"Ctrl+C, Sending all threads({len(threads.APRSDThreadList())}) exit! "
60
+ f"Can take up to 10 seconds {datetime.datetime.now()}",
61
+ )
62
+ threads.APRSDThreadList().stop_all()
63
+ if "subprocess" not in str(frame):
64
+ time.sleep(1.5)
65
+ # packets.WatchList().save()
66
+ # packets.SeenList().save()
67
+ LOG.info(stats.stats_collector.collect())
68
+ LOG.info("Telling flask to bail.")
69
+ signal.signal(signal.SIGTERM, sys.exit(0))
70
+
71
+
72
+ class SentMessages:
73
+
74
+ _instance = None
75
+ lock = threading.Lock()
76
+
77
+ data = {}
78
+
79
+ def __new__(cls, *args, **kwargs):
80
+ """This magic turns this into a singleton."""
81
+ if cls._instance is None:
82
+ cls._instance = super().__new__(cls)
83
+ return cls._instance
84
+
85
+ def is_initialized(self):
86
+ return True
87
+
88
+ @wrapt.synchronized(lock)
89
+ def add(self, msg):
90
+ self.data[msg.msgNo] = msg.__dict__
91
+
92
+ @wrapt.synchronized(lock)
93
+ def __len__(self):
94
+ return len(self.data.keys())
95
+
96
+ @wrapt.synchronized(lock)
97
+ def get(self, id):
98
+ if id in self.data:
99
+ return self.data[id]
100
+
101
+ @wrapt.synchronized(lock)
102
+ def get_all(self):
103
+ return self.data
104
+
105
+ @wrapt.synchronized(lock)
106
+ def set_status(self, id, status):
107
+ if id in self.data:
108
+ self.data[id]["last_update"] = str(datetime.datetime.now())
109
+ self.data[id]["status"] = status
110
+
111
+ @wrapt.synchronized(lock)
112
+ def ack(self, id):
113
+ """The message got an ack!"""
114
+ if id in self.data:
115
+ self.data[id]["last_update"] = str(datetime.datetime.now())
116
+ self.data[id]["ack"] = True
117
+
118
+ @wrapt.synchronized(lock)
119
+ def reply(self, id, packet):
120
+ """We got a packet back from the sent message."""
121
+ if id in self.data:
122
+ self.data[id]["reply"] = packet
123
+
124
+
125
+ # HTTPBasicAuth doesn't work on a class method.
126
+ # This has to be out here. Rely on the APRSDFlask
127
+ # class to initialize the users from the config
128
+ @auth.verify_password
129
+ def verify_password(username, password):
130
+ global users
131
+
132
+ if username in users and check_password_hash(users[username], password):
133
+ return username
134
+
135
+
136
+ def calculate_initial_compass_bearing(point_a, point_b):
137
+ """
138
+ Calculates the bearing between two points.
139
+ The formulae used is the following:
140
+ θ = atan2(sin(Δlong).cos(lat2),
141
+ cos(lat1).sin(lat2) − sin(lat1).cos(lat2).cos(Δlong))
142
+ :Parameters:
143
+ - `pointA: The tuple representing the latitude/longitude for the
144
+ first point. Latitude and longitude must be in decimal degrees
145
+ - `pointB: The tuple representing the latitude/longitude for the
146
+ second point. Latitude and longitude must be in decimal degrees
147
+ :Returns:
148
+ The bearing in degrees
149
+ :Returns Type:
150
+ float
151
+ """
152
+ if (type(point_a) is not tuple) or (type(point_b) is not tuple):
153
+ raise TypeError("Only tuples are supported as arguments")
154
+
155
+ lat1 = math.radians(point_a[0])
156
+ lat2 = math.radians(point_b[0])
157
+
158
+ diff_long = math.radians(point_b[1] - point_a[1])
159
+
160
+ x = math.sin(diff_long) * math.cos(lat2)
161
+ y = math.cos(lat1) * math.sin(lat2) - (
162
+ math.sin(lat1)
163
+ * math.cos(lat2) * math.cos(diff_long)
164
+ )
165
+
166
+ initial_bearing = math.atan2(x, y)
167
+
168
+ # Now we have the initial bearing but math.atan2 return values
169
+ # from -180° to + 180° which is not what we want for a compass bearing
170
+ # The solution is to normalize the initial bearing as shown below
171
+ initial_bearing = math.degrees(initial_bearing)
172
+ compass_bearing = (initial_bearing + 360) % 360
173
+
174
+ return compass_bearing
175
+
176
+
177
+ def _build_location_from_repeat(message):
178
+ # This is a location message Format is
179
+ # ^ld^callsign:latitude,longitude,altitude,course,speed,timestamp
180
+ a = message.split(":")
181
+ LOG.warning(a)
182
+ if len(a) == 2:
183
+ callsign = a[0].replace("^ld^", "")
184
+ b = a[1].split(",")
185
+ LOG.warning(b)
186
+ if len(b) == 6:
187
+ lat = float(b[0])
188
+ lon = float(b[1])
189
+ alt = float(b[2])
190
+ course = float(b[3])
191
+ speed = float(b[4])
192
+ time = int(b[5])
193
+ data = {
194
+ "callsign": callsign,
195
+ "lat": lat,
196
+ "lon": lon,
197
+ "altitude": alt,
198
+ "course": course,
199
+ "speed": speed,
200
+ "lasttime": time,
201
+ }
202
+ LOG.warning(f"Location data from REPEAT {data}")
203
+ return data
204
+
205
+
206
+ def _calculate_location_data(location_data):
207
+ """Calculate all of the location data from data from aprs.fi or REPEAT."""
208
+ lat = location_data["lat"]
209
+ lon = location_data["lon"]
210
+ alt = location_data["altitude"]
211
+ speed = location_data["speed"]
212
+ lasttime = location_data["lasttime"]
213
+ # now calculate distance from our own location
214
+ distance = 0
215
+ if CONF.webchat.latitude and CONF.webchat.longitude:
216
+ our_lat = float(CONF.webchat.latitude)
217
+ our_lon = float(CONF.webchat.longitude)
218
+ distance = geodesic((our_lat, our_lon), (lat, lon)).kilometers
219
+ bearing = calculate_initial_compass_bearing(
220
+ (our_lat, our_lon),
221
+ (lat, lon),
222
+ )
223
+ return {
224
+ "callsign": location_data["callsign"],
225
+ "lat": lat,
226
+ "lon": lon,
227
+ "altitude": alt,
228
+ "course": f"{bearing:0.1f}",
229
+ "speed": speed,
230
+ "lasttime": lasttime,
231
+ "distance": f"{distance:0.3f}",
232
+ }
233
+
234
+
235
+ def send_location_data_to_browser(location_data):
236
+ global socketio
237
+ callsign = location_data["callsign"]
238
+ LOG.info(f"Got location for {callsign} {callsign_locations[callsign]}")
239
+ socketio.emit(
240
+ "callsign_location", callsign_locations[callsign],
241
+ namespace="/sendmsg",
242
+ )
243
+
244
+
245
+ def populate_callsign_location(callsign, data=None):
246
+ """Populate the location for the callsign.
247
+
248
+ if data is passed in, then we have the location already from
249
+ an APRS packet. If data is None, then we need to fetch the
250
+ location from aprs.fi or REPEAT.
251
+ """
252
+ global socketio
253
+ """Fetch the location for the callsign."""
254
+ LOG.debug(f"populate_callsign_location {callsign}")
255
+ if data:
256
+ location_data = _calculate_location_data(data)
257
+ callsign_locations[callsign] = location_data
258
+ send_location_data_to_browser(location_data)
259
+ return
260
+
261
+ # First we are going to try to get the location from aprs.fi
262
+ # if there is no internets, then this will fail and we will
263
+ # fallback to calling REPEAT for the location for the callsign.
264
+ fallback = False
265
+ if not CONF.aprs_fi.apiKey:
266
+ LOG.warning(
267
+ "Config aprs_fi.apiKey is not set. Can't get location from aprs.fi "
268
+ " falling back to sending REPEAT to get location.",
269
+ )
270
+ fallback = True
271
+ else:
272
+ try:
273
+ aprs_data = plugin_utils.get_aprs_fi(CONF.aprs_fi.apiKey, callsign)
274
+ if not len(aprs_data["entries"]):
275
+ LOG.error("Didn't get any entries from aprs.fi")
276
+ return
277
+ lat = float(aprs_data["entries"][0]["lat"])
278
+ lon = float(aprs_data["entries"][0]["lng"])
279
+ try: # altitude not always provided
280
+ alt = float(aprs_data["entries"][0]["altitude"])
281
+ except Exception:
282
+ alt = 0
283
+ location_data = {
284
+ "callsign": callsign,
285
+ "lat": lat,
286
+ "lon": lon,
287
+ "altitude": alt,
288
+ "lasttime": int(aprs_data["entries"][0]["lasttime"]),
289
+ "course": float(aprs_data["entries"][0].get("course", 0)),
290
+ "speed": float(aprs_data["entries"][0].get("speed", 0)),
291
+ }
292
+ location_data = _calculate_location_data(location_data)
293
+ callsign_locations[callsign] = location_data
294
+ send_location_data_to_browser(location_data)
295
+ return
296
+ except Exception as ex:
297
+ LOG.error(f"Failed to fetch aprs.fi '{ex}'")
298
+ LOG.error(ex)
299
+ fallback = True
300
+
301
+ if fallback:
302
+ # We don't have the location data
303
+ # and we can't get it from aprs.fi
304
+ # Send a special message to REPEAT to get the location data
305
+ LOG.info(f"Sending REPEAT to get location for callsign {callsign}.")
306
+ tx.send(
307
+ packets.MessagePacket(
308
+ from_call=CONF.callsign,
309
+ to_call="REPEAT",
310
+ message_text=f"ld {callsign}",
311
+ ),
312
+ )
313
+
314
+
315
+ class WebChatProcessPacketThread(rx.APRSDProcessPacketThread):
316
+ """Class that handles packets being sent to us."""
317
+
318
+ def __init__(self, packet_queue, socketio):
319
+ self.socketio = socketio
320
+ self.connected = False
321
+ super().__init__(packet_queue)
322
+
323
+ def process_ack_packet(self, packet: packets.AckPacket):
324
+ super().process_ack_packet(packet)
325
+ ack_num = packet.get("msgNo")
326
+ SentMessages().ack(ack_num)
327
+ msg = SentMessages().get(ack_num)
328
+ if msg:
329
+ self.socketio.emit(
330
+ "ack", msg,
331
+ namespace="/sendmsg",
332
+ )
333
+ self.got_ack = True
334
+
335
+ def process_our_message_packet(self, packet: packets.MessagePacket):
336
+ global callsign_locations
337
+ # ok lets see if we have the location for the
338
+ # person we just sent a message to.
339
+ from_call = packet.get("from_call").upper()
340
+ if from_call == "REPEAT":
341
+ # We got a message from REPEAT. Is this a location message?
342
+ message = packet.get("message_text")
343
+ if message.startswith("^ld^"):
344
+ location_data = _build_location_from_repeat(message)
345
+ callsign = location_data["callsign"]
346
+ location_data = _calculate_location_data(location_data)
347
+ callsign_locations[callsign] = location_data
348
+ send_location_data_to_browser(location_data)
349
+ return
350
+ elif (
351
+ from_call not in callsign_locations
352
+ and from_call not in callsign_no_track
353
+ ):
354
+ # We have to ask aprs for the location for the callsign
355
+ # We send a message packet to wb4bor-11 asking for location.
356
+ populate_callsign_location(from_call)
357
+ # Send the packet to the browser.
358
+ self.socketio.emit(
359
+ "new", packet.__dict__,
360
+ namespace="/sendmsg",
361
+ )
362
+
363
+
364
+ class LocationProcessingThread(aprsd_threads.APRSDThread):
365
+ """Class to handle the location processing."""
366
+ def __init__(self):
367
+ super().__init__("LocationProcessingThread")
368
+
369
+ def loop(self):
370
+ pass
371
+
372
+
373
+ def set_config():
374
+ global users
375
+
376
+
377
+ def _get_transport(stats):
378
+ if CONF.aprs_network.enabled:
379
+ transport = "aprs-is"
380
+ aprs_connection = (
381
+ "APRS-IS Server: <a href='http://status.aprs2.net' >"
382
+ "{}</a>".format(stats["APRSClientStats"]["server_string"])
383
+ )
384
+ elif kiss.KISSClient.is_enabled():
385
+ transport = kiss.KISSClient.transport()
386
+ if transport == client.TRANSPORT_TCPKISS:
387
+ aprs_connection = (
388
+ "TCPKISS://{}:{}".format(
389
+ CONF.kiss_tcp.host,
390
+ CONF.kiss_tcp.port,
391
+ )
392
+ )
393
+ elif transport == client.TRANSPORT_SERIALKISS:
394
+ # for pep8 violation
395
+ aprs_connection = (
396
+ "SerialKISS://{}@{} baud".format(
397
+ CONF.kiss_serial.device,
398
+ CONF.kiss_serial.baudrate,
399
+ ),
400
+ )
401
+ elif CONF.fake_client.enabled:
402
+ transport = client.TRANSPORT_FAKE
403
+ aprs_connection = "Fake Client"
404
+
405
+ return transport, aprs_connection
406
+
407
+
408
+ @flask_app.route("/location/<callsign>", methods=["POST"])
409
+ def location(callsign):
410
+ LOG.debug(f"Fetch location for callsign {callsign}")
411
+ populate_callsign_location(callsign)
412
+
413
+
414
+ @auth.login_required
415
+ @flask_app.route("/")
416
+ def index():
417
+ stats = _stats()
418
+
419
+ # For development
420
+ html_template = "index.html"
421
+ LOG.debug(f"Template {html_template}")
422
+
423
+ transport, aprs_connection = _get_transport(stats["stats"])
424
+ LOG.debug(f"transport {transport} aprs_connection {aprs_connection}")
425
+
426
+ stats["transport"] = transport
427
+ stats["aprs_connection"] = aprs_connection
428
+ LOG.debug(f"initial stats = {stats}")
429
+ latitude = CONF.webchat.latitude
430
+ if latitude:
431
+ latitude = float(CONF.webchat.latitude)
432
+
433
+ longitude = CONF.webchat.longitude
434
+ if longitude:
435
+ longitude = float(longitude)
436
+
437
+ return flask.render_template(
438
+ html_template,
439
+ initial_stats=stats,
440
+ aprs_connection=aprs_connection,
441
+ callsign=CONF.callsign,
442
+ version=aprsd.__version__,
443
+ latitude=latitude,
444
+ longitude=longitude,
445
+ )
446
+
447
+
448
+ @auth.login_required
449
+ @flask_app.route("/send-message-status")
450
+ def send_message_status():
451
+ LOG.debug(request)
452
+ msgs = SentMessages()
453
+ info = msgs.get_all()
454
+ return json.dumps(info)
455
+
456
+
457
+ def _stats():
458
+ now = datetime.datetime.now()
459
+
460
+ time_format = "%m-%d-%Y %H:%M:%S"
461
+ stats_dict = stats.stats_collector.collect(serializable=True)
462
+ # Webchat doesnt need these
463
+ if "WatchList" in stats_dict:
464
+ del stats_dict["WatchList"]
465
+ if "SeenList" in stats_dict:
466
+ del stats_dict["SeenList"]
467
+ if "APRSDThreadList" in stats_dict:
468
+ del stats_dict["APRSDThreadList"]
469
+ if "PacketList" in stats_dict:
470
+ del stats_dict["PacketList"]
471
+ if "EmailStats" in stats_dict:
472
+ del stats_dict["EmailStats"]
473
+ if "PluginManager" in stats_dict:
474
+ del stats_dict["PluginManager"]
475
+
476
+ result = {
477
+ "time": now.strftime(time_format),
478
+ "stats": stats_dict,
479
+ }
480
+ return result
481
+
482
+
483
+ @flask_app.route("/stats")
484
+ def get_stats():
485
+ return json.dumps(_stats())
486
+
487
+
488
+ class SendMessageNamespace(Namespace):
489
+ """Class to handle the socketio interactions."""
490
+ got_ack = False
491
+ reply_sent = False
492
+ msg = None
493
+ request = None
494
+
495
+ def __init__(self, namespace=None, config=None):
496
+ super().__init__(namespace)
497
+
498
+ def on_connect(self):
499
+ global socketio
500
+ LOG.debug("Web socket connected")
501
+ socketio.emit(
502
+ "connected", {"data": "/sendmsg Connected"},
503
+ namespace="/sendmsg",
504
+ )
505
+
506
+ def on_disconnect(self):
507
+ LOG.debug("WS Disconnected")
508
+
509
+ def on_send(self, data):
510
+ global socketio
511
+ LOG.debug(f"WS: on_send {data}")
512
+ self.request = data
513
+ data["from"] = CONF.callsign
514
+ path = data.get("path", None)
515
+ if not path:
516
+ path = []
517
+ elif "," in path:
518
+ path_opts = path.split(",")
519
+ path = [x.strip() for x in path_opts]
520
+ else:
521
+ path = [path]
522
+
523
+ pkt = packets.MessagePacket(
524
+ from_call=data["from"],
525
+ to_call=data["to"].upper(),
526
+ message_text=data["message"],
527
+ path=path,
528
+ )
529
+ pkt.prepare()
530
+ self.msg = pkt
531
+ msgs = SentMessages()
532
+ msgs.add(pkt)
533
+ tx.send(pkt)
534
+ msgs.set_status(pkt.msgNo, "Sending")
535
+ obj = msgs.get(pkt.msgNo)
536
+ socketio.emit(
537
+ "sent", obj,
538
+ namespace="/sendmsg",
539
+ )
540
+
541
+ def on_gps(self, data):
542
+ LOG.debug(f"WS on_GPS: {data}")
543
+ lat = data["latitude"]
544
+ long = data["longitude"]
545
+ LOG.debug(f"Lat {lat}")
546
+ LOG.debug(f"Long {long}")
547
+ path = data.get("path", None)
548
+ if not path:
549
+ path = []
550
+ elif "," in path:
551
+ path_opts = path.split(",")
552
+ path = [x.strip() for x in path_opts]
553
+ else:
554
+ path = [path]
555
+
556
+ tx.send(
557
+ packets.BeaconPacket(
558
+ from_call=CONF.callsign,
559
+ to_call="APDW16",
560
+ latitude=lat,
561
+ longitude=long,
562
+ comment="APRSD WebChat Beacon",
563
+ path=path,
564
+ ),
565
+ direct=True,
566
+ )
567
+
568
+ def handle_message(self, data):
569
+ LOG.debug(f"WS Data {data}")
570
+
571
+ def handle_json(self, data):
572
+ LOG.debug(f"WS json {data}")
573
+
574
+ def on_get_callsign_location(self, data):
575
+ LOG.debug(f"on_callsign_location {data}")
576
+ populate_callsign_location(data["callsign"])
577
+
578
+
579
+ @trace.trace
580
+ def init_flask(loglevel, quiet):
581
+ global socketio, flask_app
582
+
583
+ socketio = SocketIO(
584
+ flask_app, logger=False, engineio_logger=False,
585
+ async_mode="threading",
586
+ )
587
+
588
+ socketio.on_namespace(
589
+ SendMessageNamespace(
590
+ "/sendmsg",
591
+ ),
592
+ )
593
+ return socketio
594
+
595
+
596
+ # main() ###
597
+ @cli.command()
598
+ @cli_helper.add_options(cli_helper.common_options)
599
+ @click.option(
600
+ "-f",
601
+ "--flush",
602
+ "flush",
603
+ is_flag=True,
604
+ show_default=True,
605
+ default=False,
606
+ help="Flush out all old aged messages on disk.",
607
+ )
608
+ @click.option(
609
+ "-p",
610
+ "--port",
611
+ "port",
612
+ show_default=True,
613
+ default=None,
614
+ help="Port to listen to web requests. This overrides the config.webchat.web_port setting.",
615
+ )
616
+ @click.pass_context
617
+ @cli_helper.process_standard_options
618
+ def webchat(ctx, flush, port):
619
+ """Web based HAM Radio chat program!"""
620
+ loglevel = ctx.obj["loglevel"]
621
+ quiet = ctx.obj["quiet"]
622
+
623
+ signal.signal(signal.SIGINT, signal_handler)
624
+ signal.signal(signal.SIGTERM, signal_handler)
625
+
626
+ level, msg = utils._check_version()
627
+ if level:
628
+ LOG.warning(msg)
629
+ else:
630
+ LOG.info(msg)
631
+ LOG.info(f"APRSD Started version: {aprsd.__version__}")
632
+
633
+ CONF.log_opt_values(logging.getLogger(), logging.DEBUG)
634
+ user = CONF.admin.user
635
+ users[user] = generate_password_hash(CONF.admin.password)
636
+ if not port:
637
+ port = CONF.webchat.web_port
638
+
639
+ # Initialize the client factory and create
640
+ # The correct client object ready for use
641
+ # Make sure we have 1 client transport enabled
642
+ if not client_factory.is_client_enabled():
643
+ LOG.error("No Clients are enabled in config.")
644
+ sys.exit(-1)
645
+
646
+ if not client_factory.is_client_configured():
647
+ LOG.error("APRS client is not properly configured in config file.")
648
+ sys.exit(-1)
649
+
650
+ packets.PacketList()
651
+ packets.PacketTrack()
652
+ packets.WatchList()
653
+ packets.SeenList()
654
+
655
+ keepalive = keep_alive.KeepAliveThread()
656
+ LOG.info("Start KeepAliveThread")
657
+ keepalive.start()
658
+
659
+ socketio = init_flask(loglevel, quiet)
660
+ rx_thread = rx.APRSDPluginRXThread(
661
+ packet_queue=threads.packet_queue,
662
+ )
663
+ rx_thread.start()
664
+ process_thread = WebChatProcessPacketThread(
665
+ packet_queue=threads.packet_queue,
666
+ socketio=socketio,
667
+ )
668
+ process_thread.start()
669
+
670
+ LOG.info("Start socketio.run()")
671
+ socketio.run(
672
+ flask_app,
673
+ # This is broken for now after removing cryptography
674
+ # and pyopenssl
675
+ # ssl_context="adhoc",
676
+ host=CONF.webchat.web_ip,
677
+ port=port,
678
+ allow_unsafe_werkzeug=True,
679
+ )
680
+
681
+ LOG.info("WebChat exiting!!!! Bye.")