aprsd 1.0.0__py3-none-any.whl → 3.4.2__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.
- aprsd/__init__.py +6 -4
- aprsd/cli_helper.py +151 -0
- aprsd/client/__init__.py +13 -0
- aprsd/client/aprsis.py +132 -0
- aprsd/client/base.py +105 -0
- aprsd/client/drivers/__init__.py +0 -0
- aprsd/client/drivers/aprsis.py +228 -0
- aprsd/client/drivers/fake.py +73 -0
- aprsd/client/drivers/kiss.py +119 -0
- aprsd/client/factory.py +88 -0
- aprsd/client/fake.py +48 -0
- aprsd/client/kiss.py +103 -0
- aprsd/client/stats.py +38 -0
- aprsd/cmds/__init__.py +0 -0
- aprsd/cmds/completion.py +22 -0
- aprsd/cmds/dev.py +162 -0
- aprsd/cmds/fetch_stats.py +156 -0
- aprsd/cmds/healthcheck.py +86 -0
- aprsd/cmds/list_plugins.py +319 -0
- aprsd/cmds/listen.py +231 -0
- aprsd/cmds/send_message.py +171 -0
- aprsd/cmds/server.py +137 -0
- aprsd/cmds/webchat.py +674 -0
- aprsd/conf/__init__.py +56 -0
- aprsd/conf/client.py +131 -0
- aprsd/conf/common.py +301 -0
- aprsd/conf/log.py +65 -0
- aprsd/conf/opts.py +80 -0
- aprsd/conf/plugin_common.py +182 -0
- aprsd/conf/plugin_email.py +105 -0
- aprsd/exception.py +13 -0
- aprsd/log/__init__.py +0 -0
- aprsd/log/log.py +138 -0
- aprsd/main.py +104 -867
- aprsd/packets/__init__.py +20 -0
- aprsd/packets/collector.py +79 -0
- aprsd/packets/core.py +823 -0
- aprsd/packets/log.py +161 -0
- aprsd/packets/packet_list.py +110 -0
- aprsd/packets/seen_list.py +49 -0
- aprsd/packets/tracker.py +103 -0
- aprsd/packets/watch_list.py +119 -0
- aprsd/plugin.py +474 -284
- aprsd/plugin_utils.py +86 -0
- aprsd/plugins/__init__.py +0 -0
- aprsd/plugins/email.py +709 -0
- aprsd/plugins/fortune.py +61 -0
- aprsd/plugins/location.py +179 -0
- aprsd/plugins/notify.py +61 -0
- aprsd/plugins/ping.py +31 -0
- aprsd/plugins/time.py +115 -0
- aprsd/plugins/version.py +31 -0
- aprsd/plugins/weather.py +405 -0
- aprsd/stats/__init__.py +20 -0
- aprsd/stats/app.py +49 -0
- aprsd/stats/collector.py +37 -0
- aprsd/threads/__init__.py +11 -0
- aprsd/threads/aprsd.py +119 -0
- aprsd/threads/keep_alive.py +131 -0
- aprsd/threads/log_monitor.py +121 -0
- aprsd/threads/registry.py +56 -0
- aprsd/threads/rx.py +354 -0
- aprsd/threads/stats.py +44 -0
- aprsd/threads/tx.py +255 -0
- aprsd/utils/__init__.py +218 -0
- aprsd/utils/counter.py +51 -0
- aprsd/utils/json.py +80 -0
- aprsd/utils/objectstore.py +123 -0
- aprsd/utils/ring_buffer.py +40 -0
- aprsd/utils/trace.py +180 -0
- aprsd/web/__init__.py +0 -0
- aprsd/web/admin/__init__.py +0 -0
- aprsd/web/admin/static/css/index.css +84 -0
- aprsd/web/admin/static/css/prism.css +4 -0
- aprsd/web/admin/static/css/tabs.css +35 -0
- aprsd/web/admin/static/images/Untitled.png +0 -0
- aprsd/web/admin/static/images/aprs-symbols-16-0.png +0 -0
- aprsd/web/admin/static/images/aprs-symbols-16-1.png +0 -0
- aprsd/web/admin/static/images/aprs-symbols-64-0.png +0 -0
- aprsd/web/admin/static/images/aprs-symbols-64-1.png +0 -0
- aprsd/web/admin/static/images/aprs-symbols-64-2.png +0 -0
- aprsd/web/admin/static/js/charts.js +235 -0
- aprsd/web/admin/static/js/echarts.js +465 -0
- aprsd/web/admin/static/js/logs.js +26 -0
- aprsd/web/admin/static/js/main.js +231 -0
- aprsd/web/admin/static/js/prism.js +12 -0
- aprsd/web/admin/static/js/send-message.js +114 -0
- aprsd/web/admin/static/js/tabs.js +28 -0
- aprsd/web/admin/templates/index.html +196 -0
- aprsd/web/chat/static/css/chat.css +115 -0
- aprsd/web/chat/static/css/index.css +66 -0
- aprsd/web/chat/static/css/style.css.map +1 -0
- aprsd/web/chat/static/css/tabs.css +41 -0
- aprsd/web/chat/static/css/upstream/bootstrap.min.css +6 -0
- aprsd/web/chat/static/css/upstream/font.woff2 +0 -0
- aprsd/web/chat/static/css/upstream/google-fonts.css +23 -0
- aprsd/web/chat/static/css/upstream/jquery-ui.css +1311 -0
- aprsd/web/chat/static/css/upstream/jquery.toast.css +28 -0
- aprsd/web/chat/static/css/upstream/themes/default/assets/fonts/LatoLatin-Bold.woff +0 -0
- aprsd/web/chat/static/css/upstream/themes/default/assets/fonts/LatoLatin-Bold.woff2 +0 -0
- aprsd/web/chat/static/css/upstream/themes/default/assets/fonts/LatoLatin-Regular.woff +0 -0
- aprsd/web/chat/static/css/upstream/themes/default/assets/fonts/LatoLatin-Regular.woff2 +0 -0
- aprsd/web/chat/static/css/upstream/themes/default/assets/fonts/icons.woff +0 -0
- aprsd/web/chat/static/css/upstream/themes/default/assets/fonts/icons.woff2 +0 -0
- aprsd/web/chat/static/css/upstream/themes/default/assets/fonts/outline-icons.woff +0 -0
- aprsd/web/chat/static/css/upstream/themes/default/assets/fonts/outline-icons.woff2 +0 -0
- aprsd/web/chat/static/images/Untitled.png +0 -0
- aprsd/web/chat/static/images/aprs-symbols-16-0.png +0 -0
- aprsd/web/chat/static/images/aprs-symbols-16-1.png +0 -0
- aprsd/web/chat/static/images/aprs-symbols-64-0.png +0 -0
- aprsd/web/chat/static/images/aprs-symbols-64-1.png +0 -0
- aprsd/web/chat/static/images/aprs-symbols-64-2.png +0 -0
- aprsd/web/chat/static/images/globe.svg +3 -0
- aprsd/web/chat/static/js/gps.js +84 -0
- aprsd/web/chat/static/js/main.js +45 -0
- aprsd/web/chat/static/js/send-message.js +585 -0
- aprsd/web/chat/static/js/tabs.js +28 -0
- aprsd/web/chat/static/js/upstream/bootstrap.bundle.min.js +7 -0
- aprsd/web/chat/static/js/upstream/jquery-3.7.1.min.js +2 -0
- aprsd/web/chat/static/js/upstream/jquery-ui.min.js +13 -0
- aprsd/web/chat/static/js/upstream/jquery.toast.js +374 -0
- aprsd/web/chat/static/js/upstream/semantic.min.js +11 -0
- aprsd/web/chat/static/js/upstream/socket.io.min.js +7 -0
- aprsd/web/chat/templates/index.html +139 -0
- aprsd/wsgi.py +315 -0
- aprsd-3.4.2.dist-info/AUTHORS +13 -0
- aprsd-3.4.2.dist-info/LICENSE +175 -0
- aprsd-3.4.2.dist-info/METADATA +793 -0
- aprsd-3.4.2.dist-info/RECORD +133 -0
- {aprsd-1.0.0.dist-info → aprsd-3.4.2.dist-info}/WHEEL +1 -1
- aprsd-3.4.2.dist-info/entry_points.txt +8 -0
- aprsd/fake_aprs.py +0 -83
- aprsd/utils.py +0 -166
- aprsd-1.0.0.dist-info/AUTHORS +0 -6
- aprsd-1.0.0.dist-info/METADATA +0 -181
- aprsd-1.0.0.dist-info/RECORD +0 -13
- aprsd-1.0.0.dist-info/entry_points.txt +0 -4
- aprsd-1.0.0.dist-info/pbr.json +0 -1
- /aprsd/{fuzzyclock.py → utils/fuzzyclock.py} +0 -0
- {aprsd-1.0.0.dist-info → aprsd-3.4.2.dist-info}/top_level.txt +0 -0
aprsd/threads/stats.py
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
import logging
|
2
|
+
import threading
|
3
|
+
import time
|
4
|
+
|
5
|
+
from oslo_config import cfg
|
6
|
+
import wrapt
|
7
|
+
|
8
|
+
from aprsd.stats import collector
|
9
|
+
from aprsd.threads import APRSDThread
|
10
|
+
from aprsd.utils import objectstore
|
11
|
+
|
12
|
+
|
13
|
+
CONF = cfg.CONF
|
14
|
+
LOG = logging.getLogger("APRSD")
|
15
|
+
|
16
|
+
|
17
|
+
class StatsStore(objectstore.ObjectStoreMixin):
|
18
|
+
"""Container to save the stats from the collector."""
|
19
|
+
lock = threading.Lock()
|
20
|
+
data = {}
|
21
|
+
|
22
|
+
@wrapt.synchronized(lock)
|
23
|
+
def add(self, stats: dict):
|
24
|
+
self.data = stats
|
25
|
+
|
26
|
+
|
27
|
+
class APRSDStatsStoreThread(APRSDThread):
|
28
|
+
"""Save APRSD Stats to disk periodically."""
|
29
|
+
|
30
|
+
# how often in seconds to write the file
|
31
|
+
save_interval = 10
|
32
|
+
|
33
|
+
def __init__(self):
|
34
|
+
super().__init__("StatsStore")
|
35
|
+
|
36
|
+
def loop(self):
|
37
|
+
if self.loop_count % self.save_interval == 0:
|
38
|
+
stats = collector.Collector().collect()
|
39
|
+
ss = StatsStore()
|
40
|
+
ss.add(stats)
|
41
|
+
ss.save()
|
42
|
+
|
43
|
+
time.sleep(1)
|
44
|
+
return True
|
aprsd/threads/tx.py
ADDED
@@ -0,0 +1,255 @@
|
|
1
|
+
import logging
|
2
|
+
import threading
|
3
|
+
import time
|
4
|
+
|
5
|
+
from oslo_config import cfg
|
6
|
+
from rush import quota, throttle
|
7
|
+
from rush.contrib import decorator
|
8
|
+
from rush.limiters import periodic
|
9
|
+
from rush.stores import dictionary
|
10
|
+
import wrapt
|
11
|
+
|
12
|
+
from aprsd import conf # noqa
|
13
|
+
from aprsd import threads as aprsd_threads
|
14
|
+
from aprsd.client import client_factory
|
15
|
+
from aprsd.packets import collector, core
|
16
|
+
from aprsd.packets import log as packet_log
|
17
|
+
from aprsd.packets import tracker
|
18
|
+
|
19
|
+
|
20
|
+
CONF = cfg.CONF
|
21
|
+
LOG = logging.getLogger("APRSD")
|
22
|
+
|
23
|
+
msg_t = throttle.Throttle(
|
24
|
+
limiter=periodic.PeriodicLimiter(
|
25
|
+
store=dictionary.DictionaryStore(),
|
26
|
+
),
|
27
|
+
rate=quota.Quota.per_second(
|
28
|
+
count=CONF.msg_rate_limit_period,
|
29
|
+
),
|
30
|
+
)
|
31
|
+
ack_t = throttle.Throttle(
|
32
|
+
limiter=periodic.PeriodicLimiter(
|
33
|
+
store=dictionary.DictionaryStore(),
|
34
|
+
),
|
35
|
+
rate=quota.Quota.per_second(
|
36
|
+
count=CONF.ack_rate_limit_period,
|
37
|
+
),
|
38
|
+
)
|
39
|
+
|
40
|
+
msg_throttle_decorator = decorator.ThrottleDecorator(throttle=msg_t)
|
41
|
+
ack_throttle_decorator = decorator.ThrottleDecorator(throttle=ack_t)
|
42
|
+
s_lock = threading.Lock()
|
43
|
+
|
44
|
+
|
45
|
+
@wrapt.synchronized(s_lock)
|
46
|
+
@msg_throttle_decorator.sleep_and_retry
|
47
|
+
def send(packet: core.Packet, direct=False, aprs_client=None):
|
48
|
+
"""Send a packet either in a thread or directly to the client."""
|
49
|
+
# prepare the packet for sending.
|
50
|
+
# This constructs the packet.raw
|
51
|
+
packet.prepare()
|
52
|
+
# Have to call the collector to track the packet
|
53
|
+
# After prepare, as prepare assigns the msgNo
|
54
|
+
collector.PacketCollector().tx(packet)
|
55
|
+
if isinstance(packet, core.AckPacket):
|
56
|
+
_send_ack(packet, direct=direct, aprs_client=aprs_client)
|
57
|
+
else:
|
58
|
+
_send_packet(packet, direct=direct, aprs_client=aprs_client)
|
59
|
+
|
60
|
+
|
61
|
+
@msg_throttle_decorator.sleep_and_retry
|
62
|
+
def _send_packet(packet: core.Packet, direct=False, aprs_client=None):
|
63
|
+
if not direct:
|
64
|
+
thread = SendPacketThread(packet=packet)
|
65
|
+
thread.start()
|
66
|
+
else:
|
67
|
+
_send_direct(packet, aprs_client=aprs_client)
|
68
|
+
|
69
|
+
|
70
|
+
@ack_throttle_decorator.sleep_and_retry
|
71
|
+
def _send_ack(packet: core.AckPacket, direct=False, aprs_client=None):
|
72
|
+
if not direct:
|
73
|
+
thread = SendAckThread(packet=packet)
|
74
|
+
thread.start()
|
75
|
+
else:
|
76
|
+
_send_direct(packet, aprs_client=aprs_client)
|
77
|
+
|
78
|
+
|
79
|
+
def _send_direct(packet, aprs_client=None):
|
80
|
+
if aprs_client:
|
81
|
+
cl = aprs_client
|
82
|
+
else:
|
83
|
+
cl = client_factory.create()
|
84
|
+
|
85
|
+
packet.update_timestamp()
|
86
|
+
packet_log.log(packet, tx=True)
|
87
|
+
try:
|
88
|
+
cl.send(packet)
|
89
|
+
except Exception as e:
|
90
|
+
LOG.error(f"Failed to send packet: {packet}")
|
91
|
+
LOG.error(e)
|
92
|
+
|
93
|
+
|
94
|
+
class SendPacketThread(aprsd_threads.APRSDThread):
|
95
|
+
loop_count: int = 1
|
96
|
+
|
97
|
+
def __init__(self, packet):
|
98
|
+
self.packet = packet
|
99
|
+
super().__init__(f"TX-{packet.to_call}-{self.packet.msgNo}")
|
100
|
+
|
101
|
+
def loop(self):
|
102
|
+
"""Loop until a message is acked or it gets delayed.
|
103
|
+
|
104
|
+
We only sleep for 5 seconds between each loop run, so
|
105
|
+
that CTRL-C can exit the app in a short period. Each sleep
|
106
|
+
means the app quitting is blocked until sleep is done.
|
107
|
+
So we keep track of the last send attempt and only send if the
|
108
|
+
last send attempt is old enough.
|
109
|
+
|
110
|
+
"""
|
111
|
+
pkt_tracker = tracker.PacketTrack()
|
112
|
+
# lets see if the message is still in the tracking queue
|
113
|
+
packet = pkt_tracker.get(self.packet.msgNo)
|
114
|
+
if not packet:
|
115
|
+
# The message has been removed from the tracking queue
|
116
|
+
# So it got acked and we are done.
|
117
|
+
LOG.info(
|
118
|
+
f"{self.packet.__class__.__name__}"
|
119
|
+
f"({self.packet.msgNo}) "
|
120
|
+
"Message Send Complete via Ack.",
|
121
|
+
)
|
122
|
+
return False
|
123
|
+
else:
|
124
|
+
send_now = False
|
125
|
+
if packet.send_count >= packet.retry_count:
|
126
|
+
# we reached the send limit, don't send again
|
127
|
+
# TODO(hemna) - Need to put this in a delayed queue?
|
128
|
+
LOG.info(
|
129
|
+
f"{packet.__class__.__name__} "
|
130
|
+
f"({packet.msgNo}) "
|
131
|
+
"Message Send Complete. Max attempts reached"
|
132
|
+
f" {packet.retry_count}",
|
133
|
+
)
|
134
|
+
pkt_tracker.remove(packet.msgNo)
|
135
|
+
return False
|
136
|
+
|
137
|
+
# Message is still outstanding and needs to be acked.
|
138
|
+
if packet.last_send_time:
|
139
|
+
# Message has a last send time tracking
|
140
|
+
now = int(round(time.time()))
|
141
|
+
sleeptime = (packet.send_count + 1) * 31
|
142
|
+
delta = now - packet.last_send_time
|
143
|
+
if delta > sleeptime:
|
144
|
+
# It's time to try to send it again
|
145
|
+
send_now = True
|
146
|
+
else:
|
147
|
+
send_now = True
|
148
|
+
|
149
|
+
if send_now:
|
150
|
+
# no attempt time, so lets send it, and start
|
151
|
+
# tracking the time.
|
152
|
+
packet.last_send_time = int(round(time.time()))
|
153
|
+
_send_direct(packet)
|
154
|
+
packet.send_count += 1
|
155
|
+
|
156
|
+
time.sleep(1)
|
157
|
+
# Make sure we get called again.
|
158
|
+
self.loop_count += 1
|
159
|
+
return True
|
160
|
+
|
161
|
+
|
162
|
+
class SendAckThread(aprsd_threads.APRSDThread):
|
163
|
+
loop_count: int = 1
|
164
|
+
max_retries = 3
|
165
|
+
|
166
|
+
def __init__(self, packet):
|
167
|
+
self.packet = packet
|
168
|
+
super().__init__(f"TXAck-{packet.to_call}-{self.packet.msgNo}")
|
169
|
+
self.max_retries = CONF.default_ack_send_count
|
170
|
+
|
171
|
+
def loop(self):
|
172
|
+
"""Separate thread to send acks with retries."""
|
173
|
+
send_now = False
|
174
|
+
if self.packet.send_count == self.max_retries:
|
175
|
+
# we reached the send limit, don't send again
|
176
|
+
# TODO(hemna) - Need to put this in a delayed queue?
|
177
|
+
LOG.debug(
|
178
|
+
f"{self.packet.__class__.__name__}"
|
179
|
+
f"({self.packet.msgNo}) "
|
180
|
+
"Send Complete. Max attempts reached"
|
181
|
+
f" {self.max_retries}",
|
182
|
+
)
|
183
|
+
return False
|
184
|
+
|
185
|
+
if self.packet.last_send_time:
|
186
|
+
# Message has a last send time tracking
|
187
|
+
now = int(round(time.time()))
|
188
|
+
|
189
|
+
# aprs duplicate detection is 30 secs?
|
190
|
+
# (21 only sends first, 28 skips middle)
|
191
|
+
sleep_time = 31
|
192
|
+
delta = now - self.packet.last_send_time
|
193
|
+
if delta > sleep_time:
|
194
|
+
# It's time to try to send it again
|
195
|
+
send_now = True
|
196
|
+
elif self.loop_count % 10 == 0:
|
197
|
+
LOG.debug(f"Still wating. {delta}")
|
198
|
+
else:
|
199
|
+
send_now = True
|
200
|
+
|
201
|
+
if send_now:
|
202
|
+
_send_direct(self.packet)
|
203
|
+
self.packet.send_count += 1
|
204
|
+
self.packet.last_send_time = int(round(time.time()))
|
205
|
+
|
206
|
+
time.sleep(1)
|
207
|
+
self.loop_count += 1
|
208
|
+
return True
|
209
|
+
|
210
|
+
|
211
|
+
class BeaconSendThread(aprsd_threads.APRSDThread):
|
212
|
+
"""Thread that sends a GPS beacon packet periodically.
|
213
|
+
|
214
|
+
Settings are in the [DEFAULT] section of the config file.
|
215
|
+
"""
|
216
|
+
_loop_cnt: int = 1
|
217
|
+
|
218
|
+
def __init__(self):
|
219
|
+
super().__init__("BeaconSendThread")
|
220
|
+
self._loop_cnt = 1
|
221
|
+
# Make sure Latitude and Longitude are set.
|
222
|
+
if not CONF.latitude or not CONF.longitude:
|
223
|
+
LOG.error(
|
224
|
+
"Latitude and Longitude are not set in the config file."
|
225
|
+
"Beacon will not be sent and thread is STOPPED.",
|
226
|
+
)
|
227
|
+
self.stop()
|
228
|
+
LOG.info(
|
229
|
+
"Beacon thread is running and will send "
|
230
|
+
f"beacons every {CONF.beacon_interval} seconds.",
|
231
|
+
)
|
232
|
+
|
233
|
+
def loop(self):
|
234
|
+
# Only dump out the stats every N seconds
|
235
|
+
if self._loop_cnt % CONF.beacon_interval == 0:
|
236
|
+
pkt = core.BeaconPacket(
|
237
|
+
from_call=CONF.callsign,
|
238
|
+
to_call="APRS",
|
239
|
+
latitude=float(CONF.latitude),
|
240
|
+
longitude=float(CONF.longitude),
|
241
|
+
comment="APRSD GPS Beacon",
|
242
|
+
symbol=CONF.beacon_symbol,
|
243
|
+
)
|
244
|
+
try:
|
245
|
+
# Only send it once
|
246
|
+
pkt.retry_count = 1
|
247
|
+
send(pkt, direct=True)
|
248
|
+
except Exception as e:
|
249
|
+
LOG.error(f"Failed to send beacon: {e}")
|
250
|
+
client_factory.create().reset()
|
251
|
+
time.sleep(5)
|
252
|
+
|
253
|
+
self._loop_cnt += 1
|
254
|
+
time.sleep(1)
|
255
|
+
return True
|
aprsd/utils/__init__.py
ADDED
@@ -0,0 +1,218 @@
|
|
1
|
+
"""Utilities and helper functions."""
|
2
|
+
|
3
|
+
import errno
|
4
|
+
import functools
|
5
|
+
import math
|
6
|
+
import os
|
7
|
+
import re
|
8
|
+
import sys
|
9
|
+
import traceback
|
10
|
+
|
11
|
+
import update_checker
|
12
|
+
|
13
|
+
import aprsd
|
14
|
+
|
15
|
+
from .fuzzyclock import fuzzy # noqa: F401
|
16
|
+
# Make these available by anyone importing
|
17
|
+
# aprsd.utils
|
18
|
+
from .ring_buffer import RingBuffer # noqa: F401
|
19
|
+
|
20
|
+
|
21
|
+
if sys.version_info.major == 3 and sys.version_info.minor >= 3:
|
22
|
+
from collections.abc import MutableMapping
|
23
|
+
else:
|
24
|
+
from collections.abc import MutableMapping
|
25
|
+
|
26
|
+
|
27
|
+
def singleton(cls):
|
28
|
+
"""Make a class a Singleton class (only one instance)"""
|
29
|
+
@functools.wraps(cls)
|
30
|
+
def wrapper_singleton(*args, **kwargs):
|
31
|
+
if wrapper_singleton.instance is None:
|
32
|
+
wrapper_singleton.instance = cls(*args, **kwargs)
|
33
|
+
return wrapper_singleton.instance
|
34
|
+
wrapper_singleton.instance = None
|
35
|
+
return wrapper_singleton
|
36
|
+
|
37
|
+
|
38
|
+
def env(*vars, **kwargs):
|
39
|
+
"""This returns the first environment variable set.
|
40
|
+
if none are non-empty, defaults to '' or keyword arg default
|
41
|
+
"""
|
42
|
+
for v in vars:
|
43
|
+
value = os.environ.get(v, None)
|
44
|
+
if value:
|
45
|
+
return value
|
46
|
+
return kwargs.get("default", "")
|
47
|
+
|
48
|
+
|
49
|
+
def mkdir_p(path):
|
50
|
+
"""Make directory and have it work in py2 and py3."""
|
51
|
+
try:
|
52
|
+
os.makedirs(path)
|
53
|
+
except OSError as exc: # Python >= 2.5
|
54
|
+
if exc.errno == errno.EEXIST and os.path.isdir(path):
|
55
|
+
pass
|
56
|
+
else:
|
57
|
+
raise
|
58
|
+
|
59
|
+
|
60
|
+
def insert_str(string, str_to_insert, index):
|
61
|
+
return string[:index] + str_to_insert + string[index:]
|
62
|
+
|
63
|
+
|
64
|
+
def end_substr(original, substr):
|
65
|
+
"""Get the index of the end of the <substr>.
|
66
|
+
|
67
|
+
So you can insert a string after <substr>
|
68
|
+
"""
|
69
|
+
idx = original.find(substr)
|
70
|
+
if idx != -1:
|
71
|
+
idx += len(substr)
|
72
|
+
return idx
|
73
|
+
|
74
|
+
|
75
|
+
def rgb_from_name(name):
|
76
|
+
"""Create an rgb tuple from a string."""
|
77
|
+
hash = 0
|
78
|
+
for char in name:
|
79
|
+
hash = ord(char) + ((hash << 5) - hash)
|
80
|
+
red = hash & 255
|
81
|
+
green = (hash >> 8) & 255
|
82
|
+
blue = (hash >> 16) & 255
|
83
|
+
return red, green, blue
|
84
|
+
|
85
|
+
|
86
|
+
def hextriplet(colortuple):
|
87
|
+
"""Convert a color tuple to a hex triplet."""
|
88
|
+
return "#" + "".join(f"{i:02X}" for i in colortuple)
|
89
|
+
|
90
|
+
|
91
|
+
def hex_from_name(name):
|
92
|
+
"""Create a hex color from a string."""
|
93
|
+
return hextriplet(rgb_from_name(name))
|
94
|
+
|
95
|
+
|
96
|
+
def human_size(bytes, units=None):
|
97
|
+
"""Returns a human readable string representation of bytes"""
|
98
|
+
if not units:
|
99
|
+
units = [" bytes", "KB", "MB", "GB", "TB", "PB", "EB"]
|
100
|
+
return str(bytes) + units[0] if bytes < 1024 else human_size(bytes >> 10, units[1:])
|
101
|
+
|
102
|
+
|
103
|
+
def strfdelta(tdelta, fmt="{hours:{width}}:{minutes:{width}}:{seconds:{width}}"):
|
104
|
+
d = {
|
105
|
+
"days": tdelta.days,
|
106
|
+
"width": "02",
|
107
|
+
}
|
108
|
+
if tdelta.days > 0:
|
109
|
+
fmt = "{days} days " + fmt
|
110
|
+
|
111
|
+
d["hours"], rem = divmod(tdelta.seconds, 3600)
|
112
|
+
d["minutes"], d["seconds"] = divmod(rem, 60)
|
113
|
+
return fmt.format(**d)
|
114
|
+
|
115
|
+
|
116
|
+
def _check_version():
|
117
|
+
# check for a newer version
|
118
|
+
try:
|
119
|
+
check = update_checker.UpdateChecker()
|
120
|
+
result = check.check("aprsd", aprsd.__version__)
|
121
|
+
if result:
|
122
|
+
# Looks like there is an updated version.
|
123
|
+
return 1, result
|
124
|
+
else:
|
125
|
+
return 0, "APRSD is up to date"
|
126
|
+
except Exception:
|
127
|
+
# probably can't get in touch with pypi for some reason
|
128
|
+
# Lets put up an error and move on. We might not
|
129
|
+
# have internet in this aprsd deployment.
|
130
|
+
return 1, "Couldn't check for new version of APRSD"
|
131
|
+
|
132
|
+
|
133
|
+
def flatten_dict(d, parent_key="", sep="."):
|
134
|
+
"""Flatten a dict to key.key.key = value."""
|
135
|
+
items = []
|
136
|
+
for k, v in d.items():
|
137
|
+
new_key = parent_key + sep + k if parent_key else k
|
138
|
+
if isinstance(v, MutableMapping):
|
139
|
+
items.extend(flatten_dict(v, new_key, sep=sep).items())
|
140
|
+
else:
|
141
|
+
items.append((new_key, v))
|
142
|
+
return dict(items)
|
143
|
+
|
144
|
+
|
145
|
+
def parse_delta_str(s):
|
146
|
+
if "day" in s:
|
147
|
+
m = re.match(
|
148
|
+
r"(?P<days>[-\d]+) day[s]*, (?P<hours>\d+):(?P<minutes>\d+):(?P<seconds>\d[\.\d+]*)",
|
149
|
+
s,
|
150
|
+
)
|
151
|
+
else:
|
152
|
+
m = re.match(r"(?P<hours>\d+):(?P<minutes>\d+):(?P<seconds>\d[\.\d+]*)", s)
|
153
|
+
|
154
|
+
if m:
|
155
|
+
return {key: float(val) for key, val in m.groupdict().items()}
|
156
|
+
else:
|
157
|
+
return {}
|
158
|
+
|
159
|
+
|
160
|
+
def load_entry_points(group):
|
161
|
+
"""Load all extensions registered to the given entry point group"""
|
162
|
+
try:
|
163
|
+
import importlib_metadata
|
164
|
+
except ImportError:
|
165
|
+
# For python 3.10 and later
|
166
|
+
import importlib.metadata as importlib_metadata
|
167
|
+
|
168
|
+
eps = importlib_metadata.entry_points(group=group)
|
169
|
+
for ep in eps:
|
170
|
+
try:
|
171
|
+
ep.load()
|
172
|
+
except Exception as e:
|
173
|
+
print(f"Extension {ep.name} of group {group} failed to load with {e}", file=sys.stderr)
|
174
|
+
print(traceback.format_exc(), file=sys.stderr)
|
175
|
+
|
176
|
+
|
177
|
+
def calculate_initial_compass_bearing(start, end):
|
178
|
+
if (type(start) != tuple) or (type(end) != tuple): # noqa: E721
|
179
|
+
raise TypeError("Only tuples are supported as arguments")
|
180
|
+
|
181
|
+
lat1 = math.radians(float(start[0]))
|
182
|
+
lat2 = math.radians(float(end[0]))
|
183
|
+
|
184
|
+
diff_long = math.radians(float(end[1]) - float(start[1]))
|
185
|
+
|
186
|
+
x = math.sin(diff_long) * math.cos(lat2)
|
187
|
+
y = math.cos(lat1) * math.sin(lat2) - (
|
188
|
+
math.sin(lat1)
|
189
|
+
* math.cos(lat2) * math.cos(diff_long)
|
190
|
+
)
|
191
|
+
|
192
|
+
initial_bearing = math.atan2(x, y)
|
193
|
+
|
194
|
+
# Now we have the initial bearing but math.atan2 return values
|
195
|
+
# from -180° to + 180° which is not what we want for a compass bearing
|
196
|
+
# The solution is to normalize the initial bearing as shown below
|
197
|
+
initial_bearing = math.degrees(initial_bearing)
|
198
|
+
compass_bearing = (initial_bearing + 360) % 360
|
199
|
+
|
200
|
+
return compass_bearing
|
201
|
+
|
202
|
+
|
203
|
+
def degrees_to_cardinal(bearing, full_string=False):
|
204
|
+
if full_string:
|
205
|
+
directions = [
|
206
|
+
"North", "North-Northeast", "Northeast", "East-Northeast", "East", "East-Southeast",
|
207
|
+
"Southeast", "South-Southeast", "South", "South-Southwest", "Southwest", "West-Southwest",
|
208
|
+
"West", "West-Northwest", "Northwest", "North-Northwest", "North",
|
209
|
+
]
|
210
|
+
else:
|
211
|
+
directions = [
|
212
|
+
"N", "NNE", "NE", "ENE", "E", "ESE",
|
213
|
+
"SE", "SSE", "S", "SSW", "SW", "WSW",
|
214
|
+
"W", "WNW", "NW", "NNW", "N",
|
215
|
+
]
|
216
|
+
|
217
|
+
cardinal = directions[round(bearing / 22.5)]
|
218
|
+
return cardinal
|
aprsd/utils/counter.py
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
from multiprocessing import RawValue
|
2
|
+
import random
|
3
|
+
import threading
|
4
|
+
|
5
|
+
import wrapt
|
6
|
+
|
7
|
+
|
8
|
+
MAX_PACKET_ID = 9999
|
9
|
+
|
10
|
+
|
11
|
+
class PacketCounter:
|
12
|
+
"""
|
13
|
+
Global Packet id counter class.
|
14
|
+
|
15
|
+
This is a singleton based class that keeps
|
16
|
+
an incrementing counter for all packets to
|
17
|
+
be sent. All new Packet objects gets a new
|
18
|
+
message id, which is the next number available
|
19
|
+
from the PacketCounter.
|
20
|
+
|
21
|
+
"""
|
22
|
+
|
23
|
+
_instance = None
|
24
|
+
lock = threading.Lock()
|
25
|
+
|
26
|
+
def __new__(cls, *args, **kwargs):
|
27
|
+
"""Make this a singleton class."""
|
28
|
+
if cls._instance is None:
|
29
|
+
cls._instance = super().__new__(cls, *args, **kwargs)
|
30
|
+
cls._instance.val = RawValue("i", random.randint(1, MAX_PACKET_ID))
|
31
|
+
return cls._instance
|
32
|
+
|
33
|
+
@wrapt.synchronized(lock)
|
34
|
+
def increment(self):
|
35
|
+
if self.val.value == MAX_PACKET_ID:
|
36
|
+
self.val.value = 1
|
37
|
+
else:
|
38
|
+
self.val.value += 1
|
39
|
+
|
40
|
+
@property
|
41
|
+
@wrapt.synchronized(lock)
|
42
|
+
def value(self):
|
43
|
+
return str(self.val.value)
|
44
|
+
|
45
|
+
@wrapt.synchronized(lock)
|
46
|
+
def __repr__(self):
|
47
|
+
return str(self.val.value)
|
48
|
+
|
49
|
+
@wrapt.synchronized(lock)
|
50
|
+
def __str__(self):
|
51
|
+
return str(self.val.value)
|
aprsd/utils/json.py
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
import datetime
|
2
|
+
import decimal
|
3
|
+
import json
|
4
|
+
import sys
|
5
|
+
|
6
|
+
from aprsd.packets import core
|
7
|
+
|
8
|
+
|
9
|
+
class EnhancedJSONEncoder(json.JSONEncoder):
|
10
|
+
def default(self, obj):
|
11
|
+
if isinstance(obj, datetime.datetime):
|
12
|
+
args = (
|
13
|
+
"year", "month", "day", "hour", "minute",
|
14
|
+
"second", "microsecond",
|
15
|
+
)
|
16
|
+
return {
|
17
|
+
"__type__": "datetime.datetime",
|
18
|
+
"args": [getattr(obj, a) for a in args],
|
19
|
+
}
|
20
|
+
elif isinstance(obj, datetime.date):
|
21
|
+
args = ("year", "month", "day")
|
22
|
+
return {
|
23
|
+
"__type__": "datetime.date",
|
24
|
+
"args": [getattr(obj, a) for a in args],
|
25
|
+
}
|
26
|
+
elif isinstance(obj, datetime.time):
|
27
|
+
args = ("hour", "minute", "second", "microsecond")
|
28
|
+
return {
|
29
|
+
"__type__": "datetime.time",
|
30
|
+
"args": [getattr(obj, a) for a in args],
|
31
|
+
}
|
32
|
+
elif isinstance(obj, datetime.timedelta):
|
33
|
+
args = ("days", "seconds", "microseconds")
|
34
|
+
return {
|
35
|
+
"__type__": "datetime.timedelta",
|
36
|
+
"args": [getattr(obj, a) for a in args],
|
37
|
+
}
|
38
|
+
elif isinstance(obj, decimal.Decimal):
|
39
|
+
return {
|
40
|
+
"__type__": "decimal.Decimal",
|
41
|
+
"args": [str(obj)],
|
42
|
+
}
|
43
|
+
else:
|
44
|
+
return super().default(obj)
|
45
|
+
|
46
|
+
|
47
|
+
class SimpleJSONEncoder(json.JSONEncoder):
|
48
|
+
def default(self, obj):
|
49
|
+
if isinstance(obj, datetime.datetime):
|
50
|
+
return obj.isoformat()
|
51
|
+
elif isinstance(obj, datetime.date):
|
52
|
+
return str(obj)
|
53
|
+
elif isinstance(obj, datetime.time):
|
54
|
+
return str(obj)
|
55
|
+
elif isinstance(obj, datetime.timedelta):
|
56
|
+
return str(obj)
|
57
|
+
elif isinstance(obj, decimal.Decimal):
|
58
|
+
return str(obj)
|
59
|
+
elif isinstance(obj, core.Packet):
|
60
|
+
return obj.to_dict()
|
61
|
+
else:
|
62
|
+
return super().default(obj)
|
63
|
+
|
64
|
+
|
65
|
+
class EnhancedJSONDecoder(json.JSONDecoder):
|
66
|
+
|
67
|
+
def __init__(self, *args, **kwargs):
|
68
|
+
super().__init__(
|
69
|
+
*args, object_hook=self.object_hook,
|
70
|
+
**kwargs,
|
71
|
+
)
|
72
|
+
|
73
|
+
def object_hook(self, d):
|
74
|
+
if "__type__" not in d:
|
75
|
+
return d
|
76
|
+
o = sys.modules[__name__]
|
77
|
+
for e in d["__type__"].split("."):
|
78
|
+
o = getattr(o, e)
|
79
|
+
args, kwargs = d.get("args", ()), d.get("kwargs", {})
|
80
|
+
return o(*args, **kwargs)
|