K01:
authordxy15521469561 <1731678236@qq.com>
Fri, 11 Oct 2024 06:32:43 +0000 (14:32 +0800)
committerdxy15521469561 <1731678236@qq.com>
Fri, 11 Oct 2024 06:32:43 +0000 (14:32 +0800)
加入AI鉴权功能 默认关闭

2.sdk-ZY/project/K01/sdk/services/microPython/ports/quectel/core/ai.py
2.sdk-ZY/project/K01/sdk/services/microPython/ports/quectel/core/main_init.py
2.sdk-ZY/project/K01/sdk/services/microPython/ports/quectel/core/mgr.py
2.sdk-ZY/project/K01/sdk/services/microPython/ports/quectel/core/protocol.py
2.sdk/project/D10/merge_bin.py
2.sdk/project/K5_English/sdk/services/microPython/ports/quectel/core/protocol.py

index 2d03e2c..7de270e 100644 (file)
@@ -633,10 +633,10 @@ al_listen_screen_icon1.add_style(style_img, lv.PART.MAIN | lv.STATE.DEFAULT)
 # 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)
@@ -1795,7 +1795,17 @@ class AIScreen(Screen):#AI_GUI
         #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")
@@ -1806,7 +1816,7 @@ class AIScreen(Screen):#AI_GUI
             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
@@ -1825,7 +1835,7 @@ class AIScreen(Screen):#AI_GUI
             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
@@ -1875,7 +1885,18 @@ class AIScreen(Screen):#AI_GUI
         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))
@@ -2080,14 +2101,15 @@ class AIScreen(Screen):#AI_GUI
         
         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)
 
@@ -2121,7 +2143,7 @@ class AIScreen(Screen):#AI_GUI
             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
@@ -2194,7 +2216,7 @@ class AIScreen(Screen):#AI_GUI
                                 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])
index 5b84457..9bc3fbd 100644 (file)
@@ -204,7 +204,7 @@ def main_init():
     
     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
 
@@ -266,6 +266,9 @@ def main_init():
     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())
index ba12914..21c4d66 100644 (file)
@@ -794,15 +794,7 @@ class DeviceInfoManager(Abstract):
         self.timer_push = 0
         date_tmp = utime.localtime()
         time_tmp = utime.mktime(date_tmp)
-        if EventMesh.DEV_RES_MANAGE_AUTHEN_PLATFORM == 1:  #使能开启
-            # print("EventMesh.DEV_RES_MANAGE_AUTHEN_PLATFORM\r\n")
-            from protocol import device_res_author_t
-            self.updata_dev_token_count+=1
-            if self.updata_dev_token_count >= 6*30: #30分钟检查一下 更新token
-                self.updata_dev_token_count = 0
-                device_res_author_t.update_dev_token() #更新接口
-
-                pass
+        
         # print("date_tmp......01....{} time_tmp:{}\r\n".format(date_tmp,time_tmp))
         
         if self.sim_old_count < 60:
index 85dd767..94a1ef8 100644 (file)
@@ -28,7 +28,7 @@ import hashlib
 import base64
 import ucryptolib
 import urandom as random
-
+import modem
 
 def server_init(server_type = None):
     #服务器选择1,防走丢,2大锤,3三基同创,4童信 5云米
@@ -124,28 +124,65 @@ def UnicodeHexStr_To_Utf8Str(str):
 
 # 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):
@@ -159,7 +196,7 @@ class device_res_author:
     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))
@@ -174,10 +211,10 @@ class device_res_author:
 
         # 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")
@@ -192,7 +229,7 @@ class device_res_author:
             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)  #签名内容
@@ -242,7 +279,7 @@ class device_res_author:
                                         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
                             #解密 之后保存本地下一次登入做使用
@@ -262,13 +299,13 @@ class device_res_author:
         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:
@@ -291,7 +328,7 @@ class device_res_author:
                 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), #加密的数据
@@ -326,28 +363,30 @@ class device_res_author:
                                         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:
@@ -364,18 +403,21 @@ class device_res_author:
                 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):
@@ -389,7 +431,6 @@ class SockManager(Abstract):
         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]
@@ -419,6 +460,7 @@ class SockManager(Abstract):
     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
@@ -443,9 +485,7 @@ class SockManager(Abstract):
             
             #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)
 
@@ -459,14 +499,7 @@ class SockManager(Abstract):
     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")#获取修改服务器状态
index ed21894..1ab622a 100644 (file)
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
-
+#直接执行 python merge_bin.py 会自动生成一个audio.bin这个名字可更改和一个list.json文件
 """
 @FileName : merge_bin.py
 @Author   : zhen.guo
index 4de565d..5990979 100644 (file)
@@ -25,7 +25,7 @@ import usys
 def server_init(server_type = None):
     #服务器选择1,防走丢,2大锤,3三基同创,4童信,5云米,6荣为,8:K5-GT
     if server_type == None:
-        server_type = 8
+        server_type = 2
     #print("server_init:server_type:{}".format(server_type))
     d_server_addr = "api.xcloudtech.com"
     d_server_port = 9000