Repository navigation
Expand file tree
/
Copy pathHTTPRequest.py
More file actions
51 lines (37 loc) · 1.48 KB
/
Copy pathHTTPRequest.py
File metadata and controls
51 lines (37 loc) · 1.48 KB
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
from urllib import parse, request
import json
import threading
import MQTT
import WSClient
def getGateway(gatewayName):
textmod = {'limit': '20', 'textSearch': gatewayName}
textmod = parse.urlencode(textmod)
print(textmod)
header_dict = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko'}
url = 'http://47.105.120.203:30080/api/v1/deviceaccess/tenant/devices/2'
req = request.Request(url='%s%s%s' % (url, '?', textmod), headers=header_dict)
res = request.urlopen(req)
res = res.read()
#print(res.decode(encoding='utf-8'))
gatewayDict = json.loads(res.decode(encoding='utf-8'))
id = gatewayDict.get("data")[0].get("id")
print(id)
WSClient.start_websocket(id)
# getCredentialbyid(id)
def getCredentialbyid(id) :
header_dict = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko'}
url = 'http://47.105.120.203:30080/api/v1/deviceaccess/credentialbyid/'+id
req = request.Request(url=url, headers=header_dict)
res = request.urlopen(req)
res = res.read()
credentialFullMessage = json.loads(res.decode(encoding='utf-8'))
token = credentialFullMessage.get("deviceToken")
print(token)
mqttThread = MQTTThread(token)
mqttThread.start()
class MQTTThread(threading.Thread):
def __init__(self, token):
threading.Thread.__init__(self)
self.token = token
def run(self):
MQTT.mqtt_start(token=self.token)