-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbluestone_common.py
99 lines (79 loc) · 2.22 KB
/
bluestone_common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'''
File: bluestone_common.py
Project: bluestone
Author: daniel dong
Email: [email protected]
Copyright 2021 - 2021 bluestone tech
'''
import uos
import ure
import modem
import log
import ujson
import _thread
log.basicConfig(level = log.INFO)
_common_log = log.getLogger("COMMON")
class BluestoneCommon(object):
inst = None
_network_state = 1
_lock = _thread.allocate_lock()
def __init__(self):
BluestoneCommon.inst = self
@staticmethod
def get_network_state():
return BluestoneCommon._network_state
@staticmethod
def set_network_state(state = 1):
BluestoneCommon._lock.acquire()
BluestoneCommon._network_state = state
BluestoneCommon._lock.release()
@staticmethod
def get_imei():
return modem.getDevImei()
@staticmethod
def get_sn():
return modem.getDevSN()
@staticmethod
def generate_random_str():
random_str = ''.join([('0' + hex(ord(uos.urandom(1)))[2:])[-2:] for x in range(8)])
return random_str
@staticmethod
def check_file_exist(file_name):
if not file_name:
return False
file_list = uos.listdir('usr')
if file_name in file_list:
return True
else:
return False
@staticmethod
def is_url(url):
#pattern = ure.compile('http[s]://.+')
return ure.match('https?://.+', url)
@staticmethod
def is_json(content):
if not content:
return False
try:
ujson.loads(content)
return True
except Exception as err:
return False
@staticmethod
def get_range(start, end, step = 1):
result = []
index = start
while True:
if start < end:
if index <= end:
result.push(index)
else:
break
index = index + step
else:
if index >= end:
result.push(index)
else:
break
index = index - step
return result