From da743fdba01b1235e38ff16c593b10481e9f2eae Mon Sep 17 00:00:00 2001 From: JackSun-qc Date: Wed, 9 Mar 2022 13:17:33 +0800 Subject: [PATCH] add: common.py; update: Singleton & move Singleton to common; --- code/alert.py | 3 ++- code/common.py | 19 +++++++++++++++++++ code/location.py | 9 +++++---- code/remote.py | 3 ++- code/settings.py | 48 +++++++++++++++++++++--------------------------- 5 files changed, 49 insertions(+), 33 deletions(-) create mode 100644 code/common.py diff --git a/code/alert.py b/code/alert.py index 4a4ea1f..84a5afa 100644 --- a/code/alert.py +++ b/code/alert.py @@ -3,6 +3,7 @@ import _thread from queue import Queue from usr import settings from usr.logging import getLogger +from usr.common import Singleton log = getLogger(__name__) @@ -57,7 +58,7 @@ def alert_process(argv): log.error('altercode (%s) is not exists. alert info: %s' % data) -class AlertMonitor(settings.Singleton): +class AlertMonitor(Singleton): ''' Recv alert signals and process them ''' diff --git a/code/common.py b/code/common.py new file mode 100644 index 0000000..221d737 --- /dev/null +++ b/code/common.py @@ -0,0 +1,19 @@ +import _thread + + +class Singleton(object): + _instance_lock = _thread.allocate_lock() + + def __init__(self, *args, **kwargs): + pass + + def __new__(cls, *args, **kwargs): + if not hasattr(cls, 'instance_dict'): + Singleton.instance_dict = {} + + if str(cls) not in Singleton.instance_dict.keys(): + with Singleton._instance_lock: + _instance = super().__new__(cls) + Singleton.instance_dict[str(cls)] = _instance + + return Singleton.instance_dict[str(cls)] diff --git a/code/location.py b/code/location.py index baeafbd..7ea0b27 100644 --- a/code/location.py +++ b/code/location.py @@ -3,12 +3,13 @@ import ure import utime import _thread import cellLocator -from wifilocator import wifilocator import usr.settings as settings -from usr.logging import getLogger from queue import Queue from machine import UART +from usr.logging import getLogger +from usr.common import Singleton +from wifilocator import wifilocator log = getLogger(__name__) @@ -43,7 +44,7 @@ def gps_data_retrieve_thread(argv): self.gps_data = self.uart_read(toRead).decode() -class GPS(settings.Singleton): +class GPS(Singleton): def __init__(self, gps_cfg): global gps_data_retrieve_queue self.uart_obj = UART( @@ -140,7 +141,7 @@ def loc_worker(argv): self.read_cb(data) -class Location(settings.Singleton): +class Location(Singleton): gps = None cellLoc = None wifiLoc = None diff --git a/code/remote.py b/code/remote.py index c83f21e..a0d6e40 100644 --- a/code/remote.py +++ b/code/remote.py @@ -10,6 +10,7 @@ from misc import Power from queue import Queue from usr.logging import getLogger from usr.battery import Battery +from usr.common import Singleton log = getLogger(__name__) @@ -206,7 +207,7 @@ def uplink_process(argv): continue -class Remote(settings.Singleton): +class Remote(Singleton): _history = '/usr/tracker_data.hist' def __init__(self): diff --git a/code/settings.py b/code/settings.py index 64ed7dd..3eee58a 100644 --- a/code/settings.py +++ b/code/settings.py @@ -7,6 +7,7 @@ import _thread import quecIot from machine import UART from usr.logging import getLogger +from usr.common import Singleton log = getLogger(__name__) @@ -15,18 +16,20 @@ tracker_settings_file = '/usr/tracker_settings.json' _settings_lock = _thread.allocate_lock() -def settings_lock(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 +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: + log.warn('_settings_lock acquire falied. func: %s, args: %s' % (func_name, args)) else: - log.warn('_settings_lock acquire falied. func: %s, args: %s' % (func.__name__, args)) - else: - log.warn('_settings_lock is locked. func: %s, args: %s' % (func.__name__, args)) - return wrapperd_fun + log.warn('_settings_lock is locked. func: %s, args: %s' % (func_name, args)) + return wrapperd_fun + return settings_lock_fun class SettingsError(Exception): @@ -37,15 +40,6 @@ class SettingsError(Exception): return repr(self.value) -class Singleton(object): - _instance = None - - def __new__(cls, *args, **kwargs): - if not cls._instance: - cls._instance = super().__new__(cls) - return cls._instance - - class default_values_app(object): ''' App default settings @@ -78,7 +72,7 @@ class default_values_app(object): phone_num = '' - loc_method = _loc_method.gps + loc_method = _loc_method.all loc_mode = _loc_mode.cycle @@ -223,7 +217,7 @@ class Settings(Singleton): self.current_settings_sys = {} self.init() - @settings_lock + @settings_lock('Settings.init') def init(self): 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) @@ -240,16 +234,16 @@ class Settings(Singleton): with open(tracker_settings_file, 'r') as f: self.current_settings = ujson.load(f) - @settings_lock + @settings_lock('Settings.get') def get(self): return self.current_settings - @settings_lock + @settings_lock('Settings.query') def query(self, remote, set_type, set_key): log.debug('remote: %s, set_type: %s, set_key: %s' % (remote, set_type, set_key)) remote.post_data(remote.DATA_NON_LOCA, {set_key: self.current_settings.get(set_type, {}).get(set_key)}) - @settings_lock + @settings_lock('Settings.set') def set(self, opt, val): if opt in self.current_settings['app']: if opt == 'phone_num': @@ -309,12 +303,12 @@ class Settings(Singleton): else: return False - @settings_lock + @settings_lock('Settings.save') def save(self): with open(tracker_settings_file, 'w') as f: ujson.dump(self.current_settings, f) - @settings_lock + @settings_lock('Settings.reset') def reset(self): uos.remove(tracker_settings_file)