diff --git a/bw_client.py b/bw_client.py index 4a2a55b..a70fd37 100644 --- a/bw_client.py +++ b/bw_client.py @@ -20,73 +20,59 @@ if not paths.makeDirIfNotExist(paths.LOG_PATH): print("cannot find/create log directory: %s", paths.LOG_PATH) exit(1) -try: - import logging - import logging.config +import logging.config +logging.config.fileConfig(paths.CONFIG_PATH + "logger_client.ini") +logging.debug("") +logging.debug("######################## NEW LOG ############################") +logging.debug("BOSWatch client has started ...") - logging.config.fileConfig(paths.CONFIG_PATH + "logger_client.ini") - logging.debug("") - logging.debug("######################## NEW LOG ############################") - logging.debug("BOSWatch client has started ...") -except Exception as e: # pragma: no cover - print("cannot load logger") - print(e) + +logging.debug("Import python modules") +import argparse +logging.debug("- argparse") +import subprocess +logging.debug("- subprocess") +import time +logging.debug("- time") + +logging.debug("Import BOSWatch modules") +from boswatch.configYaml import ConfigYAML +from boswatch.network.client import TCPClient +from boswatch.network.broadcast import BroadcastClient +from boswatch.decoder.decoder import Decoder +from boswatch.utils import header + + +header.logoToLog() +header.infoToLog() + +logging.debug("parse args") +# With -h or --help you get the Args help +parser = argparse.ArgumentParser(prog="bw_client.py", + description="""BOSWatch is a Python Script to receive and + decode german BOS information with rtl_fm and multimon-NG""", + epilog="""More options you can find in the extern client.ini + file in the folder /config""") +parser.add_argument("-c", "--config", help="Name to configuration File", required=True) +args = parser.parse_args() + +bwConfig = ConfigYAML() +if not bwConfig.loadConfigFile(paths.CONFIG_PATH + args.config): + logging.error("cannot load config file") exit(1) -try: - logging.debug("Import python modules") - import argparse - logging.debug("- argparse") - import subprocess - logging.debug("- subprocess") - # following is temp for testing - import time - - logging.debug("Import BOSWatch modules") - from boswatch.configYaml import ConfigYAML - from boswatch.network.client import TCPClient - from boswatch.network.broadcast import BroadcastClient - from boswatch.decoder.decoder import Decoder - from boswatch.utils import header - -except: # pragma: no cover - logging.exception("cannot import modules") - exit(1) - -try: - header.logoToLog() - header.infoToLog() - - logging.debug("parse args") - # With -h or --help you get the Args help - parser = argparse.ArgumentParser(prog="bw_client.py", - description="""BOSWatch is a Python Script to receive and - decode german BOS information with rtl_fm and multimon-NG""", - epilog="""More options you can find in the extern client.ini - file in the folder /config""") - parser.add_argument("-c", "--config", help="Name to configuration File", required=True) - args = parser.parse_args() - - bwConfig = ConfigYAML() - if not bwConfig.loadConfigFile(paths.CONFIG_PATH + args.config): - logging.error("cannot load config file") - exit(1) - -except: # pragma: no cover - logging.exception("error occurred") - exit(1) # ############################# begin client system try: + ip = bwConfig.get("server", "ip", default="127.0.0.1") + port = bwConfig.get("server", "port", default="8080") + if bwConfig.get("client", "useBroadcast", default=False): broadcastClient = BroadcastClient() if broadcastClient.getConnInfo(): ip = broadcastClient.serverIP port = broadcastClient.serverPort - else: - ip = bwConfig.get("server", "ip", default="127.0.0.1") - port = bwConfig.get("server", "port", default="8080") bwClient = TCPClient() if bwClient.connect(ip, port): diff --git a/bw_server.py b/bw_server.py index 0882fe0..b350686 100644 --- a/bw_server.py +++ b/bw_server.py @@ -20,74 +20,55 @@ if not paths.makeDirIfNotExist(paths.LOG_PATH): print("cannot find/create log directory: %s", paths.LOG_PATH) exit(1) -try: - import logging - import logging.config - print(paths.CONFIG_PATH + "logger_server.ini") - logging.config.fileConfig(paths.CONFIG_PATH + "logger_server.ini") - logging.debug("") - logging.debug("######################## NEW LOG ############################") - logging.debug("BOSWatch server has started ...") -except Exception as e: # pragma: no cover - print("cannot load logger") - print(e) +import logging.config +logging.config.fileConfig(paths.CONFIG_PATH + "logger_server.ini") +logging.debug("") +logging.debug("######################## NEW LOG ############################") +logging.debug("BOSWatch server has started ...") + + +logging.debug("Import python modules") +import argparse +logging.debug("- argparse") +import queue +logging.debug("- queue") +import time +logging.debug("- time") + +logging.debug("Import BOSWatch modules") +from boswatch.configYaml import ConfigYAML +from boswatch.network.server import TCPServer +from boswatch.packet import Packet +from boswatch.utils import header +from boswatch.network.broadcast import BroadcastServer +from boswatch.router import RouterManager + + +header.logoToLog() +header.infoToLog() + +logging.debug("parse args") +# With -h or --help you get the Args help +parser = argparse.ArgumentParser(prog="bw_server.py", + description="""BOSWatch is a Python Script to receive and + decode german BOS information with rtl_fm and multimon-NG""", + epilog="""More options you can find in the extern client.ini + file in the folder /config""") +parser.add_argument("-c", "--config", help="Name to configuration File", required=True) +args = parser.parse_args() + + +bwConfig = ConfigYAML() +if not bwConfig.loadConfigFile(paths.CONFIG_PATH + args.config): + logging.error("cannot load config file") exit(1) - -try: - logging.debug("Import python module") - import argparse - logging.debug("- argparse") - import queue - logging.debug("- queue") - - # following is temp for testing - import time - import sys - import threading - - logging.debug("Import BOSWatch module") - from boswatch.configYaml import ConfigYAML - from boswatch.network.server import TCPServer - from boswatch.packet import Packet - from boswatch.utils import header - from boswatch.network.broadcast import BroadcastServer - from boswatch.router import RouterManager -except: # pragma: no cover - logging.exception("cannot import module") - exit(1) - -try: - header.logoToLog() - header.infoToLog() - - logging.debug("parse args") - # With -h or --help you get the Args help - parser = argparse.ArgumentParser(prog="bw_server.py", - description="""BOSWatch is a Python Script to receive and - decode german BOS information with rtl_fm and multimon-NG""", - epilog="""More options you can find in the extern client.ini - file in the folder /config""") - parser.add_argument("-c", "--config", help="Name to configuration File", required=True) - args = parser.parse_args() - - bwConfig = ConfigYAML() - if not bwConfig.loadConfigFile(paths.CONFIG_PATH + args.config): - logging.error("cannot load config file") - exit(1) - -except: # pragma: no cover - logging.exception("error occurred") - exit(1) - - -bwRoutMan = RouterManager() -bwRoutMan.buildRouter(bwConfig) - - # ############################# begin server system try: + bwRoutMan = RouterManager() + bwRoutMan.buildRouter(bwConfig) + if bwConfig.get("server", "useBroadcast", default=False): bcServer = BroadcastServer() bcServer.start() @@ -110,7 +91,6 @@ try: bwPacket.set("clientIP", data[0]) bwPacket.addServerData(bwConfig) - # todo implement routing bwRoutMan.runRouter(bwConfig.get("alarmRouter"), bwPacket) incomingQueue.task_done() @@ -122,12 +102,19 @@ except SystemExit: # pragma: no cover except: # pragma: no cover logging.exception("BOSWatch interrupted by an error") finally: # pragma: no cover - # try-except-blocks are necessary because there is a change that the vars - # bwServer or bwPluginManager are not defined in case of an early error + try: bwServer.stop() - if "bcServer" in locals(): - bcServer.stop() - except: # pragma: no cover + except NameError: pass + except: + raise + + try: + bcServer.stop() + except NameError: + pass + except: + raise + logging.debug("BOSWatch has ended ...")