Skip to content

Commit

Permalink
SW-2304 frontend implement air filter iii details in the settings mai…
Browse files Browse the repository at this point in the history
…ntenance page (#1811)

* detect AF change and update usage info in frontend
  • Loading branch information
Josef-MrBeam committed Oct 12, 2023
1 parent 6cb906a commit b81eb4e
Show file tree
Hide file tree
Showing 12 changed files with 294 additions and 286 deletions.
55 changes: 36 additions & 19 deletions octoprint_mrbeam/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ def initialize(self):
self._event_bus.subscribe(
MrBeamEvents.LASER_HEAD_READ, self._on_laserhead_ready
)
self._event_bus.subscribe(
MrBeamEvents.AIRFILTER_CHANGED, self.on_airfilter_changed
)

self.start_time_ntp_timer()

Expand Down Expand Up @@ -511,6 +514,7 @@ def get_settings_defaults(self):
)

def on_settings_load(self):
self._send_maintenance_information(trigger=MrBeamEvents.SETTINGS_LOAD)
return dict(
svgDPI=self._settings.get(["svgDPI"]),
dxfScale=self._settings.get(["dxfScale"]),
Expand Down Expand Up @@ -569,25 +573,6 @@ def on_settings_load(self):
model_id=self.laserhead_handler.get_current_used_lh_model_id(),
model_supported=self.laserhead_handler.is_current_used_lh_model_supported(),
),
usage=dict(
totalUsage=self.usage_handler.get_total_usage(),
prefilterUsage=self.usage_handler.get_prefilter_usage(),
carbonFilterUsage=self.usage_handler.get_carbon_filter_usage(),
laserHeadUsage=self.usage_handler.get_laser_head_usage(),
gantryUsage=self.usage_handler.get_gantry_usage(),
prefilterLifespans=self.airfilter.get_lifespans(AirFilter.PREFILTER),
carbonfilterLifespans=self.airfilter.get_lifespans(
AirFilter.CARBONFILTER
),
carbonfilterShopify=self.airfilter.get_shopify_links(
AirFilter.CARBONFILTER
),
prefilterShopify=self.airfilter.get_shopify_links(AirFilter.PREFILTER),
prefilterHeavyDutyShopify=self.airfilter.get_shopify_links(
AirFilter.PREFILTER_HEAVY_DUTY
),
laserHeadLifespan=self.laserhead_handler.current_laserhead_lifespan,
),
heavyDutyPrefilter=self.is_heavy_duty_prefilter_enabled(),
tour_auto_launch=self._settings.get(["tour_auto_launch"]),
hw_features=dict(
Expand All @@ -600,6 +585,34 @@ def on_settings_load(self):
isFirstRun=self.isFirstRun(),
)

def _get_usage_data_dict(self):
return dict(
totalUsage=self.usage_handler.get_total_usage(),
prefilterUsage=self.usage_handler.get_prefilter_usage(),
carbonFilterUsage=self.usage_handler.get_carbon_filter_usage(),
laserHeadUsage=self.usage_handler.get_laser_head_usage(),
gantryUsage=self.usage_handler.get_gantry_usage(),
prefilterLifespans=self.airfilter.get_lifespans(AirFilter.PREFILTER),
carbonfilterLifespans=self.airfilter.get_lifespans(AirFilter.CARBONFILTER),
carbonfilterShopify=self.airfilter.get_shopify_links(
AirFilter.CARBONFILTER
),
prefilterShopify=self.airfilter.get_shopify_links(AirFilter.PREFILTER),
prefilterHeavyDutyShopify=self.airfilter.get_shopify_links(
AirFilter.PREFILTER, heavy_duty=True
),
laserHeadLifespan=self.laserhead_handler.current_laserhead_lifespan,
)

def on_airfilter_changed(self, *args, **kwargs):
self._send_maintenance_information(trigger=MrBeamEvents.AIRFILTER_CHANGED)

def _send_maintenance_information(self, trigger=None):
self._plugin_manager.send_plugin_message(
"mrbeam",
dict(maintenance_information=self._get_usage_data_dict(), trigger=trigger),
)

