def page_openapp(self, appname): # Check if another app is running, if so, run its stop function if self.server.activeapp == appname: return redirect("/apps/%s/" % appname) self.server.stop_current_app() if appname in self.server.apps: # robot_state: # 0: Manual start/stop # 1: Start robot automatically (alive feature according to preferences) # 2: Start robot automatically and enable alive feature # 3: Start robot automatically and disable alive feature if self.server.apps[appname].config.has_key('robot_state'): print_info(self.server.apps[appname].config['robot_state']) if self.server.apps[appname].config['robot_state'] > 0: Robot.start() self.server.activeapp = appname try: print_appstarted(appname) self.server.apps[appname].start(self.server) except AttributeError: print_info("%s has no start function" % self.server.activeapp) return redirect("/apps/%s/" % appname) else: return redirect(url_for("index"))
def start(self, appname): if appname in self.active_apps: # already activated if not self.apps[appname].config['multi_user']: flash( "This app can only be opened once, and is running elsewhere." ) return False elif appname in self.background_apps: self.background_apps.remove(appname) self.active_apps.append(appname) else: if appname in self.apps: # robot activation: if self.apps[appname].config[ 'activation'] >= Robot.Activation.AUTO: Robot.start() self.active_apps.append(appname) try: print_appstarted(appname) self.apps[appname].start(self) except AttributeError: print_info("%s has no start function" % appname) else: return False running_apps = [] running_apps.extend(self.active_apps) running_apps.extend(self.background_apps) return True
def run(self): # Setup SockJS flaskwsgi = WSGIContainer(self.flaskapp) self.socketrouter = SockJSRouter(SocketConnection, '/sockjs') tornado_app = tornado.web.Application(self.socketrouter.urls + [(r".*", tornado.web.FallbackHandler, { "fallback": flaskwsgi })]) tornado_app.listen(80) # Wake up robot Robot.wake() # Start default app startup_app = Preferences.get('general', 'startup_app', None) if startup_app in Apps.apps: self.request_handler.page_openapp(startup_app) # SSL security # http_server = tornado.httpserver.HTTPServer(tornado_app, ssl_options={ # "certfile": "/etc/ssl/certs/server.crt", # "keyfile": "/etc/ssl/private/server.key", # }) # http_server.listen(443) try: # ioloop.PeriodicCallback(UserSocketConnection.dump_stats, 1000).start() IOLoop.instance().start() except KeyboardInterrupt: print_info('Keyboard interupt')
def stop_current_app(self): Robot.stop() if self.activeapp in self.apps: print_appstopped(self.activeapp) try: self.apps[self.activeapp].stop(self) except AttributeError: print_info("%s has no stop function" % self.activeapp) self.activeapp = None
def s_setdofpos(conn, data): modulename = str(data.pop('module_name', None)) dofname = str(data.pop('dof_name', None)) pos = float(data.pop('pos', 0.0)) if modulename is None or dofname is None: conn.send_data('error', {'message': 'No valid dof name given.'}) Robot.set_dof_value(modulename, dofname, pos, 0)
def robot_dof_data(): module_name = request.form.get('module_name', type=str, default='') dof_name = request.form.get('dof_name', type=str, default='') dof_value = request.form.get('value', type=float, default=0.0) dof_value = constrain(dof_value, -1.0, 1.0) Robot.set_dof_value(module_name, dof_name, dof_value) return json.dumps({'success': True})
def robot_config_data(): config_data = request.form.get('config_data', type=str, default=None) # This function also handles the None value and returns the current configuration tempConfig = Robot.config(config_data) if config_data is not None: Robot.save_config() return json.dumps({'success': True, 'config': tempConfig})
def robot_dofs_data(): dof_values = request.form.get('dofdata', type=str, default=None) # dof_values = request.form.get('values', type=str, default='') # print(dof_values) if dof_values is not None: dof_values = yaml.load(dof_values, Loader=Loader) Robot.set_dof_values(dof_values) tempDofs = Robot.get_dof_values() return json.dumps({'success': True, 'dofs': tempDofs})
def stop_all(self): for appname in self.active_apps: print_appstopped(appname) try: self.apps[appname].stop(self) except AttributeError: print_info("%s has no stop function" % appname) self.active_apps = [] if len(self.active_apps) < 1: Robot.stop()
def at_exit(self): print_info('Goodbye!') # Sleep robot Robot.sleep() Apps.stop_all() if threading.activeCount() > 0: threads = threading.enumerate() for thread in threads: try: thread.stop() thread.join() except AttributeError: pass
def set_emotion_e(self, e=0 + 0j, anim_time=-1): """ Set an emotion with complex number e, within a certain time. """ # Print data to log # print_info("Emotion; e: " + str(e) + ", time: " + str(anim_time)) # Make sure emotion is restricted to within unity circle. if abs(e) > 1.0: e = cmath.rect(1.0, cmath.phase(e)) self._emotion = e phi = cmath.phase(self._emotion) r = abs(self._emotion) Robot.apply_poly(r, phi, anim_time)
def stop(self, appname): app_count = 0 for sock in Users.sockets: if sock.activeapp == appname: app_count += 1 if app_count > 1: return if appname in self.active_apps: print_appstopped(appname) try: self.apps[appname].stop(self) except AttributeError: print_info("%s has no stop function" % appname) self.active_apps.remove(appname) if len(self.active_apps) < 1: Robot.stop()
def set_emotion_r_phi(self, r, phi, degrees=False, anim_time=-1): """ Set an emotion with r and phi, within a certain time. """ # Print data to log # print_info("Set Emotion; r: " + str(r) + ", phi: " + str(phi) + # ", deg: " + str(degrees) + ", time: " + str(anim_time)) e = 0 + 0j # Emotion from r and phi if r is None or phi is None: raise RuntimeError( "Bad combination of parameters; r and phi need to be provided." ) if degrees: phi = phi * math.pi / 180.0 phi = constrain(phi, 0.0, 2 * math.pi) r = constrain(r, 0.0, 1.0) Robot.apply_poly(r, phi, anim_time)
def set_emotion_index(self, index, anim_time=-1): """ Set an emotion with index in defined expression list, within a certain time. """ e = 0 + 0j # Emotion from list if index is None: raise RuntimeError( "Bad combination of parameters; index needs to be provided.") index = constrain(index, 0, len(self.expressions) - 1) exp = self.expressions[index] if 'poly' in exp: # 20 values in poly, (poly * 2*pi/20) phi = constrain(exp['poly'] * math.pi / 10, 0.0, 2 * math.pi) Robot.apply_poly(1.0, phi, anim_time) if 'dofs' in exp: # send dofs directly to the robot Robot.set_dof_list(exp['dofs'], anim_time)