Skip to content

Commit

Permalink
Added initial listen/subscribe to channel(s) features
Browse files Browse the repository at this point in the history
  • Loading branch information
jmashon committed Jul 13, 2024
1 parent d41defc commit 1504b4c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
7 changes: 5 additions & 2 deletions api_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ def garden_size(self):
length_y = y_steps / y_mm
area = length_x * length_y

size_value = {'x': length_x, 'y': length_y, 'area': area}
return size_value
# return print(f'Garden size:\n\tx length = {length_x:.2f}\n\ty length = {length_y:.2f}\n\tarea = {area:.2f}')
return print(f'Garden size:\n'
f'\tx length = {length_x:.4f}\n'
f'\ty length = {length_y:.3f}\n'
f'\tarea = {area:.5f}')

def group(self, id):
return self.get_info('point_groups', id)
Expand Down
29 changes: 29 additions & 0 deletions broker_connect.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import time

from datetime import datetime
import paho.mqtt.client as mqtt
Expand Down Expand Up @@ -39,3 +40,31 @@ def publish(self, message):
f'bot/{self.token["token"]["unencoded"]["bot"]}/from_clients',
payload=json.dumps(message)
)

def on_connect(self, *_args):
# Subscribe to all channels
self.client.subscribe(f"bot/{self.token['token']['unencoded']['bot']}/#")

def on_message(self, _client, _userdata, msg):
print('-'*100)
# Print channel
print(f"Channel: {msg.topic} ({datetime.now().strftime("%Y-%m-%d %H:%M:%S")})\n")
# Print message
print(json.dumps(json.loads(msg.payload), indent=4))

def listen(self):
if self.client is None:
self.connect()

self.client.on_connect = self.on_connect
self.client.on_message = self.on_message

# Start loop in a separate thread
self.client.loop_start()

# Sleep for five seconds to listen for messages
time.sleep(5)

# Stop loop and disconnect after five seconds
self.client.loop_stop()
self.client.disconnect()
9 changes: 8 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ def get_token(self, email, password, server="https://my.farm.bot"):
self.api.token = token_data
self.api.api_connect.token = token_data

self.broker.token = token_data
# self.broker.token = token_data
self.broker.broker_connect.token = token_data
self.broker.api.api_connect.token = token_data

return token_data

Expand All @@ -36,3 +37,9 @@ def disconnect(self):

def on(self, id):
self.broker.on(id)

def toggle_peripheral(self, id):
self.broker.toggle_peripheral(id)

def listen(self):
self.broker.broker_connect.listen()

0 comments on commit 1504b4c

Please sign in to comment.