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 queue import Queue
|
||||||
from usr import settings
|
from usr import settings
|
||||||
from usr.logging import getLogger
|
from usr.logging import getLogger
|
||||||
|
from usr.common import Singleton
|
||||||
|
|
||||||
log = getLogger(__name__)
|
log = getLogger(__name__)
|
||||||
|
|
||||||
@ -57,7 +58,7 @@ def alert_process(argv):
|
|||||||
log.error('altercode (%s) is not exists. alert info: %s' % data)
|
log.error('altercode (%s) is not exists. alert info: %s' % data)
|
||||||
|
|
||||||
|
|
||||||
class AlertMonitor(settings.Singleton):
|
class AlertMonitor(Singleton):
|
||||||
'''
|
'''
|
||||||
Recv alert signals and process them
|
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 utime
|
||||||
import _thread
|
import _thread
|
||||||
import cellLocator
|
import cellLocator
|
||||||
from wifilocator import wifilocator
|
|
||||||
import usr.settings as settings
|
import usr.settings as settings
|
||||||
from usr.logging import getLogger
|
|
||||||
|
|
||||||
from queue import Queue
|
from queue import Queue
|
||||||
from machine import UART
|
from machine import UART
|
||||||
|
from usr.logging import getLogger
|
||||||
|
from usr.common import Singleton
|
||||||
|
from wifilocator import wifilocator
|
||||||
|
|
||||||
log = getLogger(__name__)
|
log = getLogger(__name__)
|
||||||
|
|
||||||
@ -43,7 +44,7 @@ def gps_data_retrieve_thread(argv):
|
|||||||
self.gps_data = self.uart_read(toRead).decode()
|
self.gps_data = self.uart_read(toRead).decode()
|
||||||
|
|
||||||
|
|
||||||
class GPS(settings.Singleton):
|
class GPS(Singleton):
|
||||||
def __init__(self, gps_cfg):
|
def __init__(self, gps_cfg):
|
||||||
global gps_data_retrieve_queue
|
global gps_data_retrieve_queue
|
||||||
self.uart_obj = UART(
|
self.uart_obj = UART(
|
||||||
@ -140,7 +141,7 @@ def loc_worker(argv):
|
|||||||
self.read_cb(data)
|
self.read_cb(data)
|
||||||
|
|
||||||
|
|
||||||
class Location(settings.Singleton):
|
class Location(Singleton):
|
||||||
gps = None
|
gps = None
|
||||||
cellLoc = None
|
cellLoc = None
|
||||||
wifiLoc = None
|
wifiLoc = None
|
||||||
|
@ -10,6 +10,7 @@ from misc import Power
|
|||||||
from queue import Queue
|
from queue import Queue
|
||||||
from usr.logging import getLogger
|
from usr.logging import getLogger
|
||||||
from usr.battery import Battery
|
from usr.battery import Battery
|
||||||
|
from usr.common import Singleton
|
||||||
|
|
||||||
|
|
||||||
log = getLogger(__name__)
|
log = getLogger(__name__)
|
||||||
@ -206,7 +207,7 @@ def uplink_process(argv):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
|
|
||||||
class Remote(settings.Singleton):
|
class Remote(Singleton):
|
||||||
_history = '/usr/tracker_data.hist'
|
_history = '/usr/tracker_data.hist'
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -7,6 +7,7 @@ import _thread
|
|||||||
import quecIot
|
import quecIot
|
||||||
from machine import UART
|
from machine import UART
|
||||||
from usr.logging import getLogger
|
from usr.logging import getLogger
|
||||||
|
from usr.common import Singleton
|
||||||
|
|
||||||
log = getLogger(__name__)
|
log = getLogger(__name__)
|
||||||
|
|
||||||
@ -15,18 +16,20 @@ tracker_settings_file = '/usr/tracker_settings.json'
|
|||||||
_settings_lock = _thread.allocate_lock()
|
_settings_lock = _thread.allocate_lock()
|
||||||
|
|
||||||
|
|
||||||
def settings_lock(func):
|
def settings_lock(func_name):
|
||||||
def wrapperd_fun(*args, **kwargs):
|
def settings_lock_fun(func):
|
||||||
if not _settings_lock.locked():
|
def wrapperd_fun(*args, **kwargs):
|
||||||
if _settings_lock.acquire():
|
if not _settings_lock.locked():
|
||||||
source_fun = func(*args, **kwargs)
|
if _settings_lock.acquire():
|
||||||
_settings_lock.release()
|
source_fun = func(*args, **kwargs)
|
||||||
return source_fun
|
_settings_lock.release()
|
||||||
|
return source_fun
|
||||||
|
else:
|
||||||
|
log.warn('_settings_lock acquire falied. func: %s, args: %s' % (func_name, args))
|
||||||
else:
|
else:
|
||||||
log.warn('_settings_lock acquire falied. func: %s, args: %s' % (func.__name__, args))
|
log.warn('_settings_lock is locked. func: %s, args: %s' % (func_name, args))
|
||||||
else:
|
return wrapperd_fun
|
||||||
log.warn('_settings_lock is locked. func: %s, args: %s' % (func.__name__, args))
|
return settings_lock_fun
|
||||||
return wrapperd_fun
|
|
||||||
|
|
||||||
|
|
||||||
class SettingsError(Exception):
|
class SettingsError(Exception):
|
||||||
@ -37,15 +40,6 @@ class SettingsError(Exception):
|
|||||||
return repr(self.value)
|
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):
|
class default_values_app(object):
|
||||||
'''
|
'''
|
||||||
App default settings
|
App default settings
|
||||||
@ -78,7 +72,7 @@ class default_values_app(object):
|
|||||||
|
|
||||||
phone_num = ''
|
phone_num = ''
|
||||||
|
|
||||||
loc_method = _loc_method.gps
|
loc_method = _loc_method.all
|
||||||
|
|
||||||
loc_mode = _loc_mode.cycle
|
loc_mode = _loc_mode.cycle
|
||||||
|
|
||||||
@ -223,7 +217,7 @@ class Settings(Singleton):
|
|||||||
self.current_settings_sys = {}
|
self.current_settings_sys = {}
|
||||||
self.init()
|
self.init()
|
||||||
|
|
||||||
@settings_lock
|
@settings_lock('Settings.init')
|
||||||
def init(self):
|
def init(self):
|
||||||
default_values_sys.locator_init_params = default_values_sys._get_locator_init_params(default_values_app.loc_method)
|
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_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:
|
with open(tracker_settings_file, 'r') as f:
|
||||||
self.current_settings = ujson.load(f)
|
self.current_settings = ujson.load(f)
|
||||||
|
|
||||||
@settings_lock
|
@settings_lock('Settings.get')
|
||||||
def get(self):
|
def get(self):
|
||||||
return self.current_settings
|
return self.current_settings
|
||||||
|
|
||||||
@settings_lock
|
@settings_lock('Settings.query')
|
||||||
def query(self, remote, set_type, set_key):
|
def query(self, remote, set_type, set_key):
|
||||||
log.debug('remote: %s, set_type: %s, set_key: %s' % (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)})
|
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):
|
def set(self, opt, val):
|
||||||
if opt in self.current_settings['app']:
|
if opt in self.current_settings['app']:
|
||||||
if opt == 'phone_num':
|
if opt == 'phone_num':
|
||||||
@ -309,12 +303,12 @@ class Settings(Singleton):
|
|||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@settings_lock
|
@settings_lock('Settings.save')
|
||||||
def save(self):
|
def save(self):
|
||||||
with open(tracker_settings_file, 'w') as f:
|
with open(tracker_settings_file, 'w') as f:
|
||||||
ujson.dump(self.current_settings, f)
|
ujson.dump(self.current_settings, f)
|
||||||
|
|
||||||
@settings_lock
|
@settings_lock('Settings.reset')
|
||||||
def reset(self):
|
def reset(self):
|
||||||
uos.remove(tracker_settings_file)
|
uos.remove(tracker_settings_file)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user