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 uos
|
|
|
|
import ure
|
2022-03-26 19:09:18 +08:00
|
|
|
import ql_fs
|
|
|
|
import ujson
|
2022-03-07 14:47:10 +08:00
|
|
|
import _thread
|
2022-03-04 13:21:48 +08:00
|
|
|
from machine import UART
|
2022-03-09 13:17:33 +08:00
|
|
|
from usr.common import Singleton
|
2022-03-06 17:15:19 +08:00
|
|
|
|
2022-03-15 13:40:23 +08:00
|
|
|
PROJECT_NAME = 'QuecPython_Tracker'
|
|
|
|
|
2022-03-26 19:09:18 +08:00
|
|
|
PROJECT_VERSION = '2.0.1'
|
2022-03-03 09:53:51 +08:00
|
|
|
|
2022-03-21 11:21:01 +08:00
|
|
|
DATA_NON_LOCA = 0x0
|
|
|
|
DATA_LOCA_NON_GPS = 0x1
|
|
|
|
DATA_LOCA_GPS = 0x2
|
|
|
|
|
2022-03-16 19:43:12 +08:00
|
|
|
ALERTCODE = {
|
|
|
|
20000: 'fault_alert',
|
|
|
|
30002: 'low_power_alert',
|
|
|
|
30003: 'over_speed_alert',
|
2022-03-25 11:56:14 +08:00
|
|
|
30004: 'sim_abnormal_alert',
|
2022-03-16 19:43:12 +08:00
|
|
|
30005: 'disassemble_alert',
|
|
|
|
40000: 'drive_behavior_alert',
|
|
|
|
50001: 'sos_alert',
|
|
|
|
}
|
|
|
|
|
2022-03-28 20:29:01 +08:00
|
|
|
DEVICE_MODULE_STATUS = {
|
|
|
|
'net_error': 1,
|
|
|
|
'gps_error': 2,
|
|
|
|
'temp_sensor_error': 3,
|
|
|
|
'light_sensor_error': 4,
|
|
|
|
'move_sensor_error': 5,
|
|
|
|
'mike_error': 6,
|
2022-03-16 19:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DRIVE_BEHAVIOR_CODE = {
|
2022-03-28 20:29:01 +08:00
|
|
|
0: 'none',
|
|
|
|
1: 'quick_start',
|
|
|
|
2: 'quick_stop',
|
|
|
|
3: 'quick_turn_left',
|
|
|
|
4: 'quick_turn_right',
|
2022-03-16 19:43:12 +08:00
|
|
|
}
|
|
|
|
|
2022-03-23 19:42:23 +08:00
|
|
|
LOWENERGYMAP = {
|
|
|
|
"EC200U": [
|
|
|
|
"POWERDOWN",
|
|
|
|
"PM",
|
|
|
|
],
|
|
|
|
"EC200U": [
|
|
|
|
"POWERDOWN",
|
|
|
|
"PM",
|
|
|
|
],
|
|
|
|
"EC600N": [
|
|
|
|
"PM",
|
|
|
|
],
|
|
|
|
"EC800G": [
|
|
|
|
"PM"
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
2022-03-03 09:53:51 +08:00
|
|
|
tracker_settings_file = '/usr/tracker_settings.json'
|
|
|
|
|
2022-03-07 14:47:10 +08:00
|
|
|
_settings_lock = _thread.allocate_lock()
|
|
|
|
|
|
|
|
|
2022-03-09 13:17:33 +08:00
|
|
|
def settings_lock(func_name):
|
|
|
|
def settings_lock_fun(func):
|
|
|
|
def wrapperd_fun(*args, **kwargs):
|
|
|
|
if not _settings_lock.locked():
|
|
|
|
if _settings_lock.acquire():
|
|
|
|
source_fun = func(*args, **kwargs)
|
|
|
|
_settings_lock.release()
|
|
|
|
return source_fun
|
|
|
|
else:
|
2022-03-15 13:40:23 +08:00
|
|
|
print('_settings_lock acquire falied. func: %s, args: %s' % (func_name, args))
|
2022-03-17 13:30:51 +08:00
|
|
|
return False
|
2022-03-07 14:47:10 +08:00
|
|
|
else:
|
2022-03-15 13:40:23 +08:00
|
|
|
print('_settings_lock is locked. func: %s, args: %s' % (func_name, args))
|
2022-03-17 13:30:51 +08:00
|
|
|
return False
|
2022-03-09 13:17:33 +08:00
|
|
|
return wrapperd_fun
|
|
|
|
return settings_lock_fun
|
2022-03-08 17:12:38 +08:00
|
|
|
|
|
|
|
|
|
|
|
class SettingsError(Exception):
|
|
|
|
def __init__(self, value):
|
|
|
|
self.value = value
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return repr(self.value)
|
2022-03-07 14:47:10 +08:00
|
|
|
|
2022-03-04 13:21:48 +08:00
|
|
|
|
2022-03-03 09:53:51 +08:00
|
|
|
class default_values_app(object):
|
|
|
|
'''
|
|
|
|
App default settings
|
|
|
|
'''
|
|
|
|
|
|
|
|
class _loc_method(object):
|
|
|
|
none = 0x0
|
|
|
|
gps = 0x1
|
|
|
|
cell = 0x2
|
|
|
|
wifi = 0x4
|
|
|
|
all = 0x7
|
|
|
|
|
2022-03-21 19:40:33 +08:00
|
|
|
class _work_mode(object):
|
2022-03-03 09:53:51 +08:00
|
|
|
cycle = 0x1
|
2022-03-21 19:40:33 +08:00
|
|
|
intelligent = 0x2
|
2022-03-03 09:53:51 +08:00
|
|
|
|
2022-03-04 13:13:49 +08:00
|
|
|
class _drive_behavior(object):
|
|
|
|
suddenly_start = 0
|
|
|
|
suddenly_stop = 1
|
|
|
|
suddenly_turn_left = 2
|
|
|
|
suddenly_turn_right = 3
|
|
|
|
|
2022-03-03 09:53:51 +08:00
|
|
|
'''
|
|
|
|
variables of App default settings below MUST NOT start with '_'
|
|
|
|
'''
|
|
|
|
|
|
|
|
phone_num = ''
|
|
|
|
|
2022-03-26 19:09:18 +08:00
|
|
|
loc_method = _loc_method.all
|
2022-03-03 09:53:51 +08:00
|
|
|
|
2022-03-23 19:42:23 +08:00
|
|
|
work_mode = _work_mode.cycle
|
2022-03-03 09:53:51 +08:00
|
|
|
|
2022-03-24 14:38:08 +08:00
|
|
|
work_cycle_period = 30
|
2022-03-03 09:53:51 +08:00
|
|
|
|
2022-03-25 18:42:11 +08:00
|
|
|
low_power_alert_threshold = 30
|
2022-03-04 13:13:49 +08:00
|
|
|
|
|
|
|
low_power_shutdown_threshold = 5
|
2022-03-03 09:53:51 +08:00
|
|
|
|
2022-03-25 18:42:11 +08:00
|
|
|
over_speed_threshold = 60
|
2022-03-09 09:51:40 +08:00
|
|
|
|
2022-03-04 13:13:49 +08:00
|
|
|
sw_ota = True
|
2022-03-03 09:53:51 +08:00
|
|
|
|
2022-03-04 13:13:49 +08:00
|
|
|
sw_ota_auto_upgrade = True
|
2022-03-03 09:53:51 +08:00
|
|
|
|
2022-03-04 13:13:49 +08:00
|
|
|
sw_voice_listen = False
|
2022-03-03 09:53:51 +08:00
|
|
|
|
|
|
|
sw_voice_record = False
|
|
|
|
|
|
|
|
sw_fault_alert = True
|
|
|
|
|
|
|
|
sw_low_power_alert = True
|
|
|
|
|
|
|
|
sw_over_speed_alert = True
|
|
|
|
|
2022-03-25 11:56:14 +08:00
|
|
|
sw_sim_abnormal_alert = True
|
2022-03-03 09:53:51 +08:00
|
|
|
|
|
|
|
sw_disassemble_alert = True
|
|
|
|
|
|
|
|
sw_drive_behavior_alert = True
|
|
|
|
|
2022-03-04 19:29:23 +08:00
|
|
|
|
2022-03-03 09:53:51 +08:00
|
|
|
class default_values_sys(object):
|
|
|
|
'''
|
|
|
|
System default settings
|
|
|
|
'''
|
|
|
|
|
|
|
|
class _cloud(object):
|
|
|
|
none = 0x0
|
|
|
|
quecIot = 0x1
|
|
|
|
AliYun = 0x2
|
|
|
|
JTT808 = 0x4
|
|
|
|
customization = 0x8
|
|
|
|
|
2022-03-15 13:40:23 +08:00
|
|
|
class _gps_mode(object):
|
|
|
|
none = 0x0
|
|
|
|
internal = 0x1
|
|
|
|
external = 0x2
|
|
|
|
|
2022-03-16 11:38:50 +08:00
|
|
|
class _ota_status(object):
|
|
|
|
none = 0
|
|
|
|
to_be_updated = 1
|
|
|
|
updating = 2
|
|
|
|
update_successed = 3
|
|
|
|
update_failed = 4
|
|
|
|
|
2022-03-21 11:21:01 +08:00
|
|
|
class _ali_burning_method(object):
|
|
|
|
one_type_one_density = 0
|
|
|
|
one_machine_one_density = 1
|
|
|
|
|
2022-03-03 09:53:51 +08:00
|
|
|
'''
|
|
|
|
variables of system default settings below MUST NOT start with '_'
|
|
|
|
'''
|
2022-03-15 13:40:23 +08:00
|
|
|
sw_log = True
|
|
|
|
|
|
|
|
checknet_timeout = 60
|
|
|
|
|
2022-03-04 19:29:23 +08:00
|
|
|
profile_idx = 1
|
2022-03-03 09:53:51 +08:00
|
|
|
|
2022-03-15 13:40:23 +08:00
|
|
|
gps_mode = _gps_mode.external
|
|
|
|
|
2022-03-16 11:38:50 +08:00
|
|
|
ota_status = _ota_status.none
|
|
|
|
|
2022-03-28 20:29:01 +08:00
|
|
|
drive_behavior_code = 0
|
|
|
|
|
2022-03-29 13:24:17 +08:00
|
|
|
cloud = _cloud.quecIot
|
2022-03-03 09:53:51 +08:00
|
|
|
|
2022-03-07 14:47:10 +08:00
|
|
|
cloud_init_params = {}
|
|
|
|
|
2022-03-24 16:23:22 +08:00
|
|
|
work_mode_timeline = 3600
|
|
|
|
|
2022-03-21 11:21:01 +08:00
|
|
|
ali_burning_method = _ali_burning_method.one_machine_one_density
|
|
|
|
|
2022-03-26 19:09:18 +08:00
|
|
|
# trackdev0304
|
2022-03-07 14:47:10 +08:00
|
|
|
_quecIot = {
|
|
|
|
'PK': 'p11275',
|
|
|
|
'PS': 'Q0ZQQndaN3pCUFd6',
|
|
|
|
'DK': 'trackdev0304',
|
|
|
|
'DS': '8eba9389af434974c3c846d1922d949f',
|
|
|
|
}
|
|
|
|
|
2022-03-26 19:09:18 +08:00
|
|
|
# # trackerdemo0326
|
|
|
|
# _quecIot = {
|
|
|
|
# 'PK': 'p11275',
|
|
|
|
# 'PS': 'Q0ZQQndaN3pCUFd6',
|
|
|
|
# 'DK': 'trackerdemo0326',
|
|
|
|
# 'DS': '32d540996e32f95c58dd98f18d473d52',
|
|
|
|
# }
|
|
|
|
|
|
|
|
# _quecIot = {
|
|
|
|
# 'PK': 'p11275',
|
|
|
|
# 'PS': 'Q0ZQQndaN3pCUFd6',
|
|
|
|
# 'DK': '',
|
|
|
|
# 'DS': '',
|
|
|
|
# }
|
|
|
|
|
2022-03-29 13:24:17 +08:00
|
|
|
# tracker_dev_jack
|
2022-03-07 14:47:10 +08:00
|
|
|
_AliYun = {
|
2022-03-29 13:24:17 +08:00
|
|
|
'PK': 'a1q1kmZPwU2',
|
|
|
|
'PS': 'HQraBqtV8WsfCEuy',
|
|
|
|
'DK': 'tracker_dev_jack',
|
|
|
|
'DS': 'bfdfcca5075715e8309eff8597663c4b',
|
2022-03-07 14:47:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_JTT808 = {
|
|
|
|
'PK': '',
|
|
|
|
'PS': '',
|
|
|
|
'DK': '',
|
|
|
|
'DS': '',
|
|
|
|
}
|
|
|
|
|
2022-03-04 15:05:30 +08:00
|
|
|
locator_init_params = {}
|
2022-03-03 09:53:51 +08:00
|
|
|
|
2022-03-04 15:05:30 +08:00
|
|
|
_gps_cfg = {
|
2022-03-04 17:25:28 +08:00
|
|
|
'UARTn': UART.UART1,
|
2022-03-04 13:21:48 +08:00
|
|
|
'buadrate': 115200,
|
|
|
|
'databits': 8,
|
|
|
|
'parity': 0,
|
|
|
|
'stopbits': 1,
|
2022-03-04 15:05:30 +08:00
|
|
|
'flowctl': 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
_cellLocator_cfg = {
|
2022-03-04 13:21:48 +08:00
|
|
|
'serverAddr': 'www.queclocator.com',
|
|
|
|
'port': 80,
|
|
|
|
'token': 'xGP77d2z0i91s67n',
|
|
|
|
'timeout': 3,
|
2022-03-04 15:05:30 +08:00
|
|
|
'profileIdx': profile_idx,
|
|
|
|
}
|
|
|
|
|
|
|
|
_wifiLocator_cfg = {
|
2022-03-04 13:21:48 +08:00
|
|
|
'token': 'xGP77d2z0i91s67n'
|
|
|
|
}
|
|
|
|
|
2022-03-06 17:15:19 +08:00
|
|
|
@staticmethod
|
|
|
|
def _get_locator_init_params(loc_method):
|
2022-03-08 17:12:38 +08:00
|
|
|
locator_init_params = {}
|
2022-03-06 17:15:19 +08:00
|
|
|
|
|
|
|
if loc_method & default_values_app._loc_method.gps:
|
|
|
|
locator_init_params['gps_cfg'] = default_values_sys._gps_cfg
|
|
|
|
if loc_method & default_values_app._loc_method.cell:
|
|
|
|
locator_init_params['cellLocator_cfg'] = default_values_sys._cellLocator_cfg
|
|
|
|
if loc_method & default_values_app._loc_method.wifi:
|
|
|
|
locator_init_params['wifiLocator_cfg'] = default_values_sys._wifiLocator_cfg
|
|
|
|
|
|
|
|
return locator_init_params
|
|
|
|
|
2022-03-07 14:47:10 +08:00
|
|
|
@staticmethod
|
|
|
|
def _get_cloud_init_params(cloud):
|
2022-03-08 17:12:38 +08:00
|
|
|
cloud_init_params = {}
|
2022-03-07 14:47:10 +08:00
|
|
|
|
|
|
|
if cloud & default_values_sys._cloud.quecIot:
|
|
|
|
cloud_init_params = default_values_sys._quecIot
|
|
|
|
if cloud & default_values_sys._cloud.AliYun:
|
|
|
|
cloud_init_params = default_values_sys._AliYun
|
|
|
|
if cloud & default_values_sys._cloud.JTT808:
|
|
|
|
cloud_init_params = default_values_sys._JTT808
|
|
|
|
|
|
|
|
return cloud_init_params
|
2022-03-04 13:21:48 +08:00
|
|
|
|
2022-03-07 14:47:10 +08:00
|
|
|
|
2022-03-09 10:51:39 +08:00
|
|
|
class Settings(Singleton):
|
2022-03-04 15:05:30 +08:00
|
|
|
|
2022-03-08 17:12:38 +08:00
|
|
|
def __init__(self):
|
|
|
|
self.current_settings = {}
|
|
|
|
self.current_settings_app = {}
|
|
|
|
self.current_settings_sys = {}
|
|
|
|
self.init()
|
2022-03-04 15:05:30 +08:00
|
|
|
|
2022-03-09 13:17:33 +08:00
|
|
|
@settings_lock('Settings.init')
|
2022-03-08 17:12:38 +08:00
|
|
|
def init(self):
|
2022-03-16 19:43:12 +08:00
|
|
|
try:
|
|
|
|
default_values_sys.locator_init_params = default_values_sys._get_locator_init_params(default_values_app.loc_method)
|
|
|
|
default_values_sys.cloud_init_params = default_values_sys._get_cloud_init_params(default_values_sys.cloud)
|
|
|
|
|
|
|
|
default_settings_app = {k: v for k, v in default_values_app.__dict__.items() if not k.startswith('_')}
|
|
|
|
default_settings_sys = {k: v for k, v in default_values_sys.__dict__.items() if not k.startswith('_')}
|
|
|
|
default_settings = {'app': default_settings_app, 'sys': default_settings_sys}
|
|
|
|
|
|
|
|
if not ql_fs.path_exists(tracker_settings_file):
|
|
|
|
with open(tracker_settings_file, 'w') as f:
|
|
|
|
ujson.dump(default_settings, f)
|
|
|
|
self.current_settings = dict(default_settings)
|
|
|
|
else:
|
|
|
|
with open(tracker_settings_file, 'r') as f:
|
|
|
|
self.current_settings = ujson.load(f)
|
|
|
|
return True
|
|
|
|
except:
|
|
|
|
return False
|
2022-03-08 17:12:38 +08:00
|
|
|
|
2022-03-09 13:17:33 +08:00
|
|
|
@settings_lock('Settings.get')
|
2022-03-08 17:12:38 +08:00
|
|
|
def get(self):
|
|
|
|
return self.current_settings
|
|
|
|
|
2022-03-09 13:17:33 +08:00
|
|
|
@settings_lock('Settings.set')
|
2022-03-08 17:12:38 +08:00
|
|
|
def set(self, opt, val):
|
|
|
|
if opt in self.current_settings['app']:
|
|
|
|
if opt == 'phone_num':
|
|
|
|
if not isinstance(val, str):
|
|
|
|
return False
|
|
|
|
pattern = ure.compile(r'^(?:(?:\+)86)?1[3-9]\d\d\d\d\d\d\d\d\d$')
|
|
|
|
if pattern.search(val):
|
|
|
|
self.current_settings['app'][opt] = val
|
|
|
|
return True
|
|
|
|
return False
|
2022-03-07 14:47:10 +08:00
|
|
|
|
2022-03-08 17:12:38 +08:00
|
|
|
elif opt == 'loc_method':
|
|
|
|
if not isinstance(val, int):
|
|
|
|
return False
|
|
|
|
if val > default_values_app._loc_method.all:
|
|
|
|
return False
|
|
|
|
self.current_settings['app'][opt] = val
|
|
|
|
self.current_settings['sys']['locator_init_params'] = default_values_sys._get_locator_init_params(val)
|
|
|
|
return True
|
2022-03-06 17:15:19 +08:00
|
|
|
|
2022-03-21 19:40:33 +08:00
|
|
|
elif opt == 'work_mode':
|
2022-03-08 17:12:38 +08:00
|
|
|
if not isinstance(val, int):
|
|
|
|
return False
|
2022-03-23 19:42:23 +08:00
|
|
|
if val > default_values_app._work_mode.intelligent:
|
2022-03-08 17:12:38 +08:00
|
|
|
return False
|
|
|
|
self.current_settings['app'][opt] = val
|
2022-03-03 09:53:51 +08:00
|
|
|
return True
|
|
|
|
|
2022-03-21 19:40:33 +08:00
|
|
|
elif opt == 'work_cycle_period':
|
2022-03-08 17:12:38 +08:00
|
|
|
if not isinstance(val, int):
|
|
|
|
return False
|
|
|
|
if val < 1:
|
|
|
|
return False
|
|
|
|
self.current_settings['app'][opt] = val
|
|
|
|
return True
|
2022-03-04 13:21:48 +08:00
|
|
|
|
2022-03-08 17:12:38 +08:00
|
|
|
elif opt == 'low_power_alert_threshold' or opt == 'low_power_shutdown_threshold':
|
|
|
|
if not isinstance(val, int):
|
|
|
|
return False
|
|
|
|
if val < 0 or val > 100:
|
|
|
|
return False
|
|
|
|
self.current_settings['app'][opt] = val
|
|
|
|
return True
|
2022-03-03 09:53:51 +08:00
|
|
|
|
2022-03-08 17:12:38 +08:00
|
|
|
elif opt in (
|
|
|
|
'sw_ota', 'sw_ota_auto_upgrade', 'sw_voice_listen', 'sw_voice_record',
|
|
|
|
'sw_fault_alert', 'sw_low_power_alert', 'sw_over_speed_alert',
|
2022-03-25 11:56:14 +08:00
|
|
|
'sw_sim_abnormal_alert', 'sw_disassemble_alert', 'sw_drive_behavior_alert'):
|
2022-03-08 17:12:38 +08:00
|
|
|
if not isinstance(val, bool):
|
|
|
|
return False
|
|
|
|
self.current_settings['app'][opt] = val
|
|
|
|
return True
|
2022-03-04 18:10:03 +08:00
|
|
|
|
2022-03-08 17:12:38 +08:00
|
|
|
else:
|
2022-03-03 09:53:51 +08:00
|
|
|
return False
|
2022-03-16 11:38:50 +08:00
|
|
|
if opt in self.current_settings['sys']:
|
|
|
|
if opt == 'sw_log':
|
|
|
|
if not isinstance(val, bool):
|
|
|
|
return False
|
2022-03-26 19:09:18 +08:00
|
|
|
self.current_settings['sys'][opt] = val
|
2022-03-16 11:38:50 +08:00
|
|
|
return True
|
|
|
|
elif opt == 'ota_status':
|
|
|
|
if not isinstance(val, int):
|
|
|
|
return False
|
2022-03-26 19:09:18 +08:00
|
|
|
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
|
2022-03-16 11:38:50 +08:00
|
|
|
return True
|
2022-03-03 09:53:51 +08:00
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2022-03-09 13:17:33 +08:00
|
|
|
@settings_lock('Settings.save')
|
2022-03-08 17:12:38 +08:00
|
|
|
def save(self):
|
2022-03-16 19:43:12 +08:00
|
|
|
try:
|
|
|
|
with open(tracker_settings_file, 'w') as f:
|
|
|
|
ujson.dump(self.current_settings, f)
|
|
|
|
return True
|
|
|
|
except:
|
|
|
|
return False
|
2022-03-04 13:21:48 +08:00
|
|
|
|
2022-03-09 13:17:33 +08:00
|
|
|
@settings_lock('Settings.reset')
|
2022-03-08 17:12:38 +08:00
|
|
|
def reset(self):
|
2022-03-16 19:43:12 +08:00
|
|
|
try:
|
|
|
|
uos.remove(tracker_settings_file)
|
|
|
|
return True
|
|
|
|
except:
|
|
|
|
return False
|
2022-03-03 09:53:51 +08:00
|
|
|
|
2022-03-09 10:51:39 +08:00
|
|
|
|
2022-03-08 17:12:38 +08:00
|
|
|
settings = Settings()
|