# al_listen_screen_icon.set_src("U:/static/AI/ai_confirm_bt.png")
# al_listen_screen_icon.add_style(style_img, lv.PART.MAIN | lv.STATE.DEFAULT)
-al_listen_screen_icon_bt = lv.label(AI_listen_screen)
-al_listen_screen_icon_bt.set_text(" ")
-al_listen_screen_icon_bt.set_size(240, 190)
-al_listen_screen_icon_bt.set_pos(0,92)
+# al_listen_screen_icon_bt = lv.label(AI_listen_screen)
+# al_listen_screen_icon_bt.set_text(" ")
+# al_listen_screen_icon_bt.set_size(240, 190)
+# al_listen_screen_icon_bt.set_pos(0,92)
al_listen_screen_title4 = lv.label(AI_listen_screen)
al_listen_screen_title4.set_pos(0, 100)
#if self.AI_client_chat_isconnect == 0:
# 创建客户端, debug=True输出日志, ip和端口需要自己填写, 或者是域名
try:
- self.AI_client_chat = uwebsocket.Client.connect('ws://118.145.158.207:80/chat/ws', debug=False)
+ if EventMesh.DEV_RES_MANAGE_AUTHEN_PLATFORM == 1: #加了设备资源管理鉴权平台的功能
+ token = EventMesh.publish("get_dev_token")
+ print("websocket_connect_asr token:{}\r\n".format(token))
+ if token == None:
+ token = ""
+ headers_t={"dt":"{}".format(token)}
+ #'ws://amp.raisound.com/chat/ws?
+ # self.AI_client_chat = uwebsocket.Client.connect('ws://amp.raisound.com/chat/ws?dt={}'.format(token), debug=False)
+ self.AI_client_chat = uwebsocket.Client.connect('ws://118.145.158.207:22080/chat/ws?dt={}'.format(token), debug=False)
+ else:
+ self.AI_client_chat = uwebsocket.Client.connect('ws://118.145.158.207:80/chat/ws', debug=False)
except Exception as e: #网络连接异常
usys.print_exception(e)
print("websocket_connect_chat error!!!\r\n")
try:
_thread.stop_thread(self.AI_client_chat_recv_id)
except Exception as e:
- usys.print_exception(e)
+ usys.print_exception(e)
self.AI_client_chat_recv_id = 0
self.AI_client_chat_recv_id = _thread.start_new_thread(self.AI_client_chat_recv, (self.AI_client_chat,))
self.AI_client_chat_isconnect = 1
try:
# 死循环接收数据
recv_data = cli.recv()
- if not recv_data:
+ if not recv_data or recv_data == None:
# 服务器关闭连接或客户端关闭连接
print("AI_client_chat_recv close")
#self.AI_client_chat_isconnect = 0
if self.AI_client_tts_isconnect == 0:
try:
# self.AI_client_tts = uwebsocket.Client.connect('ws://40.67.151:9099/tts/', debug=False)
- self.AI_client_tts = uwebsocket.Client.connect('ws://118.145.158.207:80/tts/long-text-stream', debug=False)
+ # ws://127.0.0.1:9099/tts/stream
+ if EventMesh.DEV_RES_MANAGE_AUTHEN_PLATFORM == 1: #加了设备资源管理鉴权平台的功能
+ token = EventMesh.publish("get_dev_token")
+ print("websocket_connect_asr token:{}\r\n".format(token))
+ if token == None:
+ token = ""
+ headers_t={"dt":"{}".format(token)}
+ # ws://amp.raisound.com/tts/stream?
+ # self.AI_client_tts = uwebsocket.Client.connect('ws://amp.raisound.com/tts/stream?dt={}'.format(token),debug=False) #生产环境地址
+ self.AI_client_tts = uwebsocket.Client.connect('ws://118.145.158.207:22080/tts/stream?dt={}'.format(token),debug=False) #测试地址
+ else:
+ self.AI_client_tts = uwebsocket.Client.connect('ws://118.145.158.207:80/tts/long-text-stream', debug=False)
except Exception as e: #网络连接异常
usys.print_exception(e)
print("websocket_connect_tts ERROR:{}\r\n".format(e))
try:
if EventMesh.DEV_RES_MANAGE_AUTHEN_PLATFORM == 1: #加了设备资源管理鉴权平台的功能
- token = EventMesh.publish("get_storage_config","dev_token")
+ token = EventMesh.publish("get_dev_token")
print("websocket_connect_asr token:{}\r\n".format(token))
if token == None:
token = ""
headers_t={"dt":"{}".format(token)}
- # headers_t = ujson.dumps(headers_t)#需要转义的字符串
- # headers_t = ujson.loads(headers_t)
- self.AI_client_asr = uwebsocket.Client.connect('ws://118.145.158.207:80/asr/ws',headers = headers_t, debug=False)
+ # ws://amp.raisound.com/asr/ws?dt=95666854765b48b16e67eb6b7801175a68
+ # ws://amp.raisound.com/asr/ws?
+ # self.AI_client_asr = uwebsocket.Client.connect('ws://amp.raisound.com/asr/ws?dt={}'.format(token), debug=False) #生产环境地址
+ self.AI_client_asr = uwebsocket.Client.connect('ws://118.145.158.207:22080/asr/ws?dt={}'.format(token), debug=False) #测试地址
else:
self.AI_client_asr = uwebsocket.Client.connect('ws://118.145.158.207:80/asr/ws', debug=False)
try:
# 死循环接收数据
recv_data = cli.recv()
- if not recv_data:
+ if not recv_data or recv_data == None:
# 服务器关闭连接或客户端关闭连接
print("AI_client_asr_recv close")
#self.AI_client_asr_isconnect = 0
usys.print_exception(e)
if self.AI_client_chat != None:
tmp = self.AI_client_chat.send(data[1])
- print("chat send_data:{} tmp:{}".format(len(data[1]),tmp))
+ print("chat send_data:{} tmp:{}".format(len(data[1]),tmp))
if data[0] == self.AI_client_asr_num:
if isinstance(data[1], str):
print("ASR:AI_client_asr_isconnect:{},data:{}",self.AI_client_asr_isconnect,data[1])
from protocol import RTALK_ProtoCol,TKS_ProtoCol,RCALLING_ProtoCol,AGTALK_ProtoCol,CALLING_ProtoCol,MEMBERS_ProtoCol,JPREG_ProtoCol,ppsrvvoicemsg_ProtoCol,ppdevvoicemsg_ProtoCol,srvtextmsg_ProtoCol,TK_ProtoCol,MESSAGE_ProtoCol,APPVOICEMSG_ProtoCol,restart_ProtoCol,factory_ProtoCol,SILENCETIMEFS_ProtoCol,WLKEY_ProtoCol,SILENCETIME2_ProtoCol,KEYBRD_ProtoCol,LK_ProtoCol,PING_ProtoCol,HEART_ProtoCol,OXYGEN_ProtoCol,OXYGEN_One_ProtoCol,HRSTART_ProtoCol,CENTER_ProtoCol,UPLOAD_ProtoCol,\
TEMP_ProtoCol,DLT_ProtoCol,WT_ProtoCol,UD_ProtoCol,REMIND_ProtoCol, CR_ProtoCol,KA_ProtoCol,FIND_ProtoCol,POWEROFF_ProtoCol,Time_ProtoCol,\
- pp_ProtoCol,ppfriends_ProtoCol,revpp_ProtoCol,AL_ProtoCol,reqpp_ProtoCol,delpp_ProtoCol,ProtoColResolver,PHL_ProtoCol,MONITOR_ProtoCol,bootoff_ProtoCol,WDSTART_ProtoCol,LOWBAT_ProtoCol,sosHome_dial_ProtoCol,SockManager,NetManager
+ pp_ProtoCol,ppfriends_ProtoCol,revpp_ProtoCol,AL_ProtoCol,reqpp_ProtoCol,delpp_ProtoCol,ProtoColResolver,PHL_ProtoCol,MONITOR_ProtoCol,bootoff_ProtoCol,WDSTART_ProtoCol,LOWBAT_ProtoCol,sosHome_dial_ProtoCol,device_res_author,SockManager,NetManager
from protocol import CALLCANCEL_ProtoCol,HANGUP_ProtoCol,JMEMBERS_ProtoCol
print("init21:{}".format(utime.localtime()))
print("init11:{}".format(utime.localtime()))
+ if EventMesh.DEV_RES_MANAGE_AUTHEN_PLATFORM == 1:
+ app.append_manager(device_res_author())
+
pcr = ProtoColResolver()
pcr.add_support(LK_ProtoCol())
pcr.add_support(PING_ProtoCol())
import base64
import ucryptolib
import urandom as random
-
+import modem
def server_init(server_type = None):
#服务器选择1,防走丢,2大锤,3三基同创,4童信 5云米
# Device resource authorization
#设备资源授权平台
-SHA1_BLOCK_SIZE = 64
-PAD_BLOCK_SIZE = 16
-SHA256 = uhashlib.sha256
+
# 补位
-pad = lambda s: s + chr(PAD_BLOCK_SIZE - len(s) % PAD_BLOCK_SIZE) * (PAD_BLOCK_SIZE - len(s) % PAD_BLOCK_SIZE)
-# 除去补16字节的多余字符
-unpad = lambda s: s[:-s[-1]]
-class device_res_author:
+
+class device_res_author(Abstract):
- key = b"MWrMZjdtcPhj41jWuoqFAhTmqyFluqj1"
- pid = "qAtdDOGXgXYjT2HGFr3a"
- def __init__(self) -> None:
- self.deviceId = "876226002022574"
+ def __init__(self):
+ self.deviceId = modem.getDevImei()
+ self.SHA1_BLOCK_SIZE = 64
+ self.PAD_BLOCK_SIZE = 16
+ self.SHA256 = uhashlib.sha256
+ self.pad = lambda s: s + chr(self.PAD_BLOCK_SIZE - len(s) % self.PAD_BLOCK_SIZE) * (self.PAD_BLOCK_SIZE - len(s) % self.PAD_BLOCK_SIZE)
+ # 除去补16字节的多余字符
+ self.unpad = lambda s: s[:-s[-1]]
+ self.key = b"MWrMZjdtcPhj41jWuoqFAhTmqyFluqj1" #产品producton
+ self.pid = "qAtdDOGXgXYjT2HGFr3a" #产品pid
+ EventMesh.subscribe("dev_active",self.dev_active) #设备激活
+ EventMesh.subscribe("dev_sign_in",self.dev_sign_in) #设备登入
+ EventMesh.subscribe("update_dev_token",self.update_dev_token) #设备令牌更新
+ EventMesh.subscribe("get_dev_token",self.get_dev_token) #获取token
+ self.update_dev_token_time = osTimer()
+ self.dev_res_timer=osTimer()
+ self.dev_token = None
+ self.dev_res_timerCount = 0
+
+ def post_processor_after_instantiation(self):
+ if EventMesh.DEV_RES_MANAGE_AUTHEN_PLATFORM == 1:
+ self.dev_res_timer.start(2000, 1, self.dev_timer_cb)
+ self.update_dev_token_time.start(10*60*1000,1,self.update_dev_token_time_cb) #每10分钟更新一次
+
+ def dev_timer_cb(self, *args): #设备资源管理鉴权平台定时器
+ if EventMesh.DEV_RES_MANAGE_AUTHEN_PLATFORM == 1:
+ EventMesh.publish("dev_active") #设备激活
+ EventMesh.publish("dev_sign_in") #设备登入
+ get_dev_token = EventMesh.publish("get_dev_token")
+ if get_dev_token == None:
+ get_dev_token = ""
+ if get_dev_token != None: #有token就关闭定时器s
+ if len(get_dev_token) != 0:
+ self.dev_res_timer.stop()
+ return
+ self.dev_res_timerCount+=1
+ if self.dev_res_timerCount > 30: #开机一分钟内没有获取到密码和token 等下一次更新令牌在走登入 激活流程
+ self.dev_res_timerCount=0
+ self.dev_res_timer.stop()
+
+ def update_dev_token_time_cb(self,*argc):
+ EventMesh.publish("update_dev_token")
+ def get_dev_token(self,event,msg):
+ return self.dev_token
+
def HMAC(self,k, m):
- KEY_BLOCK = k + (b'\0' * (SHA1_BLOCK_SIZE - len(k)))
+ KEY_BLOCK = k + (b'\0' * (self.SHA1_BLOCK_SIZE - len(k)))
KEY_INNER = bytes((x ^ 0x36) for x in KEY_BLOCK)
KEY_OUTER = bytes((x ^ 0x5C) for x in KEY_BLOCK)
inner_message = KEY_INNER + m
- outer_message = KEY_OUTER + SHA256(inner_message).digest()
- return SHA256(outer_message)
+ outer_message = KEY_OUTER + self.SHA256(inner_message).digest()
+ return self.SHA256(outer_message)
def get_hmac_sha256(self,k, m):
def aes_CBC_Encrypt(self,data, key, iv): # CBC模式的加密函数,data为明文,key为16字节密钥,iv为16字节的偏移量
key = key.encode('utf-8')
iv = iv.encode('utf-8') # CBC 模式下的偏移量
- data = pad(data) # 补位
+ data = self.pad(data) # 补位
data = data.encode('utf-8')
aes = ucryptolib.aes(key,ucryptolib.MODE_CBC,iv)
result = base64.b64encode(aes.encrypt(data))
# decrypt AES解密 B64decode为base64 转码
result = aes.decrypt(base64.b64decode(data))
- result = unpad(result) # 除去补16字节的多余字符
+ result = self.unpad(result) # 除去补16字节的多余字符
return str(result, 'utf-8') # 以字符串的形式返回
#激活接口 没有密码走激活
- def dev_active(self):
+ def dev_active(self,event,msg):
signal = EventMesh.publish("screen_get_sig")
if signal == -1 or EventMesh.publish("get_sim_Status") != 1:#网络不正常
print("dev_active 无 SIM卡!!!!!!!!\r\n")
date_tmp = utime.localtime() #获取当前时间
print("date_tmp:{}\r\n".format(date_tmp)) #转换时间戳s
timeStamp = (utime.mktime(date_tmp))*1000#获取时间戳ms
- deviceId = self.deviceId #"876226002022574" #以imei号
+ deviceId = modem.getDevImei() #self.deviceId #"876226002022574" #以imei号
pid = self.pid
key = self.key
sign_data = "{}{}{}".format(deviceId,pid,timeStamp) #签名内容
PASSWORD = data_tmp["password"]
# EventMesh.publish("set_storage_config",["dev_pass",PASS])
if PASSWORD != None and IV != None:
- if len(PASSWORD) != 0 and len(IV):
+ if len(PASSWORD) != 0 and len(IV) != 0:
password = PASSWORD
iv = IV
#解密 之后保存本地下一次登入做使用
return random_str
#设备登入 没有Token 走登入获取token
- def dev_sign_in(self):
+ def dev_sign_in(self,event,msg):
signal = EventMesh.publish("screen_get_sig")
if signal == -1 or EventMesh.publish("get_sim_Status") != 1:#网络不正常
print("dev_sign_in 无 SIM卡!!!!!!!!\r\n")
return
token = ""
- token = EventMesh.publish("get_storage_config","dev_token")
+ token = self.dev_token #EventMesh.publish("get_storage_config","dev_token")
dev_pass = EventMesh.publish("get_storage_config","dev_pass")
print("token:{}\r\n".format(token))
if token == None:
print("encrypt_data {}".format(encrypt_data))
#登入数据
- deviceId = self.deviceId
+ deviceId = modem.getDevImei() #self.deviceId
sign_in_data = {
"deviceId":"{}".format(deviceId), #设备ID
"password":"{}".format(encrypt_data), #加密的数据
data_tmp = data["data"]
if "token" in data_tmp: #token
token = data_tmp["token"]
- EventMesh.publish("set_storage_config",["dev_token",token])
+ self.dev_token = token
+ # EventMesh.publish("set_storage_config",["dev_token",token])
print("token:{}\r\n".format(token))
pass
#获取新的令牌 更新token
- def update_dev_token(self):
+ def update_dev_token(self,event,msg):
dev_pass = ""
dev_pass = EventMesh.publish("get_storage_config","dev_pass")
-
+ # print("update_dev_token :\r\n")
if dev_pass == None:
dev_pass = ""
if len(dev_pass) == 0: #没有密码走激活
print("no dev_pass:{}\r\n".format(dev_pass))
- self.dev_active()
- token = EventMesh.publish("get_storage_config","dev_token")
+ EventMesh.publish("dev_active") #设备激活
+ token = self.dev_token #EventMesh.publish("get_storage_config","dev_token")
if token == None:
token = ""
if len(token) == 0: #没有token走登入
print("no dev_token:{}\r\n".format(token))
- self.dev_sign_in()
+ # self.dev_sign_in()
+ EventMesh.publish("dev_sign_in") #设备登入
dev_tokenip = "http://dmp.raisound.com/bz-api/dev/auth/token" #设备新令牌更新地址
- token = EventMesh.publish("get_storage_config","dev_token")
+ token = self.dev_token #EventMesh.publish("get_storage_config","dev_token")
if token == None:
token = ""
if len(token) != 0:
if response.status_code == 200:
try:
headers_data = response.headers
- print("headers_data!!!!! headers_data:{}\r\n".format(headers_data))
+ # print("headers_data!!!!! headers_data:{}\r\n".format(headers_data))
if "bz-dt" in headers_data: #bz-dt字段是否在头部数据里面 如果有就需要更新
bz_dt = headers_data["bz-dt"]
dev_token = bz_dt.replace(" ", "") #去掉所有的空格符
print(dev_token)
- EventMesh.publish("set_storage_config",["dev_token",dev_token])
+ self.dev_token = dev_token
+ # EventMesh.publish("set_storage_config",["dev_token",dev_token])
except Exception as e:
print("headers_data!!!!! error\r\n")
pass
-device_res_author_t = device_res_author()
+
+
+
class SockManager(Abstract):
def __init__(self):
self.reconnect_time = 1000
self.reconnect_flag = 0
self.connect_tmp = 0
-
server_data = server_init()
# EventMesh.debug_log("SockManager:server_data:{}".format(server_data))
# self.server_addr = server_data[0]
def post_processor_after_instantiation(self):
EventMesh.subscribe(constants.EVENT.CHANNEL_SOCKET_SEND, self.send)
self.monitor_timer.start(7 * 60 * 1000, 1, self.check)
+
def add_support_resolver(self, resolver):
self.resolver = resolver
#utime.sleep_ms(500)
# 发送数据
- if EventMesh.DEV_RES_MANAGE_AUTHEN_PLATFORM == 1:
- self.dev_res_timer.stop()
- self.dev_res_timer.start(10000, 1, self.dev_timer_cb)
+
self.publish_timer.start(2000, 1, self.publish_timer_cb)
EventMesh.publish(constants.PROTOCOL.LK_SEND)
def publish_timer_cb(self, *args):
self.authen_set()
- def dev_timer_cb(self, *args): #设备资源管理鉴权平台定时器
- if EventMesh.DEV_RES_MANAGE_AUTHEN_PLATFORM == 1:
- device_res_author_t.dev_active() #设备激活
- device_res_author_t.dev_sign_in() #设备登入
- self.dev_res_timerCount+=1
- if self.dev_res_timerCount > 6: #开机一分钟内没有获取到密码和token 等下一次更新令牌在走登入 激活流程
- self.dev_res_timerCount=0
- self.dev_res_timer.stop()
+
def authen_set(self):
sever_state = EventMesh.publish("get_server_state")#获取修改服务器状态