-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubscriber.py
77 lines (76 loc) · 3.27 KB
/
Subscriber.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
from Client import Client
from HTTP_Request import format_request
import json
""" Class responsable for connecting to server and:
* Subscribe a subscriber to a topic...
* Tell the server this subscriber will read messages....
* Read some data from its topic...
* Tell the server this subscriber will stop reading messages
"""
class Subscriber(Client):
def __init__(self, host, port, topics):
super().__init__()
self.token = 0
self.host = host
self.port = port
self.topic = topics
def subscribe_subscriber(self):
# Subscribe an actuator to a server
self.connect(self.host, self.port)
data = '{"topic": "%s"}' %(self.topic)
self.client.send(bytes(format_request(method='POST', path='/actuator/subscribe', data=data, content_type='application/json'), 'utf-8'))
response = self.client.recv(1024)
response = response.decode('utf-8')
response = json.loads(response)
self.token = response['token']
self.client.close()
# Read a message from server
def receive(self):
self.connect(self.host, self.port)
sent_data = {}
sent_data['topic'] = self.topic
sent_data['token'] = self.token
data = json.dumps(sent_data)
self.client.send(bytes(format_request(method='POST', path='/actuator/receive', data=data, content_type='application/json'), 'utf-8'))
response = self.client.recv(1024)
self.client.close()
return response
# Get the actual status of the device
def get_status(self):
self.connect(self.host, self.port)
send = {"token": self.token}
send = json.dumps(send)
self.client.send(bytes(format_request(method='GET', path='/actuator/state',
data=send, content_type='application/json'), 'utf-8'))
response = self.client.recv(1024)
response = response.decode('utf-8')
response = json.loads(response)
status = response['command']
self.client.close()
return status
# Tells the server this subscriber will start reading messages
def start_receiving(self):
self.connect(self.host, self.port)
send = {"token": self.token, "action": "start"}
send = json.dumps(send)
self.client.send(bytes(format_request(method='POST', path='/actuator/config/device',
data=send, content_type='application/json'), 'utf-8'))
response = self.client.recv(1024)
response = response.decode('utf-8')
response = json.loads(response)
status = response['status']
self.client.close()
return status
# Tells the server this subscriber will stop reading messages
def stop_receiving(self):
self.connect(self.host, self.port)
send = {"token": self.token, "action": "stop"}
send = json.dumps(send)
self.client.send(bytes(format_request(method='POST', path='/actuator/config/device',
data=send, content_type='application/json'), 'utf-8'))
response = self.client.recv(1024)
response = response.decode('utf-8')
response = json.loads(response)
status = response['status']
self.client.close()
return status