diff --git a/src/octoprint/plugins/pi_support/__init__.py b/src/octoprint/plugins/pi_support/__init__.py index 28b4f3d03..04f85ea29 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,10 @@ class PiSupportPlugin(octoprint.plugin.EnvironmentDetectionPlugin, def _check_throttled_state_condition(self): return self._throttle_functional + def get_throttle_state(self, run_now=False): + '''public method to pass around in plugin_helpers''' + return self._throttle_state.as_dict() + def _check_throttled_state(self): command = self._settings.get(["vcgencmd_throttle_check_command"]) @@ -299,5 +305,11 @@ 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..30e65d46b 100644 --- a/src/octoprint/plugins/tracking/__init__.py +++ b/src/octoprint/plugins/tracking/__init__.py @@ -82,15 +82,23 @@ class TrackingPlugin(octoprint.plugin.SettingsPlugin, self._environment = environment ##~~ StartupPlugin + def on_startup(self, *args, **kwargs): + self._helpers_get_throttle_state = None 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): @@ -160,8 +168,12 @@ class TrackingPlugin(octoprint.plugin.SettingsPlugin, freq=self._environment[b"hardware"][b"freq"], ram=self._environment[b"hardware"][b"ram"]) + if self._helpers_get_throttle_state: + payload[b"throttle_state"] = self._helpers_get_throttle_state() + 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"] @@ -219,6 +231,11 @@ class TrackingPlugin(octoprint.plugin.SettingsPlugin, track_event = "print_failed" elif event == Events.PRINT_CANCELLED: track_event = "print_cancelled" + else: + logger.info("unknown event, skipping: {}".format(event)) + + if self._helpers_get_throttle_state: + args[b"throttle_state"] = self._helpers_get_throttle_state() if track_event is not None: self._track(track_event, **args) @@ -254,7 +271,7 @@ class TrackingPlugin(octoprint.plugin.SettingsPlugin, params=params, timeout=3.1, headers=headers) - self._logger.debug("Sent tracking event to {}".format(url)) + self._logger.info("Sent tracking event to {}, params: {}".format(url, kwargs)) except: if self._logger.isEnabledFor(logging.DEBUG): self._logger.exception("Error while sending event to anonymous usage tracking".format(url))