openwebrx-clone/owrx/__main__.py

116 lines
3.8 KiB
Python
Raw 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
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
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()
# set loglevel to info for CLI commands
if args.module is not None and not args.debug:
logging.getLogger().setLevel(logging.INFO)
2021-04-26 22:33:52 +00:00
2021-04-24 17:39:48 +00:00
if args.version:
print("OpenWebRX version {version}".format(version=openwebrx_version))
2021-04-26 17:34:50 +00:00
elif args.module == "admin":
2021-04-24 17:39:48 +00:00
run_admin_action(adminparser, args)
2021-04-26 17:34:50 +00:00
elif args.module == "config":
run_admin_action(configparser, args)
2021-04-24 17:39:48 +00:00
else:
start_receiver()
def start_receiver():
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
# config warmup
Config.validateConfig()
coreConfig = CoreConfig()
2019-12-08 16:15:48 +00:00
featureDetector = FeatureDetector()
if not featureDetector.is_available("core"):
logger.error(
2019-12-08 16:15:48 +00:00
"you are missing required dependencies to run openwebrx. "
2020-08-26 18:07:58 +00:00
"please check that the following core requirements are installed and up to date:"
2019-12-08 16:15:48 +00:00
)
logger.error(", ".join(featureDetector.get_requirements("core")))
2019-12-08 16:15:48 +00:00
return
# 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()
ReportingEngine.stopAll()
DecoderQueue.stopAll()