Skip to content

Commit

Permalink
Fixes #73 - Ignore pauses for Timelapse plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Clon1998 committed Jan 2, 2024
1 parent f65b679 commit 322b4d8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
4 changes: 3 additions & 1 deletion mobileraker/data/dtos/moonraker/printer_snapshot.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime, timedelta
from typing import Optional
import math
import pytz
from typing import Optional

from mobileraker.data.dtos.moonraker.printer_objects import GCodeFile, GCodeMove, PrintStats, Toolhead, VirtualSDCard

Expand All @@ -24,6 +24,7 @@ def __init__(
self.gcode_move: Optional[GCodeMove] = None
self.gcode_response: Optional[str] = None
self.gcode_response_hash: Optional[str] = None
self.timelapse_pause: Optional[bool] = None

def __str__(self):
return '%s(%s)' % (
Expand All @@ -43,6 +44,7 @@ def __eq__(self, other):
and self.virtual_sdcard == other.virtual_sdcard
and self.current_file == other.current_file
and self.gcode_response == other.gcode_response
and self.timelapse_pause == other.timelapse_pause
)

@property
Expand Down
6 changes: 5 additions & 1 deletion mobileraker/mobileraker_companion.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def _fulfills_evaluation_threshold(self, snapshot: PrinterSnapshot) -> bool:
self._logger.info('No last snapshot available. Evaluating!')
return True

if self._last_snapshot.print_state != snapshot.print_state:
if self._last_snapshot.print_state != snapshot.print_state and not snapshot.timelapse_pause:
self._logger.info('State changed. Evaluating!')
return True

Expand Down Expand Up @@ -310,6 +310,10 @@ def _progress_notification(self, cfg: DeviceNotificationEntry, cur_snap: Printer
# also skip if progress is at 100 since this notification is handled via the print state transition from printing to completed
if cur_snap.print_state not in ["printing", "paused"] or cur_snap.progress is None or cur_snap.progress == 100:
return None

# Ignore paused state caused by timelapse plugin
if cur_snap.timelapse_pause:
return None

self._logger.info(
'ProgressNoti preChecks: cfg.progress.config: %i - %i = %i < %i RESULT: %s',
Expand Down
42 changes: 21 additions & 21 deletions mobileraker/service/data_sync_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,26 @@
import asyncio
import hashlib
import logging
from typing import Any, Callable, Dict, List, Optional, cast
from typing import Any, Callable, Dict, List, Optional
from mobileraker.client.moonraker_client import MoonrakerClient
from mobileraker.data.dtos.moonraker.printer_objects import DisplayStatus, GCodeFile, GCodeMove, PrintStats, ServerInfo, Toolhead, VirtualSDCard
from mobileraker.data.dtos.moonraker.printer_snapshot import PrinterSnapshot




class DataSyncService:
_OBJECTS_TO_SUBSCRIBE = {
"objects": {
"print_stats": None,
"display_status": None,
"virtual_sdcard": None,
"toolhead": None,
"gcode_move": None,
"gcode_macro TIMELAPSE_TAKE_FRAME": None,
}
}

'''
This service is responsible for keeping track of the latest printer data and then
providing a snapshot of all data to any service that requires it.
Expand Down Expand Up @@ -43,6 +56,7 @@ def __init__(
self.virtual_sdcard: VirtualSDCard = VirtualSDCard()
self.current_file: Optional[GCodeFile] = None
self.gcode_response: Optional[str] = None
self.timelapse_pause: Optional[bool] = None
self.resync_retries: int = resync_retries

self._snapshot_listeners: List[Callable[[PrinterSnapshot], None]] = []
Expand Down Expand Up @@ -91,6 +105,8 @@ def _parse_objects(self, status_objects: Dict[str, Any], err: Optional[str] = No
self.toolhead = self.toolhead.updateWith(object_data)
elif key == 'gcode_move':
self.gcode_move = self.gcode_move.updateWith(object_data)
elif key == 'gcode_macro TIMELAPSE_TAKE_FRAME':
self.timelapse_pause = object_data.get('is_paused', None)

# Kinda hacky but this works!
# It would be better if the _notify_listeners()/sync current file is called in a different context since this method should only parse!
Expand Down Expand Up @@ -178,16 +194,7 @@ async def _sync_printer_data(self) -> None:
'''
try:
self._logger.info("Syncing printer Objects")
params = {
"objects": {
"print_stats": None,
# "display_status": None,
"virtual_sdcard": None,
"toolhead": None,
"gcode_move": None,
}
}
response, k_err = await self._jrpc.send_and_receive_method("printer.objects.query", params)
response, k_err = await self._jrpc.send_and_receive_method("printer.objects.query", self._OBJECTS_TO_SUBSCRIBE)
if k_err:
self._logger.warning("Could not sync printer data. Moonraker returned error %s", k_err)
return
Expand Down Expand Up @@ -218,16 +225,8 @@ async def _subscribe_for_object_updates(self) -> None:
None
'''
self._logger.info("Subscribing to printer Objects")
params = {
"objects": {
"print_stats": None,
"display_status": None,
"virtual_sdcard": None,
"toolhead": None,
"gcode_move": None,
}
}
await self._jrpc.send_method("printer.objects.subscribe", None, params)
await self._jrpc.send_method("printer.objects.subscribe", None, self._OBJECTS_TO_SUBSCRIBE)
self._logger.info("Subscribed to printer Objects")

def _notify_listeners(self):
'''
Expand Down Expand Up @@ -330,6 +329,7 @@ def take_snapshot(self) -> PrinterSnapshot:
snapshot.gcode_response = self.gcode_response
snapshot.gcode_response_hash = hashlib.sha256(snapshot.gcode_response.encode(
"utf-8")).hexdigest() if snapshot.gcode_response else ''
snapshot.timelapse_pause = self.timelapse_pause

self._logger.debug('Took a PrinterSnapshot: %s', snapshot)
return snapshot
Expand Down

0 comments on commit 322b4d8

Please sign in to comment.