Logging, more documentation

This commit is contained in:
Konstantin Lampalzer 2021-01-23 13:37:40 +01:00
parent 6eebab871e
commit 4a5f630d40
No known key found for this signature in database
GPG key ID: 9A60A522835A2AD9
11 changed files with 66 additions and 27 deletions

View file

@ -26,6 +26,7 @@ asynchronousLogstashHandler = AsynchronousLogstashHandler(
database_path='logs.db' database_path='logs.db'
) )
class StreamToLogger(object): class StreamToLogger(object):
""" """
Fake file-like stream object that redirects writes to a logger instance. Fake file-like stream object that redirects writes to a logger instance.
@ -53,6 +54,7 @@ class StreamToLogger(object):
self.linebuf = '' self.linebuf = ''
asynchronousLogstashHandler.flush() asynchronousLogstashHandler.flush()
try: try:
r = requests.head(f"http://{host}:{port}") r = requests.head(f"http://{host}:{port}")
if r.status_code == 400: if r.status_code == 400:
@ -67,12 +69,23 @@ 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): class Logging(object):
"""Can be used to manually use the logger or turn up the logging level used for debugging this library.
"""
@staticmethod @staticmethod
def get_logger() -> logging.Logger: def get_logger() -> logging.Logger:
"""Get the logger object used to communicate with logstash
:return: Python logger
:rtype: logging.Logger
"""
return logstash_logger return logstash_logger
@staticmethod @staticmethod
def set_debug(): def set_debug():
"""Turns up the logging level of the library to debug. Should be used, when debugging with the
Competition organizers
"""
logstash_logger.setLevel(logging.DEBUG) logstash_logger.setLevel(logging.DEBUG)

View file

@ -16,17 +16,21 @@ class Motor(object):
""" """
@staticmethod @staticmethod
def power(port: int, percent: int): def power(port: int, percent: float):
"""Set specified motor to percentage power """Set specified motor to percentage power
: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
:raises: IndexError
""" """
Logging.get_logger().debug(f"Motor.power {port} {percent}") Logging.get_logger().debug(f"Motor.power {port} {percent}")
if port < 0 or port >= MOTOR_COUNT: if port < 0 or port >= MOTOR_COUNT:
raise IndexError("Invalid Motor port specified!") raise IndexError("Invalid Motor port specified!")
if percent < -100 or percent > 100:
raise IndexError("Invalid Motor speed specified! Speed is between -100 and 100 percent!")
forward = True forward = True
if percent < 0: if percent < 0:
percent = abs(percent) percent = abs(percent)
@ -36,7 +40,7 @@ class Motor(object):
if port == 1: if port == 1:
forward = not forward forward = not forward
adjusted_speed = int(min(max(0, percent), 100) * MOTOR_PERCENTAGE_MULT) adjusted_speed = int(min(max(0.0, percent), 100.0) * MOTOR_PERCENTAGE_MULT)
if forward: if forward:
pwm.setMotorPwm(port * 2, adjusted_speed) pwm.setMotorPwm(port * 2, adjusted_speed)

View file

@ -35,7 +35,7 @@ extensions = [
'sphinx_rtd_theme' 'sphinx_rtd_theme'
] ]
autodoc_mock_imports = ["smbus", "compLib.PCA9685", "RPi"] autodoc_mock_imports = ["smbus", "compLib.PCA9685", "RPi", "pigpio", "flask"]
# Add any paths that contain templates here, relative to this directory. # Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates'] templates_path = ['_templates']

View file

@ -1,29 +1,29 @@
.. _lib_api: .. _lib_api:
Api Api
===== ****
Seeding Seeding
******** ========
.. autoclass:: compLib.Api.Seeding .. autoclass:: compLib.Api.Seeding
:members: :members:
Double Elimination Double Elimination
******************* ===================
.. autoclass:: compLib.Api.DoubleElim .. autoclass:: compLib.Api.DoubleElim
:members: :members:
Position Position
********* ========
.. autoclass:: compLib.Api.Position .. autoclass:: compLib.Api.Position
:members: :members:
Examples Examples
********* ========
Calling Seeding API Calling Seeding API
--------------------- ---------------------

View file

@ -1,20 +1,20 @@
.. _lib_battery: .. _lib_battery:
Battery Battery
######## ********
Class Documentation Class Documentation
******************** ====================
.. autoclass:: compLib.Battery.Battery .. autoclass:: compLib.Battery.Battery
:members: :members:
Examples Examples
********* =========
Printing percentage Printing percentage
==================== --------------------
.. code-block:: python .. code-block:: python

View file

@ -1,19 +1,19 @@
.. _lib_buzzer: .. _lib_buzzer:
Buzzer Buzzer
####### *******
Class Documentation Class Documentation
******************** ====================
.. autoclass:: compLib.Buzzer.Buzzer .. autoclass:: compLib.Buzzer.Buzzer
:members: :members:
Examples Examples
********* =========
Turning buzzer on and off Turning buzzer on and off
========================== --------------------------
.. code-block:: python .. code-block:: python

View file

@ -1,7 +1,7 @@
.. _lib_irsensor: .. _lib_irsensor:
Infrared Sensor Infrared Sensor
================ ****************
.. autoclass:: compLib.IRSensor.IRSensor .. autoclass:: compLib.IRSensor.IRSensor
:members: :members:

View file

@ -0,0 +1,22 @@
.. _lib_logging:
Logging
*******
Class Documentation
====================
.. autoclass:: compLib.LogstashLogging.Logging
:members:
Examples
=========
Turn up the logging
--------------------
.. code-block:: python
from compLib.LogstashLogging import Logging
Logging.set_debug()

View file

@ -1,19 +1,19 @@
.. _lib_motor: .. _lib_motor:
Motor Motor
###### ******
Class Documentation Class Documentation
******************** ====================
.. autoclass:: compLib.Motor.Motor .. autoclass:: compLib.Motor.Motor
:members: :members:
Examples Examples
********* =========
Driving straight (maybe) Driving straight (maybe)
========================= -------------------------
.. code-block:: python .. code-block:: python

View file

@ -1,7 +1,7 @@
.. _lib_servo: .. _lib_servo:
Servo Servo
===== ******
.. autoclass:: compLib.Servo.Servo .. autoclass:: compLib.Servo.Servo
:members: :members:

View file

@ -1,7 +1,7 @@
.. _lib_vision: .. _lib_vision:
Vision Vision
===== *******
This module provides an interface for grabbing an rtmp stream and using the images to do some processing in opencv. This module provides an interface for grabbing an rtmp stream and using the images to do some processing in opencv.
@ -13,7 +13,7 @@ How do I use this module?
4. You can view the http stream of your processed images in a web browser 4. You can view the http stream of your processed images in a web browser
Opencv Stream Opencv Stream
************* ==============
Because of the rtmp stream needing to buffer some frames and waiting for P-Frames, importing this module might take up Because of the rtmp stream needing to buffer some frames and waiting for P-Frames, importing this module might take up
to 5 Seconds. to 5 Seconds.
@ -22,10 +22,10 @@ to 5 Seconds.
:members: :members:
Examples Examples
********* =========
Using the Vision Module Using the Vision Module
--------------------- -------------------------
.. code-block:: python .. code-block:: python