Merge pull request #6198 from Ultimaker/CS-234_reduce_network_manager_instances

Only create API client when actually used
This commit is contained in:
Lipu Fei 2019-08-13 13:49:19 +02:00 committed by GitHub
commit 5294e721ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 15 deletions

View File

@ -161,15 +161,17 @@ class CloudOutputDeviceManager:
self._connectToOutputDevice(device, active_machine) self._connectToOutputDevice(device, active_machine)
elif local_network_key and device.matchesNetworkKey(local_network_key): elif local_network_key and device.matchesNetworkKey(local_network_key):
# Connect to it if we can match the local network key that was already present. # Connect to it if we can match the local network key that was already present.
active_machine.setMetaDataEntry(self.META_CLUSTER_ID, device.key)
self._connectToOutputDevice(device, active_machine) self._connectToOutputDevice(device, active_machine)
elif device.key in output_device_manager.getOutputDeviceIds(): elif device.key in output_device_manager.getOutputDeviceIds():
# Remove device if it is not meant for the active machine. # Remove device if it is not meant for the active machine.
output_device_manager.removeOutputDevice(device.key) output_device_manager.removeOutputDevice(device.key)
## Connects to an output device and makes sure it is registered in the output device manager. ## Connects to an output device and makes sure it is registered in the output device manager.
@staticmethod def _connectToOutputDevice(self, device: CloudOutputDevice, machine: GlobalStack) -> None:
def _connectToOutputDevice(device: CloudOutputDevice, active_machine: GlobalStack) -> None: machine.setName(device.name)
machine.setMetaDataEntry(self.META_CLUSTER_ID, device.key)
machine.setMetaDataEntry("group_name", device.name)
device.connect() device.connect()
active_machine.addConfiguredConnectionType(device.connectionType.value) machine.addConfiguredConnectionType(device.connectionType.value)
CuraApplication.getInstance().getOutputDeviceManager().addOutputDevice(device) CuraApplication.getInstance().getOutputDeviceManager().addOutputDevice(device)

View File

@ -38,16 +38,13 @@ class LocalClusterOutputDevice(UltimakerNetworkedPrinterOutputDevice):
parent=parent parent=parent
) )
# API client for making requests to the print cluster. self._cluster_api = None # type: Optional[ClusterApiClient]
self._cluster_api = ClusterApiClient(address, on_error=lambda error: print(error))
# We don't have authentication over local networking, so we're always authenticated. # We don't have authentication over local networking, so we're always authenticated.
self.setAuthenticationState(AuthState.Authenticated) self.setAuthenticationState(AuthState.Authenticated)
self._setInterfaceElements() self._setInterfaceElements()
self._active_camera_url = QUrl() # type: QUrl self._active_camera_url = QUrl() # type: QUrl
# Get the printers of this cluster to check if this device is a group host or not.
self._cluster_api.getPrinters(self._updatePrinters)
## Set all the interface elements and texts for this output device. ## Set all the interface elements and texts for this output device.
def _setInterfaceElements(self) -> None: def _setInterfaceElements(self) -> None:
self.setPriority(3) # Make sure the output device gets selected above local file output self.setPriority(3) # Make sure the output device gets selected above local file output
@ -81,11 +78,11 @@ class LocalClusterOutputDevice(UltimakerNetworkedPrinterOutputDevice):
@pyqtSlot(str, name="sendJobToTop") @pyqtSlot(str, name="sendJobToTop")
def sendJobToTop(self, print_job_uuid: str) -> None: def sendJobToTop(self, print_job_uuid: str) -> None:
self._cluster_api.movePrintJobToTop(print_job_uuid) self._getApiClient().movePrintJobToTop(print_job_uuid)
@pyqtSlot(str, name="deleteJobFromQueue") @pyqtSlot(str, name="deleteJobFromQueue")
def deleteJobFromQueue(self, print_job_uuid: str) -> None: def deleteJobFromQueue(self, print_job_uuid: str) -> None:
self._cluster_api.deletePrintJob(print_job_uuid) self._getApiClient().deletePrintJob(print_job_uuid)
@pyqtSlot(str, name="forceSendJob") @pyqtSlot(str, name="forceSendJob")
def forceSendJob(self, print_job_uuid: str) -> None: def forceSendJob(self, print_job_uuid: str) -> None:
@ -95,12 +92,12 @@ class LocalClusterOutputDevice(UltimakerNetworkedPrinterOutputDevice):
# \param print_job_uuid: The UUID of the print job to set the state for. # \param print_job_uuid: The UUID of the print job to set the state for.
# \param action: The action to undertake ('pause', 'resume', 'abort'). # \param action: The action to undertake ('pause', 'resume', 'abort').
def setJobState(self, print_job_uuid: str, action: str) -> None: def setJobState(self, print_job_uuid: str, action: str) -> None:
self._cluster_api.setPrintJobState(print_job_uuid, action) self._getApiClient().setPrintJobState(print_job_uuid, action)
def _update(self) -> None: def _update(self) -> None:
super()._update() super()._update()
self._cluster_api.getPrinters(self._updatePrinters) self._getApiClient().getPrinters(self._updatePrinters)
self._cluster_api.getPrintJobs(self._updatePrintJobs) self._getApiClient().getPrintJobs(self._updatePrintJobs)
self._updatePrintJobPreviewImages() self._updatePrintJobPreviewImages()
## Sync the material profiles in Cura with the printer. ## Sync the material profiles in Cura with the printer.
@ -162,4 +159,10 @@ class LocalClusterOutputDevice(UltimakerNetworkedPrinterOutputDevice):
def _updatePrintJobPreviewImages(self): def _updatePrintJobPreviewImages(self):
for print_job in self._print_jobs: for print_job in self._print_jobs:
if print_job.getPreviewImage() is None: if print_job.getPreviewImage() is None:
self._cluster_api.getPrintJobPreviewImage(print_job.key, print_job.updatePreviewImageData) self._getApiClient().getPrintJobPreviewImage(print_job.key, print_job.updatePreviewImageData)
## Get the API client instance.
def _getApiClient(self) -> ClusterApiClient:
if not self._cluster_api:
self._cluster_api = ClusterApiClient(self.address, on_error=lambda error: print(error))
return self._cluster_api