-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNAOSpeechProxy.py
More file actions
111 lines (88 loc) · 3.41 KB
/
NAOSpeechProxy.py
File metadata and controls
111 lines (88 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# -*- coding: utf-8 -*-
from naoqi import ALProxy
import socket
import json
import argparse
class NAOSpeechProxy:
def __init__(self, path_json_behavior, ip_client, port_client, ip_NAO, port_NAO, send_message=True):
self.send_message = send_message
self.path_json_behavior = path_json_behavior
self.init_NAO(ip_NAO, port_NAO)
print "waiting client"
self.connect_client(ip_client, port_client)
def init_NAO(self, ip_NAO, port_NAO):
connection_success = False
num_retry = 5
for i in range(num_retry):
try:
self.speech = NAOSpeech(ip_NAO, port_NAO)
connection_success = True
break
except:
print "[!] Some type of error has occured. Retrying to connect"
continue
if connection_success:
print "Success to connect to NAO"
else:
raise RuntimeError("Can't connect to NAO.")
def connect_client(self, ip_client, port_client):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.bind((ip_client, port_client))
self.sock.listen(1)
self.sock.settimeout(100)
try:
self.client, self.address_client = self.sock.accept()
print('Success to connect to client. IP:{}, port:{}'.format(self.address_client[0], self.address_client[1]))
except:
self.disconnect()
def wait_message(self,buffer_size=1024):
try:
json_message = self.client.recv(buffer_size)
except:
print "A vital error has occured."
self.disconnect()
try:
message = json.loads(json_message)
return message
except:
self.send('{"result":"something is wrong"}')
return None
def process_message(self):
while(1):
print 'waiting message'
message = self.wait_message()
if message==None:
pass
else:
print 'recieved message:{}'.format(message)
message_type, value = message.items()[0]
try:
self.speech.say(value)
self.send('{"result":"succeed-end"}')
except:
self.send('{"result":"something is wrong"}')
def send(self,message):
if self.send_message:
self.client.send(message)
else:
pass
class NAOSpeech:
def __init__(self, ip_NAO, port_NAO):
self.audioProxy = ALProxy("ALTextToSpeech", ip_NAO, port_NAO)
def say(self, message):
self.audioProxy.post.say(message.encode("UTF-8"))
return "succeed-end"
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Motion Proxy for NAO')
parser.add_argument('ip_NAO', type=str)
parser.add_argument('port_NAO', type=int)
parser.add_argument('ip_client', type=str)
parser.add_argument('port_client', type=int)
parser.add_argument('-m', action='store_true', help="Send messages of the result to client")
args = parser.parse_args()
path_json_behavior = "./behavior.json"
nao_proxy = NAOSpeechProxy(path_json_behavior,
args.ip_client, args.port_client,
args.ip_NAO, args.port_NAO,
send_message=args.m)
nao_proxy.process_message()