Skip to content

Commit a3fae43

Browse files
committed
get_token() in main.py now sets: self.token, api.token, broker.token
1 parent 16bccfa commit a3fae43

File tree

3 files changed

+459
-484
lines changed

3 files changed

+459
-484
lines changed

api.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,8 @@ def get_token(self, email, password, server):
6969
response = requests.post(f'{server}/api/tokens', headers=headers, json=user)
7070

7171
if self.token_handling(response) == 200:
72-
self.token = response.json()
7372
self.error = None
74-
return self.token
73+
return response.json()
7574
else:
7675
return self.error
7776

broker.py

+8-23
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
class BrokerFunctions():
77
def __init__(self):
8+
self.token = None
89
self.client = None
910

1011
# connect() --> establish connection to message broker
@@ -20,25 +21,13 @@ def __init__(self):
2021
# self.client.subscribe(f"bot/{self.token['token']['unencoded']['bot']}/#")
2122
# print('connected')
2223

23-
# def on_message(self, _client, _userdata, msg):
24-
# print('-' * 100)
25-
# # print channel
26-
# print(f'{msg.topic} ({datetime.now().strftime("%Y-%m-%d %H:%M:%S")})\n')
27-
# # print message
28-
# print(json.dumps(json.loads(msg.payload), indent=4))
24+
# def status_connect(self, client, *_args):
25+
# # Subscribe to specific channel
26+
# device_info = self.api.get('device')
27+
# device_id = device_info['id']
2928

30-
# def on_connect(client, *_args):
31-
# # subscribe to all channels
32-
# client.subscribe(f"bot/{TOKEN['token']['unencoded']['bot']}/#")
33-
# print('connected')
34-
35-
def status_connect(self, client, *_args):
36-
# Subscribe to specific channel
37-
device_info = self.api.get('device')
38-
device_id = device_info['id']
39-
40-
client.subscribe("bot/device_4652/status")
41-
print('connected via status_connect()')
29+
# client.subscribe("bot/device_4652/status")
30+
# print('connected via status_connect()')
4231

4332
def on_message(self, _client, _userdata, msg):
4433
print('-' * 100)
@@ -50,10 +39,6 @@ def on_message(self, _client, _userdata, msg):
5039
print(json.dumps(json.loads(msg.payload), indent=4))
5140

5241
def connect(self):
53-
print(self.api.token)
54-
55-
self.api.check_token()
56-
5742
self.client = mqtt.Client()
5843
self.client.username_pw_set(
5944
username=self.token['token']['unencoded']['bot'],
@@ -78,7 +63,7 @@ def disconnect(self):
7863

7964
def publish(self, message):
8065
if self.client is None:
81-
self.connect()
66+
self.connect(self.token)
8267

8368
self.client.publish(
8469
f'bot/{self.token["token"]["unencoded"]["bot"]}/from_clients',

0 commit comments

Comments
 (0)