def download_print_file(self, print_file_id, progressCb, successCb, errorCb): progressCb(1) try: r = requests.get('%s/print-files/%s' % (self.apiHost, print_file_id), auth=self.hmacAuth) data = r.json() except: data = None destFile = None if data and "download_url" in data and "name" in data and "info" in data: progressCb(2) destFile = printerManager().fileManager.getAbsolutePath(data['name'], mustExist=False) if destFile: downloadManager().startDownload({ 'downloadUrl': data["download_url"], 'destFile': destFile, 'printFileId': print_file_id, 'printFileInfo': data['info'], 'progressCb': progressCb, 'successCb': successCb, 'errorCb': errorCb }) return True else: errorCb(destFile, 'Unable to download file') return False
def download_print_file(self, print_file_id, progressCb, successCb, errorCb): fileManager = printerManager().fileManager printFile = fileManager.getFileByCloudId(print_file_id) if printFile: self._logger.error("Print file %s is already on the box as %s" % (print_file_id, printFile)) successCb(printFile, True) return True # The file wasn't there so let's go get it progressCb(1) try: r = requests.get("%s/print-files/%s" % (self.apiHost, print_file_id), auth=self.hmacAuth) data = r.json() except: data = None destFile = None if data and "download_url" in data and "name" in data and "info" in data: progressCb(2) destFile = fileManager.getAbsolutePath(data["name"], mustExist=False) if destFile: downloadManager().startDownload( { "downloadUrl": data["download_url"], "destFile": destFile, "printFileId": print_file_id, "printFileInfo": data["info"], "progressCb": progressCb, "successCb": successCb, "errorCb": errorCb, } ) return True else: errorCb(destFile, "Unable to download file") return False
def cancel_download(self, data, clientId, done): from astroprint.printfiles.downloadmanager import downloadManager print_file_id = data['printFileId'] if not downloadManager().cancelDownload(print_file_id): done({ 'error': True, 'message': 'Unable to cancel download' }) return done(None)
def cancel_design_download(print_file_id): if downloadManager().cancelDownload(print_file_id): return jsonify(SUCCESS) else: return abort(404)
def cancelDownloadPrintFile(self, printFileId, sendResponse): if downloadManager().cancelDownload(printFileId): sendResponse({'success':'no error'}) else: sendResponse('cancel_error',True)
def download_print_file(self, print_file_id, progressCb, successCb, errorCb): dm = downloadManager() if dm.isDownloading(print_file_id): #We just return, there's already a download for this file in process #which means that the events will be issued for that one. return True fileManager = printerManager().fileManager try: r = requests.get('%s/print-files/%s' % (self.apiHost, print_file_id), auth=self.hmacAuth) data = r.json() except Exception as e: data = None self._logger.error('Unable to get file info: %s' % e , exc_info = True) printFile = fileManager.getFileByCloudId(print_file_id) if printFile: self._logger.info('Print file %s is already on the box as %s' % (print_file_id, printFile)) if data and "printFileName" in data: pm = printerManager() localPrintFileName = pm.fileManager.getPrintFileName(printFile) if data["printFileName"] != localPrintFileName: pm.fileManager.setPrintFileName(printFile, data["printFileName"]) #update printFileName for this printFile in the collection if self._print_file_store: for x in self._print_file_store: if x['id'] == print_file_id: x['printFileName'] = data["printFileName"] break successCb(printFile, True) return True #The file wasn't there so let's go get it progressCb(1) destFile = None destFilename = None printFileName = None printer = None material = None quality = None image = None created = None if data and "download_url" in data and (("name" in data) or ("filename" in data)) and "info" in data: progressCb(2) if "filename" in data: destFilename = data['filename'] printFileName = data["printFileName"] else: destFilename = printFileName = data['name'] destFilename, destFileExt = os.path.splitext(destFilename) if destFileExt[1:].lower() not in fileManager.SUPPORTED_EXTENSIONS: return {"id": "wrong_file_type", "message": "The print file format is not compatible with the configured printer"} if "printer" in data: printer = data['printer'] if "material" in data: material = data['material'] if "quality" in data: quality = data['quality'] if "image" in data: image = data['image'] if "created" in data: created = data['created'] destFile = fileManager.getAbsolutePath(destFilename + destFileExt, mustExist=False) if destFile: def onSuccess(pf, error): self._print_file_store = None successCb(pf, error) dm.startDownload({ 'downloadUrl': data["download_url"], 'destFile': destFile, 'printFileId': print_file_id, 'printFileInfo': data['info'], 'printFileName': printFileName, 'printer': printer, 'material': material, 'quality': quality, 'image': image, 'created': created, 'progressCb': progressCb, 'successCb': onSuccess, 'errorCb': errorCb }) return True else: errorCb(destFile, 'Unable to download file') return {"id": "invalid_data", "message": "Invalid data from server. Can't retrieve print file"}
def received_message(self, m): self._lastReceived = time() msg = json.loads(str(m)) printer = printerManager() if msg['type'] == 'auth': self._router.processAuthenticate(msg['data'] if 'data' in msg else None) elif msg['type'] == 'set_temp': if printer.isOperational(): payload = msg['payload'] printer.setTemperature(payload['target'] or 0.0, payload['value'] or 0.0) elif msg['type'] == 'update_subscribers': self._subscribers += int(msg['data']) if not self._printerListener and self._subscribers > 0: self.registerEvents() elif self._printerListener and self._subscribers <= 0: self._subscribers = 0 self.unregisterEvents() elif msg['type'] == 'request': try: reqId = msg['reqId'] request = msg['data']['type'] data = msg['data']['payload'] if request == 'initial_state': response = { 'printing': printer.isPrinting(), 'operational': printer.isOperational(), 'paused': printer.isPaused(), 'camera': printer.isCameraConnected(), 'printCapture': self._cameraManager.timelapseInfo, 'profile': self._profileManager.data, 'remotePrint': True } elif request == 'job_info': response = printer._stateMonitor._jobData elif request == 'printerCommand': command = data['command'] options = data['options'] response = {'success': True} if command == 'pause' or command == 'resume': printer.togglePausePrint(); elif command == 'cancel': printer.cancelPrint(); elif command == 'photo': response['image_data'] = base64.b64encode(self._cameraManager.get_pic()) else: response = { 'error': True, 'message': 'Printer command [%s] is not supported' % command } elif request == 'printCapture': freq = data['freq'] if freq: if self._cameraManager.timelapseInfo: if self._cameraManager.update_timelapse(freq): response = {'success': True} else: response = { 'error': True, 'message': 'Error updating the print capture' } else: if self._cameraManager.start_timelapse(freq): response = {'success': True} else: response = { 'error': True, 'message': 'Error creating the print capture' } else: response = { 'error': True, 'message': 'Frequency required' } elif request == 'signoff': from astroprint.cloud import astroprintCloud self._logger.info('Remote signoff requested.') threading.Timer(1, astroprintCloud().remove_logged_user).start() response = {'success': True} elif request == 'print_file': from astroprint.cloud import astroprintCloud from astroprint.printfiles import FileDestinations print_file_id = data['printFileId'] em = eventManager() def progressCb(progress): em.fire( Events.CLOUD_DOWNLOAD, { "type": "progress", "id": print_file_id, "progress": progress } ) def successCb(destFile, fileInfo): if fileInfo is not True: if printer.fileManager.saveCloudPrintFile(destFile, fileInfo, FileDestinations.LOCAL): em.fire( Events.CLOUD_DOWNLOAD, { "type": "success", "id": print_file_id, "filename": printer.fileManager._getBasicFilename(destFile), "info": fileInfo["info"] } ) else: errorCb(destFile, "Couldn't save the file") return abosluteFilename = printer.fileManager.getAbsolutePath(destFile) if printer.selectFile(abosluteFilename, False, True): self._printerListener._sendUpdate('print_file_download', { 'id': print_file_id, 'progress': 100, 'selected': True }) else: self._printerListener._sendUpdate('print_file_download', { 'id': print_file_id, 'progress': 100, 'error': True, 'message': 'Unable to start printing', 'selected': False }) def errorCb(destFile, error): if error == 'cancelled': em.fire( Events.CLOUD_DOWNLOAD, { "type": "cancelled", "id": print_file_id } ) else: em.fire( Events.CLOUD_DOWNLOAD, { "type": "error", "id": print_file_id, "reason": error } ) if destFile and os.path.exists(destFile): os.remove(destFile) if astroprintCloud().download_print_file(print_file_id, progressCb, successCb, errorCb): response = {'success': True} else: response = { 'error': True, 'message': 'Unable to start download process' } elif request == 'cancel_download': from astroprint.printfiles.downloadmanager import downloadManager print_file_id = data['printFileId'] if downloadManager().cancelDownload(print_file_id): response = {'success': True} else: response = { 'error': True, 'message': 'Unable to cancel download' } else: response = { 'error': True, 'message': 'This Box does not recognize the request type [%s]' % request } self.send(json.dumps({ 'type': 'req_response', 'reqId': reqId, 'data': response })) except Exception as e: message = 'Error sending [%s] response: %s' % (request, e) self._logger.error( message ) self.send(json.dumps({ 'type': 'req_response', 'reqId': reqId, 'data': {'error': True, 'message':message } }))
def download_print_file(self, print_file_id, progressCb, successCb, errorCb): dm = downloadManager() if dm.isDownloading(print_file_id): # We just return, there's already a download for this # file in process # which means that the events will be issued for that one. return True fileManager = printerManager().fileManager try: r = requests.get('%s/api/print_files/%s' % (self.apiHost, print_file_id), auth=self.hmacAuth) data = r.json() except: data = None printFile = fileManager.getFileByCloudId(print_file_id) if printFile: self._logger.info( 'Print file %s is already on the box as %s' % \ (print_file_id, printFile)) if data and "printFileName" in data: pm = printerManager() localPrintFileName = pm.fileManager.getPrintFileName(printFile) if data["printFileName"] != localPrintFileName: pm.fileManager.setPrintFileName(printFile, data["printFileName"]) # update printFileName for this printFile in the collection if self._print_file_store: for x in self._print_file_store: if x['id'] == print_file_id: x['printFileName'] = data["printFileName"] break successCb(printFile, True) return True # The file wasn't there so let's go get it progressCb(1) destFile = None printFileName = None if data and "download_url" in data and \ (("name" in data) or ("filename" in data)) and "info" in data: progressCb(2) if "filename" in data: destFile = fileManager.getAbsolutePath(data['filename'], mustExist=False) printFileName = data["printFileName"] else: destFile = fileManager.getAbsolutePath(data['name'], mustExist=False) printFileName = data["name"] destFile = fileManager.getAbsolutePath(data['name'], mustExist=False) if destFile: def onSuccess(pf, error): self._print_file_store = None successCb(pf, error) dm.startDownload({ 'downloadUrl': data["download_url"], 'destFile': destFile, 'printFileId': print_file_id, 'printFileInfo': data['info'], 'printFileName': printFileName, 'progressCb': progressCb, 'successCb': onSuccess, 'errorCb': errorCb }) return True else: errorCb(destFile, 'Unable to download file') return False
def cancelDownloadPrintFile(self, printFileId, sendResponse): if downloadManager().cancelDownload(printFileId): sendResponse({'success': 'no error'}) else: sendResponse('cancel_error', True)