openwebrx-clone/owrx/__main__.py

131 lines
4.3 KiB
Python
Raw Permalink Normal View History

2021-04-26 22:53:45 +00:00
import logging
# the linter will complain about this, but the logging must be configured before importing all the other modules
# loglevel will be adjusted later, INFO is just for the startup
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
2021-04-26 22:53:45 +00:00
logger = logging.getLogger(__name__)
2019-12-08 16:15:48 +00:00
from http.server import HTTPServer
from owrx.http import RequestHandler
2021-02-11 18:31:44 +00:00
from owrx.config.core import CoreConfig
from owrx.config import Config
2021-04-26 17:27:12 +00:00
from owrx.config.commands import MigrateCommand
2019-12-08 16:15:48 +00:00
from owrx.feature import FeatureDetector
from owrx.sdr import SdrService
2019-12-08 16:15:48 +00:00
from socketserver import ThreadingMixIn
from owrx.service import Services
from owrx.websocket import WebSocketConnection
from owrx.reporting import ReportingEngine
2020-03-15 22:34:44 +00:00
from owrx.version import openwebrx_version
from owrx.audio.queue import DecoderQueue
2021-04-24 17:39:48 +00:00
from owrx.admin import add_admin_parser, run_admin_action
2021-01-22 16:33:53 +00:00
import signal
2021-04-24 17:39:48 +00:00
import argparse
2021-01-22 16:33:53 +00:00
2019-12-08 16:15:48 +00:00
class ThreadedHttpServer(ThreadingMixIn, HTTPServer):
pass
2021-01-22 16:33:53 +00:00
class SignalException(Exception):
pass
def handleSignal(sig, frame):
raise SignalException("Received Signal {sig}".format(sig=sig))
2019-12-08 16:15:48 +00:00
def main():
2021-04-24 17:39:48 +00:00
parser = argparse.ArgumentParser(description="OpenWebRX - Open Source SDR Web App for Everyone!")
parser.add_argument("-v", "--version", action="store_true", help="Show the software version")
2021-04-26 22:33:52 +00:00
parser.add_argument("--debug", action="store_true", help="Set loglevel to DEBUG")
2021-04-24 17:39:48 +00:00
moduleparser = parser.add_subparsers(title="Modules", dest="module")
2021-04-26 17:27:12 +00:00
adminparser = moduleparser.add_parser("admin", help="Administration actions")
2021-04-24 17:39:48 +00:00
add_admin_parser(adminparser)
2021-04-26 17:27:12 +00:00
configparser = moduleparser.add_parser("config", help="Configuration actions")
configcommandparser = configparser.add_subparsers(title="Commands", dest="command")
2021-04-26 17:34:50 +00:00
migrateparser = configcommandparser.add_parser("migrate", help="Migrate configuration files")
2021-04-26 17:27:12 +00:00
migrateparser.set_defaults(cls=MigrateCommand)
2021-04-24 17:39:48 +00:00
args = parser.parse_args()
2022-11-30 17:53:09 +00:00
if args.debug:
logging.getLogger().setLevel(logging.DEBUG)
2021-04-24 17:39:48 +00:00
if args.version:
print("OpenWebRX version {version}".format(version=openwebrx_version))
2022-06-01 15:58:06 +00:00
return 0
if args.module == "admin":
return run_admin_action(adminparser, args)
if args.module == "config":
return run_admin_action(configparser, args)
return start_receiver(loglevel=logging.DEBUG if args.debug else None)
2021-04-24 17:39:48 +00:00
def start_receiver(loglevel=None):
2019-12-08 16:15:48 +00:00
print(
"""
OpenWebRX - Open Source SDR Web App for Everyone! | for license see LICENSE file in the package
_________________________________________________________________________________________________
Author contact info: Jakob Ketterl, DD5JFK <dd5jfk@darc.de>
2020-03-15 22:39:38 +00:00
Documentation: https://github.com/jketterl/openwebrx/wiki
Support and info: https://groups.io/g/openwebrx
2019-12-08 16:15:48 +00:00
"""
)
2020-03-15 22:34:44 +00:00
logger.info("OpenWebRX version {0} starting up...".format(openwebrx_version))
2021-01-22 16:33:53 +00:00
for sig in [signal.SIGINT, signal.SIGTERM]:
signal.signal(sig, handleSignal)
2021-02-06 21:08:27 +00:00
coreConfig = CoreConfig()
# passed loglevel takes priority (used for the --debug argument)
logging.getLogger().setLevel(coreConfig.get_log_level() if loglevel is None else loglevel)
2022-11-30 17:53:09 +00:00
# config warmup
Config.validateConfig()
2019-12-08 16:15:48 +00:00
featureDetector = FeatureDetector()
failed = featureDetector.get_failed_requirements("core")
if failed:
logger.error(
2019-12-08 16:15:48 +00:00
"you are missing required dependencies to run openwebrx. "
"please check that the following core requirements are installed and up to date: %s",
", ".join(failed)
2019-12-08 16:15:48 +00:00
)
for f in failed:
description = featureDetector.get_requirement_description(f)
if description:
logger.error("description for %s:\n%s", f, description)
2022-06-01 15:58:06 +00:00
return 1
2019-12-08 16:15:48 +00:00
# Get error messages about unknown / unavailable features as soon as possible
# start up "always-on" sources right away
SdrService.getAllSources()
2019-12-08 16:15:48 +00:00
Services.start()
try:
2021-02-06 21:08:27 +00:00
server = ThreadedHttpServer(("0.0.0.0", coreConfig.get_web_port()), RequestHandler)
2019-12-08 16:15:48 +00:00
server.serve_forever()
2021-01-22 16:33:53 +00:00
except SignalException:
2021-04-11 16:42:35 +00:00
pass
WebSocketConnection.closeAll()
Services.stop()
2021-09-15 13:03:11 +00:00
SdrService.stopAllSources()
2021-04-11 16:42:35 +00:00
ReportingEngine.stopAll()
DecoderQueue.stopAll()
2022-06-01 15:58:06 +00:00
return 0