diff options
Diffstat (limited to 'extras/FeedKuksa.py')
-rw-r--r-- | extras/FeedKuksa.py | 81 |
1 files changed, 63 insertions, 18 deletions
diff --git a/extras/FeedKuksa.py b/extras/FeedKuksa.py index d75bda0..903b442 100644 --- a/extras/FeedKuksa.py +++ b/extras/FeedKuksa.py @@ -15,41 +15,86 @@ """ import time +import logging from PyQt5.QtCore import QThread from . import Kuksa_Instance as kuksa_instance class FeedKuksa(QThread): + """ + A class to handle sending values to Kuksa. + + Attributes: + ----------- + stop_flag : bool + A flag to stop the thread. + kuksa : kuksa_instance.KuksaClientSingleton.instance() + An instance of the Kuksa client. + client : kuksa_instance.KuksaClientSingleton.instance().client + A client object to interact with the Kuksa server. + """ + def __init__(self, parent=None): + """ + Constructs all the necessary attributes for the FeedKuksa object. + + Parameters: + ----------- + parent : QObject + The parent object of the FeedKuksa object. + """ QThread.__init__(self,parent) self.stop_flag = False - self.set_instance() def run(self): - print("Starting thread") + """ + Starts the thread and sets the instance of the Kuksa client. + """ + logging.info("Starting thread") self.set_instance() - while not self.stop_flag: - self.send_values() def stop(self): + """ + Stops the thread. + """ self.stop_flag = True - print("Stopping thread") + logging.info("Stopping thread") def set_instance(self): - self.kuksa = kuksa_instance.KuksaClientSingleton.get_instance() - self.client = self.kuksa.get_client() + """ + Sets the instance of the Kuksa client. + """ + self.kuksa = kuksa_instance.KuksaClientSingleton.instance() + self.client = self.kuksa.client + + def send_values(self, path=None, value=None, attribute=None): + """ + Sends values to Kuksa. + + Parameters: + ----------- + path : str + The path to the value in Kuksa. + value : str + The value to be sent to Kuksa. + attribute : str + The attribute of the value in Kuksa. - def send_values(self, Path=None, Value=None, Attribute=None): + Raises: + ------- + Exception + If there is an error sending values to Kuksa. + """ if self.client is not None: - if self.client.checkConnection() is True: - - if Attribute is not None: - self.client.setValue(Path, Value, Attribute) - else: - self.client.setValue(Path, Value) - else: - print("Could not connect to Kuksa") - self.set_instance() + if self.client.checkConnection(): + try: + if attribute is not None: + self.client.setValue(path, str(value), attribute) + else: + self.client.setValue(path, str(value)) + except Exception as e: + logging.error(f"Error sending values to kuksa {e}") + self.set_instance() else: - print("Kuksa client is None, try reconnecting") + logging.error("Kuksa client is None, try reconnecting") time.sleep(2) self.set_instance()
\ No newline at end of file |