Skip to content

Commit c25df98

Browse files
authored
Merge pull request #12 from aaronchenzhihe/master
ota
2 parents d1609e0 + 46b98d8 commit c25df98

3 files changed

Lines changed: 63 additions & 4 deletions

File tree

src/OTA_test.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from usr.protocol import *
2+
import utime
3+
import usocket as socket
4+
import umqtt
5+
import ujson as json
6+
from usr import uuid
7+
import request
8+
import _thread
9+
10+
class OTA(object):
11+
def __init__(self,mac):
12+
self.uuid = str(uuid.uuid4())
13+
self.head = {
14+
'Accept-Language': 'zh-CN',
15+
'Content-Type': 'application/json',
16+
'User-Agent': 'kevin-box-2/1.0.1',
17+
'Device-Id': mac,
18+
'Client-Id': self.uuid
19+
}
20+
self.ota_data = {
21+
"application": {
22+
"version": "1.0.1",
23+
"elf_sha256": "c8a8ecb6d6fbcda682494d9675cd1ead240ecf38bdde75282a42365a0e396033"
24+
},
25+
"board": {
26+
"type": "kevin-box",
27+
"name": "kevin-box-2",
28+
"carrier": "CHINA UNICOM",
29+
"csq": "22",
30+
"imei": "****",
31+
"iccid": "89860125801125426850"
32+
}
33+
}
34+
self.url = "https://api.tenclass.net/xiaozhi/ota/"
35+
# self.url ="http://xiaozhi.pandastroller.com:8002/xiaozhi/ota/"
36+
self.response = None
37+
self.UDP_IP = None
38+
self.UDP_PORT = None
39+
self.run()
40+
41+
def run(self):
42+
# 通过OTA得到mqtt的连接参数
43+
resp = request.post(self.url, data=json.dumps(self.ota_data), headers=self.head)
44+
self.response = resp.json()
45+
# print(self.response)
46+
return self.response
47+
48+
49+
50+

src/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ def __chat_process(self):
161161
if self.__voice_activity_event.is_set():
162162
# 有人声
163163
if not is_listen_flag:
164+
self.__protocol.abort()
164165
self.__protocol.listen("start")
165166
is_listen_flag = True
166167
self.__protocol.send(data)
@@ -201,7 +202,7 @@ def on_voice_activity_detection(self, state):
201202
gc.collect()
202203
logger.info("on_voice_activity_detection: {}".format(state))
203204
if state == 1:
204-
self.__protocol.abort()
205+
# self.__protocol.abort()
205206
self.__voice_activity_event.set() # 有人声
206207
else:
207208
self.__voice_activity_event.clear() # 无人声

src/protocol.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
from usr.threading import Thread, Condition
66
from usr.logging import getLogger
77
import sys_bus
8+
from usr.OTA_test import OTA
89

910

1011

1112
logger = getLogger(__name__)
1213

1314

15+
1416
WSS_DEBUG = True
1517
WSS_HOST = "wss://api.tenclass.net/xiaozhi/v1/"
1618
ACCESS_TOKEN = "test-token"
@@ -65,14 +67,19 @@ def validate(request, response):
6567
class WebSocketClient(object):
6668

6769
def __init__(self, host=WSS_HOST, debug=WSS_DEBUG):
70+
global WSS_HOST
71+
self.ota = OTA(mac=self.get_mac_address())
6872
self.debug = debug
69-
self.host = host
73+
WSS_HOST = self.ota.run()
74+
self.host = WSS_HOST["websocket"]["url"]
7075
self.__resp_helper = RespHelper()
7176
self.__recv_thread = None
7277
self.__audio_message_handler = None
7378
self.__json_message_handler = None
7479
self.__last_text_value = None
75-
80+
logger.info("OTA:{}".format(WSS_HOST))
81+
82+
7683
def __str__(self):
7784
return "{}(host=\"{}\")".format(type(self).__name__, self.host)
7885

@@ -129,7 +136,7 @@ def connect(self):
129136
__client__ = ws.Client.connect(
130137
self.host,
131138
headers={
132-
"Authorization": "Bearer {}".format(ACCESS_TOKEN),
139+
"Authorization": "Bearer {}".format(WSS_HOST["websocket"]["token"]),
133140
"Protocol-Version": PROTOCOL_VERSION,
134141
"Device-Id": self.get_mac_address(),
135142
"Client-Id": self.generate_uuid()
@@ -138,6 +145,7 @@ def connect(self):
138145
)
139146

140147
try:
148+
141149
self.__recv_thread = Thread(target=self.__recv_thread_worker)
142150
self.__recv_thread.start(stack_size=64)
143151
except Exception as e:

0 commit comments

Comments
 (0)