Skip to content

Commit 8c12d5f

Browse files
Juliana MashonJuliana Mashon
Juliana Mashon
authored and
Juliana Mashon
committed
Added functions to broker_functions.py and organized comments
1 parent 903fec5 commit 8c12d5f

File tree

4 files changed

+192
-129
lines changed

4 files changed

+192
-129
lines changed

api_functions.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def set_info(self, endpoint, field, value, id=None):
4242
self.api_connect.patch(endpoint, id, new_value)
4343
return self.api_connect.get(endpoint, id)
4444

45-
def env(self, id=None, field=None, new_val=None):
45+
def env(self, id=None, field=None, new_val=None): # TODO: why are there '?'
4646
if id is None:
4747
data = self.api_connect.get('farmware_envs', id=None)
4848
print(data) # ?
@@ -86,8 +86,8 @@ def garden_size(self):
8686
f'\ty length = {length_y:.2f}\n'
8787
f'\tarea = {area:.2f}')
8888

89-
def group(self, id): ## MAKE ID OPTIONAL RETURN FULL TREE W/O ID
89+
def group(self, id): # TODO: make ID optional return full tree w/o ID
9090
return self.get_info('point_groups', id)
9191

92-
def curve(self, id): ## MAKE ID OPTIONAL RETURN FULL TREE W/O ID
92+
def curve(self, id): # TODO: make ID optional return full tree w/o ID
9393
return self.get_info('curves', id)

broker_connect.py

+31-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def on_message(self, _client, _userdata, msg):
6262
new_message = json.loads(msg.payload)
6363
self.last_message = new_message
6464

65-
def listen(self, duration, channel='#'):
65+
def listen(self, duration, channel):
6666
"""Listen to messages via message broker."""
6767

6868
if self.client is None:
@@ -79,3 +79,33 @@ def listen(self, duration, channel='#'):
7979

8080
self.client.loop_stop()
8181
self.client.disconnect()
82+
83+
## FUNCTIONS -- HIDDEN
84+
85+
def hidden_on_connect(self, _client, _userdata, _flags, _rc):
86+
# Subscribe to all channels
87+
self.client.subscribe(f"bot/{self.token['token']['unencoded']['bot']}/#")
88+
89+
def hidden_on_message(self, _client, _userdata, msg):
90+
# print channel
91+
print('-' * 100)
92+
print(f'{msg.topic} ({datetime.now().strftime("%Y-%m-%d %H:%M:%S")})\n')
93+
# print message
94+
print(json.dumps(json.loads(msg.payload), indent=4))
95+
96+
def hidden_listen(self):
97+
if self.client is None:
98+
self.connect()
99+
100+
self.client.on_connect = self.hidden_on_connect
101+
self.client.on_message = self.hidden_on_message
102+
103+
# Start loop in a separate thread
104+
self.client.loop_start()
105+
106+
# Sleep for five seconds to listen for messages
107+
time.sleep(60)
108+
109+
# Stop loop and disconnect after five seconds
110+
self.client.loop_stop()
111+
self.client.disconnect()

0 commit comments

Comments
 (0)