Skip to content

Commit ed70a41

Browse files
Juliana MashonJuliana Mashon
Juliana Mashon
authored and
Juliana Mashon
committed
jobs related functions (not working)
1 parent f89d6d1 commit ed70a41

File tree

3 files changed

+60
-39
lines changed

3 files changed

+60
-39
lines changed

broker_connect.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ def on_connect(self, _client, _userdata, _flags, _rc, channel):
5858

5959
def on_message(self, _client, _userdata, msg):
6060
"""Update message queue with latest broker response."""
61-
62-
new_message = json.loads(msg.payload)
63-
self.last_message = new_message
61+
self.last_message = json.loads(msg.payload)
6462

6563
def listen(self, duration, channel):
6664
"""Listen to messages via message broker."""

broker_functions.py

+59-33
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
from datetime import datetime
23

34
from broker_connect import BrokerConnect
45
from api_functions import ApiFunctions
@@ -17,11 +18,8 @@ def __init__(self):
1718

1819
self.client = None
1920

20-
## INFORMATION
21-
2221
def read_status(self):
2322
# Get device status tree
24-
# Return status as json object: status[""]
2523
status_message = {
2624
**RPC_REQUEST,
2725
"body": {
@@ -35,6 +33,9 @@ def read_status(self):
3533

3634
status_tree = self.broker_connect.last_message
3735

36+
# Return status as json object: status[""]
37+
return status_tree
38+
3839
def read_sensor(self, id):
3940
# Get sensor data
4041
# Return sensor as json object: sensor[""]
@@ -59,8 +60,6 @@ def read_sensor(self, id):
5960
}]
6061
}
6162

62-
## MESSAGING
63-
6463
def message(self, message, type=None, channel=None):
6564
# Send new log message via broker
6665
# No inherent return value
@@ -93,8 +92,6 @@ def toast(self, message):
9392
# No inherent return value
9493
self.message(message, 'toast')
9594

96-
## BASIC COMMANDS
97-
9895
def wait(self, duration):
9996
# Tell bot to wait for some time
10097
# No inherent return value
@@ -169,8 +166,6 @@ def shutdown(self):
169166
self.broker_connect.publish(shutdown_message)
170167
return print("Triggered device shutdown.")
171168

172-
## MOVEMENT
173-
174169
def move(self, x, y, z):
175170
# Tell bot to move to new xyz coord
176171
# Return new xyz position as values
@@ -254,7 +249,6 @@ def axis_length(self, axis='all'):
254249

255250
def get_xyz(self):
256251
# Get current xyz coord
257-
# Return xyz position as values
258252
tree_data = self.read_status()
259253

260254
position = tree_data["position"]
@@ -263,24 +257,23 @@ def get_xyz(self):
263257
y_val = position['y']
264258
z_val = position['z']
265259

266-
return {'x': x_val, 'y': y_val, 'z': z_val}
260+
# Return xyz position as values
261+
return x_val, y_val, z_val
267262

268263
def check_position(self, user_x, user_y, user_z, tolerance):
269264
# Check current xyz coord = user xyz coord within tolerance
270265
# Return in or out of tolerance range
271266
user_values = [user_x, user_y, user_z]
272267

273-
position_data = self.get_xyz()
274-
actual_vals = [position_data['x'], position_data['y'], position_data['z']]
268+
position = self.get_xyz()
269+
actual_vals = list(position)
275270

276271
for user_value, actual_value in zip(user_values, actual_vals):
277272
if actual_value - tolerance <= user_value <= actual_value + tolerance:
278273
print("Farmbot is at position.")
279274
else:
280275
print("Farmbot is NOT at position.")
281276

282-
## PERIPHERALS
283-
284277
def control_peripheral(self, id, value, mode=None):
285278
# Change peripheral values
286279
# No inherent return value
@@ -332,8 +325,8 @@ def toggle_peripheral(self, id):
332325
def on(self, id):
333326
# Set peripheral to on
334327
# Return status
335-
peripheral_str = self.api.get_info('peripherals', id)
336-
mode = peripheral_str['mode']
328+
peripheral_str = self.api.get_info("peripherals", id)
329+
mode = peripheral_str["mode"]
337330

