<commit_before>import typing

from PyQt5 import QtWidgets, QtGui

from .devicestatus_ui import Ui_Form
from ....core.devices.device import Device
from ....core.services.telemetry import TelemetryInfo


class DeviceStatus(QtWidgets.QWidget, Ui_Form):
    def __init__(self, parent, device: Device):
        super().__init__(parent)
        self.device = device
        self._connections = []
        self.setupUi(self)

    def setupUi(self, Form):
        Ui_Form.setupUi(self, Form)
        self.deviceNameLabel.setText(self.device.name)
        self._connections = [self.device.connect('telemetry', self.onDeviceTelemetry),
                             self.device.connect('disconnect', self.onDeviceDisconnect),
                             self.device.connect('variable-change', self.onDeviceVariableChange),
                             self.device.connect('ready', self.onDeviceReady)]
        self.checkIfReady(self.device)
        self.onDeviceVariableChange(self.device, '_status', self.device.get_variable('_status'))

    def onDeviceReady(self, device: Device):
        self.checkIfReady(device)

    def onDeviceVariableChange(self, device: Device, var: str, value: typing.Any):
        if var == '_status':
            status = value
            auxstatus = device.get_variable('_auxstatus')
        elif var == '_auxstatus':
            status = device.get_variable('_status')
            auxstatus = value
        else:
            return False
        self.deviceStatusLabel.setText('{} ({})'.format(status, auxstatus))
        self.checkIfDeviceIsConnected(device)
        self.checkIfReady(device)
        self.checkIfBusy(device)
        return False

    def checkIfDeviceIsConnected(self, device: Device):
        if device.get_connected():
            color = QtGui.QColor('lightgreen')
            tooltip = 'Connected to {}'.format(':'.join([str(x) for x in device.deviceconnectionparameters]))
        else:
            color = QtGui.QColor('red')
            tooltip = 'Disconnected'
        self.setLabelBackgroundColor(self.connectedLabel, color)
        self.connectedLabel.setToolTip(tooltip)

    def checkIfReady(self, device: Device):
        if device.is_ready():
            color = QtGui.QColor('lightgreen')
            tooltip = 'Device is ready'
        else:
            color = QtGui.QColor('red')
            tooltip = 'Device is not ready'
        self.setLabelBackgroundColor(self.readyLabel, color)
        self.readyLabel.setToolTip(tooltip)

    def checkIfBusy(self, device: Device):
        if device.is_busy():
            color = QtGui.QColor('yellow')
            tooltip = 'Device is busy'
            text = 'BU'
        else:
            color = QtGui.QColor('lightgreen')
            tooltip = 'Device is idle'
            text = 'ID'
        self.setLabelBackgroundColor(self.busyLabel, color)
        self.busyLabel.setToolTip(tooltip)
        self.busyLabel.setText(text)

    def onDeviceDisconnect(self, device: Device, unexpected: bool):
        self.checkIfDeviceIsConnected(device)
        self.checkIfReady(device)

    def cleanup(self):
        for c in self._connections:
            self.device.disconnect(c)
        self._connections = []

    def onDeviceTelemetry(self, device: Device, telemetry: TelemetryInfo):
        if telemetry.last_recv > 5:
            color = QtGui.QColor('red')
            tooltip = 'Device is not responding. Last message received {:.3f} seconds ago'.format(telemetry.last_recv)
        else:
            color = QtGui.QColor('lightgreen')<commit_msg>Showing GeniX busy status in mainwindow correctly<commit_after>import typing

from PyQt5 import QtWidgets, QtGui

from .devicestatus_ui import Ui_Form
from ....core.devices.device import Device
from ....core.services.telemetry import TelemetryInfo


class DeviceStatus(QtWidgets.QWidget, Ui_Form):
    def __init__(self, parent, device: Device):
        super().__init__(parent)
        self.device = device
        self._connections = []
        self.setupUi(self)

    def setupUi(self, Form):
        Ui_Form.setupUi(self, Form)
        self.deviceNameLabel.setText(self.device.name)
        self._connections = [self.device.connect('telemetry', self.onDeviceTelemetry),
                             self.device.connect('disconnect', self.onDeviceDisconnect),
                             self.device.connect('variable-change', self.onDeviceVariableChange),
                             self.device.connect('ready', self.onDeviceReady)]
        self.checkIfReady(self.device)
        self.onDeviceVariableChange(self.device, '_status', self.device.get_variable('_status'))

    def onDeviceReady(self, device: Device):
        self.checkIfReady(device)

    def onDeviceVariableChange(self, device: Device, var: str, value: typing.Any):
        status = device.get_variable('_status')
        if var == '_status':
            status = value
            auxstatus = device.get_variable('_auxstatus')
        elif var == '_auxstatus':
            status = device.get_variable('_status')
            auxstatus = value
        else:
            return False
        self.deviceStatusLabel.setText('{} ({})'.format(status, auxstatus))
        self.checkIfDeviceIsConnected(device)
        self.checkIfReady(device)
        self.checkIfBusy(device, status=status)
        return False

    def checkIfDeviceIsConnected(self, device: Device):
        if device.get_connected():
            color = QtGui.QColor('lightgreen')
            tooltip = 'Connected to {}'.format(':'.join([str(x) for x in device.deviceconnectionparameters]))
        else:
            color = QtGui.QColor('red')
            tooltip = 'Disconnected'
        self.setLabelBackgroundColor(self.connectedLabel, color)
        self.connectedLabel.setToolTip(tooltip)

    def checkIfReady(self, device: Device):
        if device.is_ready():
            color = QtGui.QColor('lightgreen')
            tooltip = 'Device is ready'
        else:
            color = QtGui.QColor('red')
            tooltip = 'Device is not ready'
        self.setLabelBackgroundColor(self.readyLabel, color)
        self.readyLabel.setToolTip(tooltip)

    def checkIfBusy(self, device: Device, status=None):
        if device.is_busy(status):
            color = QtGui.QColor('yellow')
            tooltip = 'Device is busy'
            text = 'BU'
        else:
            color = QtGui.QColor('lightgreen')
            tooltip = 'Device is idle'
            text = 'ID'
        self.setLabelBackgroundColor(self.busyLabel, color)
        self.busyLabel.setToolTip(tooltip)
        self.busyLabel.setText(text)

    def onDeviceDisconnect(self, device: Device, unexpected: bool):
        self.checkIfDeviceIsConnected(device)
        self.checkIfReady(device)

    def cleanup(self):
        for c in self._connections:
            self.device.disconnect(c)
        self._connections = []

    def onDeviceTelemetry(self, device: Device, telemetry: TelemetryInfo):
        if telemetry.last_recv > 5:
            color = QtGui.QColor('red')
            tooltip = 'Device is not responding. Last message received {:.3f} seconds ago'.format(telemetry.last_recv)
        else:
            color = QtGui.QColor('lightgreen')
{'fraction_non_alphanumeric': 0.1065891472868217, 'fraction_numerical': 0.0008305647840531562, 'mean_word_length': 2.448687350835322, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '367266', 'n_tokens_mistral': 2020, 'n_tokens_neox': 1965, 'n_words': 417}