2022-03-24 16:01:12 +08:00
|
|
|
# Copyright (c) Quectel Wireless Solution, Co., Ltd.All Rights Reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2022-03-03 09:53:51 +08:00
|
|
|
|
|
|
|
import ure
|
2022-03-26 19:09:18 +08:00
|
|
|
import utime
|
2022-03-23 19:42:23 +08:00
|
|
|
import osTimer
|
2022-03-29 15:56:52 +08:00
|
|
|
import _thread
|
2022-03-04 13:21:48 +08:00
|
|
|
import cellLocator
|
|
|
|
|
2022-03-03 09:53:51 +08:00
|
|
|
from queue import Queue
|
2022-03-04 13:21:48 +08:00
|
|
|
from machine import UART
|
2022-03-28 09:32:12 +08:00
|
|
|
from wifilocator import wifilocator
|
|
|
|
|
2022-03-09 13:17:33 +08:00
|
|
|
from usr.logging import getLogger
|
|
|
|
from usr.common import Singleton
|
2022-04-12 09:13:20 +08:00
|
|
|
from usr.common import option_lock
|
2022-03-03 09:53:51 +08:00
|
|
|
|
2022-03-11 14:05:06 +08:00
|
|
|
try:
|
2022-03-14 11:37:29 +08:00
|
|
|
import quecgnss
|
2022-03-11 14:05:06 +08:00
|
|
|
except ImportError:
|
2022-03-14 11:37:29 +08:00
|
|
|
quecgnss = None
|
2022-03-11 14:05:06 +08:00
|
|
|
|
2022-03-04 17:25:28 +08:00
|
|
|
log = getLogger(__name__)
|
|
|
|
|
2022-03-29 15:56:52 +08:00
|
|
|
_gps_read_lock = _thread.allocate_lock()
|
|
|
|
|
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
class _loc_method(object):
|
|
|
|
none = 0x0
|
|
|
|
gps = 0x1
|
|
|
|
cell = 0x2
|
|
|
|
wifi = 0x4
|
|
|
|
all = 0x7
|
2022-03-29 15:56:52 +08:00
|
|
|
|
2022-03-04 13:21:48 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
class _gps_mode(object):
|
|
|
|
none = 0x0
|
|
|
|
internal = 0x1
|
|
|
|
external = 0x2
|
2022-03-11 14:05:06 +08:00
|
|
|
|
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
class GPSParsing(object):
|
2022-03-11 14:05:06 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
def read_GxRMC(self, gps_data):
|
|
|
|
rmc_re = ure.search(
|
|
|
|
r"\$G[NP]RMC,\d+\.\d+,[AV],\d+\.\d+,[NS],\d+\.\d+,[EW],\d+\.\d+,\d+\.\d+,\d+,\d*\.*\d*,[EW]*,[ADEN]*,[SCUV]*\**(\d|\w)*",
|
|
|
|
gps_data)
|
|
|
|
if rmc_re:
|
|
|
|
return rmc_re.group(0)
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def read_GxGGA(self, gps_data):
|
|
|
|
gga_re = ure.search(
|
|
|
|
r"\$G[BLPN]GGA,\d+\.\d+,\d+\.\d+,[NS],\d+\.\d+,[EW],[0126],\d+,\d+\.\d+,-*\d+\.\d+,M,-*\d+\.\d+,M,\d*,\**(\d|\w)*",
|
|
|
|
gps_data)
|
|
|
|
if gga_re:
|
|
|
|
return gga_re.group(0)
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def read_GxGGA_satellite_num(self, gps_data):
|
|
|
|
gga_data = self.read_GxGGA(gps_data)
|
|
|
|
if gga_data:
|
|
|
|
satellite_num_re = ure.search(r",[EW],[0126],\d+,", gga_data)
|
|
|
|
if satellite_num_re:
|
|
|
|
return satellite_num_re.group(0).split(",")[-2]
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def read_GxVTG(self, gps_data):
|
|
|
|
vtg_re = ure.search(r"\$G[NP]VTG,\d+\.\d+,T,\d*\.*\d*,M,\d+\.\d+,N,\d+\.\d+,K,[ADEN]*\*(\d|\w)*", gps_data)
|
|
|
|
if vtg_re:
|
|
|
|
return vtg_re.group(0)
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def read_GxVTG_speed(self, gps_data):
|
|
|
|
vtg_data = self.read_GxVTG(gps_data)
|
|
|
|
if vtg_data:
|
|
|
|
speed_re = ure.search(r",N,\d+\.\d+,K,", vtg_data)
|
|
|
|
if speed_re:
|
|
|
|
return speed_re.group(0)[3:-3]
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def read_GxGSV(self, gps_data):
|
|
|
|
gsv_re = ure.search(r"\$G[NP]GSV,\d+,\d+,\d+,\d*,\d*,\d*,\d*,\d*,\d*,\d*,\d*,\d*,\d*,\d*,\d*,\d*,\d*,\d*,\d*,\d*\**(\d|\w)*", gps_data)
|
|
|
|
if gsv_re:
|
|
|
|
return gsv_re.group(0)
|
|
|
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def read_GxGSV_satellite_num(self, gps_data):
|
|
|
|
gsv_data = self.read_GxGSV(gps_data)
|
|
|
|
if gsv_data:
|
|
|
|
satellite_num_re = ure.search(r"\$G[NP]GSV,\d+,\d+,\d+,", gsv_data)
|
|
|
|
if satellite_num_re:
|
|
|
|
return satellite_num_re.group(0).split(",")[-2]
|
|
|
|
return ""
|
2022-03-03 09:53:51 +08:00
|
|
|
|
2022-03-04 13:21:48 +08:00
|
|
|
|
2022-03-09 13:17:33 +08:00
|
|
|
class GPS(Singleton):
|
2022-04-12 09:13:20 +08:00
|
|
|
def __init__(self, gps_cfg, gps_mode):
|
2022-03-11 14:05:06 +08:00
|
|
|
self.gps_cfg = gps_cfg
|
2022-04-12 09:13:20 +08:00
|
|
|
self.gps_mode = gps_mode
|
|
|
|
self.uart_obj = None
|
|
|
|
self.gnss_obj = quecgnss
|
|
|
|
self.gps_parsing = GPSParsing()
|
|
|
|
|
|
|
|
self.__uart_retrieve_queue = None
|
2022-03-29 15:56:52 +08:00
|
|
|
self.__first_break = 0
|
|
|
|
self.__second_break = 0
|
2022-04-12 09:13:20 +08:00
|
|
|
self.__gps_timer = osTimer()
|
|
|
|
|
|
|
|
if self.gps_mode & _gps_mode.external:
|
|
|
|
self._uart_init()
|
|
|
|
elif self.gps_mode & _gps_mode.internal:
|
|
|
|
self._gnss_init()
|
2022-03-11 14:05:06 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
def _uart_init(self):
|
|
|
|
self.__uart_retrieve_queue = Queue(maxsize=8)
|
|
|
|
self._uart_open()
|
2022-04-05 12:09:23 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
def _uart_open(self):
|
2022-03-04 17:25:28 +08:00
|
|
|
self.uart_obj = UART(
|
2022-04-12 09:13:20 +08:00
|
|
|
self.gps_cfg["UARTn"], self.gps_cfg["buadrate"], self.gps_cfg["databits"],
|
|
|
|
self.gps_cfg["parity"], self.gps_cfg["stopbits"], self.gps_cfg["flowctl"]
|
2022-03-04 17:25:28 +08:00
|
|
|
)
|
2022-04-12 09:13:20 +08:00
|
|
|
self.uart_obj.set_callback(self._uart_retrieve_cb)
|
2022-04-05 12:09:23 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
def _uart_close(self):
|
2022-04-05 12:09:23 +08:00
|
|
|
self.uart_obj.close()
|
2022-03-23 19:42:23 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
def _uart_retrieve_cb(self, args):
|
|
|
|
"""
|
|
|
|
GPS data retrieve callback from UART
|
|
|
|
When data comes, send a message to queue of data length
|
|
|
|
"""
|
|
|
|
toRead = args[2]
|
|
|
|
log.debug("GPS _uart_retrieve_cb args: %s" % str(args))
|
|
|
|
if toRead:
|
|
|
|
if self.__uart_retrieve_queue.size() >= 8:
|
|
|
|
self.__uart_retrieve_queue.get()
|
|
|
|
self.__uart_retrieve_queue.put(toRead)
|
|
|
|
|
|
|
|
def _gnss_init(self):
|
|
|
|
if self.gnss_obj:
|
|
|
|
if self.gnss_obj.init() != 0:
|
|
|
|
self._gnss_open()
|
|
|
|
log.error("GNSS INIT Failed.")
|
|
|
|
else:
|
|
|
|
log.debug("GNSS INIT Success.")
|
|
|
|
else:
|
|
|
|
log.error("Module quecgnss Import Error.")
|
|
|
|
|
|
|
|
def _gnss_open(self):
|
|
|
|
if self.gnss_obj.get_state() == 0:
|
|
|
|
self.gnss_obj.gnssEnable(1)
|
|
|
|
|
|
|
|
def _gnss_close(self):
|
|
|
|
self.gnss_obj.gnssEnable(0)
|
|
|
|
|
|
|
|
def _first_gps_timer_callback(self, args):
|
2022-03-29 15:56:52 +08:00
|
|
|
self.__first_break = 1
|
2022-04-12 09:13:20 +08:00
|
|
|
if self.__uart_retrieve_queue is not None:
|
|
|
|
self.__uart_retrieve_queue.put(0)
|
2022-03-29 15:09:33 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
def _second_gps_timer_callback(self, args):
|
2022-03-29 15:56:52 +08:00
|
|
|
self.__second_break = 1
|
2022-04-12 09:13:20 +08:00
|
|
|
if self.__uart_retrieve_queue is not None:
|
|
|
|
self.__uart_retrieve_queue.put(0)
|
2022-03-23 19:42:23 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
def _uart_read(self):
|
|
|
|
self._uart_open()
|
|
|
|
log.debug("_uart_read start")
|
2022-03-25 11:56:14 +08:00
|
|
|
|
2022-03-29 15:56:52 +08:00
|
|
|
while self.__first_break == 0:
|
2022-04-12 09:13:20 +08:00
|
|
|
self.__gps_timer.start(50, 0, self._first_gps_timer_callback)
|
|
|
|
nread = self.__uart_retrieve_queue.get()
|
|
|
|
log.debug("__first_break nread: %s" % nread)
|
2022-03-24 13:30:00 +08:00
|
|
|
data = self.uart_obj.read(nread).decode()
|
2022-03-29 15:56:52 +08:00
|
|
|
self.__gps_timer.stop()
|
|
|
|
self.__first_break = 0
|
2022-03-23 19:42:23 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
data = ""
|
|
|
|
rmc_data = ""
|
|
|
|
gga_data = ""
|
|
|
|
vtg_data = ""
|
|
|
|
gsv_data = ""
|
2022-03-29 15:56:52 +08:00
|
|
|
while self.__second_break == 0:
|
2022-04-12 09:13:20 +08:00
|
|
|
get_flag = False
|
|
|
|
self.__gps_timer.start(1500, 0, self._second_gps_timer_callback)
|
|
|
|
nread = self.__uart_retrieve_queue.get()
|
|
|
|
log.debug("__second_break nread: %s" % nread)
|
2022-03-26 19:09:18 +08:00
|
|
|
if nread:
|
|
|
|
if not rmc_data:
|
2022-04-12 09:13:20 +08:00
|
|
|
rmc_data = self.gps_parsing.read_GxRMC(data)
|
|
|
|
get_flag = True
|
2022-03-26 19:09:18 +08:00
|
|
|
if not gga_data:
|
2022-04-12 09:13:20 +08:00
|
|
|
gga_data = self.gps_parsing.read_GxGGA(data)
|
|
|
|
get_flag = True
|
2022-03-26 19:09:18 +08:00
|
|
|
if not vtg_data:
|
2022-04-12 09:13:20 +08:00
|
|
|
vtg_data = self.gps_parsing.read_GxVTG(data)
|
|
|
|
get_flag = True
|
2022-04-06 19:52:49 +08:00
|
|
|
if not gsv_data:
|
2022-04-12 09:13:20 +08:00
|
|
|
gsv_data = self.gps_parsing.read_GxGSV(data)
|
|
|
|
get_flag = True
|
|
|
|
if get_flag:
|
|
|
|
data += self.uart_obj.read(nread).decode()
|
2022-04-06 19:52:49 +08:00
|
|
|
if rmc_data and gga_data and vtg_data and gsv_data:
|
2022-03-29 15:56:52 +08:00
|
|
|
self.__second_break = 1
|
|
|
|
self.__gps_timer.stop()
|
2022-04-12 09:13:20 +08:00
|
|
|
log.debug("__second_break(_uart_read) data: %s" % data)
|
2022-03-29 15:56:52 +08:00
|
|
|
self.__second_break = 0
|
2022-03-26 19:09:18 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
self._uart_close()
|
2022-03-24 13:30:00 +08:00
|
|
|
return data
|
2022-03-03 09:53:51 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
def _quecgnss_read(self):
|
|
|
|
self._gnss_init()
|
2022-03-11 14:05:06 +08:00
|
|
|
|
2022-03-29 15:56:52 +08:00
|
|
|
while self.__first_break == 0:
|
2022-04-12 09:13:20 +08:00
|
|
|
self.__gps_timer.start(50, 0, self._first_gps_timer_callback)
|
2022-03-29 15:56:52 +08:00
|
|
|
data = quecgnss.read(1024)
|
|
|
|
self.__gps_timer.stop()
|
|
|
|
self.__first_break = 0
|
2022-03-11 14:05:06 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
data = ""
|
|
|
|
rmc_data = ""
|
|
|
|
gga_data = ""
|
|
|
|
vtg_data = ""
|
|
|
|
gsv_data = ""
|
2022-03-26 19:09:18 +08:00
|
|
|
count = 0
|
2022-03-29 15:56:52 +08:00
|
|
|
while self.__second_break == 0:
|
2022-04-12 09:13:20 +08:00
|
|
|
get_flag = False
|
|
|
|
self.__gps_timer.start(1500, 0, self._second_gps_timer_callback)
|
2022-03-29 15:56:52 +08:00
|
|
|
gnss_data = quecgnss.read(1024)
|
2022-03-26 19:09:18 +08:00
|
|
|
if gnss_data and gnss_data[1]:
|
|
|
|
if not rmc_data:
|
2022-04-12 09:13:20 +08:00
|
|
|
rmc_data = self.gps_parsing.read_GxRMC(data)
|
|
|
|
get_flag = True
|
2022-03-26 19:09:18 +08:00
|
|
|
if not gga_data:
|
2022-04-12 09:13:20 +08:00
|
|
|
gga_data = self.gps_parsing.read_GxGGA(data)
|
|
|
|
get_flag = True
|
2022-03-26 19:09:18 +08:00
|
|
|
if not vtg_data:
|
2022-04-12 09:13:20 +08:00
|
|
|
vtg_data = self.gps_parsing.read_GxVTG(data)
|
|
|
|
get_flag = True
|
2022-04-06 19:52:49 +08:00
|
|
|
if not gsv_data:
|
2022-04-12 09:13:20 +08:00
|
|
|
gsv_data = self.gps_parsing.read_GxGSV(data)
|
|
|
|
get_flag = True
|
|
|
|
if get_flag:
|
|
|
|
data += gnss_data[1].decode() if len(gnss_data) > 1 and gnss_data[1] else ""
|
2022-04-06 19:52:49 +08:00
|
|
|
if rmc_data and gga_data and vtg_data and gsv_data:
|
2022-03-29 15:56:52 +08:00
|
|
|
self.__second_break = 1
|
|
|
|
self.__gps_timer.stop()
|
2022-03-24 20:27:49 +08:00
|
|
|
|
2022-03-26 19:09:18 +08:00
|
|
|
if count > 5:
|
2022-03-29 15:56:52 +08:00
|
|
|
self.__second_break = 1
|
2022-03-26 19:09:18 +08:00
|
|
|
count += 1
|
|
|
|
utime.sleep_ms(300)
|
2022-03-29 15:56:52 +08:00
|
|
|
self.__second_break = 0
|
2022-03-26 19:09:18 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
self._gnss_close()
|
2022-03-24 20:27:49 +08:00
|
|
|
return data
|
2022-03-11 14:05:06 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
@option_lock(_gps_read_lock)
|
2022-03-03 09:53:51 +08:00
|
|
|
def read(self):
|
2022-04-12 09:13:20 +08:00
|
|
|
res = -1
|
|
|
|
gps_data = ""
|
|
|
|
if self.gps_mode & _gps_mode.external:
|
|
|
|
gps_data = self._uart_read()
|
|
|
|
elif self.gps_mode & _gps_mode.internal:
|
|
|
|
gps_data = self._quecgnss_read()
|
2022-04-06 19:52:49 +08:00
|
|
|
|
|
|
|
# TODO: Disable Output Satellite Num:
|
|
|
|
if gps_data:
|
2022-04-12 09:13:20 +08:00
|
|
|
gga_satellite = self.gps_parsing.read_GxGGA_satellite_num(gps_data)
|
|
|
|
log.debug("GxGGA Satellite Num %s" % gga_satellite)
|
|
|
|
gsv_satellite = self.gps_parsing.read_GxGSV_satellite_num(gps_data)
|
|
|
|
log.debug("GxGSV Satellite Num %s" % gsv_satellite)
|
|
|
|
res = 0
|
2022-03-03 09:53:51 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
return (res, gps_data)
|
2022-03-03 09:53:51 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
def start(self):
|
|
|
|
# TODO: Set GPS ON
|
|
|
|
return True
|
2022-03-04 18:06:05 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
def stop(self):
|
|
|
|
# TODO: Set GPS OFF
|
|
|
|
return True
|
2022-03-21 11:21:01 +08:00
|
|
|
|
2022-03-04 13:21:48 +08:00
|
|
|
|
|
|
|
class CellLocator(object):
|
2022-03-03 09:53:51 +08:00
|
|
|
def __init__(self, cellLocator_cfg):
|
|
|
|
self.cellLocator_cfg = cellLocator_cfg
|
|
|
|
|
|
|
|
def read(self):
|
2022-04-12 09:13:20 +08:00
|
|
|
res = -1
|
|
|
|
loc_data = cellLocator.getLocation(
|
|
|
|
self.cellLocator_cfg["serverAddr"],
|
|
|
|
self.cellLocator_cfg["port"],
|
|
|
|
self.cellLocator_cfg["token"],
|
|
|
|
self.cellLocator_cfg["timeout"],
|
|
|
|
self.cellLocator_cfg["profileIdx"]
|
2022-03-04 13:21:48 +08:00
|
|
|
)
|
2022-04-12 09:13:20 +08:00
|
|
|
if isinstance(loc_data, tuple) and len(loc_data) == 3:
|
|
|
|
res = 0
|
|
|
|
else:
|
|
|
|
res = loc_data
|
|
|
|
loc_data = ()
|
2022-03-04 13:21:48 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
return (res, loc_data)
|
2022-03-21 11:21:01 +08:00
|
|
|
|
2022-03-03 09:53:51 +08:00
|
|
|
|
2022-03-04 19:29:23 +08:00
|
|
|
class WiFiLocator(object):
|
2022-03-03 09:53:51 +08:00
|
|
|
def __init__(self, wifiLocator_cfg):
|
2022-04-12 09:13:20 +08:00
|
|
|
self.wifilocator_obj = wifilocator(wifiLocator_cfg["token"])
|
2022-03-03 09:53:51 +08:00
|
|
|
|
|
|
|
def read(self):
|
2022-04-12 09:13:20 +08:00
|
|
|
res = -1
|
|
|
|
loc_data = self.wifilocator_obj.getwifilocator()
|
|
|
|
if isinstance(loc_data, tuple) and len(loc_data) == 3:
|
|
|
|
res = 0
|
|
|
|
else:
|
|
|
|
res = loc_data
|
|
|
|
loc_data = ()
|
2022-03-03 09:53:51 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
return (res, loc_data)
|
2022-03-21 11:21:01 +08:00
|
|
|
|
2022-03-16 19:43:12 +08:00
|
|
|
|
2022-03-09 13:17:33 +08:00
|
|
|
class Location(Singleton):
|
2022-03-08 17:12:38 +08:00
|
|
|
gps = None
|
|
|
|
cellLoc = None
|
|
|
|
wifiLoc = None
|
2022-03-03 09:53:51 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
def __init__(self, gps_mode, locator_init_params):
|
|
|
|
self.gps_mode = gps_mode
|
|
|
|
self.locator_init_params = locator_init_params
|
2022-03-08 17:12:38 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
def __locater_init(self, loc_method):
|
2022-03-03 09:53:51 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
if loc_method & _loc_method.gps:
|
2022-03-08 17:12:38 +08:00
|
|
|
if self.gps is None:
|
2022-04-12 09:13:20 +08:00
|
|
|
if self.locator_init_params.get("gps_cfg"):
|
|
|
|
self.gps = GPS(self.locator_init_params["gps_cfg"], self.gps_mode)
|
2022-03-08 17:12:38 +08:00
|
|
|
else:
|
2022-04-12 09:13:20 +08:00
|
|
|
raise ValueError("Invalid gps init parameters.")
|
2022-03-08 17:12:38 +08:00
|
|
|
else:
|
|
|
|
self.gps = None
|
2022-03-04 13:21:48 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
if loc_method & _loc_method.cell:
|
2022-03-08 17:12:38 +08:00
|
|
|
if self.cellLoc is None:
|
2022-04-12 09:13:20 +08:00
|
|
|
if self.locator_init_params.get("cellLocator_cfg"):
|
|
|
|
self.cellLoc = CellLocator(self.locator_init_params["cellLocator_cfg"])
|
2022-03-08 17:12:38 +08:00
|
|
|
else:
|
2022-04-12 09:13:20 +08:00
|
|
|
raise ValueError("Invalid cell-locator init parameters.")
|
2022-03-08 17:12:38 +08:00
|
|
|
else:
|
|
|
|
self.cellLoc = None
|
2022-03-03 09:53:51 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
if loc_method & _loc_method.wifi:
|
2022-03-08 17:12:38 +08:00
|
|
|
if self.wifiLoc is None:
|
2022-04-12 09:13:20 +08:00
|
|
|
if self.locator_init_params.get("wifiLocator_cfg"):
|
|
|
|
self.wifiLoc = WiFiLocator(self.locator_init_params["wifiLocator_cfg"])
|
2022-03-08 17:12:38 +08:00
|
|
|
else:
|
2022-04-12 09:13:20 +08:00
|
|
|
raise ValueError("Invalid wifi-locator init parameters.")
|
2022-03-08 17:12:38 +08:00
|
|
|
else:
|
|
|
|
self.wifiLoc = None
|
2022-03-03 09:53:51 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
def __read_gps(self):
|
2022-03-08 17:12:38 +08:00
|
|
|
if self.gps:
|
2022-04-12 09:13:20 +08:00
|
|
|
return self.gps.read()[1]
|
|
|
|
return ""
|
2022-03-03 09:53:51 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
def __read_cell(self):
|
2022-03-08 17:12:38 +08:00
|
|
|
if self.cellLoc:
|
2022-04-12 09:13:20 +08:00
|
|
|
return self.cellLoc.read()[1]
|
|
|
|
return ()
|
2022-03-03 09:53:51 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
def __read_wifi(self):
|
2022-03-08 17:12:38 +08:00
|
|
|
if self.wifiLoc:
|
2022-04-12 09:13:20 +08:00
|
|
|
return self.wifiLoc.read()[1]
|
2022-03-03 09:53:51 +08:00
|
|
|
return ()
|
2022-04-12 09:13:20 +08:00
|
|
|
|
|
|
|
def read(self, loc_method, read_all=False):
|
|
|
|
"""
|
|
|
|
1. If read_all Is False
|
|
|
|
1.1. Get GPS If loc_method Include GPS And GPS Data Exist;
|
|
|
|
1.2. Get Cell If loc_method Inculde Cell And Not GPS Data;
|
|
|
|
1.3. Get Wifi If loc_method Include Wifi And Not Cell Data;
|
|
|
|
2. If read_all Is True, Return loc_method Include All Loc Method Data.
|
|
|
|
|
|
|
|
Return Data Format:
|
|
|
|
|
|
|
|
{
|
|
|
|
1: "$GPGGA,XXX",
|
|
|
|
2: (0.00, 0.00, 0.00),
|
|
|
|
4: (0.00, 0.00, 0.00),
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
loc_data = {}
|
|
|
|
self.__locater_init(loc_method)
|
|
|
|
|
|
|
|
if loc_method & _loc_method.gps:
|
|
|
|
loc_data[_loc_method.gps] = self.__read_gps()
|
|
|
|
if read_all is False:
|
|
|
|
return loc_data
|
|
|
|
|
|
|
|
if loc_method & _loc_method.cell:
|
|
|
|
loc_data[_loc_method.cell] = self.__read_cell()
|
|
|
|
if read_all is False:
|
|
|
|
return loc_data
|
|
|
|
|
|
|
|
if loc_method & _loc_method.wifi:
|
|
|
|
loc_data[_loc_method.wifi] = self.__read_wifi()
|
|
|
|
if read_all is False:
|
|
|
|
return loc_data
|
|
|
|
|
|
|
|
return loc_data
|