2020-12-30 20:37:25 +00:00
|
|
|
from owrx.modes import Modes
|
2021-02-13 00:29:21 +00:00
|
|
|
from datetime import datetime, timezone
|
2019-07-14 17:32:48 +00:00
|
|
|
import json
|
2021-02-13 00:29:21 +00:00
|
|
|
import os
|
2019-07-14 17:32:48 +00:00
|
|
|
|
2019-07-19 21:15:10 +00:00
|
|
|
import logging
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-07-19 21:15:10 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2019-07-14 17:32:48 +00:00
|
|
|
|
|
|
|
class Band(object):
|
|
|
|
def __init__(self, dict):
|
|
|
|
self.name = dict["name"]
|
|
|
|
self.lower_bound = dict["lower_bound"]
|
|
|
|
self.upper_bound = dict["upper_bound"]
|
2019-07-19 21:15:10 +00:00
|
|
|
self.frequencies = []
|
|
|
|
if "frequencies" in dict:
|
2020-12-30 20:37:25 +00:00
|
|
|
availableModes = [mode.modulation for mode in Modes.getAvailableModes()]
|
2019-07-19 21:15:10 +00:00
|
|
|
for (mode, freqs) in dict["frequencies"].items():
|
2020-12-30 20:37:25 +00:00
|
|
|
if mode not in availableModes:
|
|
|
|
logger.info(
|
2021-01-20 16:01:46 +00:00
|
|
|
'Modulation "{mode}" is not available, bandplan bookmark will not be displayed'.format(
|
2020-12-30 20:37:25 +00:00
|
|
|
mode=mode
|
|
|
|
)
|
|
|
|
)
|
|
|
|
continue
|
2019-07-19 21:15:10 +00:00
|
|
|
if not isinstance(freqs, list):
|
|
|
|
freqs = [freqs]
|
|
|
|
for f in freqs:
|
|
|
|
if not self.inBand(f):
|
2019-07-21 17:40:28 +00:00
|
|
|
logger.warning(
|
|
|
|
"Frequency for {mode} on {band} is not within band limits: {frequency}".format(
|
|
|
|
mode=mode, frequency=f, band=self.name
|
|
|
|
)
|
|
|
|
)
|
2020-12-30 20:37:25 +00:00
|
|
|
continue
|
|
|
|
self.frequencies.append({"mode": mode, "frequency": f})
|
2019-07-14 17:32:48 +00:00
|
|
|
|
|
|
|
def inBand(self, freq):
|
|
|
|
return self.lower_bound <= freq <= self.upper_bound
|
|
|
|
|
|
|
|
def getName(self):
|
|
|
|
return self.name
|
|
|
|
|
2019-07-19 21:55:52 +00:00
|
|
|
def getDialFrequencies(self, range):
|
|
|
|
(low, hi) = range
|
|
|
|
return [e for e in self.frequencies if low <= e["frequency"] <= hi]
|
|
|
|
|
2019-07-14 17:32:48 +00:00
|
|
|
|
|
|
|
class Bandplan(object):
|
|
|
|
sharedInstance = None
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-07-14 17:32:48 +00:00
|
|
|
@staticmethod
|
|
|
|
def getSharedInstance():
|
|
|
|
if Bandplan.sharedInstance is None:
|
|
|
|
Bandplan.sharedInstance = Bandplan()
|
|
|
|
return Bandplan.sharedInstance
|
|
|
|
|
|
|
|
def __init__(self):
|
2021-02-13 00:29:21 +00:00
|
|
|
self.bands = []
|
|
|
|
self.file_modified = None
|
|
|
|
self.fileList = ["/etc/openwebrx/bands.json", "bands.json"]
|
2019-12-08 20:00:01 +00:00
|
|
|
|
2021-02-13 00:29:21 +00:00
|
|
|
def _refresh(self):
|
|
|
|
modified = self._getFileModifiedTimestamp()
|
|
|
|
if self.file_modified is None or modified > self.file_modified:
|
|
|
|
logger.debug("reloading bands from disk due to file modification")
|
|
|
|
self.bands = self._loadBands()
|
|
|
|
self.file_modified = modified
|
|
|
|
|
|
|
|
def _getFileModifiedTimestamp(self):
|
|
|
|
timestamp = 0
|
|
|
|
for file in self.fileList:
|
|
|
|
try:
|
|
|
|
timestamp = os.path.getmtime(file)
|
|
|
|
break
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
return datetime.fromtimestamp(timestamp, timezone.utc)
|
|
|
|
|
|
|
|
def _loadBands(self):
|
|
|
|
for file in self.fileList:
|
2019-12-08 20:00:01 +00:00
|
|
|
try:
|
|
|
|
f = open(file, "r")
|
|
|
|
bands_json = json.load(f)
|
|
|
|
f.close()
|
|
|
|
return [Band(d) for d in bands_json]
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
2019-12-15 16:44:31 +00:00
|
|
|
except json.JSONDecodeError:
|
2020-02-01 20:37:43 +00:00
|
|
|
logger.exception("error while parsing bandplan file %s", file)
|
|
|
|
return []
|
|
|
|
except Exception:
|
|
|
|
logger.exception("error while processing bandplan from %s", file)
|
2019-12-15 16:44:31 +00:00
|
|
|
return []
|
2019-12-08 20:00:01 +00:00
|
|
|
return []
|
2019-07-14 17:32:48 +00:00
|
|
|
|
2019-07-28 09:45:28 +00:00
|
|
|
def findBands(self, freq):
|
2021-02-13 00:29:21 +00:00
|
|
|
self._refresh()
|
2019-07-28 09:45:28 +00:00
|
|
|
return [band for band in self.bands if band.inBand(freq)]
|
|
|
|
|
2019-07-14 17:32:48 +00:00
|
|
|
def findBand(self, freq):
|
2019-07-28 09:45:28 +00:00
|
|
|
bands = self.findBands(freq)
|
|
|
|
if bands:
|
|
|
|
return bands[0]
|
|
|
|
else:
|
|
|
|
return None
|
2019-07-19 21:55:52 +00:00
|
|
|
|
2019-07-21 21:39:11 +00:00
|
|
|
def collectDialFrequencies(self, range):
|
2021-02-13 00:29:21 +00:00
|
|
|
self._refresh()
|
2019-07-19 21:55:52 +00:00
|
|
|
return [e for b in self.bands for e in b.getDialFrequencies(range)]
|