2018-06-15 22:20:20 +02:00
|
|
|
#!/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
|
|
|
|
|
"""
|
|
|
|
|
from boswatch.utils import paths
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
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")
|
2019-03-01 12:09:12 +01:00
|
|
|
from boswatch.configYaml import ConfigYAML
|
2018-06-15 22:20:20 +02:00
|
|
|
from boswatch.network.client import TCPClient
|
2019-02-27 12:08:55 +01:00
|
|
|
from boswatch.network.broadcast import BroadcastClient
|
2018-09-09 16:34:44 +02:00
|
|
|
from boswatch.decoder.decoder import Decoder
|
2018-06-15 22:20:20 +02:00
|
|
|
from boswatch.utils import header
|
2019-02-27 22:05:59 +01:00
|
|
|
|
|
|
|
|
except: # pragma: no cover
|
2018-06-15 22:20:20 +02:00
|
|
|
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()
|
|
|
|
|
|
2019-03-01 12:09:12 +01:00
|
|
|
bwConfig = ConfigYAML()
|
|
|
|
|
if not bwConfig.loadConfigFile(paths.CONFIG_PATH + args.config):
|
2019-02-27 12:08:55 +01:00
|
|
|
logging.error("cannot load config file")
|
2019-03-01 12:09:12 +01:00
|
|
|
exit(1)
|
2019-02-27 12:08:55 +01:00
|
|
|
|
2019-02-27 22:05:59 +01:00
|
|
|
except: # pragma: no cover
|
2019-02-27 12:08:55 +01:00
|
|
|
logging.exception("error occurred")
|
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
|
|
# ############################# begin client system
|
|
|
|
|
try:
|
|
|
|
|
|
2019-03-01 12:09:12 +01:00
|
|
|
if bwConfig.get("client", "useBroadcast", default=False):
|
2019-02-27 12:08:55 +01:00
|
|
|
broadcastClient = BroadcastClient()
|
|
|
|
|
if broadcastClient.getConnInfo():
|
|
|
|
|
ip = broadcastClient.serverIP
|
|
|
|
|
port = broadcastClient.serverPort
|
|
|
|
|
else:
|
2019-03-01 12:09:12 +01:00
|
|
|
ip = bwConfig.get("server", "ip", default="127.0.0.1")
|
|
|
|
|
port = bwConfig.get("server", "port", default="8080")
|
2018-06-15 22:20:20 +02:00
|
|
|
|
|
|
|
|
bwClient = TCPClient()
|
2019-02-27 12:08:55 +01:00
|
|
|
if bwClient.connect(ip, port):
|
2018-06-15 22:20:20 +02:00
|
|
|
|
|
|
|
|
while 1:
|
2019-02-27 22:05:59 +01:00
|
|
|
|
|
|
|
|
for i in range(0, 5): # todo implement real data receive
|
2018-06-15 22:20:20 +02:00
|
|
|
time.sleep(1)
|
|
|
|
|
print("Alarm Nr #" + str(i))
|
|
|
|
|
|
2019-02-27 22:05:59 +01:00
|
|
|
bwPacket = Decoder.decode("ZVEI1: 12345")
|
2018-06-15 22:20:20 +02:00
|
|
|
|
|
|
|
|
if bwPacket:
|
|
|
|
|
bwPacket.printInfo()
|
2019-03-02 09:17:20 +01:00
|
|
|
bwPacket.addClientData(bwConfig)
|
2018-06-15 22:20:20 +02:00
|
|
|
bwClient.transmit(str(bwPacket))
|
|
|
|
|
|
2018-09-19 14:53:56 +02:00
|
|
|
# todo should we do this in an thread, to not block receiving ??? but then we should use transmit() and receive() with Lock()
|
2018-06-15 22:20:20 +02:00
|
|
|
failedTransmits = 0
|
|
|
|
|
while not bwClient.receive() == "[ack]": # wait for ack or timeout
|
|
|
|
|
if failedTransmits >= 3:
|
2018-09-20 23:35:56 +02:00
|
|
|
logging.error("cannot transmit after 3 retires")
|
2018-06-15 22:20:20 +02:00
|
|
|
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 ...")
|