def on_settings_save(self, data):
"""See
octoprint.plugins.types.SettingsPlugin.get_settings_preprocessors to
Expand Down Expand Up @@ -683,6 +696,10 @@ def on_settings_save(self, data):
octoprint.plugin.SettingsPlugin.on_settings_save(self, data)

def set_heavy_duty_prefilter(self, value):
if value != self._settings.get(["heavyDutyPrefilter"]):
self._send_maintenance_information(
trigger=MrBeamEvents.HEAVY_DUTY_PREFILTER_CHANGED
)
self._settings.set_boolean(["heavyDutyPrefilter"], value)

def is_heavy_duty_prefilter_enabled(self):
Expand Down
127 changes: 68 additions & 59 deletions octoprint_mrbeam/iobeam/airfilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import yaml
from flask_babel import gettext

from octoprint_mrbeam.mrbeam_events import MrBeamEvents
from octoprint_mrbeam.mrb_logger import mrb_logger

_instance = None
Expand Down Expand Up @@ -39,37 +40,33 @@ class AirFilter(object):
CARBON_LIFESPAN_FALLBACK = 280
PREFILTER = "prefilter"
CARBONFILTER = "carbonfilter"
PREFILTER_HEAVY_DUTY = "prefilter_heavy_duty"
FILTERSTAGES = [PREFILTER, CARBONFILTER, PREFILTER_HEAVY_DUTY]
FILTERSTAGES = [PREFILTER, CARBONFILTER]

class ProfileParameters(Enum):
SHOPIFY_LINK = "shopify_link"
LIFESPAN = "lifespan"
HEAVY_DUTY_LIFESPAN = "heavy_duty_lifespan"
HEAVY_DUTY_SHOPIFY_LINK = "heavy_duty_shopify_link"

DEFAULT_PROFILE = {
CARBONFILTER: [
{
"lifespan": CARBON_LIFESPAN_FALLBACK,
"shopify_link": "products/aktivkohlefilter-inklusive-zehn-vorfilter?utm_source=beamos&utm_medium=beamos&utm_campaign=maintenance_page",
ProfileParameters.LIFESPAN.value: CARBON_LIFESPAN_FALLBACK,
ProfileParameters.SHOPIFY_LINK.value: "products/aktivkohlefilter-inklusive-zehn-vorfilter?utm_source=beamos&utm_medium=beamos&utm_campaign=maintenance_page",
}
],
PREFILTER: [
{
"lifespan": PREFILTER_LIFESPAN_FALLBACK,
"shopify_link": "products/vorfilter-mrbeam?utm_source=beamos&utm_medium=beamos&utm_campaign=maintenance_page",
}
],
PREFILTER_HEAVY_DUTY: [
{
"lifespan": PREFILTER_LIFESPAN_FALLBACK,
"shopify_link": "products/mr-beam-vorfilter-kartusche-5er-pack?utm_source=beamos&utm_medium=beamos&utm_campaign=maintenance_page",
ProfileParameters.LIFESPAN.value: PREFILTER_LIFESPAN_FALLBACK,
ProfileParameters.SHOPIFY_LINK.value: "products/vorfilter-mrbeam?utm_source=beamos&utm_medium=beamos&utm_campaign=maintenance_page",
ProfileParameters.HEAVY_DUTY_LIFESPAN.value: PREFILTER_LIFESPAN_FALLBACK,
ProfileParameters.HEAVY_DUTY_SHOPIFY_LINK.value: "products/mr-beam-vorfilter-kartusche-5er-pack?utm_source=beamos&utm_medium=beamos&utm_campaign=maintenance_page",
}
],
"prefilter_stages": 1,
"carbonfilter_stages": 1,
"prefilter_heavy_duty_stages": 1,
}

class ProfileParameters(Enum):
SHOPIFY_LINK = "shopify_link"
LIFESPAN = "lifespan"

def __init__(self, plugin):
self._logger = mrb_logger("octoprint.plugins.mrbeam.iobeam.airfilter")
self._plugin = plugin
Expand Down Expand Up @@ -159,30 +156,27 @@ def temperatures(self):
}
return None

@serial.setter
def serial(self, serial):
"""Sets the serial of the air filter.
Args:
serial (str): Serial of the air filter
"""
if serial != self._serial:
self._serial = serial
self._plugin.send_mrb_state()

@model_id.setter
def model_id(self, model_id):
"""Sets the model id of the air filter.
def set_airfilter(self, model_id, serial):
"""Sets the air filter.
Args:
model_id (int): Model id of the air filter
serial (str): Serial of the air filter
"""
# if model id is not the same as before, reset data
if self._model_id != model_id:
if (
serial is not None
and model_id is not None
and (serial != self._serial or model_id != self._model_id)
):
self.reset_data()
self._serial = serial
self._model_id = model_id
self._plugin.send_mrb_state()
self._load_current_profile()
self._airfilter_changed()

def _airfilter_changed(self):
self._plugin.send_mrb_state()
self._plugin._event_bus.fire(MrBeamEvents.AIRFILTER_CHANGED)

