-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaprs_client.py
111 lines (90 loc) · 4.14 KB
/
aprs_client.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
"""
fogos-2-aprs
Version: 2.1
Date: 2024-07-31
Description: Manages APRS connection and packet creation.
"""
import logging
import aprslib
from config import APRS_CALLSIGN, APRS_PASSWORD, APRS_SERVERS
from symbol_definitions import get_symbol
from datetime import datetime
class APRSClient:
def __init__(self):
pass
def connect(self):
for server in APRS_SERVERS:
try:
self.client = aprslib.IS(APRS_CALLSIGN, passwd=APRS_PASSWORD)
self.connected = False
self.client.connect(server['host'], server['port'])
self.connected = True
logging.info(f"Connected to {server}")
return True
except Exception as e:
logging.error(f"Failed to connect to {server}: {e}")
return False
def send_packet(self, object_id, lat, lon, estado, operacionais, mterrestres, maero):
if not self.connected:
logging.error("Not connected to APRS server.")
return
# Ensure the object ID is exactly 9 characters
object_name = f"{object_id:<9}"[:9]
# Format the latitude and longitude in degrees and decimal minutes
lat_str = self.format_coordinates(lat, True)
lon_str = self.format_coordinates(lon, False)
# Generate a timestamp in the format DDHHMMz
timestamp = datetime.utcnow().strftime("%d%H%Mz")
# Get the symbol table and symbol from the status
symbol_table, symbol = get_symbol(estado)
# Construct the APRS Object packet using the raw format
packet = f"{APRS_CALLSIGN}>APFOGO,TCPIP*:;{object_name}*{timestamp}{lat_str}{symbol_table}{lon_str}{symbol}Estado - {estado}, {operacionais} Operacionais, {mterrestres} Meios Terrestres e {maero} Meios Aéreos"
# Output a preview of the APRS packet
print(f"APRS Packet Preview: {packet}")
logging.info(f"Sending packet: {packet}")
try:
# Use the correct method to send the packet
self.client.sendall(packet)
logging.info(f"Packet sent: {packet}")
except Exception as e:
logging.error(f"Failed to send packet: {e}")
def format_coordinates(self, coord, is_latitude):
"""
Converts a coordinate to APRS format in degrees and decimal minutes.
coord: The coordinate to format.
is_latitude: Boolean indicating if the coordinate is latitude.
"""
degrees = int(abs(coord))
minutes = (abs(coord) - degrees) * 60
if is_latitude:
suffix = 'N' if coord >= 0 else 'S'
return f"{degrees:02d}{minutes:05.2f}{suffix}"
else:
suffix = 'E' if coord >= 0 else 'W'
return f"{degrees:03d}{minutes:05.2f}{suffix}"
def send_positionreport(self):
if not self.connected:
logging.error("Not connected to APRS server.")
return
positionreport = f"{APRS_CALLSIGN}>APFOGO,TCPIP*:!3843.23N/00914.18WoANEPC - Autoridade Nacional de Emergência e Proteção Civil"
# Output a preview of the APRS Position Report packet
print(f"APRS Position Report packet Preview: {positionreport}")
logging.info(f"Sending Position Report: {positionreport}")
try:
self.client.sendall(positionreport)
logging.info(f"Position Report sent: {positionreport}")
except Exception as e:
logging.error(f"Failed to send Position Report: {e}")
def send_statusreport(self):
if not self.connected:
logging.error("Not connected to APRS server.")
return
statusreport = f"{APRS_CALLSIGN}>APFOGO,TCPIP*,ANEPC:> ► fogos-2-aprs | Powered by F4VSE/CT7AJM"
# Output a preview of the APRS Status Report packet
print(f"APRS Position Report packet Preview: {statusreport}")
logging.info(f"Sending Status: {statusreport}")
try:
self.client.sendall(statusreport)
logging.info(f"Status sent: {statusreport}")
except Exception as e:
logging.error(f"Failed to send Status: {e}")