def create_route(): options = {} username = session['username'] try: form = request.form # Deal with POST requests. if request.method == 'POST': # If the request does not contain an "op" field. if not 'op' in request.form: raise RuntimeError('Did you click the button?') # Add a ingredient. elif form['op'] == 'add_ingred': ingred_name = get_text_input(form['ingred_name']) april_id = get_april_id(form) database.check_ingred_april_id(username, ingred_name, april_id) logger.debug('New ingredient: %s at April Tag %s' % \ (ingred_name, april_id)) # Add the ingredient into the database. database.add_ingredient(username, ingred_name, april_id) # Delete an ingredient. elif form['op'] == 'delete_ingred': # Delete the ingredient from the database. database.delete_ingredient(username, form['ingred_name']) else: raise RuntimeError('Did you click the button?') except Exception as e: logger.exception(e) options['error'] = e # Retrieve ingredients even if POST request fails. try: options['ingredients'] = database.get_ingredients(username) except Exception as e: logger.exception(e) options['error'] = e return render_template('create.html', **options)
def tell_joke(seed=randint(0, NUM_JOKES - 1)): logger.debug("wrapper called, seed %s" % seed) speak("Here is a joke") sleep(1) logger.debug("post sleep") for i in JOKES[seed]: speak(i) sleep(0.5)
def is_pressure_sensor_occupied(): ser.write('w') sleep(0.1) result = ser.read() logger.debug('Pressure sensor result: %s' % result) if result == 'G': return False elif result == 'B': return True else: raise RuntimeError('Pressure sensor result: %s' % result)
def send_to_backend(self, username, speech_input): recipes = database.get_recipes(username) if not recipes: return 'You don\'t have any recipes' else: drink = {} found = False for i in recipes: if i['drinkname'].lower() in speech_input.lower(): if found: return 'Find two drinks %s and %s' % \ (drink['drinkname'], i['drinkname']) found = True logger.debug('Drink to prepare: %s' % i) drink['drinkname'] = str(i['drinkname']) drink['ingred_amounts'], drink['ingred_names'] = \ self.prune_ingredients(i['ingredients']) if not found: return 'What drink do you want?' assert(len(drink['ingred_amounts']) == len(drink['ingred_names'])) logger.debug('Drink to prepare (pruned): %s' % drink) # Call arm controller. msg = arm_command_t() msg.size = len(drink['ingred_amounts']) msg.hole_indices = self.get_hole_indices(drink['ingred_names'], \ username) msg.stop_times = self.volume_to_time(drink['ingred_amounts']) logger.debug('LCM publishing:\n%s' % \ self.msg_to_str(msg)) if not DEBUG: self.lc.publish("ARM", msg.encode()) return '%s is being prepared...' % drink['drinkname']
def learn_route(): options = {} username = session['username'] try: form = request.form # Deal with POST requests. if request.method == 'POST': # If the request does not contain an "op" field. if not 'op' in request.form: raise RuntimeError('Did you click the button?') # Add a recipe. elif form['op'] == 'add_recipe': drinkname = get_text_input(form['drinkname']) if database.recipe_exists(username, drinkname): raise RuntimeError('Drink ' + drinkname + ' already exists') # Example ingredients: [('water', 0.0), ('vodka', 10.0)]. ingredients = [(ingredient, get_amount(form[ingredient])) \ for ingredient in database.get_ingredient_names(username)] check_total_amount(ingredients) logger.debug('New drink: %s' % ingredients) # Add the recipe into the database. database.add_recipe(username, drinkname, ingredients) # Delete a recipe. elif form['op'] == 'delete_recipe': # Delete the recipe from the database. database.delete_recipe(username, form['drinkname']) else: raise RuntimeError('Did you click the button?') except Exception as e: logger.exception(e) options['error'] = e # Retrieve recipes even if POST request fails. try: options['recipes'] = database.get_recipes(username) options['ingredients'] = database.get_ingredient_names(username) except Exception as e: logger.exception(e) options['error'] = e return render_template('learn.html', **options)
def get_hole_indices(self, ingred_names, username): # Call camera controller. # If ingred_names == ['Orange Juice'], # return [<hole_id>]. if not DEBUG: try: camera_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) camera_socket.connect(('35.2.227.11', 12000)) camera_socket.send("Get Locations" + '\0') rcv = camera_socket.recv(512) except Exception as e: raise RuntimeError('Camera\'s error: ' + str(e) + '. Please ask SM for help.') else: rcv = '{7|2}{5|0}{4|1}' logger.debug('Camera returns: %s' % rcv) ingred_hole_map = self.parse_camera_rcv(rcv, username) logger.debug('Mapping btw ingredients and holes: %s' % ingred_hole_map) rtn = [] for ingred_name in ingred_names: if not ingred_name in ingred_hole_map: raise RuntimeError(ingred_name + ' is not in stock') rtn.append(ingred_hole_map[ingred_name]) return rtn
def infer_route(): options = {} username = session['username'] if os.environ.get('ASR_ADDR_PO`RT'): options['asr_addr_port'] = os.environ.get('ASR_ADDR_PORT') else: options['asr_addr_port'] = 'ws://localhost:8081' try: form = request.form # Deal with POST requests. if request.method == 'POST': # If the request does not contain an "op" field. if not 'op' in form: raise RuntimeError('Did you click the Ask button?') # When the "op" field is equal to "infer". elif form['op'] == 'infer': # Check pressure sensor. if is_pressure_sensor_occupied(): raise RuntimeError( 'Please place an empty cup on the gripper') # # Classify the query. speech_input = get_text_input(form['speech_input'] if \ 'speech_input' in form else '') logger.debug('Speech input: %s' % speech_input) options['result'] = lcm_client.send_to_backend(username, \ speech_input) tell_joke() logger.debug('Result: %s' % options['result']) else: raise RuntimeError('Did you click the Ask button?') except Exception as e: logger.exception(e) options['result'] = e return render_template('infer.html', **options) # Display. return render_template('infer.html', **options)