Exemple #1
0
 def load_actions(self, table, table_name):
     built_actions = {}
     for item in table.all():
         new_object = Action.get_action(self, table, item.eid, item,
                                        table_name)
         built_actions[item.eid] = new_object
     return built_actions
    def receive_callback(ch, method, properties, body):
        try:
            print("Received a message.")
            #Need to initialise Python engine. 
            action_dic = json.loads(body)
            if action_dic["action"] == "init":
                MessageBroker.send_message("py_to_ele", json_data = json.dumps({'status': 'Listening'}))
                
                res = Speech2TextRequest._create_to_text_request()
                res = Text2CodeRequest._create_to_code_request(res, HEADERS, PARAMS)
                action_data = Action.get_action(res)
                print("sending message..", action_data)
                if action_data != None:
                    MessageBroker.send_message("py_to_ele", json.dumps(action_data))
                    MessageBroker.send_message("py_to_ext", json.dumps(action_data))
                else:
                    MessageBroker.send_message("py_to_ele", json.dumps({"status":"Invalid query"}))
            elif action_dic["action"] == "init_freeflow":
                MessageBroker.send_message("py_to_ele", json_data = json.dumps({'status': 'Listening'}))

                res = Speech2TextRequest._create_to_text_request()
                print(res)

                if res != None:

                    if "return" in res:
                        action_data = {
                            "status": "Free flow",
                            "action": "return",
                            "data": {
                                "args": res.split("return ")[1:]
                            }
                        }
                    elif "print" in res:
                        action_data = {
                            "status": "Free flow",
                            "action": "print",
                            "data": {
                                "args": res.split("print ")[1:]
                            }
                        }
                    else:
                        ops = {"plus": "+", "minus": "-", "multiply": "*", "divide": "/", "less than": "<", "less than or equal to": "<=", "greater than": ">", "greater than or equal to": ">=","< or equal to":"<=","> or equal to":">="}
                        for i in ops.keys():
                            res = res.replace(i, ops[i])

                        action_data = {
                                "status": "Free flow",
                                "action": "arithmetic",
                                "data": {
                                    "args": res
                                }
                        }
                    
                    print("sending message...", action_data)
                    if action_data != None:
                        MessageBroker.send_message("py_to_ele", json.dumps(action_data))
                        MessageBroker.send_message("py_to_ext", json.dumps(action_data))
                else:
                    MessageBroker.send_message("py_to_ele", json.dumps({"status": "Invalid query"}))
        except Exception:
            print(traceback.format_exc())