How do you get pyqtSignal to properly emit a signal that the rpyc service has been turned on?

31 Views Asked by At

I have two widgets that are in two separate python files. They are: A) temperature_service: acts as the rpyc service that turns off and on the service and encodes the instructions for the tasks my various clients want to carry out. B) temperature of B: acts as the client that connects to the service and asks for the temperatures every 10 seconds.

My issue is that I cannot get my client to only start asking for temperatures after the service has been turned on via the pushbutton 'Connect'. I want to do it such that when the button connect is clicked, this sends a signal to the client that the service is 'ON' and that the client can now ask for temperature values.

The service widget:

class StartServerThread(QtCore.QThread):
    def __init__(self, temp_service):
        super().__init__()
        self.temp_service = temp_service
        
    def run(self):
        self.temp_service.start_server()

class StopServerThread(QtCore.QThread):
    def __init__(self, temp_service):
        super().__init__()
        self.temp_service = temp_service

    def run(self):
        self.temp_service.stop_server()


class Ui_temp_serv(QObject):
    start_collection_signal = pyqtSignal()

    def __init__(self):
        super().__init__()
        self.thread = None
        self.temp_service = TemperatureService()
        self.server_running = False

    def setupUi(self, temp_serv):
    # Connect the "CONNECT"/"DISCONNECT" button to start/end the server
        self.temp_service_conn.clicked.connect(self.start_server_thread)
        self.temp_service_disconn.clicked.connect(self.stop_server_thread)


  def start_server_thread(self):
        if self.thread and self.thread.isRunning():
            self.thread.terminate()  # Terminate the previous thread if still running
        self.thread = StartServerThread(self.temp_service)
        self.thread.start()
        self.server_running = True
        self.start_collection_signal.emit()

    def stop_server_thread(self):
        if self.thread and self.thread.isRunning():
            self.thread.terminate()  # Terminate the previous thread if still running
        self.thread = StopServerThread(self.temp_service)
        self.thread.start()
        self.server_running = False

The temperature client:

from temperature_service import Ui_temp_serv

class TemperatureAClient:
    def __init__(self, connection):
        self.connection = connection

    def get_temperature(self):
        return self.connection.temperature_service.exposed_get_temperature()

class Ui_temp_B(object):
    def setupUi(self, temp_B):
     self.ui_temp_serv = Ui_temp_serv()
        self.ui_temp_serv.start_collection_signal.connect(self.start_data_collection)
        self.timer = QTimer(temp_B)
        self.timer.timeout.connect(self.collect_values)
        self.interval = 10000

def start_data_collection(self):
        if not self.ui_temp_serv.server_running:
            print("Server is not running. Cannot collect data")
        self.collect_values()
        self.timer.start(self.interval)

    def collect_values(self):
        connection = Temp_Connection()
        client = TemperatureAClient(connection)
        _, _, round_2 = client.get_temperature()

        if round_2:
            for entry in round_2:
                mean_value =  round(entry['mean'], 3)
                min_value = round(entry['min'], 3)
                max_value = round(entry['max'], 3)

                self.B_mean_temp.setText(str(mean_value))
                self.B_min_temp.setText(str(min_value))
                self.B_max_temp.setText(str(max_value))
                timestamp = datetime.datetime.now().strftime("%H:%M:%S")
                self.B_timestamp.setText(timestamp)
        else:
            print("Round 2 statistics list is empty.")


What I have done above gives me the following issue: when I have both widgets open and click the CONNECT button, the service says 'Server started successfully' but then nothing happens. No measurements are taken and nothing is updated on the client.

0

There are 0 best solutions below