Coverage for farmbot/functions/tools.py: 100%
41 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-09-12 12:03 -0700
« prev ^ index » next coverage.py v7.4.4, created at 2024-09-12 12:03 -0700
1"""
2ToolControls class.
3"""
5# └── functions/tools.py
6# ├── [BROKER] verify_tool()
7# ├── [BROKER] mount_tool()
8# ├── [BROKER] dismount_tool()
9# ├── [BROKER] water()
10# └── [BROKER] dispense()
12from .broker import BrokerConnect
13from .resources import Resources
16class ToolControls():
17 """Tool controls class."""
19 def __init__(self, state):
20 self.broker = BrokerConnect(state)
21 self.resource = Resources(state)
22 self.state = state
24 # TODO: verify_tool()
26 def mount_tool(self, tool_name):
27 """Mounts the given tool and pulls it out of assigned slot."""
28 self.state.print_status(description=f"Mounting {tool_name} tool.")
30 lua_code = f"mount_tool(\"{tool_name}\")"
32 self.resource.lua(lua_code)
34 def dismount_tool(self):
35 """Dismounts the currently mounted tool into assigned slot."""
36 self.state.print_status(description="Dismounting tool.")
38 lua_code = "dismount_tool()"
40 self.resource.lua(lua_code)
42 @staticmethod
43 def water_and_dispense_end_string(tool_name=None, pin=None):
44 """Prepares water or dispense end string."""
45 string = ""
46 if tool_name is not None:
47 string += ", {"
48 string += f"tool_name = \"{tool_name}\""
49 if pin is not None:
50 string += ", "
51 if tool_name is None:
52 string += "{"
53 string += f"pin = {pin}"
54 if tool_name is not None or pin is not None:
55 string += "})"
56 else:
57 string += ")"
58 return string
60 def water(self, plant_id, tool_name=None, pin=None):
61 """Moves to and waters plant based on age and assigned watering curve."""
62 self.state.print_status(description=f"Watering plant {plant_id}...")
64 lua_code = f"""
65 plant = api({
66 method = "GET",
67 url = "/api/points/{plant_id}"
68 } )
69 water(plant
70 """
71 lua_code = lua_code.strip()
72 lua_code += self.water_and_dispense_end_string(tool_name, pin)
74 self.resource.lua(lua_code)
76 def dispense(self, milliliters, tool_name=None, pin=None):
77 """Dispenses user-defined amount of liquid in milliliters."""
78 self.state.print_status(
79 description=f"Dispensing {milliliters} from tool {tool_name}...")
81 lua_code = f"dispense({milliliters}"
82 lua_code += self.water_and_dispense_end_string(tool_name, pin)
84 self.resource.lua(lua_code)