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-04-05 12:09:23 +08:00
|
|
|
|
2022-04-06 11:33:16 +08:00
|
|
|
from usr.common import Singleton
|
2022-04-12 09:13:20 +08:00
|
|
|
from usr.common import option_lock
|
2022-04-06 11:33:16 +08:00
|
|
|
from usr.settings_app import default_values_app
|
|
|
|
from usr.settings_sys import default_values_sys
|
|
|
|
|
2022-04-06 11:50:09 +08:00
|
|
|
# For Other Module To Import
|
2022-04-12 09:13:20 +08:00
|
|
|
from usr.settings_sys import PROJECT_NAME, PROJECT_VERSION, \
|
|
|
|
DEVICE_FIRMWARE_NAME, DEVICE_FIRMWARE_VERSION, ALERTCODE, LOWENERGYMAP
|
2022-04-06 11:50:09 +08:00
|
|
|
|
|
|
|
PROJECT_NAME
|
|
|
|
PROJECT_VERSION
|
2022-04-12 09:13:20 +08:00
|
|
|
DEVICE_FIRMWARE_NAME
|
2022-04-06 11:50:09 +08:00
|
|
|
DEVICE_FIRMWARE_VERSION
|
|
|
|
ALERTCODE
|
|
|
|
LOWENERGYMAP
|
2022-03-23 19:42:23 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
tracker_settings_file = "/usr/tracker_settings.json"
|
2022-03-03 09:53:51 +08:00
|
|
|
|
2022-03-07 14:47:10 +08:00
|
|
|
_settings_lock = _thread.allocate_lock()
|
|
|
|
|
|
|
|
|
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-04-12 09:13:20 +08:00
|
|
|
@option_lock(_settings_lock)
|
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)
|
2022-04-05 12:09:23 +08:00
|
|
|
default_values_sys.ota_status = default_values_sys._ota_status_init_params()
|
2022-03-16 19:43:12 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
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}
|
2022-03-16 19:43:12 +08:00
|
|
|
|
|
|
|
if not ql_fs.path_exists(tracker_settings_file):
|
2022-04-12 09:13:20 +08:00
|
|
|
with open(tracker_settings_file, "w") as f:
|
2022-03-16 19:43:12 +08:00
|
|
|
ujson.dump(default_settings, f)
|
|
|
|
self.current_settings = dict(default_settings)
|
|
|
|
else:
|
2022-04-12 09:13:20 +08:00
|
|
|
with open(tracker_settings_file, "r") as f:
|
2022-03-16 19:43:12 +08:00
|
|
|
self.current_settings = ujson.load(f)
|
|
|
|
return True
|
|
|
|
except:
|
|
|
|
return False
|
2022-03-08 17:12:38 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
@option_lock(_settings_lock)
|
2022-03-08 17:12:38 +08:00
|
|
|
def get(self):
|
|
|
|
return self.current_settings
|
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
@option_lock(_settings_lock)
|
2022-03-08 17:12:38 +08:00
|
|
|
def set(self, opt, val):
|
2022-04-12 09:13:20 +08:00
|
|
|
if opt in self.current_settings["app"]:
|
|
|
|
if opt == "phone_num":
|
2022-03-08 17:12:38 +08:00
|
|
|
if not isinstance(val, str):
|
|
|
|
return False
|
2022-04-12 09:13:20 +08:00
|
|
|
pattern = ure.compile(r"^(?:(?:\+)86)?1[3-9]\d\d\d\d\d\d\d\d\d$")
|
2022-03-08 17:12:38 +08:00
|
|
|
if pattern.search(val):
|
2022-04-12 09:13:20 +08:00
|
|
|
self.current_settings["app"][opt] = val
|
2022-03-08 17:12:38 +08:00
|
|
|
return True
|
|
|
|
return False
|
2022-04-12 09:13:20 +08:00
|
|
|
elif opt == "loc_method":
|
2022-03-08 17:12:38 +08:00
|
|
|
if not isinstance(val, int):
|
|
|
|
return False
|
|
|
|
if val > default_values_app._loc_method.all:
|
|
|
|
return False
|
2022-04-12 09:13:20 +08:00
|
|
|
self.current_settings["app"][opt] = val
|
|
|
|
self.current_settings["sys"]["locator_init_params"] = default_values_sys._get_locator_init_params(val)
|
2022-03-08 17:12:38 +08:00
|
|
|
return True
|
2022-04-12 09:13:20 +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
|
2022-04-12 09:13:20 +08:00
|
|
|
self.current_settings["app"][opt] = val
|
2022-03-03 09:53:51 +08:00
|
|
|
return True
|
2022-04-12 09:13:20 +08:00
|
|
|
elif opt in ("work_cycle_period", "over_speed_threshold"):
|
2022-03-08 17:12:38 +08:00
|
|
|
if not isinstance(val, int):
|
|
|
|
return False
|
|
|
|
if val < 1:
|
|
|
|
return False
|
2022-04-12 09:13:20 +08:00
|
|
|
self.current_settings["app"][opt] = val
|
2022-03-08 17:12:38 +08:00
|
|
|
return True
|
2022-04-12 09:13:20 +08:00
|
|
|
elif opt in ("low_power_alert_threshold", "low_power_shutdown_threshold"):
|
2022-03-08 17:12:38 +08:00
|
|
|
if not isinstance(val, int):
|
|
|
|
return False
|
|
|
|
if val < 0 or val > 100:
|
|
|
|
return False
|
2022-04-12 09:13:20 +08:00
|
|
|
self.current_settings["app"][opt] = val
|
2022-03-08 17:12:38 +08:00
|
|
|
return True
|
2022-04-12 09:13:20 +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_abnormal_alert", "sw_disassemble_alert", "sw_drive_behavior_alert"):
|
2022-03-08 17:12:38 +08:00
|
|
|
if not isinstance(val, bool):
|
|
|
|
return False
|
2022-04-12 09:13:20 +08:00
|
|
|
self.current_settings["app"][opt] = val
|
2022-04-05 12:09:23 +08:00
|
|
|
return True
|
2022-04-12 09:13:20 +08:00
|
|
|
if opt in self.current_settings["sys"]:
|
|
|
|
if opt == "sw_log":
|
2022-03-16 11:38:50 +08:00
|
|
|
if not isinstance(val, bool):
|
|
|
|
return False
|
2022-04-12 09:13:20 +08:00
|
|
|
self.current_settings["sys"][opt] = val
|
2022-03-16 11:38:50 +08:00
|
|
|
return True
|
2022-04-12 09:13:20 +08:00
|
|
|
elif opt in ("ota_status", "cloud_init_params"):
|
2022-04-05 12:09:23 +08:00
|
|
|
if not isinstance(val, dict):
|
2022-03-16 11:38:50 +08:00
|
|
|
return False
|
2022-04-12 09:13:20 +08:00
|
|
|
self.current_settings["sys"][opt] = val
|
2022-03-26 19:09:18 +08:00
|
|
|
return True
|
2022-04-12 09:13:20 +08:00
|
|
|
elif opt == "user_ota_action":
|
2022-04-06 19:52:49 +08:00
|
|
|
if not isinstance(val, int):
|
|
|
|
return False
|
2022-04-12 09:13:20 +08:00
|
|
|
self.current_settings["sys"][opt] = val
|
|
|
|
return True
|
|
|
|
return False
|
2022-03-03 09:53:51 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
@option_lock(_settings_lock)
|
2022-03-08 17:12:38 +08:00
|
|
|
def save(self):
|
2022-03-16 19:43:12 +08:00
|
|
|
try:
|
2022-04-12 09:13:20 +08:00
|
|
|
with open(tracker_settings_file, "w") as f:
|
2022-03-16 19:43:12 +08:00
|
|
|
ujson.dump(self.current_settings, f)
|
|
|
|
return True
|
|
|
|
except:
|
|
|
|
return False
|
2022-03-04 13:21:48 +08:00
|
|
|
|
2022-04-12 09:13:20 +08:00
|
|
|
@option_lock(_settings_lock)
|
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()
|