338331
if mode == 1:
339332
self.control_peripheral(id, 255)
@@ -345,8 +338,6 @@ def off(self, id):
345338
# Return status
346339
self.control_peripheral(id, 0)
347340

348-
## RESOURCES
349-
350341
# TODO: sort_points(points, method)
351342
# Order group of points with method
352343
# Return ???
@@ -445,8 +436,6 @@ def detect_weeds(self):
445436

446437
self.broker_connect.publish(detect_weeds_message)
447438

448-
## OTHER FUNCTIONS
449-
450439
def calibrate_camera(self): # TODO: fix "sequence_id"
451440
# Execute calibrate camera script
452441
# No inherent return value
@@ -601,24 +590,61 @@ def sequence(self, sequence_id):
601590

602591
self.broker_connect.publish(sequence_message)
603592

604-
# TODO: get_job() --> access status tree --> fetch all or single by name
605-
# Get all or single job by name
606-
# Return job as json object: job[""]
607-
# TODO: set_job() --> access status tree --> inject(?) new or edit single by name
608-
# Add new or edit single by name
609-
# Return job as json object: job[""]
610-
# TODO: complete_job() --> access status tree --> edit single by name
611-
# Set job status as 'complete'
612-
# Return job as json object: job[""]
593+
# https://developer.farm.bot/v15/lua/functions/jobs.html
613594

614595
def get_job(self, job_str=None):
615-
status_data = self.read_status().json()
596+
# Get all or single job by name
597+
status_data = self.read_status()
616598

617599
if job_str is None:
618600
jobs = status_data["jobs"]
619601
else:
620-
tree = status_data["jobs"]
621-
jobs = tree[job_str]
602+
jobs = status_data["jobs"][job_str]
603+
604+
# Return job as json object: job[""]
605+
return jobs
606+
607+
def set_job(self, job_str, field=None, new_val=None):
608+
# Add new or edit single by name
609+
status_data = self.read_status()
610+
jobs = status_data["jobs"]
611+
612+
good_jobs = json.dumps(jobs, indent=4)
613+
print(good_jobs)
614+
615+
# Check existing jobs to see if job_str exists
616+
# If job_str does not exist, append new job to end of jobs list
617+
if job_str not in jobs:
618+
jobs[job_str] = {
619+
"status": "Working", # Initialize 'status' to 'Working'
620+
"type": "unknown",
621+
"unit": "percent",
622+
"time": datetime.now().isoformat(), # Initialize 'time' to current time
623+
"updated_at": "null",
624+
"file_type": "null",
625+
'percent': 0 # Initialize 'percent' to '0'
626+
}
627+
628+
self.broker_connect.publish(jobs)
629+
630+
# Update field of job_str with new_val
631+
# if field and new_val:
632+
# jobs[job_str][field].update(new_val)
633+
634+
# Return job as json object: job[""]
635+
return None
636+
637+
def complete_job(self, job_str):
638+
status_data = self.read_status()
639+
jobs = status_data["jobs"]
640+
641+
# Set job status as 'complete' updated at current time
642+
set_complete = {
643+
"status": "Complete",
644+
"updated_at": datetime.now().isoformat()
645+
}
646+
647+
# Return job as json object: job[""]
622648

623649
def lua(self, code_snippet): # TODO: verify working
624650
# Send custom code snippet

main.py

-3
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ def get_info(self, endpoint, id=None):
4343
def set_info(self, endpoint, field, value, id=None):
4444
return self.api.set_info(endpoint, field, value, id)
4545

46-
def env(self, id=None, field=None, new_val=None):
47-
return self.api.env(id, field, new_val)
48-
4946
def group(self, id):
5047
return self.api.group(id)
5148

0 commit comments

Comments
 (0)