def _doRetry(self, silent=True): if self._retries < self.MAX_RETRIES: self._retries += 1 sleep(self.START_WAIT_BETWEEN_RETRIES * self.WAIT_MULTIPLIER_BETWEEN_RETRIES * (self._retries - 1)) self._logger.info('Retrying boxrouter connection. Retry #%d' % self._retries) self._silentReconnect = silent self.boxrouter_connect() else: self._logger.info('No more retries. Giving up...') self.status = self.STATUS_DISCONNECTED self._eventManager.fire(Events.ASTROPRINT_STATUS, self.status) self._retries = 0 #Are we offline? nm = networkManager() if not nm.checkOnline() and nm.isHotspotActive( ) is False: #isHotspotActive will return None if not possible #get the box hotspot up self._logger.info('AstroBox is offline. Starting hotspot...') result = nm.startHotspot() if result is True: self._logger.info('Hostspot started.') else: self._logger.error('Failed to start hostspot: %s' % result)
def boxrouter_connect(self): if not networkManager().isOnline(): return False if not self.connected: from octoprint.server import userManager loggedUser = self._settings.get(['cloudSlicer', 'loggedUser']) if loggedUser: user = userManager.findUser(loggedUser) if user: self._publicKey = user.publicKey self._privateKey = user.privateKey if self._publicKey and self._privateKey: self.status = self.STATUS_CONNECTING self._eventManager.fire(Events.ASTROPRINT_STATUS, self.status) try: self._ws = AstroprintBoxRouterClient( self._address, self) self._ws.connect() self.connected = True except Exception as e: self._logger.error( "Error connecting to boxrouter: %s" % e) self._doRetry( False) #This one should not be silent return True return False
def boxrouter_connect(self): if not networkManager().isOnline(): return False if not self.connected: from octoprint.server import userManager loggedUser = self._settings.get(['cloudSlicer', 'loggedUser']) if loggedUser: user = userManager.findUser(loggedUser) if user: self._publicKey = user.publicKey self._privateKey = user.privateKey if self._publicKey and self._privateKey: self.status = self.STATUS_CONNECTING self._eventManager.fire(Events.ASTROPRINT_STATUS, self.status); try: self._ws = AstroprintBoxRouterClient(self._address, self) self._ws.connect() self.connected = True except Exception as e: self._logger.error("Error connecting to boxrouter: %s" % e) self._doRetry(False) #This one should not be silent return True return False
def processAuthenticate(self, data): if data: if 'error' in data: self._logger.warn(data['message'] if 'message' in data else 'Unkonwn authentication error') self.status = self.STATUS_ERROR self._eventManager.fire(Events.ASTROPRINT_STATUS, self.status); self.close() elif 'success' in data: self._logger.info("Connected to astroprint service") self.connected = True; self._retries = 0; self.status = self.STATUS_CONNECTED self._eventManager.fire(Events.ASTROPRINT_STATUS, self.status); else: from octoprint.server import VERSION nm = networkManager() activeConnections = nm.getActiveConnections() if activeConnections and ( activeConnections['wired'] or activeConnections['wireless']): preferredConn = activeConnections['wired'] or activeConnections['wireless'] localIpAddress = preferredConn['ip'] else: localIpAddress = None if not self._boxId: import os boxIdFile = "%s/box-id" % os.path.dirname(self._settings._configfile) if os.path.exists(boxIdFile): with open(boxIdFile, 'r') as f: self._boxId = f.read() else: import uuid self._boxId = uuid.uuid4().hex with open(boxIdFile, 'w') as f: f.write(self._boxId) self._ws.send(json.dumps({ 'type': 'auth', 'data': { 'boxId': self._boxId, 'boxName': nm.getHostname(), 'swVersion': VERSION, 'localIpAddress': localIpAddress, 'publicKey': self._publicKey, 'privateKey': self._privateKey } }))
def signin(self, email, password): from octoprint.server import userManager from astroprint.network import networkManager user = None userLoggedIn = False if networkManager().isOnline(): private_key = self.get_private_key(email, password) if private_key: public_key = self.get_public_key(email, private_key) if public_key: #Let's protect the box now: user = userManager.findUser(email) if user: userManager.changeUserPassword(email, password) userManager.changeCloudAccessKeys( email, public_key, private_key) else: user = userManager.addUser(email, password, public_key, private_key, True) userLoggedIn = True else: user = userManager.findUser(email) userLoggedIn = user and user.check_password( userManager.createPasswordHash(password)) if userLoggedIn: login_user(user, remember=True) userId = user.get_id() self.settings.set(["cloudSlicer", "loggedUser"], userId) self.settings.save() identity_changed.send(current_app._get_current_object(), identity=Identity(userId)) eventManager().fire(Events.LOCK_STATUS_CHANGED, userId) boxrouterManager().boxrouter_connect() #let the singleton be recreated again, so new credentials are taken into use global _instance _instance = None return True return False
def processAuthenticate(self, data): if data: self._silentReconnect = False if 'error' in data: self._logger.warn(data['message'] if 'message' in data else 'Unkonwn authentication error') self.status = self.STATUS_ERROR self._eventManager.fire(Events.ASTROPRINT_STATUS, self.status) self.close() elif 'success' in data: self._logger.info("Connected to astroprint service") self.authenticated = True self._retries = 0 self.status = self.STATUS_CONNECTED self._eventManager.fire(Events.ASTROPRINT_STATUS, self.status) else: from octoprint.server import VERSION nm = networkManager() activeConnections = nm.getActiveConnections() if activeConnections and (activeConnections['wired'] or activeConnections['wireless']): preferredConn = activeConnections[ 'wired'] or activeConnections['wireless'] localIpAddress = preferredConn['ip'] else: localIpAddress = None sm = softwareManager() self._ws.send( json.dumps({ 'type': 'auth', 'data': { 'silentReconnect': self._silentReconnect, 'boxId': self.boxId, 'variantId': sm.variant['id'], 'boxName': nm.getHostname(), 'swVersion': VERSION, 'platform': sm.platform, 'localIpAddress': localIpAddress, 'publicKey': self._publicKey, 'privateKey': self._privateKey } }))
def signin(self, email, password): from octoprint.server import userManager from astroprint.network import networkManager user = None userLoggedIn = False if networkManager().isOnline(): private_key = self.get_private_key(email, password) if private_key: public_key = self.get_public_key(email, private_key) if public_key: #Let's protect the box now: user = userManager.findUser(email) if user: userManager.changeUserPassword(email, password) userManager.changeCloudAccessKeys(email, public_key, private_key) else: user = userManager.addUser(email, password, public_key, private_key, True) userLoggedIn = True else: user = userManager.findUser(email) userLoggedIn = user and user.check_password(userManager.createPasswordHash(password)) if userLoggedIn: login_user(user, remember=True) userId = user.get_id() self.settings.set(["cloudSlicer", "loggedUser"], userId) self.settings.save() identity_changed.send(current_app._get_current_object(), identity=Identity(userId)) eventManager().fire(Events.LOCK_STATUS_CHANGED, userId) boxrouterManager().boxrouter_connect() #let the singleton be recreated again, so new credentials are taken into use global _instance _instance = None return True return False
def processAuthenticate(self, data): if data: self._silentReconnect = False if 'error' in data: self._logger.warn(data['message'] if 'message' in data else 'Unkonwn authentication error') self.status = self.STATUS_ERROR self._eventManager.fire(Events.ASTROPRINT_STATUS, self.status); self.close() elif 'success' in data: self._logger.info("Connected to astroprint service") self.authenticated = True; self._retries = 0; self.status = self.STATUS_CONNECTED self._eventManager.fire(Events.ASTROPRINT_STATUS, self.status); else: from octoprint.server import VERSION nm = networkManager() activeConnections = nm.getActiveConnections() if activeConnections and ( activeConnections['wired'] or activeConnections['wireless']): preferredConn = activeConnections['wired'] or activeConnections['wireless'] localIpAddress = preferredConn['ip'] else: localIpAddress = None sm = softwareManager() self._ws.send(json.dumps({ 'type': 'auth', 'data': { 'silentReconnect': self._silentReconnect, 'boxId': self.boxId, 'variantId': sm.variant['id'], 'boxName': nm.getHostname(), 'swVersion': VERSION, 'platform': sm.platform, 'localIpAddress': localIpAddress, 'publicKey': self._publicKey, 'privateKey': self._privateKey } }))
def __init__(self): self._eventManager = eventManager() self.logger = logging.getLogger(__name__) self.variantMgr = variantManager() self.softwareMgr = softwareManager() # upnp/ssdp self._ssdp_monitor_active = False self._ssdp_monitor_thread = None self._ssdp_notify_timeout = 10 self._ssdp_last_notify = 0 self._ssdp_last_unregister = 0 # SSDP if networkManager().isOnline(): self._ssdp_register() self._eventManager.subscribe(Events.NETWORK_STATUS, self._onNetworkStateChanged)
def _ssdp_register(self): """ Registers the AstroPrint instance as basic service with a presentation URL pointing to the web interface """ time_since_last_unregister = time.time() - self._ssdp_last_unregister if time_since_last_unregister < ( self._ssdp_notify_timeout + 1 ): wait_seconds = ( self._ssdp_notify_timeout + 1) - time_since_last_unregister self.logger.info("Waiting %s seconds before starting SSDP Service..." % wait_seconds) time.sleep(wait_seconds) #Make sure that the network is still after the wait if not networkManager().isOnline(): return self._ssdp_monitor_active = True self._ssdp_monitor_thread = threading.Thread(target=self._ssdp_monitor, kwargs=dict(timeout=self._ssdp_notify_timeout)) self._ssdp_monitor_thread.daemon = True self._ssdp_monitor_thread.start()
def _doRetry(self): if self._retries < self.MAX_RETRIES: self._retries += 1 sleep(self.START_WAIT_BETWEEN_RETRIES * self.WAIT_MULTIPLIER_BETWEEN_RETRIES * (self._retries - 1) ) self._logger.info('Retrying boxrouter connection. Retry #%d' % self._retries) self.boxrouter_connect() else: self._logger.info('No more retries. Giving up...') self.status = self.STATUS_DISCONNECTED self._eventManager.fire(Events.ASTROPRINT_STATUS, self.status); self._retries = 0 #Are we offline? nm = networkManager() if not nm.checkOnline() and nm.isHotspotActive() == False: #get the box hotspot up self._logger.info('AstroBox is offline. Starting hotspot...') result = nm.startHotspot() if result is True: self._logger.info('Hostspot started.') else: self._logger.error('Failed to start hostspot: %s' % result)
def get_instance_name(self): return networkManager().getHostname()