Skip to content

Commit

Permalink
Merge branch 'jmashon/working-branch' of https://github.com/FarmBot-L…
Browse files Browse the repository at this point in the history
…abs/sidecar-starter-pack into jmashon/working-branch
  • Loading branch information
gabrielburnworth committed Jul 18, 2024
2 parents 2ff5b02 + 5d7caa2 commit 2acdd1a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 36 deletions.
18 changes: 3 additions & 15 deletions api_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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:
Expand All @@ -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)...
55 changes: 41 additions & 14 deletions broker_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
31 changes: 26 additions & 5 deletions broker_functions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

from broker_connect import BrokerConnect
from api_functions import ApiFunctions

Expand Down Expand Up @@ -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 status_tree

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": {
Expand All @@ -47,7 +56,6 @@ def read_sensor(self, id, mode, label='---'):
}]
}

self.broker_connect.publish(read_sensor_message)
# return ...

def message(self, message, type=None, channel=None):
Expand Down Expand Up @@ -298,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):
Expand Down
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2acdd1a

Please sign in to comment.