Coverage for farmbot_sidecar_starter_pack/main.py: 100%

130 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-08-30 13:00 -0700

1""" 

2Farmbot class. 

3""" 

4 

5from .state import State 

6from .functions.api import ApiConnect 

7from .functions.basic_commands import BasicCommands 

8from .functions.broker import BrokerConnect 

9from .functions.camera import Camera 

10from .functions.information import Information 

11from .functions.jobs import JobHandling 

12from .functions.messages import MessageHandling 

13from .functions.movements import MovementControls 

14from .functions.peripherals import Peripherals 

15from .functions.resources import Resources 

16from .functions.tools import ToolControls 

17 

18class Farmbot(): 

19 """Farmbot class.""" 

20 def __init__(self): 

21 self.state = State() 

22 

23 # Initialize other components without the token initially 

24 self.api = ApiConnect(self.state) 

25 self.basic = BasicCommands(self.state) 

26 self.broker = BrokerConnect(self.state) 

27 self.camera = Camera(self.state) 

28 self.info = Information(self.state) 

29 self.jobs = JobHandling(self.state) 

30 self.messages = MessageHandling(self.state) 

31 self.movements = MovementControls(self.state) 

32 self.peripherals = Peripherals(self.state) 

33 self.resources = Resources(self.state) 

34 self.tools = ToolControls(self.state) 

35 

36 def set_verbosity(self, value): 

37 """Set output verbosity level.""" 

38 self.state.verbosity = value 

39 

40 # api.py 

41 

42 def get_token(self, email, password, server="https://my.farm.bot"): 

43 """Get FarmBot authorization token. Server is 'https://my.farm.bot' by default.""" 

44 return self.api.get_token(email, password, server) 

45 

46 def set_token(self, token): 

47 """Set FarmBot authorization token.""" 

48 self.state.token = token 

49 

50 # basic_commands.py 

51 

52 def wait(self, duration): 

53 """Pauses execution for a certain number of milliseconds.""" 

54 return self.basic.wait(duration) 

55 

56 def e_stop(self): 

57 """Emergency locks (E-stops) the Farmduino microcontroller.""" 

58 return self.basic.e_stop() 

59 

60 def unlock(self): 

61 """Unlocks a locked (E-stopped) device.""" 

62 return self.basic.unlock() 

63 

64 def reboot(self): 

65 """Reboots the FarmBot OS and re-initializes the device.""" 

66 return self.basic.reboot() 

67 

68 def shutdown(self): 

69 """Shuts down the FarmBot OS and turns the device off.""" 

70 return self.basic.shutdown() 

71 

72 # broker.py 

73 

74 def connect_broker(self): 

75 """Establish persistent connection to send messages via message broker.""" 

76 return self.broker.connect() 

77 

78 def disconnect_broker(self): 

79 """Disconnect from the message broker.""" 

80 return self.broker.disconnect() 

81 

82 def listen(self, duration, channel="#"): 

83 """Listen to a message broker channel for the provided duration in seconds.""" 

84 return self.broker.listen(duration, channel) 

85 

86 # camera.py 

87 

88 def calibrate_camera(self): 

89 """Performs camera calibration. This action will reset camera calibration settings.""" 

90 return self.camera.calibrate_camera() 

91 

92 def take_photo(self): 

93 """Takes photo using the device camera and uploads it to the web app.""" 

94 return self.camera.take_photo() 

95 

96 # information.py 

97 

98 def api_get(self, endpoint, database_id=None): 

99 """Get information about a specific endpoint.""" 

100 return self.info.api_get(endpoint, database_id) 

101 

102 def api_patch(self, endpoint, new_data, database_id=None): 

103 """Change information contained within an endpoint.""" 

104 return self.info.api_patch(endpoint, new_data, database_id) 

105 

106 def api_post(self, endpoint, new_data): 

107 """Create new information contained within an endpoint.""" 

108 return self.info.api_post(endpoint, new_data) 

109 

110 def api_delete(self, endpoint, id=None): 

111 """Delete information contained within an endpoint.""" 

112 return self.info.api_delete(endpoint, id) 

113 

114 def safe_z(self): 

115 """Returns the highest safe point along the z-axis.""" 

116 return self.info.safe_z() 

117 

118 def garden_size(self): 

119 """Returns size of garden bed.""" 

120 return self.info.garden_size() 

121 

122 def group(self, group_id=None): 

123 """Returns all group info or single by id.""" 

124 return self.info.group(group_id) 

125 

126 def curve(self, curve_id=None): 

127 """Returns all curve info or single by id.""" 

128 return self.info.curve(curve_id) 

129 

130 def measure_soil_height(self): 

131 """Use the camera to determine soil height at the current location.""" 

132 return self.info.measure_soil_height() 

133 

134 def read_status(self): 

135 """Returns the FarmBot status tree.""" 

136 return self.info.read_status() 

137 

138 def read_sensor(self, sensor_name): 

139 """Reads the given sensor.""" 

140 return self.info.read_sensor(sensor_name) 

141 

142 # jobs.py 

