-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaprsUtilities.py
320 lines (264 loc) · 19 KB
/
aprsUtilities.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/python
#==============================================================================================================#
# #
# aprsUtilies - Python library of utilities for use with posting spots to APRS #
# #
# Copyright (C) 2019 Graham Collins ve3gtc #
# #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or #
# (at your option) any later version. #
# #
# http://www.gnu.org/licenses/gpl-3.0.html #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License along #
# with this program; if not, write to the Free Software Foundation, Inc., #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #
# #
#==============================================================================================================#
#
# if not already installled, use pip to install the following
#
# pip install beautifulsoup
# pip install bs4
# pip install lxml
# pip install pandas
# pip install requests
#
#==============================================================================================================#
import sys
import traceback
import string
from socket import *
import re #regex
import datetime
import time
import requests
import json
#==============================================================================================================#
# #
# aprslib - Python library for working with APRS #
# Copyright (C) 2013-2014 Rossen Georgiev #
# #
# https://github.com/rossengeorgiev/aprs-python/blob/master/aprslib/passcode.py #
# #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License along #
# with this program; if not, write to the Free Software Foundation, Inc., #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #
# #
#==============================================================================================================#
"""
Contains a function for generating passcode from callsign
"""
def passCode(pcStrCallsign):
"""
Takes a CALLSIGN and returns passcode
"""
assert isinstance(pcStrCallsign, str)
pcStrCallsign = pcStrCallsign.split('-')[0].upper()
code = 0x73e2
for i, char in enumerate(pcStrCallsign):
code ^= ord(char) << (8 if not i % 2 else 0)
return code & 0x7fff
#==============================================================================================================#
# #
# - AprsSendPacket(aspStrCallSign, aspStrAprsMessage) #
# #
# - aspStrCallSign is the callsign of WHO is posting the spot, NOT the callsign of the ballaoon #
# - the callsign of balloon is in aspStrAprsMessage #
# #
# - sends data to aprs.fi #
# #
# - http://www.aprs-is.net #
# #
#==============================================================================================================#
"""
function to upload a data packet to aprs.fi netowrk
"""
#def AprsSendPacket(aspStrCallSign, apsStrAprsMessage):
def AprsSendPacket(aspStrUserCallsign, aspStrBalloonCAllSign, aspStrLatitude, aspStrLongitude, aspStrIcon, aspStrData):
serverHost = 'second.aprs.net'
serverPort = 10152
#==============================================================================================================#
# #
# Very simple RegEx to determine is CallSign string is a valid amateur call sign: #
# #
# Determines if the callsign conforms to amateur radio international rules (#A#, #AA#, # A#, A##, or AA#). #
# #
# Callsign length must be between 3 and 6 characters. #
# #
# @return true if callsign is compliant #
# #
# only continue to upload to APRS if provided callsign is compliant #
# #
#==============================================================================================================#
if ( re.search('^(?:(?:[1-9][A-Z][A-Z]?)|(?:[A-Z][2-9A-Z]?))[0-9][A-Z]{1,3}$', aspStrUserCallsign) ):
# Pass - compliant callsign, make log entry
with open("log.txt", "a") as log_file:
log_file.write(time.ctime() + ' : ' + aspStrUserCallsign + ' is a compliant callsign \n' )
intPassCode = passCode(aspStrUserCallsign)
# create socket & connect to server
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverHost, serverPort))
# Aprs log in 'user CallSign pass ' + intPassCode + ' vers "aprsBallloon 0.1" \n'
strAprsLogIn = 'user ' + aspStrUserCallsign + ' pass ' + str(intPassCode) + ' vers "aprsBalloon 0.1" \n'
clientSocket.sendto(strAprsLogIn.encode(),(serverHost, serverPort))
# AprsSendPacket(aspStrUserCallsign, aspStrBalloonCAllSign, aspStrLatitude, aspStrLongitude, aspStrIcon, aspStrData):
aspStrAprsMessage = aspStrBalloonCAllSign + '>APRS,TCIP*:=' + aspStrLatitude + '/' + aspStrLongitude + aspStrIcon + aspStrData + '\n'
# log packet to be sent
with open("log.txt", "a") as log_file:
log_file.write(time.ctime() + ' : ' + aspStrAprsMessage )
# send aspStrAprsMessage
clientSocket.sendto(aspStrAprsMessage.encode(),(serverHost, serverPort))
# close socket -- must be closed to avoidbuffer overflow
time.sleep(5) # 5 sec. delay
clientSocket.shutdown(0)
clientSocket.close()
# log packet sent event
with open("log.txt", "a") as log_file:
log_file.write(time.ctime() + " : Packet sent at: "+ time.ctime() + '\n' )
return None
#==============================================================================================================#
# #
# latLongToGrid(Latitude, Longitude) #
# #
# - converts decimal latitude and Longitude to six character Maidenhead Grid Square #
# #
# - returns Gridsquare as string #
# #
# - using convention of North + South - West - and East + #
# #
#==============================================================================================================#
def latLongToGrid(decLatitude, decLongitude):
upper = 'ABCDEFGHIJKLMNOPQRSTUVWX'
lower = 'abcdefghijklmnopqrstuvwx'
llgLatitude = decLatitude + 90
llgLongitude = decLongitude + 180
gridSquare = upper[int(llgLongitude/20)]
gridSquare = "%s%s" % (gridSquare, upper[int(llgLatitude/10)])
gridSquare = "%s%s" % (gridSquare, str(int((llgLongitude/2)%10)))
gridSquare = "%s%s" % (gridSquare, str(int(llgLatitude%10)))
gridSquare = "%s%s" % (gridSquare, lower[int(((llgLongitude - int(llgLongitude)) * 60)/5)])
gridSquare = "%s%s" % (gridSquare, lower[int(((llgLatitude - int(llgLatitude)) * 60)/2.5)])
return gridSquare
#==============================================================================================================#
# #
# gridToLatLong(GridSquare) #
# #
# - converts 4, or 6, charcater Maidenhead Grid Square to decimal Latitude and Longitude #
# #
# - returns Latitude/Longitude as string #
# #
# - using convention of North + South - West - and East + #
# #
#==============================================================================================================#
def gridToLatLong(gllGridSquare):
if not isValidGridSquare(gllGridSquare):
return "errorInvalidGrid"
gllGridSquare = gllGridSquare.upper()
gllLongitude = -180 + ( ((ord(gllGridSquare[0]) - ord('A') ) * 20) + (int(gllGridSquare[2]) * 2 ) )
gllLatitude = -90 + ( ((ord(gllGridSquare[1]) - ord('A') ) * 10) + (int(gllGridSquare[3]) ) )
if len(gllGridSquare) > 4 :
gllLongitude = gllLongitude + ( float( (ord(gllGridSquare[4]) - ord('A') ) * 5) / 60 )
gllLatitude = gllLatitude + ( float( (ord(gllGridSquare[5]) - ord('A') ) * 2.5) / 60 )
if len(gllGridSquare) == 8 :
gllLongitude = gllLongitude + ( ( float(int(gllGridSquare[6])) * .5 ) / 60 )
gllLatitude = gllLatitude + ( ( float(int(gllGridSquare[7])) * .25 ) / 60 )
return "%s %s" % (gllLatitude,gllLongitude)
#==============================================================================================================#
# #
# isValidGridSquare(GridSqure) #
# #
# - determines if GridSquare is a valid 4, 6, or 8 charcater Maidenhead Grid Square #
# #
# - returns TRUE or FALSE #
# #
# - Grid Square pattern is one of 4, 6 or 8 characters and numbers in one of the following #
# patterns: #
# [A-R][A-R][0-9][0-9] #
# [A-R][A-R][0-9][0-9][a-x][a-x] #
# [A-R][A-R][0-9][0-9][a-x][a-x][0-9][0-9] #
# #
# - convention is that the first pair of letters is upper case and the second set of #
# letters is lower case but in reality Maidenhead Grid Squares are case insensitive #
# #
#==============================================================================================================#
def isValidGridSquare(GridSquare):
GridSquare = GridSquare.upper()
if len(GridSquare) == 4 :
if re.match( '^[A-R][A-R][0-9][0-9]$', GridSquare):
return True
else:
return False
elif len(GridSquare) == 6 :
if re.match( '^[A-R][A-R][0-9][0-9][A-X][A-X]$', GridSquare):
return True
else:
return False
elif len(GridSquare) == 8 :
if re.match( '^[A-R][A-R][0-9][0-9][A-X][A-X][0-9][0-9]$', GridSquare):
return True
else:
return False
else:
return False
#==============================================================================================================#
# #
# toDegreesMinutes(value,type) #
# #
# - converts decimal degress to degrees minutes #
# #
#==============================================================================================================#
def toDegreesMinutes(value,type):
"""
Converts a DD.dddd value into DDMM.mm Notation.
Pass value as double
type = {Latitude or Longitude} as string
returns a string as DDMM.mmDirection
modified from http://anothergisblog.blogspot.ca/2011/11/convert-decimal-degree-to-degrees.html
"""
try:
value = float(value)
except:
#print '\nERROR: Could not convert %s to float.' %(type(value))
with open("log.txt", "a") as log_file:
log_file.write(time.ctime() + ' : could not convert ' + str(value) + ' to float\n')
return 0
degrees = int(value)
minutes = abs( (value - int(value) ) * 60.0)
quadrant = ""
if type == "Longitude":
if degrees <= 0:
quadrant = "W"
elif degrees > 0:
quadrant = "E"
else:
quadrant = ""
elif type == "Latitude":
if degrees < 0:
quadrant = "S"
elif degrees >= 0:
quadrant = "N"
else:
quadrant = ""
if type == "Longitude":
degreesMinutes = str('{:0>3d}'.format(abs(degrees))) + str('{:05.2f}'.format(minutes)) + "" + quadrant
elif type == "Latitude":
degreesMinutes = str('{:0>2d}'.format(abs(degrees))) + str('{:05.2f}'.format(minutes)) + "" + quadrant
return degreesMinutes