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

1""" 

2ToolControls class. 

3""" 

4 

5# └── functions/tools.py 

6# ├── [BROKER] verify_tool() 

7# ├── [BROKER] mount_tool() 

8# ├── [BROKER] dismount_tool() 

9# ├── [BROKER] water() 

10# └── [BROKER] dispense() 

11 

12from .broker import BrokerConnect 

13from .resources import Resources 

14 

15 

16class ToolControls(): 

17 """Tool controls class.""" 

18 

19 def __init__(self, state): 

20 self.broker = BrokerConnect(state) 

21 self.resource = Resources(state) 

22 self.state = state 

23 

24 # TODO: verify_tool() 

25 

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.") 

29 

30 lua_code = f"mount_tool(\"{tool_name}\")" 

31 

32 self.resource.lua(lua_code) 

33 

34 def dismount_tool(self): 

35 """Dismounts the currently mounted tool into assigned slot.""" 

36 self.state.print_status(description="Dismounting tool.") 

37 

38 lua_code = "dismount_tool()" 

39 

40 self.resource.lua(lua_code) 

41 

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 

59 

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}...") 

63 

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) 

73 

74 self.resource.lua(lua_code) 

75 

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}...") 

80 

81 lua_code = f"dispense({milliliters}" 

82 lua_code += self.water_and_dispense_end_string(tool_name, pin) 

83 

84 self.resource.lua(lua_code)