心率血压协议对接
authord15521469561 <d155214695615@qq.com>
Wed, 12 Apr 2023 01:44:51 +0000 (09:44 +0800)
committerd15521469561 <d155214695615@qq.com>
Wed, 12 Apr 2023 01:44:51 +0000 (09:44 +0800)
3.code/1.watch/2.code/constants.py
3.code/1.watch/2.code/main_1.py
3.code/1.watch/2.code/mgr.py
3.code/1.watch/2.code/test_sms.py
3.code/1.watch/2.code/ui.py

index 8131117..c3d9fd0 100644 (file)
@@ -38,9 +38,14 @@ class PROTOCOL:
     HRTSTART_SEND = "hrtstart_SEND"
     HRTSTART_RECV = "hrtstart_RECV"
 
+    HEART_SEND = "heart_SEND"
+    HEART_RECV = "heart_RECV"
+    
+    OXYGEN_SEND = "oxygen_SEND"
+    OXYGEN_RECV = "oxygen_RECV"
+
     OXSTART_SEND = "oxstart_SEND"
     OXSTART_RECV = "oxstart_RECV"
-    
 
 
 class EVENT:
index a550eab..d6b7788 100644 (file)
@@ -60,7 +60,7 @@ if __name__ == '__main__':
     from ext.appWechat import WechatScreen,TK_ProtoCol,MESSAGE_ProtoCol
     import ext.ui_setting as _ui_setting
     from ext.appFactoryMode import  FactoryModeLocationScreen,FactoryModeScreen 
-    from ext.test_sms import hrTimeHandlerManager,HRSTART_ProtoCol,getHrTimeManager
+    from usr.test_sms import hrTimeHandlerManager,HRSTART_ProtoCol,getHrTimeManager,HEART_ProtoCol,OXYGEN_ProtoCol,OXYGEN_One_ProtoCol
 
    #from usr.ui_wechat import WechatScreen,TK_ProtoCol
     btn_device = BtnDevice(tp_cst816)
@@ -166,7 +166,9 @@ if __name__ == '__main__':
     pcr.add_support(WT_ProtoCol())
     pcr.add_support(REMIND_ProtoCol())
     pcr.add_support(HRSTART_ProtoCol())
-    # pcr.add_support(OXSTART__ProtoCol())
+    pcr.add_support(HEART_ProtoCol())
+    pcr.add_support(OXYGEN_ProtoCol())
+    pcr.add_support(OXYGEN_One_ProtoCol())
     sm = SockManager()
     sm.add_support_resolver(pcr)
     app.append_manager(sm)
index f3e76ea..bd5ae61 100644 (file)
@@ -526,7 +526,12 @@ class DeviceInfoManager(Abstract):
         self.HR_time = 0
         self.SENSOR_time = 0
         self.HR_state = 0
-        self.hr_time = 0
+        self.HR_Onestate = 0
+        self.get_hr_state = 0
+        self.get_spo2_state = 0
+        self.HR_OneTime = 0
+        self.Hr_Need_Open = 0
+        self.Spo2_Need_Open = 0
         self.old_time = 0
         self.UD_time = 0
         self.UD_timeout = 7*60
@@ -621,21 +626,35 @@ class DeviceInfoManager(Abstract):
         EventMesh.publish(constants.EVENT.PUSH_STEP, self.get_step_count())
 
         hr_time = EventMesh.publish("get_time_hr_spo2")#获取时间是否大于0
-        print("hr_time:{}".format(hr_time))
-        get_hr_state = EventMesh.publish("get_hr_state")
-        get_spo2_state = EventMesh.publish("get_spo2_state")
         
+        self.get_hr_state = EventMesh.publish("get_hr_state")
+        self.get_spo2_state = EventMesh.publish("get_spo2_state")
+        print("hr_time:{} get_hr_state:{} get_spo2_state:{}".format(hr_time,self.get_hr_state,self.get_spo2_state))
+        
+          
+           
         if hr_time != None:
+            print("hr......01....{}\r\n".format(hr_time))
             if hr_time > 0:
-                if get_hr_state != 1 and get_spo2_state != 1:
-                    print("self.old_time:{} hr_time:{}".format(self.old_time,hr_time))
+
+                self.HR_Onestate = 0
+                self.HR_OneTime = 0
+                print("get_hr_state:{} get_spo2_state:{} self.old_time:{} hr_time:{}".format(self.get_hr_state,self.get_spo2_state,self.old_time,hr_time))
+                if self.get_hr_state != 1 and self.get_spo2_state != 1:
+
                     if self.old_time != hr_time:
                         self.SENSOR_time = time_tmp#防止改变时间的时候正在开启测量突然10s就停止测量
