Add more logging and exception
This commit is contained in:
parent
cb10bb3a4d
commit
ce1c82a192
8 changed files with 56 additions and 19 deletions
|
@ -1,5 +1,7 @@
|
||||||
import smbus
|
import smbus
|
||||||
|
|
||||||
|
from compLib.LogstashLogging import Logging
|
||||||
|
|
||||||
SINGLE_ENDED = 0x84
|
SINGLE_ENDED = 0x84
|
||||||
ADDRESS = 0x48
|
ADDRESS = 0x48
|
||||||
|
|
||||||
|
@ -32,4 +34,5 @@ class ADC:
|
||||||
voltage = value1 / 255.0 * 3.3 # calculate the voltage value
|
voltage = value1 / 255.0 * 3.3 # calculate the voltage value
|
||||||
voltage = round(voltage, 2)
|
voltage = round(voltage, 2)
|
||||||
|
|
||||||
|
Logging.get_logger().debug(f"ADC.read {channel} = {voltage}")
|
||||||
return voltage
|
return voltage
|
||||||
|
|
|
@ -2,6 +2,8 @@ import requests
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
from compLib.LogstashLogging import Logging
|
||||||
|
|
||||||
API_URL = os.getenv("API_URL", "http://localhost:5000/") + "api/"
|
API_URL = os.getenv("API_URL", "http://localhost:5000/") + "api/"
|
||||||
API_URL_GET_POS = API_URL + "getPos"
|
API_URL_GET_POS = API_URL + "getPos"
|
||||||
API_URL_GET_OP = API_URL + "getOp"
|
API_URL_GET_OP = API_URL + "getOp"
|
||||||
|
@ -23,7 +25,9 @@ class Seeding:
|
||||||
:return: An int between 0 and 3
|
:return: An int between 0 and 3
|
||||||
:rtype: int
|
: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
|
@staticmethod
|
||||||
def pay_park() -> bool:
|
def pay_park() -> bool:
|
||||||
|
@ -32,7 +36,9 @@ class Seeding:
|
||||||
:return: True if successful, False if not successful
|
:return: True if successful, False if not successful
|
||||||
:rtype: bool
|
: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
|
@staticmethod
|
||||||
def simon_says() -> int:
|
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.
|
:return: An int between 0 and 3 or -1 after making this request 5 times.
|
||||||
:rtype: int
|
: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:
|
class Position:
|
||||||
|
@ -64,6 +72,7 @@ class DoubleElim:
|
||||||
:rtype: Position
|
:rtype: Position
|
||||||
"""
|
"""
|
||||||
response = json.loads(requests.get(API_URL_GET_POS).content)
|
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"])
|
return Position(response["x"], response["y"], response["degrees"])
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -74,6 +83,7 @@ class DoubleElim:
|
||||||
:rtype: Position
|
:rtype: Position
|
||||||
"""
|
"""
|
||||||
response = json.loads(requests.get(API_URL_GET_OP).content)
|
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"])
|
return Position(response["x"], response["y"], response["degrees"])
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -84,6 +94,7 @@ class DoubleElim:
|
||||||
:rtype: Position
|
:rtype: Position
|
||||||
"""
|
"""
|
||||||
response = json.loads(requests.get(API_URL_GET_GOAL).content)
|
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)
|
return Position(response["x"], response["y"], -1)
|
||||||
|
|
||||||
@staticmethod
|
@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}
|
:return: A list will all items currently on the game field. Items are dictionaries that look like: {"id": 0}
|
||||||
:rtype: list
|
: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
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
from compLib.ADC import ADC
|
from compLib.ADC import ADC
|
||||||
|
from compLib.LogstashLogging import Logging
|
||||||
|
|
||||||
BATTERY_CHANNEL = 2
|
BATTERY_CHANNEL = 2
|
||||||
BATTERY_COUNT = 2
|
BATTERY_COUNT = 2
|
||||||
|
@ -21,4 +22,6 @@ class Battery(object):
|
||||||
:rtype: int
|
:rtype: int
|
||||||
"""
|
"""
|
||||||
voltage = adc.read(BATTERY_CHANNEL) * BATTERY_MULTIPLIER
|
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
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
import atexit
|
import atexit
|
||||||
|
|
||||||
import RPi.GPIO as GPIO
|
import RPi.GPIO as GPIO
|
||||||
|
|
||||||
|
from compLib.LogstashLogging import Logging
|
||||||
|
|
||||||
GPIO.setwarnings(False)
|
GPIO.setwarnings(False)
|
||||||
Buzzer_Pin = 17
|
Buzzer_Pin = 17
|
||||||
GPIO.setmode(GPIO.BCM)
|
GPIO.setmode(GPIO.BCM)
|
||||||
|
@ -18,11 +19,13 @@ class Buzzer:
|
||||||
|
|
||||||
:param on: True if on, False if off
|
:param on: True if on, False if off
|
||||||
"""
|
"""
|
||||||
|
Logging.get_logger().debug(f"Buzzer.set {on}")
|
||||||
|
|
||||||
GPIO.output(Buzzer_Pin, on)
|
GPIO.output(Buzzer_Pin, on)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def exit():
|
def exit():
|
||||||
Buzzer.set(0)
|
Buzzer.set(False)
|
||||||
|
|
||||||
|
|
||||||
atexit.register(Buzzer.exit)
|
atexit.register(Buzzer.exit)
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import RPi.GPIO as GPIO
|
import RPi.GPIO as GPIO
|
||||||
|
|
||||||
from compLib.ADC import ADC
|
from compLib.ADC import ADC
|
||||||
|
from compLib.LogstashLogging import Logging
|
||||||
|
|
||||||
TOP_LEFT_CHANNEL = 0
|
TOP_LEFT_CHANNEL = 0
|
||||||
TOP_RIGHT_CHANNEL = 1
|
TOP_RIGHT_CHANNEL = 1
|
||||||
|
|
|
@ -5,7 +5,7 @@ import requests
|
||||||
from logstash_async.transport import HttpTransport
|
from logstash_async.transport import HttpTransport
|
||||||
from logstash_async.handler import AsynchronousLogstashHandler
|
from logstash_async.handler import AsynchronousLogstashHandler
|
||||||
|
|
||||||
host = 'logs.robo4you.at'
|
host = 'logstash.robo4you.at'
|
||||||
port = 443
|
port = 443
|
||||||
|
|
||||||
logstash_logger = logging.getLogger('logstash')
|
logstash_logger = logging.getLogger('logstash')
|
||||||
|
@ -16,7 +16,7 @@ transport = HttpTransport(
|
||||||
port,
|
port,
|
||||||
username="robo",
|
username="robo",
|
||||||
password="competition",
|
password="competition",
|
||||||
timeout=10.0,
|
timeout=60.0,
|
||||||
)
|
)
|
||||||
|
|
||||||
asynchronousLogstashHandler = AsynchronousLogstashHandler(
|
asynchronousLogstashHandler = AsynchronousLogstashHandler(
|
||||||
|
@ -54,19 +54,25 @@ class StreamToLogger(object):
|
||||||
asynchronousLogstashHandler.flush()
|
asynchronousLogstashHandler.flush()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
requests.head(f"http://{host}:{port}")
|
r = requests.head(f"http://{host}:{port}")
|
||||||
logstash_logger.addHandler(asynchronousLogstashHandler)
|
if r.status_code == 400:
|
||||||
|
logstash_logger.addHandler(asynchronousLogstashHandler)
|
||||||
|
|
||||||
sl = StreamToLogger(logging.INFO)
|
sl = StreamToLogger(logging.INFO)
|
||||||
sys.stdout = sl
|
sys.stdout = sl
|
||||||
|
else:
|
||||||
|
print(f"Could not connect to {host} -> {r.status_code}!")
|
||||||
|
|
||||||
except requests.exceptions.ConnectionError as identifier:
|
except requests.exceptions.ConnectionError as identifier:
|
||||||
print(f"Could not connect to {host}!")
|
print(f"Could not connect to {host}!")
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class Logging(object):
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_logger() -> logging.Logger:
|
||||||
|
return logstash_logger
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def set_debug():
|
||||||
|
logstash_logger.setLevel(logging.DEBUG)
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import atexit
|
import atexit
|
||||||
|
|
||||||
from compLib.PCA9685 import PCA9685
|
from compLib.PCA9685 import PCA9685
|
||||||
|
from compLib.LogstashLogging import Logging
|
||||||
|
|
||||||
pwm = PCA9685(0x40, debug=True)
|
pwm = PCA9685(0x40, debug=True)
|
||||||
pwm.setPWMFreq(50)
|
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 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
|
: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
|
forward = True
|
||||||
if percent < 0:
|
if percent < 0:
|
||||||
percent = abs(percent)
|
percent = abs(percent)
|
||||||
|
@ -44,6 +50,8 @@ class Motor(object):
|
||||||
"""
|
"""
|
||||||
Turns of all motors
|
Turns of all motors
|
||||||
"""
|
"""
|
||||||
|
Logging.get_logger().debug(f"Motor.all_off")
|
||||||
|
|
||||||
for i in range(0, MOTOR_COUNT):
|
for i in range(0, MOTOR_COUNT):
|
||||||
Motor.power(i, 0)
|
Motor.power(i, 0)
|
||||||
|
|
||||||
|
|
|
@ -3,4 +3,4 @@ __version__ = "0.0.2"
|
||||||
import compLib.LogstashLogging
|
import compLib.LogstashLogging
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
print("Starting compLib...")
|
print(f"Starting compLib! Version: {__version__} ...")
|
Reference in a new issue