Skip to content

Commit 2d465fe

Browse files
Juliana MashonJuliana Mashon
Juliana Mashon
authored and
Juliana Mashon
committed
Cleaned up and added comments for planned changes
1 parent 82d5abe commit 2d465fe

File tree

4 files changed

+161
-160
lines changed

4 files changed

+161
-160
lines changed

api_functions.py

+39-46
Original file line numberDiff line numberDiff line change
@@ -9,55 +9,31 @@ def __init__(self):
99
self.echo = True
1010
self.verbose = True
1111

12-
def __return_config(self, funct_name, return_value, is_json=False):
13-
"""Configure echo and verbosity of function returns."""
14-
15-
if self.echo is True:
16-
print('-' * 100)
17-
print(f'FUNCTION: {funct_name}\n')
18-
if is_json is True:
19-
readable_json = json.dumps(return_value, indent=4)
20-
print(readable_json)
21-
else:
22-
print(return_value)
23-
elif self.echo is False:
24-
pass
25-
else:
26-
print('-' * 100)
27-
return print("ERROR: Incompatible return configuration.")
28-
29-
return return_value
30-
3112
def get_token(self, email, password, server='https://my.farm.bot'):
13+
# Generate user authentication token
3214
token_str = self.api_connect.get_token(email, password, server)
33-
return self.__return_config("get_token()", token_str, is_json=True)
34-
35-
# data = get_info() and like functions will assign 'data' JSON object
36-
# data["name"] will access the field "name" and return the field value
15+
# Return token as json object: token[""]
16+
return token_str
3717

3818
def get_info(self, endpoint, id=None):
19+
# Get endpoint info
3920
endpoint_data = self.api_connect.get(endpoint, id)
40-
return self.__return_config("get_info()", endpoint_data, is_json=True)
21+
# Return endpoint info as json object: endpoint[""]
22+
return endpoint_data
4123

4224
def set_info(self, endpoint, field, value, id=None):
25+
# Edit endpoint info
4326
new_value = {
4427
field: value
4528
}
46-
4729
self.api_connect.patch(endpoint, id, new_value)
4830

49-
endpoint_data = self.api_connect.get(endpoint, id)
50-
return self.__return_config("set_info()", endpoint_data, is_json=True)
51-
52-
def env(self, id=None, field=None, new_val=None): # TODO: Fix
53-
if id is None:
54-
env_data = self.api_connect.get('farmware_envs', id=None)
55-
else:
56-
env_data = self.api_connect.get('farmware_envs', id)
57-
58-
return self.__return_config("env()", env_data, is_json=True)
31+
# Return endpoint info as json object: endpoint[""]
32+
new_endpoint_data = self.api_connect.get(endpoint, id)
33+
return new_endpoint_data
5934

6035
def log(self, message, type=None, channel=None):
36+
# Send new log message via API
6137
log_message = {
6238
"message": message,
6339
"type": type, # https://software.farm.bot/v15/app/intro/jobs-and-logs#log-types
@@ -68,13 +44,18 @@ def log(self, message, type=None, channel=None):
6844
id = None
6945

7046
self.api_connect.post(endpoint, id, log_message)
71-
# return ...
47+
48+
# No inherent return value
7249

7350
def safe_z(self):
74-
json_data = self.get_info('fbos_config')
75-
return json_data['safe_height']
51+
# Get safe z height via get_info()
52+
config_data = self.get_info('fbos_config')
53+
z_value = config_data["safe_height"]
54+
# Return safe z height as value
55+
return z_value
7656

7757
def garden_size(self):
58+
# Get garden size parameters via get_info()
7859
json_data = self.get_info('firmware_config')
7960

8061
x_steps = json_data['movement_axis_nr_steps_x']
@@ -87,13 +68,25 @@ def garden_size(self):
8768
length_y = y_steps / y_mm
8869
area = length_x * length_y
8970

90-
return print(f'Garden size:\n'
91-
f'\tx length = {length_x:.2f}\n'
92-
f'\ty length = {length_y:.2f}\n'
93-
f'\tarea = {area:.2f}')
71+
# Return garden size parameters as values
72+
return length_x, length_y, area
73+
74+
def group(self, id=None):
75+
# Get all groups or single by id
76+
if id is None:
77+
group_data = self.get_info("point_groups")
78+
else:
79+
group_data = self.get_info('point_groups', id)
80+
81+
# Return group as json object: group[""]
82+
return group_data
9483

95-
def group(self, id): # TODO: make ID optional return full tree w/o ID
96-
return self.get_info('point_groups', id)
84+
def curve(self, id=None):
85+
# Get all curves or single by id
86+
if id is None:
87+
curve_data = self.get_info("curves")
88+
else:
89+
curve_data = self.get_info('curves', id)
9790

98-
def curve(self, id): # TODO: make ID optional return full tree w/o ID
99-
return self.get_info('curves', id)
91+
# Return curve as json object: curve[""]
92+
return curve_data

broker_connect.py

-30
Original file line numberDiff line numberDiff line change
@@ -79,33 +79,3 @@ def listen(self, duration, channel):
7979

8080
self.client.loop_stop()
8181
self.client.disconnect()
82-
83-
## FUNCTIONS -- HIDDEN
84-
85-
def hidden_on_connect(self, _client, _userdata, _flags, _rc):
86-
# Subscribe to all channels
87-
self.client.subscribe(f"bot/{self.token['token']['unencoded']['bot']}/#")
88-
89-
def hidden_on_message(self, _client, _userdata, msg):
90-
# print channel
91-
print('-' * 100)
92-
print(f'{msg.topic} ({datetime.now().strftime("%Y-%m-%d %H:%M:%S")})\n')
93-
# print message
94-
print(json.dumps(json.loads(msg.payload), indent=4))
95-
96-
def hidden_listen(self):
97-
if self.client is None:
98-
self.connect()
99-
100-
self.client.on_connect = self.hidden_on_connect
101-
self.client.on_message = self.hidden_on_message
102-
103-
# Start loop in a separate thread
104-
self.client.loop_start()
105-
106-
# Sleep for five seconds to listen for messages
107-
time.sleep(60)
108-
109-
# Stop loop and disconnect after five seconds
110-
self.client.loop_stop()
111-
self.client.disconnect()

0 commit comments

Comments
 (0)