-                        if get_hr_state == 2 and self.HR_state == 1:#自动检测的心率还在测量立马关了测试
+                        if self.get_hr_state == 2 and self.HR_state == 1:#自动检测的心率还在测量立马关了测试
+                            heart_value = EventMesh.publish(constants.EVENT.GET_HEART)
+                            if heart_value > 50 and heart_value != None:
+                                EventMesh.publish(constants.PROTOCOL.HEART_SEND,heart_value)
                             EventMesh.publish(constants.EVENT.STOP_HEART)
                             self.HR_state = 0
-                        elif get_spo2_state ==2 and self.HR_state == 2:
+                        elif self.get_spo2_state == 2 and self.HR_state == 2:
                             self.HR_state = 0
+                            spo2_value = EventMesh.publish(constants.EVENT.GET_BLOOD_OXYGEN)
+                            if spo2_value > 85 and spo2_value != None:
+                                EventMesh.publish(constants.PROTOCOL.OXYGEN_SEND,spo2_value)
                             EventMesh.publish(constants.EVENT.STOP_BLOOD_OXYGEN)
                     self.old_time = hr_time
 
@@ -649,6 +668,9 @@ class DeviceInfoManager(Abstract):
                     elif math.fabs(time_tmp - self.SENSOR_time) >= (hr_time+60) and self.HR_state == 1:
                         EventMesh.publish("set_hr_state",0)#2代表后台进入测试
                         EventMesh.publish(constants.EVENT.STOP_HEART)
+                        heart_value = EventMesh.publish(constants.EVENT.GET_HEART)
+                        if heart_value > 50 and heart_value != None:
+                            EventMesh.publish(constants.PROTOCOL.HEART_SEND,heart_value)#上传心率数据
                         #获取心率数据上传数据app
                         print("STOP_HEART:")
                         EventMesh.publish("set_spo2_state",2)#2代表后台进入血氧测试
@@ -659,25 +681,103 @@ class DeviceInfoManager(Abstract):
                         print("STOP_BLOOD_OXYGEN:")
                         EventMesh.publish("set_spo2_state",0)#2代表后台进入血氧测试
                         EventMesh.publish(constants.EVENT.STOP_BLOOD_OXYGEN)#停止血氧测试
+                        spo2_value = EventMesh.publish(constants.EVENT.GET_BLOOD_OXYGEN)
+                        print("spo2_value:{}".format(spo2_value))
+                        if spo2_value > 85 and spo2_value != None:
+                            EventMesh.publish(constants.PROTOCOL.OXYGEN_SEND,spo2_value)
                         self.HR_state = 0
                         self.SENSOR_time = time_tmp
                 else:
+                    print("endtime.........\r\n")
                     self.SENSOR_time = time_tmp
                     self.HR_state = 0
                     self.old_time = 0      
             else:
+                print("hr......01....{} {} self.HR_Onestate {} {}".format(self.get_hr_state,self.get_spo2_state,self.HR_OneTime,self.HR_Onestate))
                 self.SENSOR_time = time_tmp#进入最后一次的时间戳
                 self.old_time = 0
-                if get_hr_state != 1 or get_spo2_state != 1:#
-                    if get_hr_state == 2 and self.HR_state == 1:#自动检测的心率还在测量立马关了测试
-                        EventMesh.publish(constants.EVENT.STOP_HEART)
-                        self.HR_state = 0
-                    elif get_spo2_state ==2 and self.HR_state == 2:
+
+                if self.get_hr_state != 1 or self.get_spo2_state != 1:
+                    if self.get_spo2_state == 3 or self.get_hr_state == 3:#单次测量
                         self.HR_state = 0
