Commit 74ec0065 authored by Benjamin Mummery's avatar Benjamin Mummery 💻

Redefined base handler class to inheret from QObject since all current handlers…

Redefined base handler class to inheret from QObject since all current handlers use signal functionality
parent 2cc3812c
Pipeline #1421 failed with stages
......@@ -3,18 +3,15 @@ from handler_library.handler import PayloadHandler
from PySide2.QtCore import Signal, QObject
class BatteryHandler(PayloadHandler, QObject):
class BatteryHandler(PayloadHandler):
"""
Subclass of the PayloadHandler class (handler.py) to handle alarm data.
Inherits from QObject to give us access to pyside2's signal class.
"""
UpdateBatteryDisplay = Signal(dict)
def __init__(self, *args, **kwargs):
super().__init__(["BATTERY"], *args, **kwargs)
QObject.__init__(self)
def active_payload(self, *args, **kwargs) -> int:
"""
......
from handler_library.handler import PayloadHandler
from PySide2.QtCore import Signal, QObject, QTimer
from PySide2.QtCore import Signal
import numpy as np
from threading import Lock
class DataHandler(PayloadHandler, QObject):
class DataHandler(PayloadHandler):
UpdatePlots = Signal(dict)
def __init__(self, *args, plot_history_length=500, **kwargs):
super().__init__(["DATA"], *args, **kwargs)
QObject.__init__(self)
self.__plot_history_length = plot_history_length
self.__plots_database = {
"data": np.zeros((plot_history_length, 4)),
......
......@@ -3,35 +3,28 @@ handler.py
"""
from threading import Lock
from PySide2.QtCore import QObject
class PayloadHandler:
class GenericDataHandler(QObject):
"""
Base class for the payload data handlers.
Base class for non-payload data handlers.
"""
def __init__(self, payload_types: list):
for key in payload_types:
if not isinstance(key, str):
raise TypeError(
"payload types must be of type 'str', not %s", type(key)
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.__database = {}
self.__lock = Lock()
self.__payload_types = payload_types
def set_db(self, payload: dict) -> int:
def set_db(self, data: dict) -> int:
"""
If the provided database is of the correct type, copy its contents to the database
Copy the contents of 'data' to the internal database.
"""
if payload["type"] not in self.__payload_types:
return 1
with self.__lock:
for key in payload[payload["type"]]:
self.__database[key] = payload[payload["type"]][key]
for key in data:
self.__database[key] = data[key]
self.active_payload(payload)
self.on_data_set()
return 0
def get_db(self) -> dict:
......@@ -41,32 +34,41 @@ class PayloadHandler:
with self.__lock:
return dict(self.__database)
def active_payload(self, payload: dict):
def on_data_set(self):
"""
Overridable function called after recieving new data. Passes in the full payload
so that we have access to the full context of the information.
Overridable function called after recieving new data.
"""
pass
class GenericDataHandler:
class PayloadHandler(GenericDataHandler):
"""
Base class for non-payload data handlers.
Base class for the payload data handlers.
"""
def __init__(self):
def __init__(self, payload_types: list, *args, **kwargs):
super().__init__(*args, **kwargs)
for key in payload_types:
if not isinstance(key, str):
raise TypeError(
"payload types must be of type 'str', not %s", type(key)
)
self.__database = {}
self.__lock = Lock()
self.__payload_types = payload_types
def set_db(self, data: dict) -> int:
def set_db(self, payload: dict) -> int:
"""
Copy the contents of 'data' to the internal database.
If the provided database is of the correct type, copy its contents to the database
"""
if payload["type"] not in self.__payload_types:
return 1
with self.__lock:
for key in data:
self.__database[key] = data[key]
for key in payload[payload["type"]]:
self.__database[key] = payload[payload["type"]][key]
self.on_data_set()
self.active_payload(payload)
return 0
def get_db(self) -> dict:
......@@ -76,7 +78,9 @@ class GenericDataHandler:
with self.__lock:
return dict(self.__database)
def on_data_set(self):
def active_payload(self, payload: dict):
"""
Overridable function called after recieving new data.
Overridable function called after recieving new data. Passes in the full payload
so that we have access to the full context of the information.
"""
pass
......@@ -3,22 +3,19 @@ measurement_handler.py
"""
from handler_library.handler import PayloadHandler
from PySide2.QtCore import Signal, QObject
from PySide2.QtCore import Signal
import logging
class MeasurementHandler(PayloadHandler, QObject):
class MeasurementHandler(PayloadHandler):
"""
Subclass of the PayloadHandler class (handler.py) to handle cycle and readback data.
Inherits from QObject to give us access to pyside2's signal class.
"""
UpdateMeasurements = Signal(dict)
def __init__(self, *args, **kwargs):
super().__init__(["CYCLE", "READBACK"], *args, **kwargs)
QObject.__init__(self)
def active_payload(self, *args, **kwargs) -> int:
cycle_data = self.get_db()
......
......@@ -3,15 +3,13 @@ personal_handler.py
"""
from handler_library.handler import PayloadHandler
from PySide2.QtCore import Signal, QObject
from PySide2.QtCore import Signal
class PersonalHandler(PayloadHandler, QObject):
class PersonalHandler(PayloadHandler):
"""
Subclass of the PayloadHandler class (handler.py) to handle personal data.
Inherits from QObject to give us access to pyside2's signal class.
Adds the UpdatePersonalDisplay signal designed to convey information to be displayed
to the display widget.
"""
......@@ -20,7 +18,6 @@ class PersonalHandler(PayloadHandler, QObject):
def __init__(self, *args, **kwargs):
super().__init__(["PERSONAL"], *args, **kwargs)
QObject.__init__(self)
def active_payload(self, *args, **kwargs) -> int:
"""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment