Coverage for farmbot_sidecar_starter_pack/main.py: 100%

145 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-09-04 17:38 -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 

18VERSION = "1.5.0" 

19 

20 

21class Farmbot(): 

22 """Farmbot class.""" 

23 __version__ = VERSION 

24 

25 def __init__(self): 

26 self.state = State() 

27 

28 # Initialize other components without the token initially 

29 self.api = ApiConnect(self.state) 

30 self.basic = BasicCommands(self.state) 

31 self.broker = BrokerConnect(self.state) 

32 self.camera = Camera(self.state) 

33 self.info = Information(self.state) 

34 self.jobs = JobHandling(self.state) 

35 self.messages = MessageHandling(self.state) 

36 self.movements = MovementControls(self.state) 

37 self.peripherals = Peripherals(self.state) 

38 self.resources = Resources(self.state) 

39 self.tools = ToolControls(self.state) 

40 

41 def set_verbosity(self, value): 

42 """Set output verbosity level.""" 

43 self.state.verbosity = value 

44 

45 def set_timeout(self, duration, key="listen"): 

46 """Set timeout value in seconds.""" 

47 if key == "all": 

48 for timeout_key in self.state.timeout: 

49 self.state.timeout[timeout_key] = duration 

50 else: 

51 self.state.timeout[key] = duration 

52 

53 def set_token(self, token): 

54 """Set FarmBot authorization token.""" 

55 self.state.token = token 

56 

57 # api.py 

58 

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

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

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

62 

63 # basic_commands.py 

64 

65 def wait(self, duration): 

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

67 return self.basic.wait(duration) 

68 

69 def e_stop(self): 

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

71 return self.basic.e_stop() 

72 

73 def unlock(self): 

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

75 return self.basic.unlock() 

76 

77 def reboot(self): 

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

79 return self.basic.reboot() 

80 

81 def shutdown(self): 

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

83 return self.basic.shutdown() 

84 

85 # broker.py 

86 

87 def connect_broker(self): 

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

89 return self.broker.connect() 

90 

91 def disconnect_broker(self): 

92 """Disconnect from the message broker.""" 

93 return self.broker.disconnect() 

94 

95 def publish(self, message): 

96 """Publish message to the message broker.""" 

97 return self.broker.publish(message) 

98 

99 def listen(self, channel="#", duration=None, stop_count=1): 

100 """Listen to a message broker channel.""" 

101 return self.broker.listen(channel, duration, stop_count=stop_count) 

102 

103 def listen_for_status_changes(self, duration=None, stop_count=1, diff_only=True, info_path=None): 

104 """Listen for status changes.""" 

105 return self.broker.listen( 

106 channel="status", 

107 duration=duration, 

108 stop_count=stop_count, 

109 message_options={"diff_only": diff_only, "path": info_path}, 

110 ) 

111 

112 # camera.py 

113 

114 def calibrate_camera(self): 

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

116 return self.camera.calibrate_camera() 

117 

118 def take_photo(self): 

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

120 return self.camera.take_photo() 

121 

122 # information.py 

123 

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

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

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

127 

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

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

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

131 

132 def api_post(self, endpoint, new_data): 

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

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

135 

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

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

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

139 

140 def safe_z(self): 

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

142 return self.info.safe_z() 

143 

144 def garden_size(self): 

145 """Returns size of garden bed.""" 

146 return self.info.garden_size() 

147 

148 def group(self, group_id=None): 

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

150 return self.info.group(group_id) 

151 

152 def curve(self, curve_id=None): 

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

154 return self.info.curve(curve_id) 

155 

156 def measure_soil_height(self): 

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

158 return self.info.measure_soil_height() 

159 

160 def read_status(self): 

161 """Returns the FarmBot status tree.""" 

162 return self.info.read_status() 

163 

164 def read_pin(self, pin_number, mode=0): 

165 """Reads the current value of the specified pin.""" 

166 return self.info.read_pin(pin_number, mode) 

167 

168 def read_sensor(self, sensor_name): 

169 """Reads the given sensor.""" 

170 return self.info.read_sensor(sensor_name) 

171 

172 # jobs.py 

173 

174 def get_job(self, job_str=None): 

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

176 return self.jobs.get_job(job_str) 

177 

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

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

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

181 

182 def complete_job(self, job_str): 

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

184 return self.jobs.complete_job(job_str) 

185 

186 # messages.py 

187 

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

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

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

191 

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

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

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

195 

196 def debug(self, message_str): 

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

198 return self.messages.debug(message_str) 

199 

200 def toast(self, message_str): 

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

202 return self.messages.toast(message_str) 

203 

204 # movements.py 

205 

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

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

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

209 

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

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

212 return self.movements.set_home(axis) 

213 

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

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

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

217 

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

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

220 return self.movements.find_axis_length(axis) 

221 

222 def get_xyz(self): 

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

224 return self.movements.get_xyz() 

225 

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

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

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

229 

230 # peripherals.py 

231 

232 def control_servo(self, pin, angle): 

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

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

235 

236 def write_pin(self, pin_number, value, mode=0): 

237 """Writes a new value to the specified pin.""" 

238 return self.peripherals.write_pin(pin_number, value, mode) 

239 

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

241 """Set peripheral value and mode.""" 

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

243 

244 def toggle_peripheral(self, peripheral_name): 

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

246 return self.peripherals.toggle_peripheral(peripheral_name) 

247 

248 def on(self, pin_number): 

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

250 return self.peripherals.on(pin_number) 

251 

252 def off(self, pin_number): 

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

254 return self.peripherals.off(pin_number) 

255 

256 # resources.py 

257 

258 def sequence(self, sequence_name): 

259 """Executes a predefined sequence.""" 

260 return self.resources.sequence(sequence_name) 

261 

262 def get_seed_tray_cell(self, tray_name, tray_cell): 

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

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

265 

266 def detect_weeds(self): 

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

268 return self.resources.detect_weeds() 

269 

270 def lua(self, code_snippet): 

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

272 return self.resources.lua(code_snippet) 

273 

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

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

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

277 

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

279 """Evaluates an expression.""" 

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

281 

282 # tools.py 

283 

284 def mount_tool(self, tool_str): 

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

286 return self.tools.mount_tool(tool_str) 

287 

288 def dismount_tool(self): 

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

290 return self.tools.dismount_tool() 

291 

292 def water(self, plant_id): 

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

294 return self.tools.water(plant_id) 

295 

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

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

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