-                        EventMesh.publish(constants.EVENT.STOP_BLOOD_OXYGEN)
+                        # 心率
+                        self.HR_OneTime+=1
+                        if self.HR_OneTime == 1 and self.HR_Onestate == 0:
+                            print("hr..........\r\n")
+                            if self.get_hr_state == 3:
+                                self.Hr_Need_Open = 1
+                                EventMesh.publish("set_hr_state",3)#2代表后台进入测试
+                                EventMesh.publish(constants.EVENT.START_HEART)
+                            else:
+                                self.Hr_Need_Open = 0
+
+                            if self.get_spo2_state == 3 and self.Hr_Need_Open == 0:
+                                self.Spo2_Need_Open = 1
+                                EventMesh.publish("set_spo2_state",3)#2代表后台进入测试
+                                EventMesh.publish(constants.EVENT.START_BLOOD_OXYGEN)#K开血氧
+                            else:
+                                self.Spo2_Need_Open = 0
+                            self.HR_Onestate = 1
+                            print("self.HR_Onestate....1..:{} {} {} {} {}".format(self.HR_Onestate,self.get_hr_state,self.get_spo2_state,self.Hr_Need_Open,self.Spo2_Need_Open))
+                        elif self.HR_OneTime > 6 and self.HR_Onestate == 1:
+                            print("self.HR_Onestate...2....:{} {} {} {} {}".format(self.HR_Onestate,self.get_hr_state,self.get_spo2_state,self.Hr_Need_Open,self.Spo2_Need_Open))
+                            if self.Hr_Need_Open == 1:#需要关闭心率测量
+                                EventMesh.publish(constants.EVENT.STOP_HEART)
+                                heart_value = EventMesh.publish(constants.EVENT.GET_HEART)
+                                if heart_value > 50 and heart_value != None:
+                                    EventMesh.publish(constants.PROTOCOL.HEART_SEND,heart_value)#上传心率数据
+                                    print("STOP_HEART:")
+                                if self.Spo2_Need_Open == 0 and self.get_spo2_state == 3:
+                                    EventMesh.publish("set_spo2_state",3)#2代表后台进入血氧测试
+                                    EventMesh.publish(constants.EVENT.START_BLOOD_OXYGEN)#K开血氧
+                                    print("START_BLOOD_OXYGEN:")
+                                    self.HR_Onestate = 2
+                                else:
+                                    self.HR_Onestate = 0
+                                    self.HR_OneTime = 0
+                                    print("START_end:")
+                                    EventMesh.publish("set_hr_state",0)#2代表后台进入血氧测试
+                                    EventMesh.publish("set_spo2_state",0)#2代表后台进入血氧测试
+                            else:
+                                if self.Spo2_Need_Open == 1:#停止测试血氧
+                                    EventMesh.publish("set_spo2_state",0)#2代表后台进入血氧测试
+                                    EventMesh.publish(constants.EVENT.STOP_BLOOD_OXYGEN)#K开血氧
+                                    spo2_value = EventMesh.publish(constants.EVENT.GET_BLOOD_OXYGEN)
+                                    print("spo2_value:{}".format(spo2_value))
+                                    if spo2_value > 85 and spo2_value != None:
+                                        EventMesh.publish(constants.PROTOCOL.OXYGEN_SEND,spo2_value)
+                                    self.HR_Onestate = 0
+                                    self.HR_OneTime = 0
+                                    print("STOP_BLOOD_OXYGEN:")
+                                    
+                        elif self.HR_OneTime > 6+6 and self.HR_Onestate == 2:
+                            print("HR_OneTime:")
+                            EventMesh.publish("set_spo2_state",0)#2代表后台进入血氧测试
+                            EventMesh.publish(constants.EVENT.STOP_BLOOD_OXYGEN)#停止血氧测试
+                            spo2_value = EventMesh.publish(constants.EVENT.GET_BLOOD_OXYGEN)
+                            print("spo2_value:{}".format(spo2_value))
+                            if spo2_value > 85 and spo2_value != None:
+                                EventMesh.publish(constants.PROTOCOL.OXYGEN_SEND,spo2_value)
+                            self.HR_Onestate = 0
+                    else:
+                        if self.get_hr_state == 2 and self.HR_state == 1:#自动检测的心率还在测量立马关了测试
+                            EventMesh.publish(constants.EVENT.STOP_HEART)
+                            heart_value = EventMesh.publish(constants.EVENT.GET_HEART)
+                            print("endtime....heart_value....{}".format(heart_value))
+                            if heart_value > 50 and heart_value != None:
+                                EventMesh.publish(constants.PROTOCOL.HEART_SEND,heart_value)#上传心率数据
+                            self.HR_state = 0
+                        elif self.get_spo2_state == 2 and self.HR_state == 2:
+                            self.HR_state = 0
+                            spo2_value = EventMesh.publish(constants.EVENT.GET_BLOOD_OXYGEN)
+                            print("endtime....spo2_value....{}".format(heart_value))
+                            if spo2_value > 85 and spo2_value != None:
+                                EventMesh.publish(constants.PROTOCOL.OXYGEN_SEND,spo2_value)
+                            EventMesh.publish(constants.EVENT.STOP_BLOOD_OXYGEN)
                 else:
                     self.HR_state = 0
-        
+                    
+            
         voiceCall.setCallback(voice_callback)
             
         #quecgnss.gnssEnable(0)  
@@ -1016,7 +1116,13 @@ class SockManager(Abstract):
             self.connect_tmp = 1 #有数据,就认为是连接状态
             print("TCP:recv:len:{} type:{}".format(len(data),type(data)))
             EventMesh.publish(constants.EVENT.SET_CONNECT, 1)
-            self.resolver.handler(data)
+            data_str = data.decode('ascii')
+            data_str = data_str.split(']')
+            if len(data_str) < 3 :
+                self.resolver.handler(data)
+            else :
+                for i in range(len(data_str)):
+                    self.resolver.handler(data_str[i].decode("utf-8"))
             data = b''
 
 
index 5b7ccb5..514b337 100644 (file)
@@ -244,7 +244,7 @@ class hrTimeHandlerManager(Abstract):
         print("---!self.second!!!!---:{} time_tmp:{} rtc_now_time:{}".format(self.second,time_tmp,rtc_now_time))
         
 class HRSTART_ProtoCol(ProtoCol):
-    """心率数据"""
+    """心率频率数据"""
     TYPE = "hrtstart"
 
 
@@ -271,14 +271,104 @@ class HRSTART_ProtoCol(ProtoCol):
         msg_tmp = msg_tmp[1]
         # print("HRSTART_ProtoCol RECV msg:{} msg_:{}".format(msg,msg_tmp))
         hrTotal = int(msg_tmp)
-        if hrTotal < 300 and hrTotal > 0:
-            hrTotal = 300
-
-        with open("/ext/static/storage_file/setHrTime.txt","w") as fd:
-            fd.write(str(hrTotal))
-        EventMesh.publish("set_time_hr_spo2",hrTotal)
+        if hrTotal == 1:#单次测量
+            get_hr_state = EventMesh.publish("get_hr_state")
+            get_spo2_state = EventMesh.publish("get_spo2_state")
+            print("get_hr_state:{} get_spo2_state:{} hrTotal:{}".format(get_hr_state,get_spo2_state,hrTotal))
+            if get_hr_state == 0 and get_spo2_state == 0:#只要两个不在测量 单次优先级最低
+                EventMesh.publish("set_hr_state",3)#3代表单次测量
+        elif hrTotal < 30 and hrTotal > 1:
+            hrTotal = 30
+            with open("/ext/static/storage_file/setHrTime.txt","w") as fd:
+                fd.write(str(hrTotal))
+            EventMesh.publish("set_time_hr_spo2",hrTotal)
+       
         EventMesh.publish(constants.PROTOCOL.HRTSTART_SEND)
 
+class HEART_ProtoCol(ProtoCol):
+    """心率数据"""
+    TYPE = "heart"
+
+
+    def send(self, event, msg):
+        # [SZKW*334588000000156*len*heart,x]
+        # print("HEART:send:{}".format(msg))
+        len_size = len(str(msg))+6
+        send_data = "SZKW*%s*%04X*heart"%(self.imei,len_size)
+        MD5_SIGN = self.pack_MD5_head(send_data)
+        data = "[%s%s,%d]"%(str(MD5_SIGN),send_data,msg)
+        # print("HEART_ProtoCol:data:%s"%(data))
+        # 通过socket发送到服务器端
+        EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data)
+
+    def recv(self, event, msg):
+        """
+        @param event: 心率数据下发
+        @param msg: 接收到的下行数据
+        """
+        # print("HRSTART_ProtoCol RECV msg:")
+        #EventMesh.publish(constants.PROTOCOL.HEART_SEND)
+class OXYGEN_ProtoCol(ProtoCol):
+    """上传血氧测试数据"""
+    TYPE = "oxygen"
+    def send(self, event, msg):
+        # [SZKW*334588000000156*len*heart,x]
+        # print("oxstart:send:{}".format(msg))
+        len_size = len(str(msg))+6
+        send_data = "SZKW*%s*%04X*oxygen"%(self.imei,len_size)
+        MD5_SIGN = self.pack_MD5_head(send_data)
+        data = "[%s%s,%d]"%(str(MD5_SIGN),send_data,msg)
+        # print("HEART_ProtoCol:data:%s"%(data))
+        # 通过socket发送到服务器端
+        EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data)
+
+    def recv(self, event, msg):
+        """
+        @param event: 心率数据下发
+        @param msg: 接收到的下行数据
+        """
+        # print("HRSTART_ProtoCol RECV msg:")
+        #EventMesh.publish(constants.PROTOCOL.OXYGEN_SEND)
+
+class OXYGEN_One_ProtoCol(ProtoCol):#单次测量
+    """上传血氧测试数据"""
+    TYPE = "oxstart"
+
+# 终端发送[XT*YYYYYYYYYYYYYYY*LEN*oxygen,x]
+# x 表示血氧饱和度
+# 示例[XT*334588000000156*0009*oxygen,15]
+# 平台回复:handler -> b'[SZKW*860132060004322*0009*oxstart,1]'
+# [XT*334588000000156*0006*oxygen]
+    def send(self, event, msg):
+        # [SZKW*334588000000156*len*heart,x]
+        print("oxstart:")
+        send_data = "SZKW*%s*0007*oxstart"%(self.imei)
+        MD5_SIGN = self.pack_MD5_head(send_data)
+        data = "[%s%s]"%(str(MD5_SIGN),send_data)
+        # print("HEART_ProtoCol:data:%s"%(data))
+        # 通过socket发送到服务器端
+        EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data)
+
+    def recv(self, event, msg):
+        """
+        @param event: 血氧单次测量下发
+        @param msg: 接收到的下行数据
+        """
+        print("oxstart:{}".format(msg))
+        msg = msg[1:-1]
+        msg_tmp = msg.decode('ascii')
+        msg_tmp = msg_tmp.split(',')
+        print("msg_tmp:{}".format(msg_tmp))
+        msg_tmp = msg_tmp[1]
+        state = int(msg_tmp)
+        get_hr_state = EventMesh.publish("get_hr_state")
+        get_spo2_state = EventMesh.publish("get_spo2_state")
+        print("get_hr_state:{} get_spo2_state:{} state:{}".format(get_hr_state,get_spo2_state,state))
+        if state == 1:
+            if get_hr_state == 0 and get_spo2_state == 0:#只要两个不在测量 单次优先级最低
+                EventMesh.publish("set_spo2_state",3)#3代表单次测量
+        EventMesh.publish(constants.PROTOCOL.OXSTART_SEND)
+
 #设置和获取心率上传频率时间
 
 class getHrTimeManager(Abstract):
@@ -304,21 +394,18 @@ class getHrTimeManager(Abstract):
         self.set_time = msg
 
     def get_time_hr_spo2(self, event, msg): 
-        print("self.set_time:".format(self.set_time))
         return self.set_time
 
     def set_hr_state(self, event, msg): 
         self.set_hr = msg
 
     def get_hr_state(self, event, msg): 
-        print("self.set_hr:".format(self.set_hr))
         return self.set_hr
     
     def set_spo2_state(self, event, msg): 
         self.set_spo2 = msg
 
     def get_spo2_state(self, event, msg): 
-        print("self.set_spo2:".format(self.set_spo2))
         return self.set_spo2
            
        
\ No newline at end of file
index 995a0a1..c82c2fc 100644 (file)
@@ -1742,6 +1742,7 @@ class HeartrateScreen(Screen):
         heartrate_bpm_label.set_text(" ")
         heartrate_start_label.set_text("正在测量中.")
         EventMesh.publish("set_hr_state",1)#1代表手动进入测试界面
+        
         EventMesh.publish(constants.EVENT.START_HEART)
 
 
@@ -1768,6 +1769,8 @@ class HeartrateScreen(Screen):
 
         if self.point > 60: #测20s
             self.heart_value = EventMesh.publish(constants.EVENT.GET_HEART)
+            if self.heart_value > 50:
+                EventMesh.publish(constants.PROTOCOL.HEART_SEND,self.heart_value)
             if self.heart_value != 0:
                 heartrate_start_label.set_text(" ")
                 heartrate_bpm_label.set_text("{}  bpm".format(self.heart_value))
@@ -2583,6 +2586,9 @@ class Spo2Screen(Screen):
 
         if self.point > 60: #测20s
             self.spo2_value = EventMesh.publish(constants.EVENT.GET_BLOOD_OXYGEN)
+            print("ui self.spo2_value :{}".format(self.spo2_value))
+            if self.spo2_value > 85 and self.spo2_value != None:
+                EventMesh.publish(constants.PROTOCOL.OXYGEN_SEND,self.spo2_value)
             spo2_label.set_text("{} %".format(self.spo2_value))
             if self.spo2_value == 0:
                 spo2_label_1.set_text("退出,请重新测量")