-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
100 lines (85 loc) · 2.36 KB
/
main.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import socket
from contextlib import closing
import xml.etree.ElementTree as ET
import time
import os
import sys
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='UTF-8')
from bot import Bot
print(sys.getdefaultencoding())
print(sys.stdout.encoding)
sys.path.append("/home/pi")
#from irkit import main as power_on
from cpschromecast import CpsChromecast
# TODO
sys.path.append("/home/pi")
# TODO what?
time.sleep(5)
home_path = '/home/pi'
bot = Bot()
is_state_recieve = False
def main():
host = 'localhost'
port = 10500
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host,port))
text_queue = ""
separator = ".\n"
bot.say('まいまい起動しました')
while True:
bufsize = 4096
recv_data = sock.recv(4096)
text_queue += recv_data.decode('utf-8')
if separator in text_queue:
parts = text_queue.split(separator)
xmls, text_queue = parts[:-1], parts[-1]
print(".", end="")
search_xmls(xmls)
def search_xmls(xmls):
for xml_text in xmls:
try:
root = ET.fromstring(xml_text)
dispatch_command(root)
except ET.ParseError:
print()
print('parce error--')
print(xml_text)
print('--')
def dispatch_command(root):
global is_state_recieve
for word in root.iter('WHYPO'):
command = word.get('WORD')
cm = float(word.get('CM'))
print()
print(command + ": [" + str(cm) + "]")
if cm < 0.50:
print('skip')
continue
if command == 'shutup' and cm > 0.90:
bot.stop_say()
# 前回受け取ったコマンドが 'start' であるか
elif command == 'start':
if cm <= 0.94:
continue
is_state_recieve = True
bot.say('はい')
break
elif is_state_recieve:
if cm < 0.94:
bot.say('え?')
break
else:
print('>>>' + command)
bot.listen(command)
is_state_recieve = False
if __name__ == '__main__':
try:
main()
except:
import traceback
traceback.print_exc()
# bot.listen('exit')