diff --git a/code/location.py b/code/location.py index 5b1e962..f67d619 100644 --- a/code/location.py +++ b/code/location.py @@ -13,6 +13,7 @@ # limitations under the License. import ure +import utime import osTimer import cellLocator import usr.settings as settings @@ -80,7 +81,12 @@ class GPS(Singleton): gps_data_retrieve_queue = Queue(maxsize=8) def gps_timer_callback(self, args): - self.break_flag = 1 + if self.break_flag == 0: + self.break_flag = 1 + elif self.break_flag == 1: + self.break_flag = 2 + if gps_data_retrieve_queue is not None: + gps_data_retrieve_queue.put(0) def uart_read(self): global gps_data_retrieve_queue @@ -95,21 +101,24 @@ class GPS(Singleton): rmc_data = '' gga_data = '' vtg_data = '' - while True: + while self.break_flag == 1: + self.gps_timer.start(3000, 1, self.gps_timer_callback) nread = gps_data_retrieve_queue.get() - udata = self.uart_obj.read(nread).decode() - if not rmc_data: - rmc_data = self.read_location_GxRMC(udata) - if not gga_data: - gga_data = self.read_location_GxGGA(udata) - if not vtg_data: - vtg_data = self.read_location_GxVTG(udata) - if rmc_data or gga_data or vtg_data: - data += udata - if rmc_data and gga_data and vtg_data: - break - + if nread: + udata = self.uart_obj.read(nread).decode() + if not rmc_data: + rmc_data = self.read_location_GxRMC(udata) + if not gga_data: + gga_data = self.read_location_GxGGA(udata) + if not vtg_data: + vtg_data = self.read_location_GxVTG(udata) + if rmc_data or gga_data or vtg_data: + data += udata + if rmc_data and gga_data and vtg_data: + self.break_flag = 2 + self.gps_timer.stop() self.break_flag = 0 + return data def quecgnss_read(self): @@ -125,21 +134,30 @@ class GPS(Singleton): rmc_data = '' gga_data = '' vtg_data = '' - while True: + count = 0 + while self.break_flag == 1: + self.gps_timer.start(3000, 1, self.gps_timer_callback) gnss_data = quecgnss.read(4096) - udata = gnss_data[1].decode() if len(gnss_data) > 1 and gnss_data[1] else '' - if not rmc_data: - rmc_data = self.read_location_GxRMC(udata) - if not gga_data: - gga_data = self.read_location_GxGGA(udata) - if not vtg_data: - vtg_data = self.read_location_GxVTG(udata) - if rmc_data or gga_data or vtg_data: - data += udata - if rmc_data and gga_data and vtg_data: - break + if gnss_data and gnss_data[1]: + udata = gnss_data[1].decode() if len(gnss_data) > 1 and gnss_data[1] else '' + if not rmc_data: + rmc_data = self.read_location_GxRMC(udata) + if not gga_data: + gga_data = self.read_location_GxGGA(udata) + if not vtg_data: + vtg_data = self.read_location_GxVTG(udata) + if rmc_data or gga_data or vtg_data: + data += udata + if rmc_data and gga_data and vtg_data: + self.break_flag = 2 + self.gps_timer.stop() + if count > 5: + self.break_flag = 2 + count += 1 + utime.sleep_ms(300) self.break_flag = 0 + return data def read(self): @@ -182,8 +200,10 @@ class GPS(Singleton): return "" def read_quecIot(self): + res = {} data = [] gps_data = self.read() + log.debug('read_quecIot gps_data: %s' % gps_data) r = self.read_location_GxRMC(gps_data) if r: data.append(r) @@ -195,13 +215,16 @@ class GPS(Singleton): r = self.read_location_GxVTG(gps_data) if r: data.append(r) + if data: + res = {'gps': data} - return {'gps': data} + return res def read_aliyun(self): + gps_info = {} gps_data = self.read() gga_data = self.read_location_GxGGA(gps_data) - data = {'CoordinateSystem': 1} + data = {} if gga_data: Latitude_re = ure.search(r",[0-9]+\.[0-9]+,[NS],", gga_data) if Latitude_re: @@ -212,7 +235,10 @@ class GPS(Singleton): Altitude_re = ure.search(r"-*[0-9]+\.[0-9]+,M,", gga_data) if Altitude_re: data['Altitude'] = round(float(Altitude_re.group(0)[:-3]), 2) - gps_info = {'GeoLocation': data} + if data: + data['CoordinateSystem'] = 1 + if data: + gps_info = {'GeoLocation': data} return gps_info @@ -233,8 +259,10 @@ class CellLocator(object): return {'non_gps': ['LBS']} def read_aliyun(self): + gps_info = {} gps_data = self.read() - gps_info = {'GeoLocation': {'Longtitude': round(gps_data[0], 2), 'Latitude': round(gps_data[1], 2), 'Altitude': 0.0, 'CoordinateSystem': 1}} + if gps_data: + gps_info = {'GeoLocation': {'Longtitude': round(gps_data[0], 2), 'Latitude': round(gps_data[1], 2), 'Altitude': 0.0, 'CoordinateSystem': 1}} return gps_info @@ -246,11 +274,14 @@ class WiFiLocator(object): return self.wifilocator_obj.getwifilocator() def read_quecIot(self): - return {'non_gps': []} + # TODO: {'non_gps': []} + return {} def read_aliyun(self): + gps_info = {} gps_data = self.read() - gps_info = {'GeoLocation': {'Longtitude': round(gps_data[0], 2), 'Latitude': round(gps_data[1], 2), 'Altitude': 0.0, 'CoordinateSystem': 1}} + if gps_data: + gps_info = {'GeoLocation': {'Longtitude': round(gps_data[0], 2), 'Latitude': round(gps_data[1], 2), 'Altitude': 0.0, 'CoordinateSystem': 1}} return gps_info diff --git a/code/main.py b/code/main.py index 5d9d361..fc70172 100644 --- a/code/main.py +++ b/code/main.py @@ -21,7 +21,7 @@ log = getLogger(__name__) PROJECT_NAME = 'QuecPython_Tracker' -PROJECT_VERSION = '2.0.0' +PROJECT_VERSION = '2.0.1' def main(): diff --git a/code/quecthing.py b/code/quecthing.py index 67a5ab7..8550499 100644 --- a/code/quecthing.py +++ b/code/quecthing.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import utime import osTimer import quecIot @@ -70,13 +71,28 @@ class QuecThing(object): self.downlink_queue = downlink_queue self.post_result_wait_queue = Queue(maxsize=16) self.quec_timer = osTimer() + self.queciot_init(pk, ps, dk, ds) + def queciot_init(self, pk, ps, dk, ds): quecIot.init() quecIot.setEventCB(self.eventCB) quecIot.setProductinfo(pk, ps) quecIot.setDkDs(dk, ds) quecIot.setServer(1, "iot-south.quectel.com:2883") quecIot.setConnmode(1) + if not ds and dk: + count = 0 + while count < 3: + ndk, nds = quecIot.getDkDs() + if nds: + break + count += 1 + utime.sleep(count) + current_settings = settings.get() + cloud_init_params = current_settings['sys']['cloud_init_params'] + cloud_init_params['DS'] = nds + settings.set('cloud_init_params', cloud_init_params) + settings.save() def get_post_res(self): current_settings = settings.get() diff --git a/code/settings.py b/code/settings.py index 519a0cd..98f4648 100644 --- a/code/settings.py +++ b/code/settings.py @@ -12,18 +12,17 @@ # See the License for the specific language governing permissions and # limitations under the License. -import ql_fs -import ujson import uos import ure +import ql_fs +import ujson import _thread -import quecIot from machine import UART from usr.common import Singleton PROJECT_NAME = 'QuecPython_Tracker' -PROJECT_VERSION = '2.0.0' +PROJECT_VERSION = '2.0.1' DATA_NON_LOCA = 0x0 DATA_LOCA_NON_GPS = 0x1 @@ -131,7 +130,7 @@ class default_values_app(object): phone_num = '' - loc_method = _loc_method.gps + loc_method = _loc_method.all work_mode = _work_mode.cycle @@ -213,6 +212,7 @@ class default_values_sys(object): ali_burning_method = _ali_burning_method.one_machine_one_density + # trackdev0304 _quecIot = { 'PK': 'p11275', 'PS': 'Q0ZQQndaN3pCUFd6', @@ -220,6 +220,22 @@ class default_values_sys(object): 'DS': '8eba9389af434974c3c846d1922d949f', } + # # trackerdemo0326 + # _quecIot = { + # 'PK': 'p11275', + # 'PS': 'Q0ZQQndaN3pCUFd6', + # 'DK': 'trackerdemo0326', + # 'DS': '32d540996e32f95c58dd98f18d473d52', + # } + + # _quecIot = { + # 'PK': 'p11275', + # 'PS': 'Q0ZQQndaN3pCUFd6', + # 'DK': '', + # 'DS': '', + # } + + # TrackerEC600N _AliYun = { 'PK': 'guqqtu3edVY', 'PS': 'xChL7HREtPyYCtPM', @@ -227,6 +243,22 @@ class default_values_sys(object): 'DS': 'a3153ed0c2f68db6e2f47e0769f966a2', } + # # alitrackerdemo0326 + # _AliYun = { + # 'PK': 'guqqtu3edVY', + # 'PS': 'xChL7HREtPyYCtPM', + # 'DK': 'alitrackerdemo0326', + # 'DS': 'b01307100686698bbc71fc4af230baf9', + # } + + # # ali_tracker_demo_0306_jun + # _AliYun = { + # 'PK': 'guqqtu3edVY', + # 'PS': 'xChL7HREtPyYCtPM', + # 'DK': 'ali_tracker_demo_0306_jun', + # 'DS': '38d78764407d08776c74e319a7802d88', + # } + _JTT808 = { 'PK': '', 'PS': '', @@ -276,7 +308,6 @@ class default_values_sys(object): if cloud & default_values_sys._cloud.quecIot: cloud_init_params = default_values_sys._quecIot - cloud_init_params = default_values_sys._quecIot_init_params(cloud_init_params) if cloud & default_values_sys._cloud.AliYun: cloud_init_params = default_values_sys._AliYun if cloud & default_values_sys._cloud.JTT808: @@ -284,17 +315,6 @@ class default_values_sys(object): return cloud_init_params - @staticmethod - def _quecIot_init_params(cloud_init_params): - if not cloud_init_params['DK'] or not cloud_init_params['DS']: - if quecIot.init(): - if quecIot.setProductinfo(cloud_init_params['PK'], cloud_init_params['PS']): - if quecIot.setDkDs(cloud_init_params['DK'], cloud_init_params['DS']): - ndk, nds = quecIot.getDkDs() - cloud_init_params['DK'] = ndk - cloud_init_params['DS'] = nds - return cloud_init_params - class Settings(Singleton): @@ -389,12 +409,17 @@ class Settings(Singleton): if opt == 'sw_log': if not isinstance(val, bool): return False - self.current_settings['app'][opt] = val + self.current_settings['sys'][opt] = val return True elif opt == 'ota_status': if not isinstance(val, int): return False - self.current_settings['app'][opt] = val + self.current_settings['sys'][opt] = val + return True + elif opt == 'cloud_init_params': + if not isinstance(val, dict): + return False + self.current_settings['sys'][opt] = val return True else: return False diff --git a/code/test_tracker.py b/code/test_tracker.py index 495b251..654a132 100644 --- a/code/test_tracker.py +++ b/code/test_tracker.py @@ -73,9 +73,6 @@ def test_settings(): log.info('[x] start test_settings') current_settings = settings.settings.get() log.info("current_settings", current_settings) - settings.Settings().reset() - current_settings = settings.settings.get() - log.info("current_settings", current_settings) log.info('[x] end test_settings') diff --git a/code/tracker.py b/code/tracker.py index 69605a5..50d1d45 100644 --- a/code/tracker.py +++ b/code/tracker.py @@ -99,6 +99,7 @@ class Tracker(Singleton): loc_info = self.locator.read() if loc_info: + log.debug('loc_method: %s' % loc_info[0]) device_data.update(loc_info[1]) current_settings = settings.settings.get()