Skip to content

Commit

Permalink
Add support for ignoring filament sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
Clon1998 committed Mar 16, 2024
1 parent b745f5c commit c252974
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ snapshot_rotation: 0
# The rotation applied to the image. Valid values : 0, 90, 180, 270
# Default: 0
# Optional
ignore_filament_sensors:
# Comma separated list of filament sensors to ignore, ignored filament sensors do not trigger
# a notification if they are triggered. This is useful if you have a filament sensor that is used
# in a MMU setup like ERCF.
# IMPORTANT, do not include the sensor type. E.g. if your sensor is configured
# like: [filament_switch_sensor printhead_sensor] add `printhead_sensor` to the list.
# Default: empty
# Optional

```
> [!IMPORTANT]
Expand Down Expand Up @@ -219,6 +227,7 @@ moonraker_uri: ws://ratrig.home:7125/websocket
# Default value: ws://127.0.0.1:7125/websocket
moonraker_api_key: False
# Moonraker API key if force_logins is active!
ignore_filament_sensors: printhead_sensor, sensor_name2
```

> [!NOTE]
Expand Down
4 changes: 3 additions & 1 deletion installer/Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Config:
"printer.moonraker_api_key": ["Moonraker API key if force_logins or trusted clients is active!", "Default value: False", "Optional"],
"printer.snapshot_uri": ["!! SUPPORTER ONLY - This feature requires beeing a supporter of Mobileraker as of now!", "The ABSOLUT url to the webcam, the companion should make a screenshot of.", "Example MJPEG SnapShot url: http://192.168.178.135/webcam/?action=snapshot", "Optional"],
"printer.snapshot_rotation": ["!! SUPPORTER ONLY - This feature requires beeing a supporter of Mobileraker as of now!", "The rotation of the snapshot in degrees", "Default value: 0", "Valud Values: 0,90,180,270", "Optional"],
"printer.ignore_filament_sensors": ["Comma separated list of filament sensors to ignore, ignored filament sensors do not trigger", "a notification if they are triggered. This is useful if you have a filament sensor that is used", "in a MMU setup like ERCF.","IMPORTANT, do not include the sensor type. E.g. if your sensor is configured", "like: [filament_switch_sensor printhead_sensor] add `printhead_sensor` to the list.", "Default: empty", "Optional"],
}


