diff --git a/src/octoprint/plugins/pi_support/__init__.py b/src/octoprint/plugins/pi_support/__init__.py index 28b4f3d03..8ea0af7be 100644 --- a/src/octoprint/plugins/pi_support/__init__.py +++ b/src/octoprint/plugins/pi_support/__init__.py @@ -35,10 +35,9 @@ _FLAG_PAST_UNDERVOLTAGE = 1 << 16 _FLAG_PAST_FREQ_CAPPED = 1 << 17 _FLAG_PAST_THROTTLED = 1 << 18 - class ThrottleState(object): @classmethod - def from_value(cls, value): + def from_value(cls, value=0): if value == 0: return ThrottleState() @@ -47,10 +46,12 @@ class ThrottleState(object): throttled=_FLAG_THROTTLED & value == _FLAG_THROTTLED, past_undervoltage=_FLAG_PAST_UNDERVOLTAGE & value == _FLAG_PAST_UNDERVOLTAGE, past_freq_capped=_FLAG_PAST_FREQ_CAPPED & value == _FLAG_PAST_FREQ_CAPPED, - past_throttled=_FLAG_PAST_THROTTLED & value == _FLAG_PAST_THROTTLED) + past_throttled=_FLAG_PAST_THROTTLED & value == _FLAG_PAST_THROTTLED, + raw_value=value) return ThrottleState(**kwargs) def __init__(self, **kwargs): + self._raw_value = kwargs.get('raw_value', -1) self._undervoltage = False self._freq_capped = False self._throttled = False @@ -111,7 +112,8 @@ class ThrottleState(object): and self._past_throttled == other._past_throttled def as_dict(self): - return dict(current_undervoltage=self.current_undervoltage, + return dict(raw_value=self._raw_value, + current_undervoltage=self.current_undervoltage, past_undervoltage=self.past_undervoltage, current_overheat=self.current_overheat, past_overheat=self.past_overheat, @@ -242,6 +244,16 @@ class PiSupportPlugin(octoprint.plugin.EnvironmentDetectionPlugin, def _check_throttled_state_condition(self): return self._throttle_functional + def get_throttle_state(self, run_now=False): + """Exposed as public helper.""" + if run_now: + self._check_throttled_state() + + if not self._throttle_functional: + return False + + return self._throttle_state.as_dict() + def _check_throttled_state(self): command = self._settings.get(["vcgencmd_throttle_check_command"]) @@ -278,6 +290,7 @@ class PiSupportPlugin(octoprint.plugin.EnvironmentDetectionPlugin, self._plugin_manager.send_plugin_message(self._identifier, dict(type="throttle_state", state=self._throttle_state.as_dict())) + self._event_bus.fire("plugin_pi_support_throttle_state", self._throttle_state.as_dict()) __plugin_name__ = "Pi Support Plugin" @@ -299,5 +312,9 @@ def __plugin_check__(): return "raspberry pi" in proc_dt_model.lower() def __plugin_load__(): + plugin = PiSupportPlugin() global __plugin_implementation__ - __plugin_implementation__ = PiSupportPlugin() + __plugin_implementation__ = plugin + + global __plugin_helpers__ + __plugin_helpers__ = dict(get_throttled=plugin.get_throttle_state) diff --git a/src/octoprint/plugins/tracking/__init__.py b/src/octoprint/plugins/tracking/__init__.py index 108b275e5..436ab1ed1 100644 --- a/src/octoprint/plugins/tracking/__init__.py +++ b/src/octoprint/plugins/tracking/__init__.py @@ -39,6 +39,8 @@ class TrackingPlugin(octoprint.plugin.SettingsPlugin, def __init__(self): self._environment = None + self._throttle_state = None + self._helpers_get_throttle_state = None self._printer_connection_parameters = None self._url = None self._ping_worker = None @@ -60,7 +62,8 @@ class TrackingPlugin(octoprint.plugin.SettingsPlugin, printjob=True, plugin=True, update=True, - printer=True)) + printer=True, + throttled=True)) def get_settings_restricted_paths(self): return dict(admin=[["enabled"], ["unique_id"], ["events"]], @@ -84,13 +87,19 @@ class TrackingPlugin(octoprint.plugin.SettingsPlugin, ##~~ StartupPlugin def on_after_startup(self): - self._track_startup() - ping = self._settings.get_int(["ping"]) if ping: self._ping_worker = RepeatedTimer(ping, self._track_ping) self._ping_worker.start() + # cautiously look for the get_throttled helper from pi_support + pi_helper = self._plugin_manager.get_helpers("pi_support", "get_throttled") + if pi_helper and 'get_throttled' in pi_helper: + self._helpers_get_throttle_state = pi_helper['get_throttled'] + + # now that we have everything set up, phone home. + self._track_startup() + ##~~ ShutdownPlugin def on_shutdown(self): @@ -101,8 +110,11 @@ class TrackingPlugin(octoprint.plugin.SettingsPlugin, def on_event(self, event, payload): if event.startswith("plugin_pluginmanager_"): self._track_plugin_event(event, payload) - if event.startswith("plugin_softwareupdate_"): + elif event.startswith("plugin_softwareupdate_"): self._track_update_event(event, payload) + elif event in ("plugin_pi_support_throttle_state",): + self._throttle_state = payload + self._track_throttle_event(event, payload) elif event in (Events.PRINT_STARTED, Events.PRINT_DONE, Events.PRINT_FAILED, Events.PRINT_CANCELLED): self._track_printjob_event(event, payload) elif event in (Events.CONNECTED,): @@ -162,6 +174,7 @@ class TrackingPlugin(octoprint.plugin.SettingsPlugin, if b"plugins" in self._environment and b"pi_support" in self._environment[b"plugins"]: payload[b"pi_model"] = self._environment[b"plugins"][b"pi_support"][b"model"] + if b"octopi_version" in self._environment[b"plugins"][b"pi_support"]: payload[b"octopi_version"] = self._environment[b"plugins"][b"pi_support"][b"octopi_version"] @@ -195,6 +208,26 @@ class TrackingPlugin(octoprint.plugin.SettingsPlugin, elif event.endswith("_update_failed"): self._track("update_failed", target=payload.get("target"), from_version=payload.get("from_version"), to_version=payload.get("to_version")) + def _track_throttle_event(self, event, payload): + if not self._settings.get_boolean(["events", "throttled"]): + return + + args = dict(throttled_now=payload[b"current_issue"], + throttled_past=payload[b"past_issue"], + throttled_mask=payload[b"raw_value"], + throttled_voltage_now=payload[b"current_undervoltage"], + throttled_voltage_past=payload[b"past_undervoltage"], + throttled_overheat_now=payload[b"current_overheat"], + throttled_overheat_past=payload[b"past_overheat"]) + + if payload[b"current_issue"]: + track_event = "system_throttled" + else: + track_event = "system_unthrottled" + + if track_event is not None: + self._track(track_event, **args) + def _track_printjob_event(self, event, payload): if not self._settings.get_boolean(["events", "printjob"]): return @@ -220,6 +253,17 @@ class TrackingPlugin(octoprint.plugin.SettingsPlugin, elif event == Events.PRINT_CANCELLED: track_event = "print_cancelled" + if callable(self._helpers_get_throttle_state): + try: + throttle_state = self._helpers_get_throttle_state(run_now=True) + if throttle_state and (throttle_state.get(b"current_issue", False) or throttle_state.get(b"past_issue", False)): + args[b"throttled_now"] = throttle_state[b"current_issue"] + args[b"throttled_past"] = throttle_state[b"past_issue"] + args[b"throttled_mask"] = throttle_state[b"raw_value"] + except: + # ignored + pass + if track_event is not None: self._track(track_event, **args) @@ -246,15 +290,18 @@ class TrackingPlugin(octoprint.plugin.SettingsPlugin, server = self._settings.get([b"server"]) url = server.format(id=self._settings.get([b"unique_id"]), event=event) + # Don't print the URL or UUID! That would expose the UUID in forums/tickets + # if pasted. It's okay for the user to know their uuid, but it shouldn't be shared. headers = {"User-Agent": "OctoPrint/{}".format(get_octoprint_version_string())} try: params = urlencode(kwargs, doseq=True).replace("+", "%20") + requests.get(url, params=params, timeout=3.1, headers=headers) - self._logger.debug("Sent tracking event to {}".format(url)) + self._logger.info("Sent tracking event {}, payload: {!r}".format(event, kwargs)) except: if self._logger.isEnabledFor(logging.DEBUG): self._logger.exception("Error while sending event to anonymous usage tracking".format(url)) diff --git a/src/octoprint/plugins/tracking/templates/tracking_settings.jinja2 b/src/octoprint/plugins/tracking/templates/tracking_settings.jinja2 index 3a5fa8a08..217bc26c6 100644 --- a/src/octoprint/plugins/tracking/templates/tracking_settings.jinja2 +++ b/src/octoprint/plugins/tracking/templates/tracking_settings.jinja2 @@ -37,6 +37,9 @@ + diff --git a/tests/plugins/pi_support/test_pi_support.py b/tests/plugins/pi_support/test_pi_support.py index be33f8c71..c7607bde8 100644 --- a/tests/plugins/pi_support/test_pi_support.py +++ b/tests/plugins/pi_support/test_pi_support.py @@ -150,4 +150,5 @@ class ThrottleStateTestCase(unittest.TestCase): current_overheat=False, past_overheat=True, current_issue=True, - past_issue=True)) + past_issue=True, + raw_value=-1))