From ce1c82a19274c0ed936f6975d1ba8f4256fc651b Mon Sep 17 00:00:00 2001 From: Konstantin Lampalzer Date: Thu, 21 Jan 2021 16:43:01 +0000 Subject: [PATCH] Add more logging and exception --- compLib/ADC.py | 3 +++ compLib/Api.py | 21 +++++++++++++++++---- compLib/Battery.py | 5 ++++- compLib/Buzzer.py | 7 +++++-- compLib/IRSensor.py | 1 + compLib/LogstashLogging.py | 28 +++++++++++++++++----------- compLib/Motor.py | 8 ++++++++ compLib/__init__.py | 2 +- 8 files changed, 56 insertions(+), 19 deletions(-) diff --git a/compLib/ADC.py b/compLib/ADC.py index bb7e5b8..b4297ab 100644 --- a/compLib/ADC.py +++ b/compLib/ADC.py @@ -1,5 +1,7 @@ import smbus +from compLib.LogstashLogging import Logging + SINGLE_ENDED = 0x84 ADDRESS = 0x48 @@ -32,4 +34,5 @@ class ADC: voltage = value1 / 255.0 * 3.3 # calculate the voltage value voltage = round(voltage, 2) + Logging.get_logger().debug(f"ADC.read {channel} = {voltage}") return voltage diff --git a/compLib/Api.py b/compLib/Api.py index e59f646..794d86d 100644 --- a/compLib/Api.py +++ b/compLib/Api.py @@ -2,6 +2,8 @@ import requests import json import os +from compLib.LogstashLogging import Logging + API_URL = os.getenv("API_URL", "http://localhost:5000/") + "api/" API_URL_GET_POS = API_URL + "getPos" API_URL_GET_OP = API_URL + "getOp" @@ -23,7 +25,9 @@ class Seeding: :return: An int between 0 and 3 :rtype: int """ - return json.loads(requests.get(API_URL_GET_PARK).content)["id"] + result = json.loads(requests.get(API_URL_GET_PARK).content)["id"] + Logging.get_logger().debug(f"Seeding.get_park = {result}") + return result @staticmethod def pay_park() -> bool: @@ -32,7 +36,9 @@ class Seeding: :return: True if successful, False if not successful :rtype: bool """ - return requests.get(API_URL_PAY_PARK).status_code == 200 + result = requests.get(API_URL_PAY_PARK).status_code == 200 + Logging.get_logger().debug(f"Seeding.pay_park = {result}") + return result @staticmethod def simon_says() -> int: @@ -41,7 +47,9 @@ class Seeding: :return: An int between 0 and 3 or -1 after making this request 5 times. :rtype: int """ - return json.loads(requests.get(API_URL_SIMON_SAYS).content)["id"] + result = json.loads(requests.get(API_URL_SIMON_SAYS).content)["id"] + Logging.get_logger().debug(f"Seeding.simon_says = {result}") + return result class Position: @@ -64,6 +72,7 @@ class DoubleElim: :rtype: Position """ response = json.loads(requests.get(API_URL_GET_POS).content) + Logging.get_logger().debug(f"DoubleElim.get_position = {response}") return Position(response["x"], response["y"], response["degrees"]) @staticmethod @@ -74,6 +83,7 @@ class DoubleElim: :rtype: Position """ response = json.loads(requests.get(API_URL_GET_OP).content) + Logging.get_logger().debug(f"DoubleElim.get_opponent = x:{response}") return Position(response["x"], response["y"], response["degrees"]) @staticmethod @@ -84,6 +94,7 @@ class DoubleElim: :rtype: Position """ response = json.loads(requests.get(API_URL_GET_GOAL).content) + Logging.get_logger().debug(f"DoubleElim.get_goal = x:{response}") return Position(response["x"], response["y"], -1) @staticmethod @@ -93,4 +104,6 @@ class DoubleElim: :return: A list will all items currently on the game field. Items are dictionaries that look like: {"id": 0} :rtype: list """ - return json.loads(requests.get(API_URL_GET_ITEMS).content) + result = json.loads(requests.get(API_URL_GET_ITEMS).content) + Logging.get_logger().debug(f"DoubleElim.get_items = {result}") + return result diff --git a/compLib/Battery.py b/compLib/Battery.py index 9cb76e9..2fc0eb1 100644 --- a/compLib/Battery.py +++ b/compLib/Battery.py @@ -1,4 +1,5 @@ from compLib.ADC import ADC +from compLib.LogstashLogging import Logging BATTERY_CHANNEL = 2 BATTERY_COUNT = 2 @@ -21,4 +22,6 @@ class Battery(object): :rtype: int """ voltage = adc.read(BATTERY_CHANNEL) * BATTERY_MULTIPLIER - return int(((voltage / BATTERY_COUNT) - BATTERY_MIN_VOLTAGE) / (BATTERY_MAX_VOLTAGE - BATTERY_MIN_VOLTAGE) * 100) + result = int(((voltage / BATTERY_COUNT) - BATTERY_MIN_VOLTAGE) / (BATTERY_MAX_VOLTAGE - BATTERY_MIN_VOLTAGE) * 100) + Logging.get_logger().debug(f"Battery.percent = {result}") + return result diff --git a/compLib/Buzzer.py b/compLib/Buzzer.py index 58739ef..4b1329d 100644 --- a/compLib/Buzzer.py +++ b/compLib/Buzzer.py @@ -1,7 +1,8 @@ import atexit - import RPi.GPIO as GPIO +from compLib.LogstashLogging import Logging + GPIO.setwarnings(False) Buzzer_Pin = 17 GPIO.setmode(GPIO.BCM) @@ -18,11 +19,13 @@ class Buzzer: :param on: True if on, False if off """ + Logging.get_logger().debug(f"Buzzer.set {on}") + GPIO.output(Buzzer_Pin, on) @staticmethod def exit(): - Buzzer.set(0) + Buzzer.set(False) atexit.register(Buzzer.exit) diff --git a/compLib/IRSensor.py b/compLib/IRSensor.py index f696ed9..0477c7d 100644 --- a/compLib/IRSensor.py +++ b/compLib/IRSensor.py @@ -1,6 +1,7 @@ import RPi.GPIO as GPIO from compLib.ADC import ADC +from compLib.LogstashLogging import Logging TOP_LEFT_CHANNEL = 0 TOP_RIGHT_CHANNEL = 1 diff --git a/compLib/LogstashLogging.py b/compLib/LogstashLogging.py index d507925..8ff02e3 100644 --- a/compLib/LogstashLogging.py +++ b/compLib/LogstashLogging.py @@ -5,7 +5,7 @@ import requests from logstash_async.transport import HttpTransport from logstash_async.handler import AsynchronousLogstashHandler -host = 'logs.robo4you.at' +host = 'logstash.robo4you.at' port = 443 logstash_logger = logging.getLogger('logstash') @@ -16,7 +16,7 @@ transport = HttpTransport( port, username="robo", password="competition", - timeout=10.0, + timeout=60.0, ) asynchronousLogstashHandler = AsynchronousLogstashHandler( @@ -54,19 +54,25 @@ class StreamToLogger(object): asynchronousLogstashHandler.flush() try: - requests.head(f"http://{host}:{port}") - logstash_logger.addHandler(asynchronousLogstashHandler) + r = requests.head(f"http://{host}:{port}") + if r.status_code == 400: + logstash_logger.addHandler(asynchronousLogstashHandler) - sl = StreamToLogger(logging.INFO) - sys.stdout = sl + sl = StreamToLogger(logging.INFO) + sys.stdout = sl + else: + print(f"Could not connect to {host} -> {r.status_code}!") except requests.exceptions.ConnectionError as identifier: print(f"Could not connect to {host}!") pass +class Logging(object): - - - - - + @staticmethod + def get_logger() -> logging.Logger: + return logstash_logger + + @staticmethod + def set_debug(): + logstash_logger.setLevel(logging.DEBUG) diff --git a/compLib/Motor.py b/compLib/Motor.py index bdff928..73fd1b3 100644 --- a/compLib/Motor.py +++ b/compLib/Motor.py @@ -1,6 +1,7 @@ import atexit from compLib.PCA9685 import PCA9685 +from compLib.LogstashLogging import Logging pwm = PCA9685(0x40, debug=True) pwm.setPWMFreq(50) @@ -21,6 +22,11 @@ class Motor(object): :param port: Port, which the motor is connected to. 0-3, 0 -> top left, 3 -> top right :param percent: Percentage of max speed. between -100 and 100 """ + Logging.get_logger().debug(f"Motor.power {port} {percent}") + + if port < 0 or port >= MOTOR_COUNT: + raise IndexError("Invalid Motor port specified!") + forward = True if percent < 0: percent = abs(percent) @@ -44,6 +50,8 @@ class Motor(object): """ Turns of all motors """ + Logging.get_logger().debug(f"Motor.all_off") + for i in range(0, MOTOR_COUNT): Motor.power(i, 0) diff --git a/compLib/__init__.py b/compLib/__init__.py index 8afaf7d..36693bb 100644 --- a/compLib/__init__.py +++ b/compLib/__init__.py @@ -3,4 +3,4 @@ __version__ = "0.0.2" import compLib.LogstashLogging import logging -print("Starting compLib...") \ No newline at end of file +print(f"Starting compLib! Version: {__version__} ...") \ No newline at end of file