Skip to content

Commit

Permalink
Merge pull request #994 from mrbeam/mrbeam2-beta
Browse files Browse the repository at this point in the history
v0.7.5
  • Loading branch information
irlaec authored Sep 15, 2020
2 parents 6a35b63 + c5d6d4b commit f5831a6
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 52 deletions.
6 changes: 5 additions & 1 deletion octoprint_mrbeam/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,11 @@ def on_settings_save(self, data):
if "backlash_compensation_x" in data['machine']:
min_mal = -1.0
max_val = 1.0
val = data['machine']['backlash_compensation_x']
val = 0.0
try:
val = float(data['machine']['backlash_compensation_x'])
except:
pass
val = max(min(max_val, val), min_mal)
self._settings.set_float(["machine", "backlash_compensation_x"], val)
if "analyticsEnabled" in data:
Expand Down
2 changes: 1 addition & 1 deletion octoprint_mrbeam/__version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.7.4"
__version__ = "0.7.5"
28 changes: 1 addition & 27 deletions octoprint_mrbeam/analytics/analytics_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,33 +296,6 @@ def add_camera_image(self, payload):
except Exception as e:
self._logger.exception('Exception during add_camera_image: {}'.format(e), analytics=True)

# IOBEAM_HANDLER
def add_iobeam_message_log(self, iobeam_version, message, from_plugin=False):
try:
iobeam_data = {
ak.Log.Iobeam.VERSION: iobeam_version,
ak.Log.Iobeam.MESSAGE: message,
ak.Log.Iobeam.FROM_PLUGIN: from_plugin,
}

self._add_log_event(ak.Log.Event.IOBEAM, payload=iobeam_data)
except Exception as e:
self._logger.exception('Exception during add_iobeam_message_log: {}'.format(e), analytics=True)

def add_iobeam_i2c_monitoring(self, iobeam_version, state, method, current_devices, lost_devices, new_devices):
try:
iobeam_data = {
ak.Log.I2cMonitoring.VERSION: iobeam_version,
ak.Log.I2cMonitoring.STATE: state,
ak.Log.I2cMonitoring.METHOD: method,
ak.Log.I2cMonitoring.CURRENT_DEVICES: current_devices,
ak.Log.I2cMonitoring.LOST_DEVICES: lost_devices,
ak.Log.I2cMonitoring.NEW_DEVICES: new_devices,
}
self._add_log_event(ak.Log.Event.I2C_MONITORING, payload=iobeam_data)
except Exception as e:
self._logger.exception('Exception during add_iobeam_i2c_monitoring: {}'.format(e), analytics=True)

# ACC_WATCH_DOG
def add_cpu_log(self, temp, throttle_alerts):
try:
Expand Down Expand Up @@ -615,6 +588,7 @@ def _add_other_plugin_data(self, event, event_payload):
if plugin:
event_name = event_payload.get('eventname')
data = event_payload.get('data', None)
data.update({'plugin_version': event_payload.get('plugin_version', None)})
self._add_event_to_queue(plugin, event_name, payload=data)
else:
self._logger.warn("Invalid plugin: '%s'. payload: %s", plugin, event_payload)
Expand Down
15 changes: 0 additions & 15 deletions octoprint_mrbeam/analytics/analytics_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,10 @@ class Log:

class Event:
EVENT_LOG = 'log_event'
IOBEAM = 'iobeam'
CPU = 'cpu'
CAMERA = 'camera'
OS_HEALTH = 'os_health'
ANALYTICS_FILE_CROP = 'analytics_file_crop'
I2C_MONITORING = 'i2c_monitoring'

class Level:
EXCEPTION = 'exception'
Expand All @@ -175,19 +173,6 @@ class Caller:
LINE = 'line'
FUNCTION = 'function'

class Iobeam:
VERSION = 'version'
MESSAGE = 'message'
FROM_PLUGIN = 'from_plugin'

class I2cMonitoring:
VERSION = 'version'
STATE = 'state'
METHOD = 'method'
CURRENT_DEVICES = 'current_devices'
LOST_DEVICES = 'lost_devices'
NEW_DEVICES = 'new_devices'

class Cpu:
TEMP = 'temp'
THROTTLE_ALERTS = 'throttle_alerts'
Expand Down
6 changes: 6 additions & 0 deletions octoprint_mrbeam/analytics/usage_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ def _set_time(self, job_duration):
)
self._analytics_handler.add_job_ntp_sync_details(ntp_details)

self._logger.info('NTP shift fix - Job duration before: {}, after: {} --> shift: {}'.format(
ntp_details.get('time_shift'),
ntp_details.get('job_duration_before'),
ntp_details.get('job_duration_after'),
))

dust_factor = self._calculate_dust_factor()
self._usage_data['total']['job_time'] = self.start_time_total + job_duration
self._usage_data['laser_head'][self._laser_head_serial]['job_time'] = \
Expand Down
8 changes: 6 additions & 2 deletions octoprint_mrbeam/iobeam/hw_malfunction_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def __init__(self, plugin):
self._event_bus.subscribe(MrBeamEvents.MRB_PLUGIN_INITIALIZED, self._on_mrbeam_plugin_initialized)

def _on_mrbeam_plugin_initialized(self, event, payload):
self._analytics_handler = self._plugin.analytics_handler
self._iobeam_handler = self._plugin.iobeam

def report_hw_malfunction_from_plugin(self, malfunction_id, msg, payload=None):
Expand All @@ -66,7 +65,12 @@ def report_hw_malfunction(self, dataset, from_plugin=False):
self._messages_to_show[malfunction_id] = data
self._plugin.fire_event(MrBeamEvents.HARDWARE_MALFUNCTION, dict(id=malfunction_id, msg=msg, data=data))