Expand Down Expand Up @@ -282,4 +283,5 @@ def _add_printer(self, name: str,context: Context, config: configparser.ConfigPa
config.set(sec, "snapshot_uri", "http://127.0.0.1:4408/webcam/?action=snapshot")
else:
config.set(sec, "snapshot_uri", "http://127.0.0.1/webcam/?action=snapshot")
config.set(sec, "snapshot_rotation", "0")
config.set(sec, "snapshot_rotation", "0")
config.set(sec, "ignore_filament_sensors", "")
1 change: 1 addition & 0 deletions mobileraker.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def main() -> None:
printer_name=printer_name,
loop=event_loop,
companion_config=local_config,
exclude_sensors=p_config['excluded_filament_sensors'],
)
event_loop.create_task(client.start())
event_loop.run_forever()
Expand Down
17 changes: 15 additions & 2 deletions mobileraker/mobileraker_companion.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def __init__(
snapshot_client: SnapshotClient,
printer_name: str,
loop: AbstractEventLoop,
companion_config: CompanionLocalConfig
companion_config: CompanionLocalConfig,
exclude_sensors: List[str] = []
) -> None:
super().__init__()
self._jrpc: MoonrakerClient = jrpc
Expand All @@ -46,6 +47,7 @@ def __init__(
self.printer_name: str = printer_name
self.loop: AbstractEventLoop = loop
self.companion_config: CompanionLocalConfig = companion_config
self.exclude_sensors: List[str] = exclude_sensors
self.last_request: Optional[DeviceRequestDto] = None
# TODO: Fetch this from a remote server for easier configuration :)
self.remote_config = CompanionRemoteConfig()
Expand All @@ -55,6 +57,9 @@ def __init__(
self._last_apns_message: Optional[int] = None
self._evaulate_noti_lock: Lock = Lock()

self._logger.info('MobilerakerCompanion client created for %s, it will ignore the following sensors: %s',
printer_name, exclude_sensors)

self._jrpc.register_connection_listener(
lambda d: self.loop.create_task(self._update_meta_data()) if d else None)
self._data_sync_service.register_snapshot_listener(
Expand Down Expand Up @@ -233,6 +238,10 @@ def _fulfills_evaluation_threshold(self, snapshot: PrinterSnapshot) -> bool:

# check if any of the new sensors is enabled and
for key, sensor in cur_sensors.items():
# Skip sensors the user wants to ignore
if key in self.exclude_sensors:
continue

# do not skip disabled sensors, as they might have been enabled in the meantime
last_sensor = last_sensors.get(key)
if last_sensor is None:
Expand Down Expand Up @@ -466,6 +475,10 @@ def _filament_sensor_notifications(self, cfg: DeviceNotificationEntry, cur_snap:

# Check if any of the sensors is enabled and no filament was detected before
for key, sensor in cur_snap.filament_sensors.items():
# Skip sensors the user wants to ignore
if key in self.exclude_sensors:
continue

# If the sensor is not enabled, skip it
if not sensor.enabled:
continue
Expand Down Expand Up @@ -564,7 +577,7 @@ async def _update_app_snapshot(self, cfg: DeviceNotificationEntry, printer_snap:
progress_live_activity_update = printer_snap.progress

# get list of all filament that have no filament detected (Enabled and Disabled, if we only use enabled once, this might trigger a notification if the user enables a sensor and it detects no filament)
filament_sensors = [key for key, sensor in printer_snap.filament_sensors.items() if not sensor.filament_detected]
filament_sensors = [key for key, sensor in printer_snap.filament_sensors.items() if not sensor.filament_detected and not key in self.exclude_sensors]

updated = last.copy_with(
state=printer_snap.print_state if last.state != printer_snap.print_state and not printer_snap.is_timelapse_pause else None,
Expand Down
9 changes: 6 additions & 3 deletions mobileraker/util/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
import os
import pathlib
from typing import Any, Dict, Optional, Union
from typing import Any, Dict, List, Optional, Union
from dateutil import tz

home_dir = os.path.expanduser("~/")
Expand Down Expand Up @@ -47,6 +47,7 @@ def __init__(self, passed_config_file: str) -> None:
super().__init__()
self.config: configparser.ConfigParser = configparser.ConfigParser()
self.printers: Dict[str, Dict[str, Any]] = {}
self.ignore_sensors: List[str] = []

config_file_path = self.get_config_file_location(passed_config_file)
if config_file_path:
Expand All @@ -69,15 +70,17 @@ def __init__(self, passed_config_file: str) -> None:
"moonraker_uri": self.config.get(printer, "moonraker_uri", fallback="ws://127.0.0.1:7125/websocket"),
"moonraker_api_key": None if api_key == 'False' or not api_key else api_key,
"snapshot_uri": self.config.get(printer, "snapshot_uri", fallback="http://127.0.0.1/webcam/?action=snapshot"),
"snapshot_rotation": rotation
"snapshot_rotation": rotation,
"excluded_filament_sensors": [sensor.strip() for sensor in self.config.get(printer, "ignore_filament_sensors", fallback="").split(",") if sensor.strip() != ""]
}

if len(self.printers) <= 0:
self.printers['_Default'] = {
"moonraker_uri": "ws://127.0.0.1:7125/websocket",
"moonraker_api_key": None,
"snapshot_uri": "http://127.0.0.1/webcam/?action=snapshot",
"snapshot_rotation": 0
"snapshot_rotation": 0,
"excluded_filament_sensors": []
}
logging.info("Read %i printer config sections" % len(self.printers))

Expand Down

0 comments on commit c252974

Please sign in to comment.