def sendEventToPeer(self, type, data=None): try: boxrouterManager().sendEventToClient(self.clientId, type, data) except: self._logger.error('Error sending event [%s] to peer %s' % (type, self.clientId), exc_info=True)
def run(self): #We need to give the UI a chance to update before starting so that the message can be sent... self._progressCb("download", 0.0, "Starting...") #disconnect from the cloud during software upgrade. The reboot will take care of reconnect boxrouterManager().boxrouter_disconnect() time.sleep(2) self._installNextPackage()
def signinWithKey(self, email, private_key, hasSessionContext=True): from octoprint.server import userManager user = None userLoggedIn = False online = networkManager().isOnline() if online: public_key = self.get_public_key(email, private_key) if public_key: #Let's protect the box now: user = userManager.findUser(email) if user and user.has_password(): userManager.changeCloudAccessKeys(email, public_key, private_key) else: self._logger.info( "New user signing requires password method") return False userLoggedIn = True else: user = userManager.findUser(email) userLoggedIn = user and user.check_privateKey(private_key) if userLoggedIn: if hasSessionContext: login_user(user, remember=True) userId = user.get_id() self.settings.set(["cloudSlicer", "loggedUser"], userId) self.settings.save() boxrouterManager().boxrouter_connect() if hasSessionContext: identity_changed.send(current_app._get_current_object(), identity=Identity(userId)) #let the singleton be recreated again, so new credentials are taken into use global _instance _instance = None eventManager().fire(Events.LOCK_STATUS_CHANGED, userId) return True elif not online: raise AstroPrintCloudNoConnectionException() return False
def pingPongRounder(self, params=None): for sessionId in self._connectedPeers.keys(): peer = self._connectedPeers[sessionId] #if peer != 'local': if isinstance(peer, ConnectionPeer): try: boxrouterManager().sendRequestToClient( peer.clientId, 'ping', None, 10, self.pongCallback, [sessionId]) except: self._logger.error('Error sending ping to peer %s' % peer.clientId, exc_info=True)
def getStatus(self): printer = printerManager() cm = cameraManager() ppm = printerProfileManager() cloudInstance = astroprintCloud() fileName = None if printer.isPrinting(): currentJob = printer.getCurrentJob() fileName = currentJob["file"]["name"] return { 'id': boxrouterManager().boxId, 'name': networkManager().getHostname(), 'orgId' : cloudInstance.orgId, 'groupId' : cloudInstance.groupId, 'printing': printer.isPrinting(), 'fileName': fileName, 'printerModel': ppm.data['printer_model'] if ppm.data['printer_model']['id'] else None, 'filament' : ppm.data['filament'], 'material': None, 'operational': printer.isOperational(), 'paused': printer.isPaused(), 'camera': cm.isCameraConnected(), #'printCapture': cm.timelapseInfo, 'remotePrint': True, 'capabilities': ['remotePrint'] + cm.capabilities }
def connect_boxrouter(): br = boxrouterManager() if br.boxrouter_connect(): return jsonify() else: return abort(400)
def remove_logged_user(self): loggedUser = self.settings.get(['cloudSlicer', 'loggedUser']) from octoprint.server import userManager #Method could be call twice (boxrouter, touch), and now user is deleted if loggedUser: userManager.removeUser(loggedUser) self.settings.set(["cloudSlicer", "loggedUser"], None) self.settings.set(["materialSelected"], None) self.settings.set(["printerSelected"], None) self.settings.set(["qualitySelected"], None) self.settings.set(["customQualitySelected"], None) self.settings.save() boxrouterManager().boxrouter_disconnect() #let the singleton be recreated again, so credentials and print_files are forgotten global _instance _instance = None eventManager().fire(Events.LOCK_STATUS_CHANGED, None)
def sendLogs(self, ticketNo=None, message=None): import zipfile from tempfile import gettempdir try: boxId = boxrouterManager().boxId #Create the zip file zipFilename = '%s/%s-logs.zip' % (gettempdir(), boxId) zipf = zipfile.ZipFile(zipFilename, 'w') for root, dirs, files in os.walk( self._settings.getBaseFolder("logs")): for file in files: zipf.write(os.path.join(root, file), file) zipf.close() except Exception as e: self._logger.error('Error while zipping logs: %s' % e) return False zipf = open(zipFilename, 'rb') #send the file to the server r = requests.post('%s/astrobox/software/logs' % roConfig('cloud.apiHost'), data={ 'ticket': ticketNo, 'message': message, 'boxId': boxId }, files={'file': (zipFilename, zipf)}, auth=self._checkAuth(), headers=self._requestHeaders) zipf.close() #remove the file os.remove(zipFilename) if r.status_code == 200: return True else: self._logger.error('Error while sending logs: %d' % r.status_code) return False
def __init__(self): self.settings = settings() self._eventManager = eventManager() self.hmacAuth = None self.boxId = boxrouterManager().boxId self.tryingLoggingTimes = 0 self.apiHost = roConfig('cloud.apiHost') self._print_file_store = None self._sm = softwareManager() self._logger = logging.getLogger(__name__) loggedUser = self.settings.get(['cloudSlicer', 'loggedUser']) if loggedUser: from octoprint.server import userManager user = userManager.findUser(loggedUser) if user and user.publicKey and user.privateKey: self.hmacAuth = HMACAuth(user.publicKey, user.privateKey, self.boxId, user.orgId, user.groupId)
def getDeviceId(self, data=None, sendResponse=None): boxId = boxrouterManager().boxId if sendResponse: sendResponse(boxId) return boxId
def signin(self, email, password, hasSessionContext=True): from octoprint.server import userManager user = None userLoggedIn = False online = networkManager().isOnline() if online: data_private_key = self.get_private_key(email, password) if data_private_key: private_key = data_private_key['private_key'] public_key = self.get_public_key(email, private_key) orgId = data_private_key['organization_id'] groupId = data_private_key['group_id'] if public_key: #Let's protect the box now: #We need to keep this code for a while, or it can generate errors it the user who is loging whast loged before user = userManager.findUser(email) if user: userManager.changeUserPassword(email, password) userManager.changeCloudAccessKeys( email, public_key, private_key, orgId, groupId) else: user = userManager.addUser(email, password, public_key, private_key, orgId, groupId, True) userLoggedIn = True else: user = userManager.findUser(email) userLoggedIn = user and user.check_password( userManager.createPasswordHash(password)) if userLoggedIn: if hasSessionContext: login_user(user, remember=True) userId = user.get_id() self.settings.set(["cloudSlicer", "loggedUser"], userId) self.settings.save() boxrouterManager().boxrouter_connect() if hasSessionContext: identity_changed.send(current_app._get_current_object(), identity=Identity(userId)) #let the singleton be recreated again, so new credentials are taken into use global _instance _instance = None eventManager().fire(Events.LOCK_STATUS_CHANGED, userId) return True elif not online: raise AstroPrintCloudNoConnectionException() return False
def createPasswordHash(password): if password is not None: return hashlib.sha512(password + boxrouterManager().boxId).hexdigest() return None
def get_uuid(self): return boxrouterManager().boxId