mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-07-07 23:17:32 -06:00
Fix checking if still connected or not
This commit is contained in:
parent
77adbdd286
commit
945e5f08a3
2 changed files with 41 additions and 15 deletions
|
@ -1,6 +1,7 @@
|
||||||
# Copyright (c) 2019 Ultimaker B.V.
|
# Copyright (c) 2019 Ultimaker B.V.
|
||||||
# Cura is released under the terms of the LGPLv3 or higher.
|
# Cura is released under the terms of the LGPLv3 or higher.
|
||||||
import os
|
import os
|
||||||
|
from time import time
|
||||||
from typing import List, Optional, Dict
|
from typing import List, Optional, Dict
|
||||||
|
|
||||||
from PyQt5.QtCore import pyqtProperty, pyqtSignal, QObject, pyqtSlot, QUrl
|
from PyQt5.QtCore import pyqtProperty, pyqtSignal, QObject, pyqtSlot, QUrl
|
||||||
|
@ -10,7 +11,7 @@ from UM.Qt.Duration import Duration, DurationFormat
|
||||||
from cura.CuraApplication import CuraApplication
|
from cura.CuraApplication import CuraApplication
|
||||||
from cura.PrinterOutput.Models.PrinterOutputModel import PrinterOutputModel
|
from cura.PrinterOutput.Models.PrinterOutputModel import PrinterOutputModel
|
||||||
from cura.PrinterOutput.NetworkedPrinterOutputDevice import NetworkedPrinterOutputDevice, AuthState
|
from cura.PrinterOutput.NetworkedPrinterOutputDevice import NetworkedPrinterOutputDevice, AuthState
|
||||||
from cura.PrinterOutput.PrinterOutputDevice import ConnectionType
|
from cura.PrinterOutput.PrinterOutputDevice import ConnectionType, ConnectionState
|
||||||
|
|
||||||
from .Utils import formatTimeCompleted, formatDateCompleted
|
from .Utils import formatTimeCompleted, formatDateCompleted
|
||||||
from .ClusterOutputController import ClusterOutputController
|
from .ClusterOutputController import ClusterOutputController
|
||||||
|
@ -39,6 +40,9 @@ class UltimakerNetworkedPrinterOutputDevice(NetworkedPrinterOutputDevice):
|
||||||
# States indicating if a print job is queued.
|
# States indicating if a print job is queued.
|
||||||
QUEUED_PRINT_JOBS_STATES = {"queued", "error"}
|
QUEUED_PRINT_JOBS_STATES = {"queued", "error"}
|
||||||
|
|
||||||
|
# Time in seconds since last network response after which we consider this device offline.
|
||||||
|
NETWORK_RESPONSE_CONSIDER_OFFLINE = 10.0
|
||||||
|
|
||||||
def __init__(self, device_id: str, address: str, properties: Dict[bytes, bytes], connection_type: ConnectionType,
|
def __init__(self, device_id: str, address: str, properties: Dict[bytes, bytes], connection_type: ConnectionType,
|
||||||
parent=None) -> None:
|
parent=None) -> None:
|
||||||
super().__init__(device_id=device_id, address=address, properties=properties, connection_type=connection_type,
|
super().__init__(device_id=device_id, address=address, properties=properties, connection_type=connection_type,
|
||||||
|
@ -47,6 +51,9 @@ class UltimakerNetworkedPrinterOutputDevice(NetworkedPrinterOutputDevice):
|
||||||
# Trigger the printersChanged signal when the private signal is triggered.
|
# Trigger the printersChanged signal when the private signal is triggered.
|
||||||
self.printersChanged.connect(self._clusterPrintersChanged)
|
self.printersChanged.connect(self._clusterPrintersChanged)
|
||||||
|
|
||||||
|
# Keeps track the last network response to determine if we are still connected.
|
||||||
|
self._time_of_last_response = time()
|
||||||
|
|
||||||
# Set the display name from the properties
|
# Set the display name from the properties
|
||||||
self.setName(self.getProperty("name"))
|
self.setName(self.getProperty("name"))
|
||||||
|
|
||||||
|
@ -182,22 +189,28 @@ class UltimakerNetworkedPrinterOutputDevice(NetworkedPrinterOutputDevice):
|
||||||
def formatDuration(self, seconds: int) -> str:
|
def formatDuration(self, seconds: int) -> str:
|
||||||
return Duration(seconds).getDisplayString(DurationFormat.Format.Short)
|
return Duration(seconds).getDisplayString(DurationFormat.Format.Short)
|
||||||
|
|
||||||
## Load Monitor tab QML.
|
def _update(self) -> None:
|
||||||
def _loadMonitorTab(self):
|
self._checkStillConnected()
|
||||||
plugin_registry = CuraApplication.getInstance().getPluginRegistry()
|
|
||||||
if not plugin_registry:
|
|
||||||
Logger.log("e", "Could not get plugin registry")
|
|
||||||
return
|
|
||||||
plugin_path = plugin_registry.getPluginPath("UM3NetworkPrinting")
|
|
||||||
if not plugin_path:
|
|
||||||
Logger.log("e", "Could not get plugin path")
|
|
||||||
return
|
|
||||||
self._monitor_view_qml_path = os.path.join(plugin_path, "resources", "qml", "MonitorStage.qml")
|
|
||||||
|
|
||||||
def _update(self):
|
|
||||||
super()._update()
|
super()._update()
|
||||||
|
|
||||||
|
## Check if we're still connected by comparing the last timestamps for network response and the current time.
|
||||||
|
# This implementation is similar to the base NetworkedPrinterOutputDevice, but is tweaked slightly.
|
||||||
|
def _checkStillConnected(self) -> None:
|
||||||
|
time_since_last_response = time() - self._time_of_last_response
|
||||||
|
if time_since_last_response > self.NETWORK_RESPONSE_CONSIDER_OFFLINE:
|
||||||
|
self.setConnectionState(ConnectionState.Closed)
|
||||||
|
if self.key in CuraApplication.getInstance().getOutputDeviceManager().getOutputDeviceIds():
|
||||||
|
CuraApplication.getInstance().getOutputDeviceManager().removeOutputDevice(self.key)
|
||||||
|
elif self.connectionState == ConnectionState.Closed:
|
||||||
|
self.setConnectionState(ConnectionState.Connected)
|
||||||
|
if self.key not in CuraApplication.getInstance().getOutputDeviceManager().getOutputDeviceIds():
|
||||||
|
CuraApplication.getInstance().getOutputDeviceManager().addOutputDevice(self)
|
||||||
|
|
||||||
|
def _responseReceived(self) -> None:
|
||||||
|
self._time_of_last_response = time()
|
||||||
|
|
||||||
def _updatePrinters(self, remote_printers: List[ClusterPrinterStatus]) -> None:
|
def _updatePrinters(self, remote_printers: List[ClusterPrinterStatus]) -> None:
|
||||||
|
self._responseReceived()
|
||||||
|
|
||||||
# Keep track of the new printers to show.
|
# Keep track of the new printers to show.
|
||||||
# We create a new list instead of changing the existing one to get the correct order.
|
# We create a new list instead of changing the existing one to get the correct order.
|
||||||
|
@ -237,6 +250,7 @@ class UltimakerNetworkedPrinterOutputDevice(NetworkedPrinterOutputDevice):
|
||||||
## Updates the local list of print jobs with the list received from the cluster.
|
## Updates the local list of print jobs with the list received from the cluster.
|
||||||
# \param remote_jobs: The print jobs received from the cluster.
|
# \param remote_jobs: The print jobs received from the cluster.
|
||||||
def _updatePrintJobs(self, remote_jobs: List[ClusterPrintJobStatus]) -> None:
|
def _updatePrintJobs(self, remote_jobs: List[ClusterPrintJobStatus]) -> None:
|
||||||
|
self._responseReceived()
|
||||||
|
|
||||||
# Keep track of the new print jobs to show.
|
# Keep track of the new print jobs to show.
|
||||||
# We create a new list instead of changing the existing one to get the correct order.
|
# We create a new list instead of changing the existing one to get the correct order.
|
||||||
|
@ -279,3 +293,15 @@ class UltimakerNetworkedPrinterOutputDevice(NetworkedPrinterOutputDevice):
|
||||||
return
|
return
|
||||||
printer.updateActivePrintJob(model)
|
printer.updateActivePrintJob(model)
|
||||||
model.updateAssignedPrinter(printer)
|
model.updateAssignedPrinter(printer)
|
||||||
|
|
||||||
|
## Load Monitor tab QML.
|
||||||
|
def _loadMonitorTab(self) -> None:
|
||||||
|
plugin_registry = CuraApplication.getInstance().getPluginRegistry()
|
||||||
|
if not plugin_registry:
|
||||||
|
Logger.log("e", "Could not get plugin registry")
|
||||||
|
return
|
||||||
|
plugin_path = plugin_registry.getPluginPath("UM3NetworkPrinting")
|
||||||
|
if not plugin_path:
|
||||||
|
Logger.log("e", "Could not get plugin path")
|
||||||
|
return
|
||||||
|
self._monitor_view_qml_path = os.path.join(plugin_path, "resources", "qml", "MonitorStage.qml")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue