-
Notifications
You must be signed in to change notification settings - Fork 2
/
basicdef.py
88 lines (75 loc) · 2.02 KB
/
basicdef.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name: basicdef
Description : 常用方法
Author : Slientsky
date: 2018-04-23
-------------------------------------------------
Change Activity:
2018-04-23
-------------------------------------------------
"""
import time, os, sys
import json, argparse
class BasicDef():
deviceid = None
apikey = None
#检测网络状态
NETWORK_STATUS = False
def set_network_status(value):
BasicDef.NETWORK_STATUS = value
def get_network_status():
BasicDef.NETWORK_STATUS = check_network_status()
return BasicDef.NETWORK_STATUS
def set_device_id(value):
BasicDef.deviceid = value
def set_apikey(value):
BasicDef.apikey = value
def get_device_id():
return BasicDef.deviceid
def get_apikey():
return BasicDef.apikey
@staticmethod
def bytes2human(n):
"""
>>> bytes2human(10000)
'9K'
>>> bytes2human(100001221)
'95M'
"""
symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
prefix = {}
for i, s in enumerate(symbols):
prefix[s] = 1 << (i + 1) * 10
for s in reversed(symbols):
if n >= prefix[s]:
value = int(float(n) / prefix[s])
return '%s%s' % (value, s)
return "%sB" % n
def get_pares_info(argv, key, int=0):
args = parse_args(argv[1:])
# print('args: ', args)
config = parse(args.configfile[int])
info = config[args.device]
value = info[key]
return value
def parse_args(args):
parser = argparse.ArgumentParser(prog="OneNetServer")
parser.add_argument('configfile', nargs=1, type=str, help='')
parser.add_argument('--device', default="device", type=str, help='')
return parser.parse_args(args)
def parse(filename):
configfile = open(filename)
jsonconfig = json.load(configfile)
configfile.close()
return jsonconfig
def check_network_status():
exit_code = os.system('ping www.baidu.com -c 1 > /dev/null')
# print(exit_code)
if exit_code:
print("Out offline!")
return False
else:
return True