BW3-Core/bw_client.py
2019-03-09 11:42:50 +01:00

170 lines
5.4 KiB
Python

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
____ ____ ______ __ __ __ _____
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
German BOS Information Script
by Bastian Schroll
@file: bw_client.py
@date: 09.12.2017
@author: Bastian Schroll
@description: BOSWatch client application
"""
# pylint: disable=wrong-import-position
# pylint: disable=wrong-import-order
from boswatch.utils import paths
if not paths.makeDirIfNotExist(paths.LOG_PATH):
print("cannot find/create log directory: %s", paths.LOG_PATH)
exit(1)
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.debug("Import python modules")
import argparse
logging.debug("- argparse")
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
from boswatch.processManager import ProcessManager
header.logoToLog()
header.infoToLog()
# sox = ProcessManager("_bin/win/sox/sox.exe")
# rtl = ProcessManager("_bin/win/rtl_fm/rtl_fm.exe")
# multimon = ProcessManager("_bin/win/multimon/multimon-ng.exe", True)
#
#
# try:
# rtl.addArgument("-f 85.235MHz")
# rtl.addArgument("-d 1")
# rtl.addArgument("-M fm -s 22050 -g 100")
# rtl.setStderr(None)
# #rtl.start()
#
# sox.addArgument("-t ogg 88022.ogg -esigned-integer -b16 -r 22050 -t raw -")
# sox.setStderr(None)
# sox.start()
#
#
# #.\_bin\win\sox\sox.exe -t ogg -esigned-integer -b16 -r 22050 88022.ogg -
#
# multimon.addArgument("-a ZVEI1 -a FMSFSK")
# multimon.addArgument("-t raw")
# #multimon.addArgument("-v 3")
# multimon.addArgument("-")
# multimon.setStdin(sox.stdout)
# multimon.setStderr(None)
#
# multimon.start()
#
# logging.debug("go")
#
# while multimon.isRunning:
# data = multimon.readline()
# if data is not None:
# print(data)
#
# logging.debug("end")
#
# except:
# logging.exception("batsch")
#
# finally:
#
# sox.stop()
# multimon.stop()
# rtl.stop()
#
# exit()
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)
# ############################# 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
bwClient = TCPClient()
if bwClient.connect(ip, port):
testFile = open(paths.TEST_PATH + "testdata.list", "r")
while 1:
for testData in testFile:
if (len(testData.rstrip(' \t\n\r')) == 0) or ("#" in testData[0]):
continue
logging.debug("Test: %s", testData)
bwPacket = Decoder.decode(testData)
if bwPacket:
bwPacket.printInfo()
bwPacket.addClientData(bwConfig)
bwClient.transmit(str(bwPacket))
# todo should we do this in an thread, to not block receiving ??? but then we should use transmit() and receive() with Lock()
failedTransmits = 0
while not bwClient.receive() == "[ack]": # wait for ack or timeout
if failedTransmits >= 3:
logging.error("cannot transmit after 3 retires")
break
failedTransmits += 1
logging.warning("attempt %d to resend packet", failedTransmits)
bwClient.transmit(str(bwPacket)) # try to resend
logging.debug("ack ok")
bwClient.disconnect()
break
# test for server ####################################
except KeyboardInterrupt: # pragma: no cover
logging.warning("Keyboard interrupt")
except SystemExit: # pragma: no cover
logging.error("BOSWatch interrupted by an error")
except: # pragma: no cover
logging.exception("BOSWatch interrupted by an error")
finally: # pragma: no cover
logging.debug("BOSWatch has ended ...")