From 229f1fa8196a5f9ba4b7d65de37c3d3bf7c5057c Mon Sep 17 00:00:00 2001 From: Juliana Mashon Date: Thu, 18 Jul 2024 11:02:40 -0700 Subject: [PATCH 1/2] Has listen(duration)--working, not implemented --- api_functions.py | 18 +++------------ broker_connect.py | 55 +++++++++++++++++++++++++++++++++------------ broker_functions.py | 21 +++++++++++++---- main.py | 4 ++-- 4 files changed, 63 insertions(+), 35 deletions(-) diff --git a/api_functions.py b/api_functions.py index bc63825..83fe537 100644 --- a/api_functions.py +++ b/api_functions.py @@ -14,7 +14,6 @@ def get_token(self, email, password, server='https://my.farm.bot'): def get_info(self, endpoint, id=None): return self.api_connect.get(endpoint, id) - # return self.api_connect.get(endpoint, id)... def set_info(self, endpoint, field, value, id=None): new_value = { @@ -23,7 +22,6 @@ def set_info(self, endpoint, field, value, id=None): self.api_connect.patch(endpoint, id, new_value) return self.api_connect.get(endpoint, id) - # return self.api_connect.get(endpoint, id)... def env(self, id=None, field=None, new_val=None): if id is None: @@ -50,37 +48,27 @@ def log(self, message, type=None, channel=None): def safe_z(self): json_data = self.get_info('fbos_config') return json_data['safe_height'] - # return json_data['safe_height']... def garden_size(self): json_data = self.get_info('firmware_config') - # Get x axis length in steps x_steps = json_data['movement_axis_nr_steps_x'] - - # Get number of steps per millimeter on the x axis x_mm = json_data['movement_step_per_mm_x'] - # Get y axis length in steps y_steps = json_data['movement_axis_nr_steps_y'] - - # Get number of steps per millimeter on the y axis y_mm = json_data['movement_step_per_mm_y'] length_x = x_steps / x_mm length_y = y_steps / y_mm area = length_x * length_y - # 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}') + f'\tx length = {length_x:.2f}\n' + f'\ty length = {length_y:.2f}\n' + f'\tarea = {area:.2f}') def group(self, id): return self.get_info('point_groups', id) - # return self.get_info('point_groups', id)... def curve(self, id): return self.get_info('curves', id) - # return self.get_info('curves', id)... diff --git a/broker_connect.py b/broker_connect.py index 9cf1320..6d1434c 100644 --- a/broker_connect.py +++ b/broker_connect.py @@ -9,6 +9,8 @@ def __init__(self): self.token = None self.client = None + self.last_message = None + def connect(self): """Establish persistent connection with message broker.""" self.client = mqtt.Client() @@ -41,30 +43,55 @@ def publish(self, message): 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_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'{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() + + def on_connect(self, _client, _userdata, _flags, _rc, channel): + # Subscribe to specified channel + self.client.subscribe(f"bot/{self.token['token']['unencoded']['bot']}/{channel}") def on_message(self, _client, _userdata, msg): - print('-' * 100) - # print channel - print(f'{msg.topic} ({datetime.now().strftime("%Y-%m-%d %H:%M:%S")})\n') - # print message - print(json.dumps(json.loads(msg.payload), indent=4)) + message = json.loads(msg.payload) + + self.last_message = message # Update last_message - def listen(self): + def listen(self, duration, channel='#'): if self.client is None: self.connect() - self.client.on_connect = self.on_connect + # Wrap on_connect and on_message methods to pass channel argument + self.client.on_connect = lambda client, userdata, flags, rc: self.on_connect(client, userdata, flags, rc, channel) 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) + # Listen to messages for duration (seconds) + time.sleep(duration) - # Stop loop and disconnect after five seconds self.client.loop_stop() self.client.disconnect() diff --git a/broker_functions.py b/broker_functions.py index e138a7f..0d36bc4 100644 --- a/broker_functions.py +++ b/broker_functions.py @@ -1,3 +1,5 @@ +import json + from broker_connect import BrokerConnect from api_functions import ApiFunctions @@ -26,16 +28,23 @@ def read_status(self): } self.broker_connect.publish(status_message) - # return ... + self.broker_connect.listen(5, 'status') + + status_tree = self.broker_connect.last_message + + return print(json.dumps(status_tree, indent=4)) + + def read_sensor(self, id): + peripheral_str = self.api.get_info('peripherals', id) + mode = peripheral_str['mode'] - def read_sensor(self, id, mode, label='---'): read_sensor_message = { **RPC_REQUEST, "body": [{ "kind": "read_pin", "args": { "pin_mode": mode, - "label": label, + "label": '---', "pin_number": { "kind": "named_pin", "args": { @@ -48,7 +57,11 @@ def read_sensor(self, id, mode, label='---'): } self.broker_connect.publish(read_sensor_message) - # return ... + self.broker_connect.listen(5, 'status') + + sensor_data = self.broker_connect.last_message + + return print(sensor_data) def message(self, message, type=None, channel=None): message_message = { diff --git a/main.py b/main.py index 6888c99..91cb055 100644 --- a/main.py +++ b/main.py @@ -50,8 +50,8 @@ def curve(self, id): def read_status(self): return self.broker.read_status() - def read_sensor(self, id, mode, label='---'): - return self.broker.read_sensor(id, mode, label) + def read_sensor(self, id): + return self.broker.read_sensor(id) def message(self, message, type=None, channel=None): return self.broker.message(message, type, channel) From 5d7caa24a4020271cf431be932e4db66cc09e2c2 Mon Sep 17 00:00:00 2001 From: Juliana Mashon Date: Thu, 18 Jul 2024 11:08:59 -0700 Subject: [PATCH 2/2] Added get_xyz() --- broker_functions.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/broker_functions.py b/broker_functions.py index 0d36bc4..387c3c4 100644 --- a/broker_functions.py +++ b/broker_functions.py @@ -32,7 +32,7 @@ def read_status(self): status_tree = self.broker_connect.last_message - return print(json.dumps(status_tree, indent=4)) + return status_tree def read_sensor(self, id): peripheral_str = self.api.get_info('peripherals', id) @@ -56,12 +56,7 @@ def read_sensor(self, id): }] } - self.broker_connect.publish(read_sensor_message) - self.broker_connect.listen(5, 'status') - - sensor_data = self.broker_connect.last_message - - return print(sensor_data) + # return ... def message(self, message, type=None, channel=None): message_message = { @@ -311,7 +306,20 @@ def detect_weeds(self): self.broker_connect.publish(detect_weeds_message) # return ... - # get_xyz() --> requires read_status() --> LUA + def get_xyz(self): + tree_data = self.read_status() + + position = tree_data["position"] + + x_val = position['x'] + y_val = position['y'] + z_val = position['z'] + + return print(f'Garden size:\n' + f'\tx = {x_val:.2f}\n' + f'\ty = {y_val:.2f}\n' + f'\tz = {z_val:.2f}') + # check_position() --> requires read_status() --> LUA def move(self, x, y, z):