import audio
from machine import I2C
from Gsensor import SC7A20
+import pm
try :
from Hsensor import GH3018
except Exception as e:
# print("date_tmp......01....{} time_tmp:{}\r\n".format(date_tmp,time_tmp))
EventMesh.publish("countdown_end_cb")
"""定时检查是否关闭gps失败"""
- gnss_state = quecgnss.get_state()
- get_location_flag = EventMesh.publish("get_location_flag")
- if get_location_flag != None:
- if gnss_state == 2 and get_location_flag == 0:
- EventMesh.publish(constants.EVENT.GPSCLOSE)
+ # gnss_state = quecgnss.get_state()
+ # get_location_flag = EventMesh.publish("get_location_flag")
+ # if get_location_flag != None:
+ # # if gnss_state == 2 and get_location_flag == 0:
+ # # EventMesh.publish(constants.EVENT.GPSCLOSE)
+ # if(get_location_flag == 0):
+ # EventMesh.publish(constants.EVENT.START_LOCATION,constants.PROTOCOL.UD_SEND) #开始UD定位
"""7分钟一次心跳"""
if math.fabs(time_tmp - self.KA_time) > 7*60: #7分钟一次心跳
"""定时定位"""
self.upload_time = EventMesh.publish("get_upload_data")
#print("self.upload_time:{} math.fabs(time_tmp - self.UD_time):{}".format(self.upload_time,math.fabs(time_tmp - self.UD_time)))
- if self.upload_time != None:
+ if self.upload_time != None :
if math.fabs(time_tmp - self.UD_time) > self.upload_time: #30分钟一次定位
self.UD_time = time_tmp
- step_tmp = self.gsensor.get_step()
- if EventMesh.publish(constants.EVENT.GET_CONNECT) == 1 and step_tmp != self.GPS_step_t :
+ status_tmp = self.gsensor.get_status()#获取运动次数
+ #status_tmp = 4
+ #EventMesh.txt_log("status_count:{}".format(status_tmp))
+ if EventMesh.publish(constants.EVENT.GET_CONNECT) == 1 and status_tmp > 3 :
EventMesh.publish(constants.EVENT.START_LOCATION,constants.PROTOCOL.UD_SEND) #开始UD定位
- self.GPS_step_t = step_tmp#记录步数
+ self.gsensor.clean_status()#清除运动次数
+ else : #关闭定位
+ #EventMesh.txt_log("NO status")
+ print("NO status")
+ EventMesh.publish(constants.EVENT.CLOSE_LOCATION)#关闭定位
EventMesh.publish(constants.EVENT.PUSH_STEP, self.get_step_count())
- EventMesh.publish("get_class_in_time_page")
+
+ """电池平均值"""
temp = Power.getVbatt()
if self.bat_count < 10:
def wifiScan_cb(args):
EventMesh.publish(constants.EVENT.WIFIINFO, args)
+ wifiScan.control(0)
class LocationManager(Abstract):
self.match = GPSMatch()
self.timer_read_GPS_info = osTimer()
self.wifi_data = []
- self.gps_data = []
+ self.gps_data = {}
self.cell_data = []
self.location_timer_count = 0
self.location_check_data_timer_count = 0
+ self.GGA_count = 0
+ self.RMC_A_count = 0
self.location_type = 0 #定位类型,0:正常定位,有gps数据或60秒关闭;1:测试模式,只有发CLOSE_LOCATION消息才关闭
self.location_flag = 0
self.start_location_flag = 0
- self.location_thread_id = 0
- self.location_thread_id = _thread.start_new_thread(self.location_thread, ())
+ self.location_thread_id = 0
self.location_queue = Queue(100)
+ self.lpm_fd = None
#quecgnss.init()
#quecgnss.gnssEnable(0)
@staticmethod
return self.location_flag
def start_location(self, event, msg):
self.location_queue.put(msg)#测试类型入队
+ self.gps_data = {}
+ self.wifi_data = []
+ self.cell_data = []
+ self.location_timer_count = 0
+ self.GSV_sig_ok_count = 0
print("start_location:{}".format(msg))
- self.timer_read_GPS_info.start(1000, 1, self.location_timer)
if( self.location_flag == 1):
return
- self.location_timer_count = 0
+ self.location_thread_id = _thread.start_new_thread(self.location_thread, ())
self.location_flag = 1
#self.gps_open(event, msg)
- self.start_location_flag = 1
def start_location_test(self, event, msg):
print("start_location_test")
- self.timer_read_GPS_info.start(1000, 1, self.location_timer)
+
+ self.location_type = 1
if( self.location_flag == 1):
return
+ self.location_thread_id = _thread.start_new_thread(self.location_thread, ())
self.location_timer_count = 0
self.location_flag = 1
#self.gps_open(event, msg)
self.location_type = 1
- self.start_location_flag = 1
+
def close_location(self, event, msg):
self.location_flag = 0
self.location_type = 0
- self.gps.power_switch(0)
- wifiScan.control(0)
- self.timer_read_GPS_info.stop()
+
def WIFI_INFO(self, event, msg):
self.wifi_data = msg
def WIFI_scan_open(self, event, msg):
#print("WIFI_scan_open")
- self.wifi_data = []
- if wifiScan.support():
- if wifiScan.getState() == False :
- wifiScan.control(1)
- wifiScan.setConfig(4, 2, 5, 6, 0) #设置WiFi参数,10秒上层,两轮,最多6个,6秒底层,PS优先
+
+
+ if wifiScan.getState() == False :
+ self.wifi_data = []
+ wifiScan.control(1)
+ wifiScan.setCfgParam(6, 2, 5, 0) #设置WiFi参数,10秒上层,两轮,最多6个,6秒底层,PS优先
wifiScan.setCallback(wifiScan_cb) #注册回调函数
wifiScan.asyncStart()
#print("wifiScan.asyncStart")
def Location_send(self, msg) :
#print("Location_send1")
+ if self.location_queue.empty() == True:
+ return
date = "{:04d}{:02d}{:02d},{:02d}{:02d}{:02d}".format(*utime.localtime()[0:6])
date = date[2:]
+ #date = "180916,025723"
GPS_type = "V"
latitude = "0.000000,N"
longitude = "0.000000,E"
direction = "0.0"
high = "100.0"
number = "0"
- if msg[0] != [] :#有GPS数据
+ precision = "24"
+ if msg[0] :#有GPS数据
GPS_type = "A"
# 纬度
latitude = msg[0]["latitude"]
high = "%s"%(high_tmp[0])
# 讯号质量:搜索到的卫星数目
number = msg[0]["number"]
+ precision = msg[0]["precision"]
+ #date = msg[0]["date"]
+
#gsm 信号强度
gsm_sig = str((100//31)*EventMesh.publish("screen_get_sig"))
# 电池电量百分比
step = 0
step = EventMesh.publish("get_old_step_value")
if step != 0:
-
-
tmp_data = EventMesh.publish("get_storage_config","old_step_new")
if tmp_data != None:
old_step = tmp_data
-
if old_step != step:
EventMesh.publish("set_storage_config",["old_step",step])
EventMesh.publish("set_storage_config",["old_step_new",step])
send_data += ",%s,%s,%s"%(battery_caption,step,turn_count)
send_data += ",%s,%d,%s"%(state,CELL_num,CELL_data)
send_data += ",%d,%s"%(WIFI_num,WIFI_data)
+ send_data+=",%s"%(precision)
#print("Location_send2:{} q_size:{}".format(send_data,self.location_queue.size()))
while True:
if self.location_queue.empty() == False:
def location_thread(self):
- gnss_r_data = ""
- while True:
- try :
- if self.location_flag == 1 :
- if self.start_location_flag == 1 :
- self.start_location_flag = 0
- self.wifi_data = []
- self.gps_data = []
- self.cell_data = []
- gnss_r_data = ""
- print("location_thread:1")
- EventMesh.publish(constants.EVENT.GPSOPEN)
- print("location_thread:2")
- EventMesh.publish(constants.EVENT.WIFISCANOPEN)
- print("location_thread:3")
- utime.sleep_ms(1000)
- quecgnss.read(4096)#去掉第一次错误数据
- match_gga_data = []
- match_gsv_data = []
-
+ self.wifi_data = []
+ self.gps_data = {}
+ self.cell_data = []
+ gnss_r_data = []
+ self.GSV_sig_ok_count = 0
+ self.GGA_count = 0
+ self.RMC_A_count = 0
+ if self.lpm_fd == None:
+ self.lpm_fd = pm.create_wakelock("test_lock", len("test_lock"))
+ pm.wakelock_lock(self.lpm_fd)
+ print("location_thread:1")
+ EventMesh.publish(constants.EVENT.GPSOPEN)
+ print("location_thread:2")
+ self.timer_read_GPS_info.start(1000, 1, self.location_timer)
+
+ #utime.sleep_ms(1000)
+ self.location_timer_count = 0
+ #quecgnss.read(4096)#去掉第一次错误数据
+ #EventMesh.txt_log("START_LOCATION")
+ # pm.autosleep(0)#唤醒
+ # pm.sclkEx_set(0, 1, 5)
+ while self.location_flag == 1:
+ try :
+
+ match_gga_data = []
+ #match_gsv_data = []
+ gnss_r_data = []
+ # if self.location_check_data_timer_count >= 3:
+ # pm.autosleep(0)#唤醒
+ # pm.sclkEx_set(0, 1, 5)
+ if self.location_check_data_timer_count >= 5 :#5秒查一次基站数据和wifi数据
+ self.location_check_data_timer_count = 0
+ #print(self.location_timer_count)
+ #GSV_dict= {}
+
#检查GNSS数据
- if (self.gps_data == [] or self.location_type == 1) and quecgnss.get_state() == 2:
+ if ( self.gps_data == {} or self.location_type == 1) and quecgnss.get_state() == 2:
+
self.gps.__gps_nmea_data_clean()
+ #gnss_r_data = [0,1]
gnss_r_data = quecgnss.read(4096)
- #print("location gnss_r_data:{}".format(gnss_r_data[1].decode()))
+ #EventMesh.txt_log("gps Datalen:{}".format(gnss_r_data[0]))
+ print("location gnss_r_data:len{}\n{}".format(gnss_r_data[0],gnss_r_data[1].decode()))
+ #print("gnss_r_data len:{}".format(gnss_r_data[0]))
if gnss_r_data[0] > 0 : #有数据
- match_gga_data = self.match.GxGGA(gnss_r_data[1]).split(",")
- match_gsv_data = self.match.GxGSV(gnss_r_data[1]).split(",")
- if self.gps.__check_gps_valid(gnss_r_data[1].decode()): #数据齐全
- match_gmc_data = self.match.GxRMC(gnss_r_data[1]).split(",")
- match_vtg_data = self.match.GxVTG(gnss_r_data[1]).split(",")
- match_gga_data = self.match.GxGGA(gnss_r_data[1]).split(",")
- gps_data_tmp = self.gps.read_coordinates(gnss_r_data[1])
- latitude_tmp = str(gps_data_tmp[1]).split(".")
- if len(latitude_tmp) >= 2 and len(latitude_tmp[1])>=5:
- latitude = "%s.%s"%(latitude_tmp[0],latitude_tmp[1][:6])
- latitude = "%s,%s"%(latitude,match_gmc_data[4] )
- longitude_tmp = str(gps_data_tmp[0]).split(".")
- if len(longitude_tmp) >= 2 and len(longitude_tmp[1])>=5:
- longitude = "%s.%s"%(longitude_tmp[0],longitude_tmp[1][:6])
- longitude = "%s,%s"%(longitude,match_gmc_data[6] ) #match_gmc_data[6] + str(gnss_r_data[1])
- high = str(gps_data_tmp[2])
- self.gps_data = {
- "latitude": latitude,
- "longitude": longitude,
- "high": high,
- "direction": match_vtg_data[1],
- "speed": match_vtg_data[5],
- "number": match_gga_data[7],
- }
- if self.location_check_data_timer_count > 5 :#5秒查一次基站数据和wifi数据
- self.location_check_data_timer_count = 0
- #print(self.location_timer_count)
- #检查wifi数据
- if self.wifi_data == [] or (self.wifi_data != [] and self.wifi_data[0] == 0):#and self.wifi_data[0] <= 0:
- EventMesh.publish(constants.EVENT.WIFISCANOPEN)
+ tmp_ok_count = 0
+ gsv_data = gnss_r_data[1].split("GSV")
+
+ if len(gsv_data) >= 2 :
+ gsv_data = gsv_data[1:]
+ for i in range(len(gsv_data)) :
+ gsv_data[i] = gsv_data[i].split("\r\n")[0]
+ gsv_tmp = gsv_data[i].split(",")
+ if len(gsv_tmp) < 7 :
+ break
+
+ gsv_tmp = gsv_tmp[4:]
+ #print("gsv_tmp:{}".format(gsv_tmp))
+ for i in range(len(gsv_tmp)) :
+ if i % 4 == 0:
+ #GSV_dict[gsv_tmp[i]] = gsv_tmp[i+3]
+ try:
+ #print("gsv_tmp[i+3]:{}".format(gsv_tmp[i+3]))
+ if gsv_tmp[i+3] != "" :
+ if int(gsv_tmp[i+3])>=20:
+ tmp_ok_count +=1
+ except Exception as e:
+ usys.print_exception(e)
+ #print("GSV_dict:{}:[{}]".format(gsv_tmp[i],gsv_tmp[i+3]))
+ if len(gsv_tmp)-i < 4:
+ break
+
+ if(tmp_ok_count > self.GSV_sig_ok_count ):
+ self.GSV_sig_ok_count = tmp_ok_count
+
+
+ # if len(match_gga_data) > 8:
+ # #print(match_gga_data)
+ # if(match_gga_data[7] ):
+ # gga_count_tmp = int(match_gga_data[7]) #可用卫星
+ # if(gga_count_tmp > self.GGA_count):
+ # self.GGA_count = gga_count_tmp
+ print("GGA_count:{} GSV_sig_ok_coun:{} ".format(self.GGA_count,self.GSV_sig_ok_count))
+
+ match_rmc_data = self.match.GxRMC(gnss_r_data[1]).split(",")
+
+ #print("match_rmc_data:\n{}".format(match_rmc_data))
+ if len (match_rmc_data)>3: #有rmc数据
+ if match_rmc_data[2] == 'A':
+
+ if self.gps.__check_gps_valid(gnss_r_data[1].decode()): #数据齐全 :
+
+ #print("hhhhhh:{}\n\n".format(self.gps.__gsv_data))
+ match_vtg_data = self.gps.__vtg_data.split(",")#self.match.GxVTG(gnss_r_data[1]).split(",")
+ match_gga_data = self.gps.__gga_data.split(",")#self.match.GxGGA(gnss_r_data[1]).split(",")
+ #match_gga_data = self.match.GxGGA(gnss_r_data[1]).split(",")
+
+ EventMesh.txt_log("GGA:{}".format(match_gga_data))
+ # EventMesh.txt_log("VTG:{}".format(match_vtg_data))
+ # EventMesh.txt_log("RMC:{}".format(match_rmc_data))
+ # EventMesh.txt_log("timer:{} ok_count:{} count:{}\n".format(self.location_timer_count,self.GSV_sig_ok_count,self.GGA_count))
+ print("LOCATION_ok :time:{}!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n\n".format(self.location_timer_count))
+ gps_date = "%s,%s"%(match_rmc_data[9],match_rmc_data[1][:6])#日期
+ gps_data_tmp = self.gps.read_coordinates(gnss_r_data[1])
+ latitude = "0,N" #纬度
+ latitude_tmp = str(gps_data_tmp[1]).split(".")
+ if len(latitude_tmp) >= 2 and len(latitude_tmp[1])>=5:
+ latitude = "%s.%s"%(latitude_tmp[0],latitude_tmp[1][:6])
+ latitude = "%s,%s"%(latitude,match_rmc_data[4] )
+
+ longitude = "0,E"#经度
+ longitude_tmp = str(gps_data_tmp[0]).split(".")
+ if len(longitude_tmp) >= 2 and len(longitude_tmp[1])>=5:
+ longitude = "%s.%s"%(longitude_tmp[0],longitude_tmp[1][:6])
+ longitude = "%s,%s"%(longitude,match_rmc_data[6] ) #match_rmc_data[6] + str(gnss_r_data[1])
+
+ high = "0"#高度
+ high = str(gps_data_tmp[2])
+ number = "0"#可用卫星
+ precision = "5.8"#精度
+ if len(match_gga_data)>5 :
+ if match_gga_data[7]:
+ number = match_gga_data[7]
+ if match_gga_data[8]:
+ precision = match_gga_data[8]
+ direction = "249.5" #方向
+ speed = '0.00' #速度
+ if(len(match_vtg_data)>5 ):
+ if match_vtg_data[1]:
+ direction = match_vtg_data[1]
+ if match_vtg_data[7]:
+ speed = match_vtg_data[7]
+ self.gps_data = {
+ "latitude": latitude,
+ "longitude": longitude,
+ "high": high,
+ "direction": direction,
+ "speed": speed,
+ "number": number,
+ "date": gps_date,
+ "precision": precision
+ }
+ else :
+ EventMesh.txt_log("gps数据不全")
+ else :
+ EventMesh.txt_log("gps 无数据")
+
+
+
+ #检查wifi数据
+ if self.wifi_data == [] or (self.wifi_data != [] and self.wifi_data[0] == 0):#and self.wifi_data[0] <= 0:
+ EventMesh.publish(constants.EVENT.WIFISCANOPEN)
+
+ #检查基站数据
+ if self.cell_data == []:
+ data_tmp = net.getCellInfo()
+ if(len(data_tmp)>=2):
+ for cell in data_tmp:
+ if cell and cell[0][0] == 0 and len(cell[0]) > 1:
+ self.cell_data = cell
+ break
+
+ if self.location_type == 1: #测试模式
+ self.location_timer_count = 0
+ if len(gnss_r_data) >= 1:
+ if gnss_r_data[0] > 0:
+ EventMesh.publish("PUSH_LOCATION_DATA", [gnss_r_data,self.wifi_data,self.cell_data])
+ if self.gps_data != {} : #发送数据
+ EventMesh.publish(constants.EVENT.CLOSE_LOCATION)#关闭定位
+ self.location_queue.put(constants.PROTOCOL.UD_SEND)
+ self.Location_send([self.gps_data,self.wifi_data,self.cell_data])
- #检查基站数据
- if self.cell_data == []:
- data_tmp = net.getCellInfo()
- for cell in data_tmp:
- if cell and cell[0][0] == 0 and len(cell[0]) > 1:
- self.cell_data = cell
- break
- if self.location_timer_count >= 60*4 : #4分钟关闭
+
+ elif self.location_type == 0: #正常模式
+ if self.gps_data != {} or self.location_timer_count > 100: #or( self.wifi_data != [] and self.cell_data != []):
+ if self.location_queue.empty() == False:#有定位队列
+ EventMesh.txt_log("LOCATION:timer:{} ok_count:{} ".format(self.location_timer_count,self.GSV_sig_ok_count))
+ upload_time = EventMesh.publish("get_upload_data")
+
+
+ self.Location_send([self.gps_data,self.wifi_data,self.cell_data])
+ #if upload_time > 200 :
EventMesh.publish(constants.EVENT.CLOSE_LOCATION)#关闭定位
+ # else: #定位间隔小于3分钟不关闭gps,不影响无信号判断
+ # self.location_timer_count = 0
+ # self.GSV_sig_ok_count = 0
+
+
+ # if (self.GGA_count < 3 and self.GSV_sig_ok_count < 2) and self.wifi_data and self.cell_data and self.location_timer_count > 30 and self.location_timer_count < 40: #超过20秒且有效卫星个数少于3
+ # EventMesh.publish(constants.EVENT.CLOSE_LOCATION)#关闭定位
+ # EventMesh.txt_log("GGA_count<3:timer:{} ok_count:{} count:{}".format(self.location_timer_count,self.GSV_sig_ok_count,self.GGA_count))
+ # self.Location_send([self.gps_data,self.wifi_data,self.cell_data])
+
+ if self.GSV_sig_ok_count <= 1 and self.wifi_data != [] and self.cell_data != [] and self.location_timer_count > 20: #超过40秒且信号大于30的不足3个
+ EventMesh.publish(constants.EVENT.CLOSE_LOCATION)#关闭定位
+ EventMesh.txt_log("GSV_sig_ok_count<3:timer:{} ok_count:{} count:{}".format(self.location_timer_count,self.GSV_sig_ok_count,self.GGA_count))
+ self.Location_send([self.gps_data,self.wifi_data,self.cell_data])
#break
- if self.location_type == 1: #测试模式
- EventMesh.publish("PUSH_LOCATION_DATA", [gnss_r_data,self.wifi_data,self.cell_data])
- elif self.location_type == 0: #正常模式
- if self.gps_data != [] or self.location_timer_count > 70: #or( self.wifi_data != [] and self.cell_data != []):
- self.Location_send([self.gps_data,self.wifi_data,self.cell_data])
- EventMesh.publish(constants.EVENT.CLOSE_LOCATION)#关闭定位
- #break
- if len(match_gsv_data) >= 3:
- #print("match_gsv_data[3]:{}".format(match_gsv_data[3]))
- #if (self.wifi_data != [] and self.cell_data != [] and self.location_timer_count > 10): #超过10秒且卫星个数少于3
- if (int(match_gsv_data[3]) < 3 and self.wifi_data != [] and self.cell_data != [] and self.location_timer_count > 20): #超过10秒且卫星个数少于3
- self.Location_send([self.gps_data,self.wifi_data,self.cell_data])
-
- EventMesh.publish(constants.EVENT.CLOSE_LOCATION)#关闭定位
- #break
- utime.sleep_ms(1000)
- #print("location_timer_count:{}".format(self.location_timer_count))
- else :
- utime.sleep_ms(2000)
+ # if self.location_type != 1 and self.location_timer_count >= 60*4 : #4分钟关闭
+ # EventMesh.publish(constants.EVENT.CLOSE_LOCATION)#关闭定位
+ #break
+ #EventMesh.txt_log("self.GSV_sig_ok_count :{}".format(self.GSV_sig_ok_count))
+ #EventMesh.publish("set_old_step_value",self.location_timer_count)
+
+ #print("location_timer_count:{}".format(self.location_timer_count))
+
except Exception as e:
usys.print_exception(e)
- utime.sleep_ms(1000)
- print("location_thread:ERROR:{}\r\n{}".format(e,Exception))
+ EventMesh.txt_log("location_thread:ERROR:".format(e))
+ #print("location_thread:ERROR:{}".format(usys.print_exception(e)))
print("self.gps_data{},self.wifi_data{},self.cell_data{}".format(self.gps_data,self.wifi_data,self.cell_data))
- self.Location_send([self.gps_data,self.wifi_data,self.cell_data])
+ #self.Location_send([self.gps_data,self.wifi_data,self.cell_data])
EventMesh.publish(constants.EVENT.CLOSE_LOCATION)#关闭定位
+ utime.sleep_ms(500)
+ self.timer_read_GPS_info.stop()
+ self.gps.power_switch(0)
+ wifiScan.control(0)
+ # if EventMesh.publish("get_lcd_state" ) == 0:#休眠的时候,恢复休眠
+ # pm.autosleep(1)
+ # pm.sclkEx_set(1, 1, 5)
+ if self.lpm_fd != None:
+ pm.wakelock_unlock(self.lpm_fd)
+ #pm.delete_wakelock(lpm_fd)
+ print("CLOSE_LOCATION")
+ #EventMesh.txt_log("CLOSE_LOCATION")