FreeDATA/tnc/explorer.py

68 lines
2.3 KiB
Python
Raw Normal View History

2022-11-05 21:27:33 +00:00
# -*- coding: UTF-8 -*-
"""
Created on 05.11.23
@author: DJ2LS
"""
# pylint: disable=invalid-name, line-too-long, c-extension-no-member
# pylint: disable=import-outside-toplevel, attribute-defined-outside-init
import requests
import threading
import time
import ujson as json
import structlog
import static
log = structlog.get_logger("explorer")
class explorer():
def __init__(self):
self.explorer_url = "https://explorer.freedata.app/api.php"
self.publish_interval = 120
2022-11-05 21:27:33 +00:00
self.interval_thread = threading.Thread(target=self.interval, name="interval", daemon=True)
self.interval_thread.start()
def interval(self):
while True:
self.push()
threading.Event().wait(self.publish_interval)
2022-11-05 21:27:33 +00:00
def push(self):
2022-12-26 11:49:01 +00:00
frequency = 0 if static.HAMLIB_FREQUENCY is None else static.HAMLIB_FREQUENCY
2022-11-05 21:27:33 +00:00
band = "USB"
callsign = str(static.MYCALLSIGN, "utf-8")
gridsquare = str(static.MYGRID, "utf-8")
version = str(static.VERSION)
bandwidth = str(static.LOW_BANDWIDTH_MODE)
2022-11-08 08:22:17 +00:00
beacon = str(static.BEACON_STATE)
2022-11-05 21:27:33 +00:00
log.info("[EXPLORER] publish", frequency=frequency, band=band, callsign=callsign, gridsquare=gridsquare, version=version, bandwidth=bandwidth)
headers = {"Content-Type": "application/json"}
station_data = {'callsign': callsign, 'gridsquare': gridsquare, 'frequency': frequency, 'band': band, 'version': version, 'bandwidth': bandwidth, 'beacon': beacon, "lastheard": []}
for i in static.HEARD_STATIONS:
2022-12-27 09:37:34 +00:00
try:
callsign = str(i[0], "UTF-8")
grid = str(i[1], "UTF-8")
2022-12-27 10:33:16 +00:00
try:
snr = i[4].split("/")[1]
except AttributeError:
snr = str(i[4])
2022-12-27 09:37:34 +00:00
station_data["lastheard"].append({"callsign": callsign, "grid": grid, "snr": snr})
except Exception as e:
log.debug("[EXPLORER] not publishing station", e=e)
2022-11-05 21:27:33 +00:00
station_data = json.dumps(station_data)
try:
response = requests.post(self.explorer_url, json=station_data, headers=headers)
# print(response.status_code)
# print(response.content)
except Exception as e:
log.warning("[EXPLORER] connection lost")