def set_pressure(
self,
Expand Down Expand Up @@ -339,7 +333,7 @@ def get_lifespan(self, filter_stage, stage_id=0):
self._logger.error("filter_stage {} is not known".format(filter_stage))
return None

if filter_stage in {self.PREFILTER, self.PREFILTER_HEAVY_DUTY}:
if filter_stage == self.PREFILTER:
fallbackvalue = self.PREFILTER_LIFESPAN_FALLBACK
elif filter_stage == self.CARBONFILTER:
fallbackvalue = self.CARBON_LIFESPAN_FALLBACK
Expand All @@ -348,8 +342,37 @@ def get_lifespan(self, filter_stage, stage_id=0):
self._logger.warn(
"The selected filter stage does not have a fallback value, we will use 0 instead"
)
# Handle the exceptions

lifespan_key = self.ProfileParameters.LIFESPAN.value
if (
self.heavy_duty_prefilter_enabled()
and not self._is_lifespan_value_not_present(
current_airfilter_profile,
filter_stage,
stage_id,
self.ProfileParameters.HEAVY_DUTY_LIFESPAN.value,
)
):
self._logger.debug("use heavy duty lifespan")
lifespan_key = self.ProfileParameters.HEAVY_DUTY_LIFESPAN.value
# Handle the exceptions
if self._is_lifespan_value_not_present(
current_airfilter_profile, filter_stage, stage_id, lifespan_key
):
# Apply fallback
self._logger.error(
"Current {} ID:{} lifespan couldn't be retrieved, fallback to the fallback value of: {}".format(
filter_stage, stage_id, fallbackvalue
)
)
return fallbackvalue
# Reaching here means, everything looks good
return current_airfilter_profile[filter_stage][stage_id][lifespan_key]

def _is_lifespan_value_not_present(
self, current_airfilter_profile, filter_stage, stage_id, lifespan_key
):
return (
(isinstance(current_airfilter_profile, dict) is False)
or (filter_stage not in current_airfilter_profile)
or (isinstance(current_airfilter_profile[filter_stage], list) is False)
Expand All @@ -358,27 +381,15 @@ def get_lifespan(self, filter_stage, stage_id=0):
isinstance(current_airfilter_profile[filter_stage][stage_id], dict)
is False
)
or (lifespan_key not in current_airfilter_profile[filter_stage][stage_id])
or (
isinstance(
current_airfilter_profile[filter_stage][stage_id][
self.ProfileParameters.LIFESPAN.value
],
current_airfilter_profile[filter_stage][stage_id][lifespan_key],
int,
)
is False
)
):
# Apply fallback
self._logger.error(
"Current {} ID:{} lifespan couldn't be retrieved, fallback to the fallback value of: {}".format(
filter_stage, stage_id, fallbackvalue
)
)
return fallbackvalue
# Reaching here means, everything looks good
return current_airfilter_profile[filter_stage][stage_id][
self.ProfileParameters.LIFESPAN.value
]
)

def get_lifespans(self, filter_stage):
"""Returns the lifespan of the given filter stage and sub stages.
Expand All @@ -391,12 +402,6 @@ def get_lifespans(self, filter_stage):
"""
self._logger.debug("get lifespans of filter_stage: {}".format(filter_stage))
current_airfilter_profile = self.profile
if filter_stage == self.PREFILTER and self.heavy_duty_prefilter_enabled():
self._logger.debug("heavy duty prefilter is enabled")
filter_stage = self.PREFILTER_HEAVY_DUTY
else:
if filter_stage == self.PREFILTER:
self._logger.debug("heavy duty prefilter is disabled")
if current_airfilter_profile is not None:
stages = current_airfilter_profile.get(filter_stage + "_stages")
if stages is not None:
Expand All @@ -417,16 +422,20 @@ def get_lifespans(self, filter_stage):
)
return None

