-
Notifications
You must be signed in to change notification settings - Fork 3
/
getZachtek.py
174 lines (149 loc) · 8.53 KB
/
getZachtek.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
#!/usr/bin/python
#==============================================================================================================#
# #
# getZachtec #
# #
# Copyright (C) 2023 Mike Pate - K5MAP #
# #
# 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. #
# #
#==============================================================================================================#
#
# if not already installled, use pip to install the following
#
# pip install urllib3
# pip install pprint
#
#==============================================================================================================#
# Resources
# Github for Zachtek tracker: https://github.com/HarrydeBug/WSPR-transmitters/
#==============================================================================================================#
import logging
import traceback
import urllib.request, urllib.error
import json
#import time
#import datetime
from socket import *
import pprint
from miscFunctions import *
from constants import __version__, SOFTWARE_NAME
#--------------------------------------------------------------------------------------------------------------#
def matchZachtekRecords(aDateTime: list[dict], index: int) -> int:
"""
Determine if 1st record has a matching 2nd record
: param aDateTime: list, index: integer
: return: integer
"""
# add 2 minutes to found record datetime0
sDateTime2 = adjDateTime(aDateTime[index]['time'])
record_found = False
for x in range(index+1, len(aDateTime)):
if aDateTime[x]['time'] == sDateTime2:
# found 2nd record to process
record_found = True
break
if record_found:
return x
else:
return -1
#--------------------------------------------------------------------------------------------------------------#
def getZachtek(wCallsign: str, uCallsign: str, bCallsign: str, timeslot: str, last_date: str, strComment: str) -> tuple[int, list[dict], str]:
"""
Function to retrieve WSPR records, create data structure and then upload to APRS-IS or SondeHub
: param wCallsign: string, uCallsign: string, bCallsign: string, timeslot: string, last_date: string (YYYY-MM-DD HH:MM:SS). strComment: string
: return: integer, list, string
"""
logging.info("#" + ("-"*130))
logging.info(" Function getZachtek start" )
query = "SELECT * FROM rx WHERE tx_sign='" + wCallsign + "' AND time > '" + last_date + "' ORDER BY time"
logging.info(" SQL query = " + query )
url = "https://db1.wspr.live/?query=" + urllib.parse.quote_plus(query + " FORMAT JSON")
# download contents from wspr.live
try:
contents = urllib.request.urlopen(url).read()
except urllib.error.URLError as erru:
logging.critical(f" URL error - {erru.reason}" )
return -1, None
except urllib.error.HTTPError as errh:
logging.critical(f" HTTP error - {errh}" )
return -1, None
except socket.timeout as errt:
logging.critical(f" Connection timeout - {errt}" )
return -1, None
except:
logging.critical(f" Unexpected error calling URL - {traceback.format_exc()}" )
return -1, None
# check on how many rows returned
jWsprData = json.loads(contents.decode("UTF-8"))["data"]
record_count = len(jWsprData)
logging.info(f" WSPR Live records downloaded = {record_count}" )
if record_count < 2:
logging.warning(" Exit function, insufficient WSPR records to process" )
return 0, None, None
#pprint.pp(jWsprData)
aDateTime = []
aMatch = []
# build array of 'time' from WSPR data (i.e., 2023-07-23 06:36:00)
for i in range(len(jWsprData)):
try:
aDateTime.index(jWsprData[i]['time'])
except ValueError:
rCode = matchZachtekRecords(jWsprData, i)
if rCode != -1:
aDateTime.append(jWsprData[i]['time'])
aMatch.append(i)
aMatch.append(rCode)
logging.info(f" Found 1st record (index={i}) to process = {jWsprData[i]['time']}")
logging.info(f" Found 2nd record (index={rCode}) to process = {jWsprData[rCode]['time']}")
logging.info(f" Number of unique records identified: {len(aMatch)}")
logging.debug(f" aMatch = {aMatch}")
logging.debug(f" wDateTime = {aDateTime}")
if len(aMatch) < 2:
logging.warning(" No new WSPR records matched to process")
return 0, None, None
logging.info(f" New lDateTime = {jWsprData[aMatch[len(aMatch)-1]]['time']}")
# found at least 1 set matched, assemble json for upload
jUploadData = []
for i in range(0, len(aMatch), 2):
x = aMatch[i] # Index to 1st record
y = aMatch[i+1] # Index to 2nd record
# calc lat/lon from grid
pos = GridtoLatLon(jWsprData[y]['tx_loc']) # use Grid from 2nd packet
lat = round(pos[0],3)
lon = round(pos[1],3)
# calc altitude from power
# reference https://www.zachtek.com/post/a-new-wspr-transmitter-for-balloons -- table near the bottom
# reference https://github.com/HarrydeBug/WSPR-transmitters/blob/master/Standard%20Firmware/Release/Hardware_Version_2_ESP8285/WSPR-TX2.05/WSPR-TX2.05.ino line 1430
pwr1 = int(jWsprData[x]['power']) * 300
pwr2 = int(jWsprData[y]['power']) * 20
#altitude = (int(jWsprData[x]['power']) + int(jWsprData[y]['power'])) * 300
altitude = pwr1 + pwr2
logging.debug(f" alt1/power = {jWsprData[x]['power']}, alt2/power = {jWsprData[y]['power']}, altitude = {altitude}m")
logging.info(f" DateTime: {jWsprData[y]['time']}, Grid: {jWsprData[y]['tx_loc']}, Lat: {lat}, Lon: {lon}, Alt: {altitude}m {round(altitude*3.28084)}ft, x-y {x} {y}")
# reformat time from WSPR format to Zulu
datetime1 = reformatDateTime(jWsprData[y]['time'], 0)
datetime2 = reformatDateTime(jWsprData[y]['time'], 10)
# assemble json for upload to SondeHub
JSON = {"software_name" : SOFTWARE_NAME, "software_version" : __version__, "uploader_callsign" : uCallsign, "time_received" : datetime1,
"payload_callsign" : bCallsign, "datetime" : datetime2, "lat" : lat, "lon" : lon, "alt" : altitude, "grid" : jWsprData[y]['tx_loc'], "comment" : strComment}
jUploadData.append(JSON)
logging.debug(f" jUploadData records = {len(jUploadData)}")
print("----------------------------------------")
#pprint.pp(jUploadData[i], indent=2)
l = len(aMatch)
pprint.pp(jWsprData[aMatch[l-2]], indent=2)
pprint.pp(jWsprData[aMatch[l-1]], indent=2)
return 1, jUploadData, jWsprData[y]['time']