Exemplo n.º 1
0
def startListener(thread):
    thread.start()
    while True:
        # 標準入力を読み込み続ける
        # subprpcessから何か返答があれば標準入力として帰ってくる
        # (input()もstdinと同じこと _stubファイルはinputで適当な値を入力するようにしている)
        # sys.stdin.flush()
        data = json.loads(input())
        log.communication('client_data:' + str(data))
        request = data['request']
        response = data['response']
        # 実行したやつのmodule名
        module = request['module']
        # リスナーのkeyがmodule, valueはcallback
        try:
            if len(listeners[module]) > 0:
                thread = CallbackThread(listeners[module][0], response)
                thread.start()
                # 各モジュールリストに入ったcallback関数をpopし実行していく
                listeners[module].pop(0)
        except KeyError:
            continue
        except EOFError:
            request = {'module': 'msg', 'message': 'EOFError'}
            print(request, flush=True)
Exemplo n.º 2
0
 def run(self, request=None):
     if not request:
         return
     if(request['cmd'] == 'check_dist'):
         distance = dm()
         response = {"dist" : distance}
         log.communication('dist_thread:' + str(distance))
         jsn = json.dumps({"response": response, 'request': request})
         self.app.stdin.write((jsn + '\n').encode('utf-8'))
         self.app.stdin.flush()
         print('dist_thread->dist: {}:{}'.format(response, request))
Exemplo n.º 3
0
def func_motor(request):
    cmd = request['command']
    if cmd == 'stop':
        log.communication('motor: send ' + cmd)
        proc['motor'].stdin.write(cmd + '\n')
    elif cmd == 'move' or cmd == 'back':
        right_speed = request['left_speed']
        left_speed = request['right_speed']
        right = str(right_speed)
        left = str(left_speed)
        log.communication('motor: send ' + cmd + ' ' + right + ' ' + left)
        proc['motor'].stdin.write('move_spd ' + right + ' ' + left + '\n')
    elif cmd == 'right' or cmd == 'left':
        angle = request['angle']
        log.communication('motor: send ' + cmd + ' ' + str(angle))
        proc['motor'].stdin.write(cmd + ' ' + str(angle) + '\n')
    elif cmd == 'move_acc':
        dst = request['dst']
        log.communication('motor: send ' + cmd + ' ' + str(dst))
        proc['motor'].stdin.write('move ' + str(dst) + ' ' + str(dst) + '\n')
Exemplo n.º 4
0
    def run(self, request=None):
        if not request:
            return
        print('viuce_thread->voice: {}:{}'.format(self.cnt, request))
        self.cnt += 1

        # juliusのプロセスIDを取得
        pid = str(self.voice.stdout.read().decode('utf-8'))
        time.sleep(5)
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.connect((host, port))

        data =""
        killword = ""
        while True:
            while (1):
                if '</RECOGOUT>\n.' in data:
                    #data = data + sock.recv(1024)
                    strTemp = ""
                    for line in data.split('\n'):
                        index = line.find('WORD="')
                        if index != -1:
                            line = line[index+6:line.find('"',index+6)]
                            strTemp += str(line)
                        response={}
                        if strTemp == 'カメラ':
                            if killword != 'カメラ':
                                print ("Result: " + strTemp)
                                #カメラを立ち上げて,自動的にbreak
                                response['module'] = 'camera'
                                print ("<<<please speak>>>")
                                killword = "カメラ"

                        elif strTemp == '進め':
                            if killword != '進め':
                                print("Result: " + strTemp)
                                response['module'] = 'motor'
                                print ("<<<please speak>>>")
                                killword = "進め"

                        elif strTemp == '前':
                            if killword != "前":
                                print("Result: " + strTemp)
                                #request['module'] = 'motor'
                                response['motor_cmd'] = 'front'
                                print ("<<<please speak>>>")
                                killword = "前"

                        elif strTemp == '後ろ':
                            if killword != "後ろ":
                                print("Result: " + strTemp)
                                #request['module'] = 'motor'
                                response['motor_cmd'] = 'back'
                                print("<<<please speak>>>")
                                killword = "後ろ"

                        elif strTemp == '右':
                            if killword != "右":
                                print("Result: " + strTemp)
                                #request['module'] = 'motor'
                                response['motor_cmd'] = 'right'
                                print ("<<<please speak>>>")
                                killword = "右"

                        elif strTemp == '左':
                            if killword != "左":
                                print("Result: " + strTemp)
                                #request['module'] = 'motor'
                                response['motor_cmd'] = 'left'
                                print ("<<<please speak>>>")
                                killword = "左"

                        elif strTemp == 'とまれ':
                            if killword != "とまれ":
                                print("Result: " + strTemp)
                                respinse['motor_cmd'] = 'stop'
                                print ("<<<please speak>>>")
                                killword = "とまれ"

                        elif strTemp == 'ストップ':
                            if killword != "ストップ":
                                print("Result: " + strTemp)
                                response['motor_cmd'] = 'stop'
                                print ("<<<please speak>>>")
                                killword = "ストップ"

                        elif strTemp == '友達':
                            if killword != "友達":
                                print("Result: " + strTemp)
                                #スルー
                                print ("<<<please speak>>>")
                                killword = "友達"

                        elif strTemp == 'ついて来て':
                            if killword != "ついて来て":
                                print("Result: " + strTemp)
                                #何か返答
                                print ("<<<please speak>>>")
                                killword = "ついて来て"

                        elif strTemp == '終了':
                            if killword != "終了":
                                print("Result: " + strTemp)
                                response['module'] = 'quit'
                                self.exitCore()
                                print ("<<<please speak>>>")
                                killword = "終了"

                        else:
                            print("Result:" + strTemp)
                            print ("<<<please speak>>>")
                        data = ""

                        jsn = json.dumps({"response":response, "request":request})
                        log.communication('voice_thread:' + str(response))
                        self.app.stdin.write((jsn + '\n').encode('utf-8'))
                        self.app.stdin.flush()
                        print('voice_thread->app: {}:{}'.format(response, request))

                else:
                    data += str(sock.recv(1024).decode('utf-8'))
Exemplo n.º 5
0
def func_speech(request):
    cmd = request['command']
    if cmd == 'speak':
        msg = request['message']
        log.communication('speech: send ' + msg)
        proc['speech'].stdin.write(msg + '\n')
Exemplo n.º 6
0
def func_speech(request):
    cmd = request['command']
    if cmd == 'speak':
        msg = request['message']
        log.communication('speech: send ' + msg)
        proc['speech'].stdin.write(msg + '\n')


# start voice thread
func_voice()
func_shoe()

while True:
    #get module and command from app
    raw_request = proc['app'].stdout.readline()
    try:
        request = json.loads(raw_request)
    except ValueError:
        continue
    log.communication('app: receive ' + raw_request)
    if request['module'] == 'camera':
        func_camera(request)
    elif request['module'] == 'voice':
        func_voice(request)
    elif request['module'] == 'motor':
        func_motor(request)
    elif request['module'] == 'sensor':
        func_sensor(request)
    elif request['module'] == 'speech':
        func_speech(request)