Skip to content

Commit fddfcd8

Browse files
move status prints to top of functions
1 parent 0207100 commit fddfcd8

12 files changed

+82
-54
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ bot.set_verbosity(2)
9797
| Verbosity | Example using `e_stop()` |
9898
| :--- | :--- |
9999
| `0` The function will return with no output. | No output. |
100-
| `1` A description of the action will be output with any additional results data. | `Triggered device emergency stop` |
100+
| `1` A description of the action will be output with any additional results data. | `Emergency stopping device` |
101101
| `2` The name of the function and the timestamp will be output. | `'e_stop()' called at: 2024-08-21 11:16:18.547813` |
102102

103103
### Test 1: Add a new plant to your garden

functions/basic_commands.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def wait(self, duration):
3434
def e_stop(self):
3535
"""Emergency locks (E-stops) the Farmduino microcontroller."""
3636

37-
self.state.print_status(description="Triggered device emergency stop")
37+
self.state.print_status(description="Emergency stopping device")
3838

3939
stop_message = {
4040
"kind": "emergency_lock",
@@ -47,7 +47,7 @@ def e_stop(self):
4747
def unlock(self):
4848
"""Unlocks a locked (E-stopped) device."""
4949

50-
self.state.print_status(description="Triggered device unlock")
50+
self.state.print_status(description="Unlocking device")
5151

5252
unlock_message = {
5353
"kind": "emergency_unlock",
@@ -60,7 +60,7 @@ def unlock(self):
6060
def reboot(self):
6161
"""Reboots the FarmBot OS and re-initializes the device."""
6262

63-
self.state.print_status(description="Triggered device reboot")
63+
self.state.print_status(description="Rebooting device")
6464

6565
reboot_message = {
6666
"kind": "reboot",
@@ -74,7 +74,7 @@ def reboot(self):
7474
def shutdown(self):
7575
"""Shuts down the FarmBot OS and turns the device off."""
7676

77-
self.state.print_status(description="Triggered device shutdown")
77+
self.state.print_status(description="Shutting down device")
7878

7979
shutdown_message = {
8080
"kind": "power_off",

functions/camera.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def __init__(self, state):
1818
def calibrate_camera(self):
1919
"""Performs camera calibration. This action will reset camera calibration settings."""
2020

21-
self.state.print_status(description="Triggered camera calibration")
21+
self.state.print_status(description="Calibrating camera")
2222

2323
calibrate_message = {
2424
"kind": "execute_script",
@@ -32,7 +32,7 @@ def calibrate_camera(self):
3232
def take_photo(self):
3333
"""Takes photo using the device camera and uploads it to the web app."""
3434

35-
self.state.print_status(description="Took a photo")
35+
self.state.print_status(description="Taking a photo")
3636

3737
photo_message = {
3838
"kind": "take_photo",

functions/information.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,17 @@ def api_delete(self, endpoint, database_id=None):
6767

6868
def safe_z(self):
6969
"""Returns the highest safe point along the z-axis."""
70+
self.state.print_status(description="Retrieving safe z value...")
7071

7172
config_data = self.api_get('fbos_config')
7273
z_value = config_data["safe_height"]
7374

74-
self.state.print_status(description=f"Safe z={z_value}")
75+
self.state.print_status(description=f"Safe z={z_value}", update_only=True)
7576
return z_value
7677

7778
def garden_size(self):
7879
"""Return size of garden bed."""
80+
self.state.print_status(description="Retrieving garden size...")
7981

8082
json_data = self.api_get('firmware_config')
8183

@@ -94,33 +96,36 @@ def garden_size(self):
9496
"z": z_steps / z_mm,
9597
}
9698

97-
self.state.print_status(endpoint_json=garden_size)
99+
self.state.print_status(endpoint_json=garden_size, update_only=True)
98100
return garden_size
99101

100102
def group(self, group_id=None):
101103
"""Returns all group info or single by id."""
104+
self.state.print_status(description="Retrieving group information...")
102105

103106
if group_id is None:
104107
group_data = self.api_get("point_groups")
105108
else:
106109
group_data = self.api_get('point_groups', group_id)
107110

108-
self.state.print_status(endpoint_json=group_data)
111+
self.state.print_status(endpoint_json=group_data, update_only=True)
109112
return group_data
110113

111114
def curve(self, curve_id=None):
112115
"""Returns all curve info or single by id."""
116+
self.state.print_status(description="Retrieving curve information...")
113117

114118
if curve_id is None:
115119
curve_data = self.api_get("curves")
116120
else:
117121
curve_data = self.api_get('curves', curve_id)
118122

119-
self.state.print_status(endpoint_json=curve_data)
123+
self.state.print_status(endpoint_json=curve_data, update_only=True)
120124
return curve_data
121125

122126
def measure_soil_height(self):
123127
"""Use the camera to measure the soil height at the current location."""
128+
self.state.print_status(description="Measuring soil height...")
124129

125130
measure_soil_height_message = {
126131
"kind": "execute_script",

functions/jobs.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def __init__(self, state):
2222

2323
def get_job(self, job_str=None):
2424
"""Retrieves the status or details of the specified job."""
25+
self.state.print_status(description="Retrieving job data...")
2526

2627
status_data = self.info.read_status()
2728

@@ -36,11 +37,12 @@ def get_job(self, job_str=None):
3637
else:
3738
jobs = status_data["jobs"][job_str]
3839

39-
self.state.print_status(endpoint_json=jobs)
40+
self.state.print_status(endpoint_json=jobs, update_only=True)
4041
return jobs
4142

4243
def set_job(self, job_str, status_message, value):
4344
"""Initiates or modifies job with given parameters."""
45+
self.state.print_status(description=f"Marking job {job_str} as {value}% {status_message}.")
4446

4547
lua_code = f"""
4648
local job_name = "{job_str}"
@@ -55,15 +57,12 @@ def set_job(self, job_str, status_message, value):
5557

5658
self.resource.lua(lua_code)
5759

58-
self.state.print_status(description=f"Marked job {job_str} as {value}% complete.")
59-
6060
def complete_job(self, job_str):
6161
"""Marks job as completed and triggers any associated actions."""
62+
self.state.print_status(description=f"Marking job {job_str} as `complete`.")
6263

6364
lua_code = f"""
6465
complete_job("{job_str}")
6566
"""
6667

6768
self.resource.lua(lua_code)
68-
69-
self.state.print_status(description=f"Marked job {job_str} as `complete`.")

functions/messages.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def __init__(self, state):
3636

3737
def log(self, message_str, message_type="info", channel="ticker"):
3838
"""Sends new log message via the API."""
39+
self.state.print_status(description="Sending new log message to the API.")
3940

4041
validate_log_options(message_type, channel)
4142

@@ -47,10 +48,9 @@ def log(self, message_str, message_type="info", channel="ticker"):
4748

4849
self.info.api_post("logs", log_message)
4950

50-
self.state.print_status(description="New log message sent via API.")
51-
5251
def message(self, message_str, message_type="info", channel="ticker"):
5352
"""Sends new log message via the message broker."""
53+
self.state.print_status(description="Sending new log message to the message broker.")
5454

5555
validate_log_options(message_type, channel)
5656

@@ -70,18 +70,14 @@ def message(self, message_str, message_type="info", channel="ticker"):
7070

7171
self.broker.publish(message)
7272

73-
self.state.print_status(description="New log message sent via message broker.")
74-
7573
def debug(self, message_str):
7674
"""Sends debug message used for developer information or troubleshooting."""
75+
self.state.print_status(description="Sending debug message to the message broker.")
7776

7877
self.message(message_str, "debug", "ticker")
7978

80-
self.state.print_status(description="New debug message sent via message broker.")
81-
8279
def toast(self, message_str):
8380
"""Sends a message that pops up on the user interface briefly."""
81+
self.state.print_status(description="Sending toast message to the message broker.")
8482

8583
self.message(message_str, "info", "toast")
86-
87-
self.state.print_status(description="New toast message sent via message broker.")

functions/movements.py

+27-10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
from .broker import BrokerConnect
1414
from .information import Information
1515

16+
AXES = ["x", "y", "z", "all"]
17+
18+
def validate_axis(axis):
19+
"""Validate axis."""
20+
if axis not in AXES:
21+
raise ValueError(f"Invalid axis: {axis} not in {AXES}")
22+
1623
class MovementControls():
1724
"""MovementControls class."""
1825
def __init__(self, state):
@@ -22,6 +29,7 @@ def __init__(self, state):
2229

2330
def move(self, x, y, z):
2431
"""Moves to the specified (x, y, z) coordinate."""
32+
self.state.print_status(description=f"Moving to ({x}, {y}, {z}).")
2533

2634
def axis_overwrite(axis, value):
2735
return {
@@ -49,11 +57,11 @@ def axis_overwrite(axis, value):
4957

5058
self.broker.publish(move_message)
5159

52-
self.state.print_status(description=f"Moved to coordinates: ({x}, {y}, {z}).")
53-
return x, y, z
54-
5560
def set_home(self, axis="all"):
5661
"""Sets the current position as the home position for a specific axis."""
62+
self.state.print_status(description="Setting home position")
63+
64+
validate_axis(axis)
5765

5866
set_home_message = {
5967
"kind": "zero",
@@ -63,10 +71,11 @@ def set_home(self, axis="all"):
6371
}
6472
self.broker.publish(set_home_message)
6573

66-
self.state.print_status(description="Updated home coordinate.")
67-
6874
def find_home(self, axis="all", speed=100):
6975
"""Moves the device to the home position for a specified axis."""
76+
self.state.print_status(description="Finding home position")
77+
78+
validate_axis(axis)
7079

7180
if speed > 100 or speed < 1:
7281
error = "ERROR: Speed constrained to 1-100."
@@ -83,10 +92,11 @@ def find_home(self, axis="all", speed=100):
8392
}
8493
self.broker.publish(message)
8594

86-
self.state.print_status(description="Moved to home position.")
87-
8895
def find_axis_length(self, axis="all"):
8996
"""Finds the length of a specified axis."""
97+
self.state.print_status(description="Finding axis length")
98+
99+
validate_axis(axis)
90100

91101
find_axis_length_message = {
92102
"kind": "calibrate",
@@ -99,6 +109,7 @@ def find_axis_length(self, axis="all"):
99109

100110
def get_xyz(self):
101111
"""Returns the current (x, y, z) coordinates of the FarmBot."""
112+
self.state.print_status(description="Getting current coordinates")
102113

103114
tree_data = self.info.read_status()
104115
if tree_data is None:
@@ -112,13 +123,15 @@ def get_xyz(self):
112123
y_val = position["y"]
113124
z_val = position["z"]
114125

115-
self.state.print_status(description=f"Current coordinate: ({x_val}, {y_val}, {z_val}).")
126+
self.state.print_status(description=f"Current position: {position}.", update_only=True)
116127
return position
117128

118129
def check_position(self, user_x, user_y, user_z, tolerance):
119130
"""Verifies position of the FarmBot within specified tolerance range."""
120131

121132
user_values = {'x': user_x, 'y': user_y, 'z': user_z}
133+
self.state.print_status(description=f"Checking if position is {user_values} with tolerance: {tolerance}.")
134+
122135
actual_vals = self.get_xyz()
123136

124137
if actual_vals is None:
@@ -132,8 +145,12 @@ def check_position(self, user_x, user_y, user_z, tolerance):
132145
user_value = user_values[axis]
133146
actual_value = actual_vals[axis]
134147
if not actual_value - tolerance <= user_value <= actual_value + tolerance:
135-
self.state.print_status(description=f"Farmbot is NOT at position\n Current coordinates: ({x_val}, {y_val}, {z_val}).")
148+
self.state.print_status(
149+
description=f"Farmbot is NOT at position.\n Current position: {actual_vals}.",
150+
update_only=True)
136151
return False
137152

138-
self.state.print_status(description=f"Farmbot is at position: ({x_val}, {y_val}, {z_val}).")
153+
self.state.print_status(
154+
description=f"Farmbot is at position: {actual_vals}.",
155+
update_only=True)
139156
return True

functions/peripherals.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def __init__(self, state):
2121

2222
def control_servo(self, pin, angle):
2323
"""Set servo angle between 0-100 degrees."""
24+
self.state.print_status(description=f"Setting servo angle to {angle}.")
2425

2526
if angle < 0 or angle > 180:
2627
error = "ERROR: Servo angle constrained to 0-180 degrees."
@@ -38,7 +39,6 @@ def control_servo(self, pin, angle):
3839

3940
self.broker.publish(control_servo_message)
4041

41-
self.state.print_status(description=f"Set servo angle to {angle}.")
4242
return
4343

4444
def control_peripheral(self, peripheral_name, value, mode=None):

functions/resources.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def __init__(self, state):
4747

4848
def sequence(self, sequence_name):
4949
"""Executes a predefined sequence."""
50-
self.state.print_status(description="Triggering {sequence_name} sequence.")
50+
self.state.print_status(description="Running {sequence_name} sequence.")
5151

5252
sequence = self.info.get_resource_by_name("sequences", sequence_name, "name")
5353
if sequence is None:
@@ -140,6 +140,7 @@ def get_seed_tray_cell(self, tray_name, tray_cell):
140140

141141
def detect_weeds(self):
142142
"""Scans the garden to detect weeds."""
143+
self.state.print_status(description="Detecting weeds...")
143144

144145
detect_weeds_message = {
145146
"kind": "execute_script",
@@ -152,6 +153,7 @@ def detect_weeds(self):
152153

153154
def lua(self, code_snippet):
154155
"""Executes custom Lua code snippets to perform complex tasks or automations."""
156+
self.state.print_status(description="Running Lua code")
155157

156158
lua_message = {
157159
"kind": "lua",
@@ -162,12 +164,10 @@ def lua(self, code_snippet):
162164

163165
self.broker.publish(lua_message)
164166

165-
self.state.print_status(description="Triggered lua code execution .")
166-
167167
def if_statement(self, variable, operator, value, then_sequence_name=None, else_sequence_name=None, named_pin_type=None):
168168
"""Performs conditional check and executes actions based on the outcome."""
169169

170-
self.state.print_status(description="Triggering if statement.")
170+
self.state.print_status(description="Executing if statement.")
171171

172172
validate_if_statement_args(named_pin_type, variable, operator)
173173
if named_pin_type is not None:
@@ -213,7 +213,7 @@ def if_statement(self, variable, operator, value, then_sequence_name=None, else_
213213

214214
def assertion(self, code, assertion_type, recovery_sequence_name=None):
215215
"""Evaluates an expression."""
216-
self.state.print_status(description="Triggering assertion.")
216+
self.state.print_status(description="Executing assertion.")
217217

218218
validate_assertion_type(assertion_type)
219219

0 commit comments

Comments
 (0)