Camera can now be started & stopped to prevent bandwith issues

CURA-2411
This commit is contained in:
Jaime van Kessel 2016-11-04 09:42:05 +01:00
parent e7d04f7dc0
commit bf670d325a

View file

@ -124,14 +124,14 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice):
self._update_timer.timeout.connect(self._update) self._update_timer.timeout.connect(self._update)
self._camera_timer = QTimer() self._camera_timer = QTimer()
self._camera_timer.setInterval(2000) # Todo: Add preference for camera update interval self._camera_timer.setInterval(500) # Todo: Add preference for camera update interval
self._camera_timer.setSingleShot(False) self._camera_timer.setSingleShot(False)
self._camera_timer.timeout.connect(self._update_camera) self._camera_timer.timeout.connect(self._updateCamera)
self._image_request = None self._image_request = None
self._image_reply = None self._image_reply = None
self._use_stream = False self._use_stream = True
self._stream_buffer = b"" self._stream_buffer = b""
self._stream_buffer_start_index = -1 self._stream_buffer_start_index = -1
@ -233,14 +233,28 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice):
def ipAddress(self): def ipAddress(self):
return self._address return self._address
def _start_camera_stream(self): def _stopCamera(self):
self._camera_timer.stop()
if self._image_reply:
self._image_reply.abort()
self._image_reply.downloadProgress.disconnect(self._onStreamDownloadProgress)
self._image_reply = None
self._image_request = None
def _startCamera(self):
if self._use_stream:
self._startCameraStream()
else:
self._camera_timer.start()
def _startCameraStream(self):
## Request new image ## Request new image
url = QUrl("http://" + self._address + ":8080/?action=stream") url = QUrl("http://" + self._address + ":8080/?action=stream")
self._image_request = QNetworkRequest(url) self._image_request = QNetworkRequest(url)
self._image_reply = self._manager.get(self._image_request) self._image_reply = self._manager.get(self._image_request)
self._image_reply.downloadProgress.connect(self._onStreamDownloadProgress) self._image_reply.downloadProgress.connect(self._onStreamDownloadProgress)
def _update_camera(self): def _updateCamera(self):
if not self._manager.networkAccessible(): if not self._manager.networkAccessible():
return return
## Request new image ## Request new image
@ -489,13 +503,8 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice):
# Stop update timers # Stop update timers
self._update_timer.stop() self._update_timer.stop()
self._camera_timer.stop()
if self._image_reply: self.stopCamera()
self._image_reply.abort()
self._image_reply.downloadProgress.disconnect(self._onStreamDownloadProgress)
self._image_reply = None
self._image_request = None
## Request the current scene to be sent to a network-connected printer. ## Request the current scene to be sent to a network-connected printer.
# #
@ -625,7 +634,7 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice):
self.setConnectionState(ConnectionState.connecting) self.setConnectionState(ConnectionState.connecting)
self._update() # Manually trigger the first update, as we don't want to wait a few secs before it starts. self._update() # Manually trigger the first update, as we don't want to wait a few secs before it starts.
if not self._use_stream: if not self._use_stream:
self._update_camera() self._updateCamera()
Logger.log("d", "Connection with printer %s with ip %s started", self._key, self._address) Logger.log("d", "Connection with printer %s with ip %s started", self._key, self._address)
## Check if this machine was authenticated before. ## Check if this machine was authenticated before.
@ -633,10 +642,7 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice):
self._authentication_key = Application.getInstance().getGlobalContainerStack().getMetaDataEntry("network_authentication_key", None) self._authentication_key = Application.getInstance().getGlobalContainerStack().getMetaDataEntry("network_authentication_key", None)
self._update_timer.start() self._update_timer.start()
if self._use_stream: #self.startCamera()
self._start_camera_stream()
else:
self._camera_timer.start()
## Stop requesting data from printer ## Stop requesting data from printer
def disconnect(self): def disconnect(self):
@ -969,6 +975,8 @@ class NetworkPrinterOutputDevice(PrinterOutputDevice):
def _onStreamDownloadProgress(self, bytes_received, bytes_total): def _onStreamDownloadProgress(self, bytes_received, bytes_total):
# An MJPG stream is (for our purpose) a stream of concatenated JPG images. # An MJPG stream is (for our purpose) a stream of concatenated JPG images.
# JPG images start with the marker 0xFFD8, and end with 0xFFD9 # JPG images start with the marker 0xFFD8, and end with 0xFFD9
if self._image_reply is None:
return
self._stream_buffer += self._image_reply.readAll() self._stream_buffer += self._image_reply.readAll()
if self._stream_buffer_start_index == -1: if self._stream_buffer_start_index == -1: