2022-03-03 09:53:51 +08:00
|
|
|
|
|
|
|
import ql_fs
|
|
|
|
import ujson
|
|
|
|
import uos
|
|
|
|
import ure
|
2022-03-07 14:47:10 +08:00
|
|
|
import _thread
|
|
|
|
import quecIot
|
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-15 13:56:16 +08:00
|
|
|
PROJECT_VERSION = '2.0.0'
|
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',
|
|
|
|
30004: 'sim_out_alert',
|
|
|
|
30005: 'disassemble_alert',
|
|
|
|
40000: 'drive_behavior_alert',
|
|
|
|
50001: 'sos_alert',
|
|
|
|
}
|
|
|
|
|
|
|
|
FAULT_CODE = {
|
|
|
|
20001: 'net_error',
|
|
|
|
20002: 'gps_error',
|
|
|
|
20003: 'temp_sensor_error',
|
|
|
|
20004: 'light_sensor_error',
|
|
|
|
20005: 'move_sensor_error',
|
|
|
|
20006: 'mike_error',
|
|
|
|
}
|
|
|
|
|
|
|
|
DRIVE_BEHAVIOR_CODE = {
|
|
|
|
40001: 'quick_start',
|
|
|
|
40002: 'quick_stop',
|
|
|
|
40003: 'quick_turn_left',
|
|
|
|
40004: 'quick_turn_right',
|
|
|
|
}
|
|
|
|
|
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-09 15:14:03 +08:00
|
|
|
loc_method = _loc_method.gps
|
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-04 13:13:49 +08:00
|
|
|
low_power_alert_threshold = 20
|
|
|
|
|
|
|
|
low_power_shutdown_threshold = 5
|
2022-03-03 09:53:51 +08:00
|
|
|
|
2022-03-09 09:51:40 +08:00
|
|
|
over_speed_threshold = 120
|
|
|
|
|
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
|
|
|
|
|
|
|
|
sw_sim_out_alert = True
|
|
|
|
|
|
|
|
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-24 14:38:08 +08:00
|
|
|
cloud = _cloud.AliYun
|
2022-03-03 09:53:51 +08:00
|
|
|
|
2022-03-07 14:47:10 +08:00
|
|
|
cloud_init_params = {}
|
|
|
|
|
2022-03-18 17:58:15 +08:00
|
|
|
cloud_timeout = 180
|
2022-03-17 16:44:47 +08:00
|
|
|
|
2022-03-21 11:21:01 +08:00
|
|
|
ali_burning_method = _ali_burning_method.one_machine_one_density
|
|
|
|
|
2022-03-07 14:47:10 +08:00
|
|
|
_quecIot = {
|
|
|
|
'PK': 'p11275',
|
|
|
|
'PS': 'Q0ZQQndaN3pCUFd6',
|
|
|
|
'DK': 'trackdev0304',
|
|
|
|
'DS': '8eba9389af434974c3c846d1922d949f',
|
|
|
|
}
|
|
|
|
|
|
|
|
_AliYun = {
|
2022-03-18 17:53:46 +08:00
|
|
|
'PK': 'guqqtu3edVY',
|
|
|
|
'PS': 'xChL7HREtPyYCtPM',
|
|
|
|
'DK': 'TrackerEC600N',
|
|
|
|
'DS': 'a3153ed0c2f68db6e2f47e0769f966a2',
|
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
|
|
|
|
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:
|
|
|
|
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
|
|
|
@staticmethod
|
|
|
|
def _quecIot_init_params(cloud_init_params):
|
|
|
|
if not cloud_init_params['DK'] or not cloud_init_params['DS']:
|
|
|
|
if quecIot.init():
|
2022-03-07 16:46:44 +08:00
|
|
|
if quecIot.setProductinfo(cloud_init_params['PK'], cloud_init_params['PS']):
|
|
|
|
if quecIot.setDkDs(cloud_init_params['DK'], cloud_init_params['DS']):
|
2022-03-07 14:47:10 +08:00
|
|
|
ndk, nds = quecIot.getDkDs()
|
|
|
|
cloud_init_params['DK'] = ndk
|
|
|
|
cloud_init_params['DS'] = nds
|
|
|
|
return cloud_init_params
|
|
|
|
|
|
|
|
|
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',
|
|
|
|
'sw_sim_out_alert', 'sw_disassemble_alert', 'sw_drive_behavior_alert'):
|
|
|
|
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
|
|
|
|
self.current_settings['app'][opt] = val
|
|
|
|
return True
|
|
|
|
elif opt == 'ota_status':
|
|
|
|
if not isinstance(val, int):
|
|
|
|
return False
|
|
|
|
self.current_settings['app'][opt] = val
|
|
|
|
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()
|