mirror of
https://gitee.com/qpy-solutions/tracker-v2.git
synced 2025-05-18 18:48:25 +08:00
add: common.py; update: Singleton & move Singleton to common;
This commit is contained in:
parent
8649a011a0
commit
da743fdba0
@ -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
|
||||
'''
|
||||
|
19
code/common.py
Normal file
19
code/common.py
Normal file
@ -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)]
|
@ -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
|
||||
|
@ -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):
|
||||
|
@ -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,7 +16,8 @@ tracker_settings_file = '/usr/tracker_settings.json'
|
||||
_settings_lock = _thread.allocate_lock()
|
||||
|
||||
|
||||
def settings_lock(func):
|
||||
def settings_lock(func_name):
|
||||
def settings_lock_fun(func):
|
||||
def wrapperd_fun(*args, **kwargs):
|
||||
if not _settings_lock.locked():
|
||||
if _settings_lock.acquire():
|
||||
@ -23,10 +25,11 @@ def settings_lock(func):
|
||||
_settings_lock.release()
|
||||
return source_fun
|
||||
else:
|
||||
log.warn('_settings_lock acquire falied. func: %s, args: %s' % (func.__name__, args))
|
||||
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))
|
||||
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)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user