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 ujson as json
|
|
|
|
import structlog
|
2023-11-12 22:22:53 +00:00
|
|
|
from global_instances import HamlibParam, Modem
|
2022-11-05 21:27:33 +00:00
|
|
|
|
|
|
|
log = structlog.get_logger("explorer")
|
|
|
|
|
|
|
|
class explorer():
|
2023-11-12 22:22:53 +00:00
|
|
|
def __init__(self, config, states):
|
|
|
|
self.config = config
|
|
|
|
self.states = states
|
2023-02-02 10:53:52 +00:00
|
|
|
self.explorer_url = "https://api.freedata.app/explorer.php"
|
2022-11-06 16:36:33 +00:00
|
|
|
self.publish_interval = 120
|
2023-11-13 21:35:54 +00:00
|
|
|
self.enable_explorer = config["STATION"]["enable_explorer"]
|
2022-11-05 21:27:33 +00:00
|
|
|
|
2023-11-13 21:35:54 +00:00
|
|
|
if self.enable_explorer:
|
|
|
|
self.interval_thread = threading.Thread(target=self.interval, name="interval", daemon=True)
|
|
|
|
self.interval_thread.start()
|
2022-11-05 21:27:33 +00:00
|
|
|
|
|
|
|
def interval(self):
|
|
|
|
while True:
|
|
|
|
self.push()
|
2022-12-12 11:28:52 +00:00
|
|
|
threading.Event().wait(self.publish_interval)
|
2022-11-05 21:27:33 +00:00
|
|
|
|
|
|
|
def push(self):
|
|
|
|
|
2023-04-27 19:43:56 +00:00
|
|
|
frequency = 0 if HamlibParam.hamlib_frequency is None else HamlibParam.hamlib_frequency
|
2022-11-05 21:27:33 +00:00
|
|
|
band = "USB"
|
2023-11-13 16:40:46 +00:00
|
|
|
callsign = str(self.config['STATION']['mycall'])
|
|
|
|
gridsquare = str(self.config['STATION']['mygrid'])
|
2023-10-20 12:12:20 +00:00
|
|
|
version = str(Modem.version)
|
2023-11-13 16:40:46 +00:00
|
|
|
bandwidth = str(self.config['MODEM']['enable_low_bandwidth_mode'])
|
2023-11-12 22:22:53 +00:00
|
|
|
beacon = str(self.states.is_beacon_running)
|
2023-04-27 19:43:56 +00:00
|
|
|
strength = str(HamlibParam.hamlib_strength)
|
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"}
|
2023-01-30 11:41:58 +00:00
|
|
|
station_data = {'callsign': callsign, 'gridsquare': gridsquare, 'frequency': frequency, 'strength': strength, 'band': band, 'version': version, 'bandwidth': bandwidth, 'beacon': beacon, "lastheard": []}
|
2022-12-26 20:14:23 +00:00
|
|
|
|
2023-10-20 12:12:20 +00:00
|
|
|
for i in Modem.heard_stations:
|
2022-12-27 09:37:34 +00:00
|
|
|
try:
|
|
|
|
callsign = str(i[0], "UTF-8")
|
|
|
|
grid = str(i[1], "UTF-8")
|
2023-01-05 14:28:41 +00:00
|
|
|
timestamp = i[2]
|
2023-04-22 09:02:40 +00:00
|
|
|
frequency = i[6]
|
2022-12-27 10:33:16 +00:00
|
|
|
try:
|
|
|
|
snr = i[4].split("/")[1]
|
|
|
|
except AttributeError:
|
|
|
|
snr = str(i[4])
|
2023-04-22 09:02:40 +00:00
|
|
|
station_data["lastheard"].append({"callsign": callsign, "grid": grid, "snr": snr, "timestamp": timestamp, "frequency": frequency})
|
2022-12-27 09:37:34 +00:00
|
|
|
except Exception as e:
|
|
|
|
log.debug("[EXPLORER] not publishing station", e=e)
|
2022-12-26 20:14:23 +00:00
|
|
|
|
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")
|