-
Notifications
You must be signed in to change notification settings - Fork 0
/
csmapi.py
66 lines (49 loc) · 1.82 KB
/
csmapi.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
import requests
ENDPOINT = None
TIMEOUT=10
IoTtalk = requests.Session()
passwordKey = None
class CSMError(Exception):
pass
def register(mac_addr, profile, UsingSession=IoTtalk):
global passwordKey
r = UsingSession.post(
ENDPOINT + '/' + mac_addr,
json={'profile': profile}, timeout=TIMEOUT
)
if r.status_code != 200: raise CSMError(r.text)
else: passwordKey = r.json().get('password')
return True
def deregister(mac_addr, UsingSession=IoTtalk):
r = UsingSession.delete(ENDPOINT + '/' + mac_addr)
if r.status_code != 200: raise CSMError(r.text)
return True
def push(mac_addr, df_name, data, UsingSession=IoTtalk):
r = UsingSession.put(
ENDPOINT + '/' + mac_addr + '/' + df_name,
json={'data': data},
timeout=TIMEOUT,
headers = {'password-key': passwordKey}
)
if r.status_code != 200: raise CSMError(r.text)
return True
def pull(mac_addr, df_name, UsingSession=IoTtalk):
r = UsingSession.get(
ENDPOINT + '/' + mac_addr + '/' + df_name,
timeout=TIMEOUT,
headers = {'password-key': passwordKey}
)
if r.status_code != 200: raise CSMError(r.text)
return r.json()['samples']
def get_alias(mac_addr, df_name, UsingSession=IoTtalk):
r = UsingSession.get(ENDPOINT + '/get_alias/' + mac_addr + '/' + df_name, timeout=TIMEOUT)
if r.status_code != 200: raise CSMError(r.text)
return r.json()['alias_name']
def set_alias(mac_addr, df_name, s, UsingSession=IoTtalk):
r = UsingSession.get(ENDPOINT + '/set_alias/' + mac_addr + '/' + df_name + '/alias?name=' + s, timeout=TIMEOUT)
if r.status_code != 200: raise CSMError(r.text)
return True
def tree(UsingSession=IoTtalk):
r = UsingSession.get(ENDPOINT + '/tree')
if r.status_code != 200: raise CSMError(r.text)
return r.json()