|
31 | 31 | import math
|
32 | 32 |
|
33 | 33 | import re
|
34 |
| -import requests |
| 34 | +# import requests |
35 | 35 | import threading
|
36 | 36 |
|
37 | 37 | from timeit import default_timer as timer
|
| 38 | +from octoprint.events import Events |
| 39 | + |
38 | 40 | from .zprobe import ZProbe
|
39 | 41 | from .xyprobe import XyProbe
|
40 | 42 |
|
@@ -214,6 +216,180 @@ def cleanup_due_to_uninstall(_plugin, remove_profile=True):
|
214 | 216 |
|
215 | 217 | _plugin._settings.save()
|
216 | 218 |
|
| 219 | +# #-- EventHandlerPlugin mix-in |
| 220 | +def on_event(_plugin, event, payload): |
| 221 | + subscribed_events = (Events.FILE_SELECTED, Events.FILE_ADDED, Events.PRINT_STARTED, Events.PRINT_CANCELLED, Events.PRINT_CANCELLING, |
| 222 | + Events.PRINT_PAUSED, Events.PRINT_RESUMED, Events.PRINT_DONE, Events.PRINT_FAILED, |
| 223 | + Events.PLUGIN_PLUGINMANAGER_UNINSTALL_PLUGIN, Events.PLUGIN_PLUGINMANAGER_DISABLE_PLUGIN, Events.UPLOAD, |
| 224 | + Events.CONNECTING, Events.CONNECTED, Events.DISCONNECTING, Events.DISCONNECTED, Events.STARTUP, Events.SHUTDOWN) |
| 225 | + |
| 226 | + if event not in subscribed_events and payload is not None and payload.get("state_id") not in ("PAUSING", "STARTING"): |
| 227 | + _plugin._logger.debug('event [{}] received but not subscribed - discarding'.format(event)) |
| 228 | + return |
| 229 | + |
| 230 | + _plugin._logger.debug("_bgs: on_event event=[{}] payload=[{}]".format(event, payload)) |
| 231 | + |
| 232 | + # our plugin is being uninstalled |
| 233 | + if event in (Events.PLUGIN_PLUGINMANAGER_UNINSTALL_PLUGIN, Events.PLUGIN_PLUGINMANAGER_DISABLE_PLUGIN) and payload["id"] == _plugin._identifier: |
| 234 | + _plugin._logger.debug('we are being uninstalled/disabled :(') |
| 235 | + cleanup_due_to_uninstall(_plugin) |
| 236 | + _plugin._logger.debug('plugin cleanup completed (this house is clean)') |
| 237 | + return |
| 238 | + |
| 239 | + if _plugin._printer_profile_manager.get_current_or_default()["id"] != "_bgs": |
| 240 | + return |
| 241 | + |
| 242 | + # - CONNECTING |
| 243 | + if event == Events.CONNECTING: |
| 244 | + _plugin.connectionState = event |
| 245 | + # let's make sure we don't have any commands queued up |
| 246 | + _plugin.grblCmdQueue.clear() |
| 247 | + |
| 248 | + # - CONNECTED |
| 249 | + if event == Events.CONNECTED: |
| 250 | + _plugin._logger.debug('machine connected') |
| 251 | + |
| 252 | + _plugin.connectionState = event |
| 253 | + _plugin.whenConnected = time.time() |
| 254 | + _plugin.autoSleepTimer = time.time() |
| 255 | + |
| 256 | + _plugin.is_operational = True |
| 257 | + _plugin._settings.set_boolean(["is_operational"], _plugin.is_operational) |
| 258 | + |
| 259 | + queue_cmds_and_send(_plugin, ["$I", "$G"]) |
| 260 | + _plugin._printer.fake_ack() |
| 261 | + |
| 262 | + # Disconnecting & Disconnected |
| 263 | + if event in (Events.DISCONNECTING, Events.DISCONNECTED): |
| 264 | + _plugin.connectionState = event |
| 265 | + _plugin.handshakeSent = False |
| 266 | + _plugin.grblState = "N/A" |
| 267 | + _plugin._plugin_manager.send_plugin_message(_plugin._identifier, dict(type="grbl_state", state="N/A")) |
| 268 | + |
| 269 | + _plugin.is_operational = False |
| 270 | + _plugin._settings.set_boolean(["is_operational"], _plugin.is_operational) |
| 271 | + |
| 272 | + # Print Starting |
| 273 | + if payload is not None and payload.get("state_id") == "STARTING": |
| 274 | + add_to_notify_queue(_plugin, ["Pgm Begin"]) |
| 275 | + # threading.Thread(target=send_command_now, args=(_plugin._printer, _plugin._logger, "?")).start() |
| 276 | + _plugin._printer.commands("?", force=True) |
| 277 | + return |
| 278 | + |
| 279 | + # 'PrintStarted' |
| 280 | + if event == Events.PRINT_STARTED: |
| 281 | + if "HOLD" in _plugin.grblState.upper(): |
| 282 | + _plugin._printer.commands(["~"], force=True) |
| 283 | + elif not _plugin.grblState.upper() in ("IDLE", "CHECK"): |
| 284 | + # we have to stop This |
| 285 | + _plugin._printer.cancel_print() |
| 286 | + return |
| 287 | + |
| 288 | + # reset our rate overrides |
| 289 | + _plugin.feedRate = 0 |
| 290 | + _plugin.plungeRate = 0 |
| 291 | + _plugin.powerRate = 0 |
| 292 | + |
| 293 | + _plugin.grblState = "Run" |
| 294 | + _plugin._plugin_manager.send_plugin_message(_plugin._identifier, dict(type="grbl_state", state="Run")) |
| 295 | + |
| 296 | + _plugin.is_printing = True |
| 297 | + _plugin._settings.set_boolean(["is_printing"], _plugin.is_printing) |
| 298 | + |
| 299 | + if _plugin.autoCooldown: |
| 300 | + activate_auto_cooldown(_plugin) |
| 301 | + |
| 302 | + return |
| 303 | + |
| 304 | + # Print ended (finished / failed / cancelled) |
| 305 | + if event in (Events.PRINT_CANCELLED, Events.PRINT_DONE, Events.PRINT_FAILED): |
| 306 | + _plugin.grblState = "Idle" |
| 307 | + _plugin._plugin_manager.send_plugin_message(_plugin._identifier, dict(type="grbl_state", state="Idle")) |
| 308 | + |
| 309 | + _plugin.is_printing = False |
| 310 | + _plugin._settings.set_boolean(["is_printing"], _plugin.is_printing) |
| 311 | + |
| 312 | + return |
| 313 | + |
| 314 | + # Print Cancelling |
| 315 | + if event == Events.PRINT_CANCELLING: |
| 316 | + _plugin._logger.debug("cancelling job") |
| 317 | + |
| 318 | + if "HOLD" in _plugin.grblState.upper(): |
| 319 | + _plugin._printer.commands(["~", "M5"], force=True) |
| 320 | + else: |
| 321 | + _plugin._printer.commands(["M5"], force=True) |
| 322 | + |
| 323 | + # Print Pausing |
| 324 | + if payload is not None and payload.get("state_id") == "PAUSING": |
| 325 | + _plugin._logger.debug("pausing job") |
| 326 | + |
| 327 | + _plugin.pausedPower = _plugin.grblPowerLevel |
| 328 | + _plugin.pausedPositioning = _plugin.positioning |
| 329 | + |
| 330 | + _plugin._printer.fake_ack() |
| 331 | + |
| 332 | + # retract Z 5 if not laser mode |
| 333 | + if not is_laser_mode(_plugin): |
| 334 | + _plugin._printer.commands(["G91 G0 Z5"], force=True) |
| 335 | + |
| 336 | + _plugin._printer.commands(["M5", "?"], force=True) |
| 337 | + |
| 338 | + # Print Paused |
| 339 | + if event == Events.PRINT_PAUSED: |
| 340 | + _plugin._logger.debug("paused job") |
| 341 | + _plugin._printer.commands(["M5", "?", "!", "?"], force=True) |
| 342 | + |
| 343 | + # Print Resumed |
| 344 | + if event == Events.PRINT_RESUMED: |
| 345 | + _plugin._logger.debug("resuming job") |
| 346 | + _plugin._printer.commands(["~", "M3"], force=True) |
| 347 | + |
| 348 | + # move our spindle back down 5 |
| 349 | + if not is_laser_mode(_plugin): |
| 350 | + _plugin._printer.commands(["G4 P10", "G91 G0 Z-5"], force=True) |
| 351 | + |
| 352 | + # make sure we are using whatever positioning mode was active before we paused |
| 353 | + _plugin._printer.commands(["G91" if _plugin.pausedPositioning == 1 else "G90"], force=True) |
| 354 | + |
| 355 | + _plugin.grblState = "Run" |
| 356 | + _plugin._plugin_manager.send_plugin_message(_plugin._identifier, dict(type="grbl_state", state="Run")) |
| 357 | + |
| 358 | + # starting up |
| 359 | + if event == Events.STARTUP: |
| 360 | + _plugin._logger.info("starting up") |
| 361 | + |
| 362 | + # shutting down |
| 363 | + if event == Events.SHUTDOWN: |
| 364 | + _plugin._logger.info("shutting down") |
| 365 | + _plugin._settings.save() |
| 366 | + |
| 367 | + # File uploaded |
| 368 | + if event == Events.UPLOAD: |
| 369 | + if payload["path"].endswith(".gc") or payload["path"].endswith(".nc"): |
| 370 | + renamed_file = payload["path"][:len(payload["path"]) - 2] + "gcode" |
| 371 | + |
| 372 | + _plugin._logger.debug("renaming [%s] to [%s]", payload["path"], renamed_file) |
| 373 | + |
| 374 | + _plugin._file_manager.remove_file(payload["target"], renamed_file) |
| 375 | + _plugin._file_manager.move_file(payload["target"], payload["path"], renamed_file) |
| 376 | + |
| 377 | + generate_metadata_for_file(_plugin, renamed_file, notify=False, force=True) |
| 378 | + |
| 379 | + # 'FileAdded' |
| 380 | + if event == Events.FILE_ADDED: |
| 381 | + generate_metadata_for_file(_plugin, payload["path"], notify=False, force=True) |
| 382 | + |
| 383 | + # 'FileSelected' |
| 384 | + if event == Events.FILE_SELECTED: |
| 385 | + generate_metadata_for_file(_plugin, payload["path"], notify=True) |
| 386 | + return |
| 387 | + |
| 388 | + if event == Events.FILE_DESELECTED: |
| 389 | + return |
| 390 | + |
| 391 | + return |
| 392 | + |
217 | 393 |
|
218 | 394 | def do_framing(_plugin, data):
|
219 | 395 | _plugin._logger.debug("_bgs: do_framing data=[{}]".format(data))
|
|
0 commit comments