1
1
import json
2
+ from datetime import datetime
2
3
3
4
from broker_connect import BrokerConnect
4
5
from api_functions import ApiFunctions
@@ -17,11 +18,8 @@ def __init__(self):
17
18
18
19
self .client = None
19
20
20
- ## INFORMATION
21
-
22
21
def read_status (self ):
23
22
# Get device status tree
24
- # Return status as json object: status[""]
25
23
status_message = {
26
24
** RPC_REQUEST ,
27
25
"body" : {
@@ -35,6 +33,9 @@ def read_status(self):
35
33
36
34
status_tree = self .broker_connect .last_message
37
35
36
+ # Return status as json object: status[""]
37
+ return status_tree
38
+
38
39
def read_sensor (self , id ):
39
40
# Get sensor data
40
41
# Return sensor as json object: sensor[""]
@@ -59,8 +60,6 @@ def read_sensor(self, id):
59
60
}]
60
61
}
61
62
62
- ## MESSAGING
63
-
64
63
def message (self , message , type = None , channel = None ):
65
64
# Send new log message via broker
66
65
# No inherent return value
@@ -93,8 +92,6 @@ def toast(self, message):
93
92
# No inherent return value
94
93
self .message (message , 'toast' )
95
94
96
- ## BASIC COMMANDS
97
-
98
95
def wait (self , duration ):
99
96
# Tell bot to wait for some time
100
97
# No inherent return value
@@ -169,8 +166,6 @@ def shutdown(self):
169
166
self .broker_connect .publish (shutdown_message )
170
167
return print ("Triggered device shutdown." )
171
168
172
- ## MOVEMENT
173
-
174
169
def move (self , x , y , z ):
175
170
# Tell bot to move to new xyz coord
176
171
# Return new xyz position as values
@@ -254,7 +249,6 @@ def axis_length(self, axis='all'):
254
249
255
250
def get_xyz (self ):
256
251
# Get current xyz coord
257
- # Return xyz position as values
258
252
tree_data = self .read_status ()
259
253
260
254
position = tree_data ["position" ]
@@ -263,24 +257,23 @@ def get_xyz(self):
263
257
y_val = position ['y' ]
264
258
z_val = position ['z' ]
265
259
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
267
262
268
263
def check_position (self , user_x , user_y , user_z , tolerance ):
269
264
# Check current xyz coord = user xyz coord within tolerance
270
265
# Return in or out of tolerance range
271
266
user_values = [user_x , user_y , user_z ]
272
267
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 )
275
270
276
271
for user_value , actual_value in zip (user_values , actual_vals ):
277
272
if actual_value - tolerance <= user_value <= actual_value + tolerance :
278
273
print ("Farmbot is at position." )
279
274
else :
280
275
print ("Farmbot is NOT at position." )
281
276
282
- ## PERIPHERALS
283
-
284
277
def control_peripheral (self , id , value , mode = None ):
285
278
# Change peripheral values
286
279
# No inherent return value
@@ -332,8 +325,8 @@ def toggle_peripheral(self, id):
332
325
def on (self , id ):
333
326
# Set peripheral to on
334
327
# 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" ]
337
330
338
331
if mode == 1 :
339
332
self .control_peripheral (id , 255 )
@@ -345,8 +338,6 @@ def off(self, id):
345
338
# Return status
346
339
self .control_peripheral (id , 0 )
347
340
348
- ## RESOURCES
349
-
350
341
# TODO: sort_points(points, method)
351
342
# Order group of points with method
352
343
# Return ???
@@ -445,8 +436,6 @@ def detect_weeds(self):
445
436
446
437
self .broker_connect .publish (detect_weeds_message )
447
438
448
- ## OTHER FUNCTIONS
449
-
450
439
def calibrate_camera (self ): # TODO: fix "sequence_id"
451
440
# Execute calibrate camera script
452
441
# No inherent return value
@@ -601,24 +590,61 @@ def sequence(self, sequence_id):
601
590
602
591
self .broker_connect .publish (sequence_message )
603
592
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
613
594
614
595
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 ()
616
598
617
599
if job_str is None :
618
600
jobs = status_data ["jobs" ]
619
601
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[""]
622
648
623
649
def lua (self , code_snippet ): # TODO: verify working
624
650
# Send custom code snippet
0 commit comments