Skip to content

Commit

Permalink
rename only_specific_beds, basic automation api
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasBaecker committed Jan 31, 2025
1 parent 92f84df commit 7415691
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
32 changes: 32 additions & 0 deletions field_friend/api/automation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from fastapi import Request
from nicegui import app

from field_friend.system import System


class Automation:
def __init__(self, system: System) -> None:
self.system = system

@app.post('/api/automation/start')
async def start_automation(request: Request):
# TODO add error handling
self.system.current_navigation = self.system.field_navigation
field_data = await request.json()
self.system.field_navigation.field_id = field_data['field_id']
self.system.field_provider.only_specific_beds = field_data['only_specific_beds']
self.system.field_provider.selected_beds = field_data['selected_beds']
self.system.automator.start()
return {'automation_started': True}

@app.post('/api/automation/stop')
def stop_automation():
assert self.system.automator.is_running
self.system.automator.stop(because='API call')
return {'automation_stopped': True}

@app.post('/api/automation/pause')
def pause_automation():
assert self.system.automator.is_running
self.system.automator.pause(because='API call')
return {'automation_paused': True}
10 changes: 5 additions & 5 deletions field_friend/automations/field_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self) -> None:
self.FIELD_SELECTED = rosys.event.Event()
"""A field has been selected."""

self._only_specific_beds: bool = False
self.only_specific_beds: bool = False
self._selected_beds: list[int] = []

@property
Expand Down Expand Up @@ -54,7 +54,7 @@ def invalidate(self) -> None:
self.FIELDS_CHANGED.emit()
if self.selected_field and self.selected_field not in self.fields:
self.selected_field = None
self._only_specific_beds = False
self.only_specific_beds = False
self.clear_selected_beds()
self.FIELD_SELECTED.emit()

Expand Down Expand Up @@ -143,7 +143,7 @@ def update_field_parameters(self, *,
self.invalidate()

def clear_selected_beds(self) -> None:
self._only_specific_beds = False
self.only_specific_beds = False
self.selected_beds = []

def get_rows_to_work_on(self) -> list[Row]:
Expand All @@ -152,7 +152,7 @@ def get_rows_to_work_on(self) -> list[Row]:
return []
if self.selected_field.bed_count == 1:
return self.selected_field.rows
if not self._only_specific_beds:
if not self.only_specific_beds:
return self.selected_field.rows
if len(self.selected_beds) == 0:
self.log.warning('No beds selected. Cannot get rows to work on.')
Expand All @@ -165,7 +165,7 @@ def get_rows_to_work_on(self) -> list[Row]:
return rows_to_work_on

def is_row_in_selected_beds(self, row_index: int) -> bool:
if not self._only_specific_beds:
if not self.only_specific_beds:
return True
if self.selected_field is None:
return False
Expand Down
2 changes: 1 addition & 1 deletion field_friend/interface/components/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def field_setting(self) -> None:
if self.field_provider.selected_field.bed_count > 1:
with ui.row().classes('w-full'):
beds_checkbox = ui.checkbox('Select specific beds').classes('w-full') \
.bind_value(self.system.field_provider, '_only_specific_beds')
.bind_value(self.system.field_provider, 'only_specific_beds')
with ui.row().bind_visibility_from(beds_checkbox, 'value').classes('w-full'):
ui.select(list(range(1, int(self.field_provider.selected_field.bed_count) + 1)),
multiple=True, label='selected beds', clearable=True) \
Expand Down
6 changes: 3 additions & 3 deletions tests/test_navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ async def test_complete_field_with_selected_beds(system: System, field_with_beds
assert ROBOT_GEO_START_POSITION is not None
assert system.gnss.last_measurement.point.distance(ROBOT_GEO_START_POSITION) < 0.01
system.field_provider.select_field(field_with_beds.id)
system.field_provider._only_specific_beds = True
system.field_provider.only_specific_beds = True
system.field_provider.selected_beds = [1, 3]
system.current_navigation = system.field_navigation
system.automator.start()
Expand All @@ -505,7 +505,7 @@ async def test_complete_field_without_second_bed(system: System, field_with_beds
assert ROBOT_GEO_START_POSITION is not None
assert system.gnss.last_measurement.point.distance(ROBOT_GEO_START_POSITION) < 0.01
system.field_provider.select_field(field_with_beds.id)
system.field_provider._only_specific_beds = True
system.field_provider.only_specific_beds = True
system.field_provider.selected_beds = [1, 3, 4]
system.current_navigation = system.field_navigation
system.automator.start()
Expand All @@ -522,7 +522,7 @@ async def test_field_with_first_row_excluded(system: System, field_with_beds: Fi
assert system.gnss.last_measurement.point.distance(ROBOT_GEO_START_POSITION) < 0.01

system.field_provider.select_field(field_with_beds.id)
system.field_provider._only_specific_beds = True
system.field_provider.only_specific_beds = True
system.field_provider.selected_beds = [2, 3, 4] # Exclude bed 1

system.current_navigation = system.field_navigation
Expand Down

0 comments on commit 7415691

Please sign in to comment.