143 

144 def get_job(self, job_str=None): 

145 """Retrieves the status or details of the specified job.""" 

146 return self.jobs.get_job(job_str) 

147 

148 def set_job(self, job_str, status_message, value): 

149 """Initiates or modifies job with given parameters.""" 

150 return self.jobs.set_job(job_str, status_message, value) 

151 

152 def complete_job(self, job_str): 

153 """Marks job as completed and triggers any associated actions.""" 

154 return self.jobs.complete_job(job_str) 

155 

156 # messages.py 

157 

158 def log(self, message_str, message_type="info", channel="ticker"): 

159 """Sends new log message via the API.""" 

160 return self.messages.log(message_str, message_type, channel) 

161 

162 def message(self, message_str, message_type="info", channel="ticker"): 

163 """Sends new log message via the message broker.""" 

164 return self.messages.message(message_str, message_type, channel) 

165 

166 def debug(self, message_str): 

167 """Sends debug message used for developer information or troubleshooting.""" 

168 return self.messages.debug(message_str) 

169 

170 def toast(self, message_str): 

171 """Sends a message that pops up on the user interface briefly.""" 

172 return self.messages.toast(message_str) 

173 

174 # movements.py 

175 

176 def move(self, x, y, z): 

177 """Moves to the specified (x, y, z) coordinate.""" 

178 return self.movements.move(x, y, z) 

179 

180 def set_home(self, axis="all"): 

181 """Sets the current position as the home position for a specific axis.""" 

182 return self.movements.set_home(axis) 

183 

184 def find_home(self, axis="all", speed=100): 

185 """Moves the device to the home position for a specified axis.""" 

186 return self.movements.find_home(axis, speed) 

187 

188 def find_axis_length(self, axis="all"): 

189 """Finds the length of a specified axis.""" 

190 return self.movements.find_axis_length(axis) 

191 

192 def get_xyz(self): 

193 """Returns the current (x, y, z) coordinates of the FarmBot.""" 

194 return self.movements.get_xyz() 

195 

196 def check_position(self, user_x, user_y, user_z, tolerance): 

197 """Verifies position of the FarmBot within specified tolerance range.""" 

198 return self.movements.check_position(user_x, user_y, user_z, tolerance) 

199 

200 # peripherals.py 

201 

202 def control_servo(self, pin, angle): 

203 """Set servo angle between 0-100 degrees.""" 

204 return self.peripherals.control_servo(pin, angle) 

205 

206 def control_peripheral(self, peripheral_name, value, mode=None): 

207 """Set peripheral value and mode.""" 

208 return self.peripherals.control_peripheral(peripheral_name, value, mode) 

209 

210 def toggle_peripheral(self, peripheral_name): 

211 """Toggles the state of a specific peripheral between `on` and `off`.""" 

212 return self.peripherals.toggle_peripheral(peripheral_name) 

213 

214 def on(self, pin_number): 

215 """Turns specified pin number `on` (100%).""" 

216 return self.peripherals.on(pin_number) 

217 

218 def off(self, pin_number): 

219 """Turns specified pin number `off` (0%).""" 

220 return self.peripherals.off(pin_number) 

221 

222 # resources.py 

223 

224 def sequence(self, sequence_name): 

225 """Executes a predefined sequence.""" 

226 return self.resources.sequence(sequence_name) 

227 

228 def get_seed_tray_cell(self, tray_name, tray_cell): 

229 """Identifies and returns the location of specified cell in the seed tray.""" 

230 return self.resources.get_seed_tray_cell(tray_name, tray_cell) 

231 

232 def detect_weeds(self): 

233 """Scans the garden to detect weeds.""" 

234 return self.resources.detect_weeds() 

235 

236 def lua(self, code_snippet): 

237 """Executes custom Lua code snippets to perform complex tasks or automations.""" 

238 return self.resources.lua(code_snippet) 

239 

240 def if_statement(self, variable, operator, value, then_sequence_name=None, else_sequence_name=None, named_pin_type=None): 

241 """Performs conditional check and executes actions based on the outcome.""" 

242 return self.resources.if_statement(variable, operator, value, then_sequence_name, else_sequence_name, named_pin_type) 

243 

244 def assertion(self, code, assertion_type, recovery_sequence_name=None): 

245 """Evaluates an expression.""" 

246 return self.resources.assertion(code, assertion_type, recovery_sequence_name) 

247 

248 # tools.py 

249 

250 def mount_tool(self, tool_str): 

251 """Mounts the given tool and pulls it out of assigned slot.""" 

252 return self.tools.mount_tool(tool_str) 

253 

254 def dismount_tool(self): 

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

256 return self.tools.dismount_tool() 

257 

258 def water(self, plant_id): 

259 """Moves to and waters plant based on age and assigned watering curve.""" 

260 return self.tools.water(plant_id) 

261 

262 def dispense(self, milliliters, tool_name, pin): 

263 """Dispenses user-defined amount of liquid in milliliters.""" 

264 return self.tools.dispense(milliliters, tool_name, pin)