self._analytics_handler.add_iobeam_message_log(self._iobeam_handler.iobeam_version, dataset, from_plugin=from_plugin)
dataset.update({'from_plugin': from_plugin})
self._iobeam_handler.send_iobeam_analytics(
eventname='hw_malfunction',
data=dataset,
)

self._start_notification_timer()

def _start_notification_timer(self):
Expand Down
57 changes: 51 additions & 6 deletions octoprint_mrbeam/iobeam/iobeam_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class IoBeamHandler(object):
DATASET_I2C = "i2c"
DATASET_I2C_MONITORING = "i2c_monitoring"
DATASET_REED_SWITCH = "reed_switch"
DATASET_ANALYTICS = "analytics"

def __init__(self, plugin):
self._plugin = plugin
Expand All @@ -169,7 +170,6 @@ def __init__(self, plugin):
self._callbacks_lock = RWLock()

self._laserhead_handler = None
self._analytics_handler = None

self.iobeam_version = None

Expand All @@ -188,7 +188,6 @@ def __init__(self, plugin):

def _on_mrbeam_plugin_initialized(self, event, payload):
self._laserhead_handler = self._plugin.laserhead_handler
self._analytics_handler = self._plugin.analytics_handler
self._hw_malfunction_handler = self._plugin.hw_malfunction_handler
self._user_notification_system = self._plugin.user_notification_system

Expand Down Expand Up @@ -242,6 +241,13 @@ def send_compressor_command(self, value=0):
self._logger.info("send_compressor_command(): succ: %s, command: %s",succ, command)
return succ, command['request_id']

def send_analytics_request(self, *args, **kwargs):
"""
Requests a analytics dataset from iobeam
:return: True if the command was sent successful (does not mean it was successfully executed)
"""
return self._send_command(self.get_request_msg([self.DATASET_ANALYTICS]))

def _send_command(self, command):
"""
Sends a command to iobeam
Expand Down Expand Up @@ -357,6 +363,10 @@ def __execute_callback_called_by_new_thread(self, _trigger_event, acquire_lock,

def _subscribe(self):
self._event_bus.subscribe(OctoPrintEvents.SHUTDOWN, self.shutdown)
self._event_bus.subscribe(OctoPrintEvents.PRINT_DONE, self.send_analytics_request)
self._event_bus.subscribe(OctoPrintEvents.PRINT_FAILED, self.send_analytics_request)
self._event_bus.subscribe(OctoPrintEvents.PRINT_CANCELLED, self.send_analytics_request)
self._event_bus.subscribe(OctoPrintEvents.ERROR, self.send_analytics_request)

def _initWorker(self, socket_file=None):
self._logger.debug("initializing worker thread")
Expand Down Expand Up @@ -590,6 +600,8 @@ def _handle_dataset(self, name, dataset):
err = self._handle_i2c_monitoring(dataset)
elif name == self.DATASET_REED_SWITCH:
err = self._handle_reed_switch(dataset)
elif name == self.DATASET_ANALYTICS:
err = self._handle_analytics_dataset(dataset)
elif name == self.MESSAGE_DEVICE_UNUSED:
pass
elif name == self.MESSAGE_ERROR:
Expand Down Expand Up @@ -680,15 +692,20 @@ def _handle_i2c_monitoring(self, dataset):
self._logger.error("i2c_monitoring state change reported: %s", dataset, analytics=False)
if not self._last_i2c_monitoring_dataset is None and not self._last_i2c_monitoring_dataset.get('state', None) == dataset.get('state', None):
dataset_data = dataset.get('data', dict())
params = dict(iobeam_version=self.iobeam_version,
params = dict(
state=dataset.get('state', None),
method=dataset_data.get('test_mode', None),
current_devices=dataset_data.get('current_devices', []),
lost_devices=dataset_data.get('lost_devices', []),
new_devices=dataset_data.get('new_devices', []))
self._analytics_handler.add_iobeam_i2c_monitoring(**params)
new_devices=dataset_data.get('new_devices', [])
)

self.send_iobeam_analytics(
eventname='i2c_monitoring',
data=params
)
self._last_i2c_monitoring_dataset = dataset

def _handle_reed_switch(self, dataset):
self._logger.info("reed_switch: %s", dataset)
return 0
Expand Down Expand Up @@ -875,6 +892,14 @@ def _handle_hw_malfunction(self, dataset):
def _handle_i2c(self, dataset):
self._logger.info("i2c_state: %s", dataset)

def _handle_analytics_dataset(self, dataset):
if dataset.get('communication_errors', None):
self.send_iobeam_analytics(
eventname='communication_errors',
data=dataset.get('communication_errors')
)
return 0

def _handle_debug(self, dataset):
"""
Handle debug dataset
Expand Down Expand Up @@ -1109,3 +1134,23 @@ def _get_connected_val(self, value):
elif value == 'true':
connected = True
return connected

def send_iobeam_analytics(self, eventname, data):
"""
This will send analytics data using the MrBeam event system, to mimic what the rest of the plugins do.
Everything will be saved to the type='iobeam'.
Args:
eventname: the name of the event for Datastore ('e')
data: the payload of the event for Datastore ('data')
Returns:
"""
payload = dict(
plugin='iobeam',
plugin_version=self.iobeam_version,
eventname=eventname,
data=data,
)

self._plugin.fire_event(MrBeamEvents.ANALYTICS_DATA, payload)

0 comments on commit f5831a6

Please sign in to comment.