def get_shopify_links(self, filter_stage):
def get_shopify_links(self, filter_stage, heavy_duty=False):
"""Returns the shopify links of the given filter stage and sub stages.
Args:
filter_stage (str): name of the filter stage [FILTERSTAGES]
heavy_duty (bool): True if heavy duty prefilter is enabled
Returns:
list: list of shopify links of the given filter stage or None if the profile is None
"""
current_airfilter_profile = self.profile
shopify_link_key = self.ProfileParameters.SHOPIFY_LINK.value
if heavy_duty:
shopify_link_key = self.ProfileParameters.HEAVY_DUTY_SHOPIFY_LINK.value
if current_airfilter_profile is not None:
stages = current_airfilter_profile.get(filter_stage + "_stages")
if stages is not None:
Expand All @@ -436,7 +445,7 @@ def get_shopify_links(self, filter_stage):
shopify_links.append(
gettext("https://www.mr-beam.org/en/")
+ current_airfilter_profile[filter_stage][i][
self.ProfileParameters.SHOPIFY_LINK.value
shopify_link_key
]
)
except KeyError:
Expand Down
5 changes: 3 additions & 2 deletions octoprint_mrbeam/iobeam/iobeam_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1111,8 +1111,9 @@ def _handle_exhaust(self, dataset):
pressure_dataset,
temperature_dataset,
)
self._airfilter.serial = device_dataset.get("serial_num")
self._airfilter.model_id = device_dataset.get("type")
self._airfilter.set_airfilter(
serial=device_dataset.get("serial_num"), model_id=device_dataset.get("type")
)
self._airfilter.set_pressure(
pressure1=pressure_dataset.get("pressure1"),
pressure2=pressure_dataset.get("pressure2"),
Expand Down
5 changes: 5 additions & 0 deletions octoprint_mrbeam/mrbeam_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# NOTICE: This is used by the camera plugin
class MrBeamEvents(object):

HEAVY_DUTY_PREFILTER_CHANGED = "HeavyDutyPrefilterChanged"
SETTINGS_LOAD = "SettingsLoad"
MRB_PLUGIN_INITIALIZED = "MrbPluginInitialized"
BOOT_GRACE_PERIOD_END = "BootGracePeriodEnd"

Expand Down Expand Up @@ -87,6 +89,9 @@ class MrBeamEvents(object):
BLINK_PRINT_LABELS = "BlinkPrintLabels"
INITIAL_CALIBRATION = "initial_calibration"

# Airfilter
AIRFILTER_CHANGED = "AirfilterChanged"

@classmethod
def register_with_octoprint(cls):
"""this has to be called during plugin's Constructor."""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#No fan or airfilter 1 system
prefilter_stages: 1
carbonfilter_stages: 1
prefilter_heavy_duty_stages: 1
prefilter:
- lifespan: 40
shopify_link: 'products/vorfilter-mrbeam?utm_source=beamos&utm_medium=beamos&utm_campaign=maintenance_page'
prefilter_heavy_duty:
- lifespan: 80
shopify_link: 'products/mr-beam-vorfilter-kartusche-5er-pack?utm_source=beamos&utm_medium=beamos&utm_campaign=maintenance_page'
heavy_duty_lifespan: 80
heavy_duty_shopify_link: 'products/mr-beam-vorfilter-kartusche-5er-pack?utm_source=beamos&utm_medium=beamos&utm_campaign=maintenance_page'
carbonfilter:
- lifespan: 280
shopify_link: 'products/aktivkohlefilter-inklusive-zehn-vorfilter?utm_source=beamos&utm_medium=beamos&utm_campaign=maintenance_page'
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#airfilter 2 system
prefilter_stages: 1
carbonfilter_stages: 1
prefilter_heavy_duty_stages: 1
prefilter:
- lifespan: 40
shopify_link: 'products/vorfilter-mrbeam?utm_source=beamos&utm_medium=beamos&utm_campaign=maintenance_page'
prefilter_heavy_duty:
- lifespan: 80
shopify_link: 'products/mr-beam-vorfilter-kartusche-5er-pack?utm_source=beamos&utm_medium=beamos&utm_campaign=maintenance_page'
heavy_duty_lifespan: 80
heavy_duty_shopify_link: 'products/mr-beam-vorfilter-kartusche-5er-pack?utm_source=beamos&utm_medium=beamos&utm_campaign=maintenance_page'
carbonfilter:
- lifespan: 280
shopify_link: 'products/aktivkohlefilter-inklusive-zehn-vorfilter?utm_source=beamos&utm_medium=beamos&utm_campaign=maintenance_page'
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#airfilter 2 system
prefilter_stages: 1
carbonfilter_stages: 1
prefilter_heavy_duty_stages: 1
prefilter:
- lifespan: 40
shopify_link: 'products/vorfilter-mrbeam?utm_source=beamos&utm_medium=beamos&utm_campaign=maintenance_page'
prefilter_heavy_duty:
- lifespan: 80
shopify_link: 'products/mr-beam-vorfilter-kartusche-5er-pack?utm_source=beamos&utm_medium=beamos&utm_campaign=maintenance_page'
heavy_duty_lifespan: 80
heavy_duty_shopify_link: 'products/mr-beam-vorfilter-kartusche-5er-pack?utm_source=beamos&utm_medium=beamos&utm_campaign=maintenance_page'
carbonfilter:
- lifespan: 280
shopify_link: 'products/aktivkohlefilter-inklusive-zehn-vorfilter?utm_source=beamos&utm_medium=beamos&utm_campaign=maintenance_page'
Loading

0 comments on commit b81eb4e

Please sign in to comment.