-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtlcclient.py
More file actions
193 lines (171 loc) · 5.75 KB
/
tlcclient.py
File metadata and controls
193 lines (171 loc) · 5.75 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# coding: utf-8
import sys
import socket
import time
from datetime import datetime
def __sendmsg(soc, msg) :
result = True
n = len(msg)
remain = n
while remain > 0 :
try :
t = soc.send(msg)
remain -= t
msg = msg[t:]
except :
result = False
break
return result
def doCommu4Command(soc, cmd) :
result = True
cmdlist = []
initstr = 'M0 0 800 -40 -800 40 0 0 0 0 0 0 -50 0\n'
if cmd == '*init*' :
cmdlist = ["S\n"]
elif cmd == '*terminate*' :
cmdlist = ["E\n"]
elif cmd == 'tlc;MoveRightHand' :
#cmdlist.append(initstr)
#cmdlist.append("*%d" % 2000)
cmdlist.append("M0 0 800 -40 800 40 0 0 0 0 0 0 -50 0\n")
cmdlist.append("*%d" % 1000)
cmdlist.append("M0 0 800 -40 -800 40 0 0 0 0 0 0 -50 0\n")
#cmdlist.append("*%d" % 2000)
elif cmd == 'tlc;MoveLeftHand' :
#cmdlist.append(initstr)
#cmdlist.append("*%d" % 2000)
cmdlist.append("M0 0 -800 -40 -800 40 0 0 0 0 0 0 -50 0\n")
cmdlist.append("*%d" % 1000)
cmdlist.append("M0 0 800 -40 -800 40 0 0 0 0 0 0 -50 0\n")
#cmdlist.append("*%d" % 2000)
elif cmd == 'tlc;MoveBothHand' :
#cmdlist.append(initstr)
#cmdlist.append("*%d" % 2000)
cmdlist.append("M0 0 -800 -40 800 40 0 0 0 0 0 0 -50 0\n")
cmdlist.append("*%d" % 1000)
cmdlist.append("M0 0 800 -40 -800 40 0 0 0 0 0 0 -50 0\n")
#cmdlist.append("*%d" % 2000)
else :
pass
for command in cmdlist :
if len(command) == 0 : continue
#print("command =", command)
if command[0] == '*' :
n = int(command[1:]) / 1000
time.sleep(n)
else :
if not __sendmsg(soc, command.encode('utf-8')) :
result = False
break
try :
msg = soc.recv(1024)
except :
result = False
break
return result
#
def login(soc, sid):
sf = soc.makefile()
try :
data = soc.recv(4096).decode()
print(str(data))
cmd = str(data.rstrip())
line = 'id;%s' % sid
#print(line)
soc.send(line.encode('utf-8'))
#rdata = soc.recv(4096).decode()
# サーバから受信したデータを出力
#print(rdata)
except :
print('サーバーとの通信に失敗しました。')
sys.exit(0)
def loop(serversoc, adr,port, tlc):
#tlc = ("localhost", 1234)
#tlc = ("10.186.42.31", 1234)
sota = (adr, port)
st = serversoc.makefile()
tlc_soc = None
while True:
if tlc_soc is None :
tlc_soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
tlc_soc.connect(tlc)
except :
print('Commu4Serverとの通信に失敗しました :', datetime.now())
tlc_soc = None
time.sleep(5)
continue
if not doCommu4Command(tlc_soc, '*init*') :
print('Commu4Serverとの通信に失敗しました :', datetime.now())
tlc_soc.close()
tlc_soc = None
time.sleep(5)
continue
try :
data = st.readline().strip()
#data = serversoc.recv(4096)
print(data)
#data = data.decode()
except socket.timeout :
continue
cmd = str(data.rstrip())
#cmd = st.readline()
if len(cmd) <= 3 :
continue
#print(cmd)
header = cmd[:3]
if header == 'tlc' :
if not doCommu4Command(tlc_soc, cmd) :
print('Commu4Serverとの通信に失敗しました :', datetime.now())
tlc_soc.close()
tlc_soc = None
continue
else :
try :
sota_soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sota_soc.connect(sota)
sota_soc.send(cmd.encode('utf-8')) #(bytes(cmd))
#cmd = st.readline()
sota_soc.close()
sota_soc = None
except :
print('SOTAとの通信に失敗しました :', datetime.now())
if cmd == "cmd;motion;Nodding":
print("Nodding")
elif cmd == "cmd;motion;HeadShaking":
print("HeadShaking")
elif cmd == "cmd;motion;RightHandWaving":
print("RightHandWaving")
elif cmd == "cmd;motion;LeftHandWaving":
print("LeftHandWaving")
elif cmd == "cmd;motion;BothHandWaving":
print("BothHandWaving")
elif cmd == "cmd;motion;BothHandRaising":
print("BothHandRaising")
elif cmd == "cmd;end":
print("End")
else:
pass
#print("Unknown command")
if __name__ == '__main__':
if (len(sys.argv) != 7):
print("Usage: {} <id> <server ip> <server port> <sota ip> <sota port> <teleco ip>".format(sys.argv[0]))
sys.exit(1)
sid = sys.argv[1]
serverip = sys.argv[2]
serverport = sys.argv[3]
sotaip = sys.argv[4]
sotaport = int(sys.argv[5])
server = (serverip, int(serverport))
tlc = (sys.argv[6], 1234)
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#soc.settimeout(10.0)
try :
soc.connect(server)
soc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
except:
print('serverとの接続に失敗しました。 : %s:%s' % (serverip, serverport))
sys.exit(0)
login(soc, sid)
loop(soc, sotaip, sotaport, tlc)
soc.close()