Why is my update_values not receiving the signal from starting the service?

42 Views Asked by At

I am creating a GUI using pyqt5 and rpyc. I have two widgets that are in two separate python files:

  1. powersupply_service which essentially turns on the service for other widgets to connect to.
from PowerSupply_service import PowerSupplyService

from A_power_out import Ui_A_power_out


from rpyc.utils.server import ThreadedServer
import logging
from threading import Thread

# Configure logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

class StartServerThread(QtCore.QThread):
    server_started = QtCore.pyqtSignal()

    def __init__(self, power_supply_service, ui_A_power_out):
        super().__init__()
        self.power_supply_service = power_supply_service
        self.ui_A_power_out = ui_A_power_out  # Pass the instance, not the class

    def run(self):
        self.power_supply_service.start_server()
        print("Signal emitted")
        self.server_started.emit()
        self.ui_A_power_out.server_running = True

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

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

class Ui_powersupply_service_2(object):
    def __init__(self):
        self.thread = None
        self.power_supply_service = PowerSupplyService()
        self.ui_A_power_out = Ui_A_power_out()  # Initialize Ui_A_power_out instance as None initially

    def setupUi(self, powersupply_service_2):
        powersupply_service_2.setObjectName("powersupply_service_2")
        powersupply_service_2.resize(442, 300)
        self.Power_service_widget = QtWidgets.QWidget(powersupply_service_2)
        self.Power_service_widget.setGeometry(QtCore.QRect(20, 40, 401, 71))
        self.Power_service_widget.setObjectName("Power_service_widget")

        self.powersupply_service = QtWidgets.QLabel(self.Power_service_widget)
        self.powersupply_service.setGeometry(QtCore.QRect(10, 20, 211, 31))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(12)
        self.powersupply_service.setFont(font)
        self.powersupply_service.setObjectName("powersupply_service")

        self.power_service_conn = QtWidgets.QPushButton(self.Power_service_widget)
        self.power_service_conn.setGeometry(QtCore.QRect(210, 20, 71, 28))
        self.power_service_conn.setObjectName("power_service_conn")

        self.power_service_disconn = QtWidgets.QPushButton(self.Power_service_widget)
        self.power_service_disconn.setGeometry(QtCore.QRect(290, 20, 91, 28))
        self.power_service_disconn.setObjectName("power_service_disconn")

        self.retranslateUi(powersupply_service_2)
        QtCore.QMetaObject.connectSlotsByName(powersupply_service_2)

        # Connect the "CONNECT"/"DISCONNECT" button to start/end the server
        self.power_service_conn.clicked.connect(self.start_server_thread)
        self.power_service_disconn.clicked.connect(self.stop_server_thread)

    def retranslateUi(self, powersupply_service_2):
        _translate = QtCore.QCoreApplication.translate
        powersupply_service_2.setWindowTitle(_translate("powersupply_service_2", "Form"))
        self.powersupply_service.setText(_translate("powersupply_service_2", "Power supply service:"))
        self.power_service_conn.setText(_translate("powersupply_service_2", "CONNECT"))
        self.power_service_disconn.setText(_translate("powersupply_service_2", "DISCONNECT"))

    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.power_supply_service, self.ui_A_power_out)
        self.thread.server_started.connect(self.ui_A_power_out.update_values)
        self.thread.start()

    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.power_supply_service)
        self.thread.start()
  1. A_power_out which connects to the service, finds the voltage value and updates the value displayed on the widget every 3 seconds.
class PowerDisplayClient:
    def __init__(self, connection):
        self.connection = connection

    def set_address(self, address):
        self.connection.power_service.exposed_set_address(address)
    
    def get_identity(self):
        return self.connection.power_service.exposed_get_identity()

    def get_voltage(self):
        return self.connection.power_service.exposed_get_voltage()
    
    def get_current(self):
        return self.connection.power_service.exposed_get_current()



class Ui_A_power_out(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.server_running = False


    def setupUi(self, A_power_out):
        A_power_out.setObjectName("A_power_out")
        A_power_out.resize(687, 428)
        self.A_power_out = QtWidgets.QWidget(A_power_out)
        self.A_power_out.setObjectName("A_power_out")

        font = QtGui.QFont()
        font.setPointSize(10)

        self.P_out_label = QtWidgets.QLabel(self.A_power_out)
        self.P_out_label.setGeometry(QtCore.QRect(30, 90, 121, 31))
        self.P_out_label.setFont(font)
        self.P_out_label.setObjectName("P_out_label")

        self.A_I_out_label = QtWidgets.QLabel(self.A_power_out)
        self.A_I_out_label.setGeometry(QtCore.QRect(30, 60, 121, 31))
        self.A_I_out_label.setFont(font)
        self.A_I_out_label.setObjectName("A_I_out_label")

        self.A_V_out_label = QtWidgets.QLabel(self.A_power_out)
        self.A_V_out_label.setGeometry(QtCore.QRect(30, 30, 121, 31))
        self.A_V_out_label.setFont(font)
        self.A_V_out_label.setObjectName("A_V_out_label")

        self.A_V_out = QtWidgets.QLabel(self.A_power_out)  # Initialize A_V_out
        self.A_V_out.setGeometry(QtCore.QRect(150, 40, 55, 16))
        self.A_V_out.setText("")
        self.A_V_out.setObjectName("A_V_out")
        self.A_V_out.setFont(font)

        self.retranslateUi(A_power_out)
        QtCore.QMetaObject.connectSlotsByName(A_power_out)

        self.timer = QtCore.QTimer(A_power_out)
        self.timer.timeout.connect(self.update_values)
        self.timer.start(3000)

    def update_values(self):
        print("Updating values...")  # Add this line for debugging
        if not self.server_running:
            print("Server not running, skipping update")  # Add this line for debugging
            return 
        connection = Connection()
        client = PowerDisplayClient(connection)
        address_A = 1
        client.set_address(address_A)
        client.get_identity()
        voltage = client.get_voltage()
        current = client.get_current()
        power = voltage * current
        self.A_V_out.setText(str(voltage))
        #self.A_I_out.setText(str(current))
        #self.A_P_out.setText(str(power))

My issue is that even when the service has been turned on and the signal that it is on is sent out, the update_values is not receiving the signal and hence is never getting out of the if not self.server_running statement.

I have tried to debug and have narrowed down the issue to the fact that update_values is not receiving the signal that the service is on, but I am unsure how to correct this.

0

There are 0 best solutions below