forked from wangzhenandy/snoti_client_python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.py
90 lines (78 loc) · 2.3 KB
/
request.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
#! python3
# -*- coding: utf-8 -*-
import json
class Request:
def __init__(self, cmd):
self._cmd = cmd
self.req_data = {"cmd": self._cmd}
def build_json(self):
return json.dumps(self.req_data) + '\n'
class LoginReq(Request):
def __init__(self, prefetch_count = 50):
self._cmd = 'login_req'
self._prefetch_count = prefetch_count
self._data = []
self.req_data = ''
def add_login_data(self, product_key, auth_id, auth_secret, subkey, events):
data = {
'product_key': product_key,
'auth_id': auth_id,
'auth_secret': auth_secret,
'subkey': subkey,
'events': events
}
self._data.append(data)
def build_json(self):
self.req_data = {
'cmd': self._cmd,
'prefetch_count': self._prefetch_count,
'data': self._data
}
return Request.build_json(self)
class AckReq(Request):
def __init__(self, delivery_id):
self._cmd = 'event_ack'
self._delivery_id = delivery_id
def build_json(self):
self.req_data = {
'cmd': self._cmd,
'delivery_id': self._delivery_id
}
return Request.build_json(self)
class ControlReq(Request):
def __init__(self, msg_id=None):
self._cmd = 'remote_control_req'
self._msg_id = msg_id
self._data = []
def add_control_raw(self, cmd, did, mac, product_key, raw):
data = {
'cmd': cmd,
'data': {
'did': did,
'mac': mac,
'product_key': product_key,
'raw': raw
}
}
self._data.append(data)
def add_control_kv(self, cmd, did, mac, product_key, attrs):
data = {
'cmd': cmd,
'data': {
'did': did,
'mac': mac,
'product_key': product_key,
'attrs': attrs
}
}
self._data.append(data)
def build_json(self):
self.req_data = {
'cmd': self._cmd,
'msg_id': self._msg_id,
'data': self._data
}
return Request.build_json(self)
if __name__ == '__main__':
test_req = Request('ping')
print(test_req.build_json())