Skip to content

Commit

Permalink
Merge branch 'development' v1.16.0
Browse files Browse the repository at this point in the history
  • Loading branch information
j7126 committed Nov 9, 2020
2 parents adbb3c0 + a44d0af commit 25025f5
Show file tree
Hide file tree
Showing 8 changed files with 1,507 additions and 1,222 deletions.
118 changes: 87 additions & 31 deletions octoprint_dashboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
import subprocess
import json
import platform
import flask

class DashboardPlugin(octoprint.plugin.SettingsPlugin,
octoprint.plugin.StartupPlugin,
octoprint.plugin.AssetPlugin,
octoprint.plugin.TemplatePlugin,
octoprint.plugin.EventHandlerPlugin):
octoprint.plugin.EventHandlerPlugin,
octoprint.plugin.SimpleApiPlugin):

printer_message = ""
extruded_filament = 0.0
Expand All @@ -34,8 +36,8 @@ class DashboardPlugin(octoprint.plugin.SettingsPlugin,
disk_usage = 0
layer_times = []
layer_labels = []
cmd_commands= []
cmd_results = []
cmd_commands = []
cmd_timers = []

if noAccessPermissions == False:
def get_additional_permissions(*args, **kwargs):
Expand Down Expand Up @@ -72,40 +74,77 @@ def psUtilGetStats(self):
self.virtual_memory_percent = str(psutil.virtual_memory().percent)
self.disk_usage = str(psutil.disk_usage("/").percent)

def cmdGetStats(self):
#self._logger.info("Running Dashboard Commands: " + str(self.cmd_commands))
del self.cmd_results[:]
def runCmd(self, cmdIndex):
cmd = self.cmd_commands[cmdIndex].get("command")

process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
stdout, stderr = process.communicate()
if (sys.version_info > (3, 5)):
# Python 3.5
result = stdout.strip().decode('ascii') + stderr.strip().decode('ascii')
else:
# Python 2
result = stdout.strip() + stderr.strip()

results = {"index": cmdIndex, "result": result}

self._plugin_manager.send_plugin_message(self._identifier, dict(cmdResults=json.dumps(results)))

def testCmd(self, cmd):
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
stdout, stderr = process.communicate()
if (sys.version_info > (3, 5)):
# Python 3.5
result = stdout.strip().decode('ascii') + stderr.strip().decode('ascii')
else:
# Python 2
result = stdout.strip() + stderr.strip()

results = {"result": result}

self._plugin_manager.send_plugin_message(self._identifier, dict(cmdTest=json.dumps(results)))

def updateCmds(self):
for timer in self.cmd_timers:
timer.cancel()

del self.cmd_timers[:]

index = 0
for command in self.cmd_commands:
#self._logger.info("Running Dashboard Command: " + command.get("command") )
process = subprocess.Popen(command.get("command"), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
stdout, stderr = process.communicate()
if (sys.version_info > (3, 5)):
# Python 3.5
result = stdout.strip().decode('ascii') + stderr.strip().decode('ascii')
else:
# Python 2
result = stdout.strip() + stderr.strip()
#self._logger.info("Result: " + result)
self.cmd_results.append(result)
self._plugin_manager.send_plugin_message(self._identifier, dict(cmdResults=json.dumps(self.cmd_results)))
t = ResettableTimer(60.0, self.cmdGetStats)
t.daemon = True
t.start()
if (command.get("enabled")):

t = RepeatedTimer(float(command.get("interval")), self.runCmd, [index], run_first=True)
t.start()
self.cmd_timers.append(t)

index += 1

##~~ SimpleApiPlugin mixin
def get_api_commands(self):
return dict(
testCmdWidget=["cmd"]
)

def on_api_command(self, command, data):
if command == "testCmdWidget":
if (noAccessPermissions or Permissions.PLUGIN_DASHBOARD_ADMIN.can()):
self._logger.info("testCmdWidget called, cmd is {cmd}".format(**data))
self.testCmd(data["cmd"])
else:
self._logger.info("testCmdWidget called, but without proper permissions")

# ~~ StartupPlugin mixin
def on_after_startup(self):
self._logger.info("Dashboard started")
self.cmd_commands = self._settings.get(["commandWidgetArray"])
self.cmdGetStats()
self.updateCmds()
self.timer = RepeatedTimer(3.0, self.send_notifications, run_first=True)
self.timer.start()


def send_notifications(self):
self.psUtilGetStats()
#self.cmdGetStats()
#self._logger.info(str(self.cmd_results))
self._plugin_manager.send_plugin_message(self._identifier, dict(cpuPercent=str(self.cpu_percent),
virtualMemPercent=str(self.virtual_memory_percent),
diskUsagePercent=str(self.disk_usage),
Expand All @@ -114,8 +153,7 @@ def send_notifications(self):
extrudedFilament=str( round( (sum(self.extruded_filament_arr) + self.extruded_filament) / 1000, 2) ),
layerTimes=str(self.layer_times),
layerLabels=str(self.layer_labels),
printerMessage=str(self.printer_message),
cmdResults=json.dumps(self.cmd_results)))
printerMessage=str(self.printer_message)))

def on_event(self, event, payload):
if event == "DisplayLayerProgress_layerChanged" or event == "DisplayLayerProgress_fanspeedChanged" or event == "DisplayLayerProgress_heightChanged":
Expand All @@ -131,7 +169,7 @@ def on_event(self, event, payload):
lastLayerDurationInSeconds=payload.get('lastLayerDurationInSeconds'),
averageLayerDuration=payload.get('averageLayerDuration'),
averageLayerDurationInSeconds=payload.get('averageLayerDurationInSeconds'),
changeFilamentTimeLeftInSeconds=payload.get('changeFilamentTimeLeftInSeconds'),
changeFilamentTimeLeft=payload.get('changeFilamentTimeLeft'),
changeFilamentCount=payload.get('changeFilamentCount')))

if event == "DisplayLayerProgress_layerChanged" and payload.get('lastLayerDurationInSeconds') != "-" and int(payload.get('lastLayerDurationInSeconds')) > 0:
Expand Down Expand Up @@ -165,11 +203,13 @@ def get_settings_defaults(self):
showWebCam=False,
showSystemInfo=False,
showProgress=True,
showTimeProgress=True,
showLayerProgress=False,
hideHotend=False,
supressDlpWarning=False,
showFullscreen=True,
showFilament=True,
showFilamentChangeTime=True,
showLayerGraph=False,
layerGraphType="normal",
showPrinterMessage=False,
Expand All @@ -178,14 +218,17 @@ def get_settings_defaults(self):
cpuTempWarningThreshold="70",
cpuTempCriticalThreshold="85",
showTempGaugeColors=False,
enableTempGauges=True,
targetTempDeviation="10",
useThemeifyColor=True,
showCommandWidgets=False,
disableWebcamNonce=False,
commandWidgetArray=[dict(
icon='command-icon.png',
name='Default',
command="echo 9V")],
command="echo 9V",
enabled=False,
interval="10")],
enableDashMultiCam=False,
_webcamArray=[dict(
name='Default',
Expand All @@ -195,8 +238,20 @@ def get_settings_defaults(self):
rotate=octoprint.settings.settings().get(["webcam","rotate90"]),
disableNonce=False,
streamRatio=octoprint.settings.settings().get(["webcam","streamRatio"]),
)]

)],
defaultWebcam=0,
dashboardOverlayFull=False,
fsSystemInfo=True,
fsTempGauges=True,
fsFan=True,
fsCommandWidgets=True,
fsJobControlButtons=False,
fsSensorInfo=True,
fsPrinterMessage=True,
fsProgressGauges=True,
fsLayerGraph=False,
fsFilament=True,
fsWebCam=True
)

def on_settings_save(self, data):
Expand All @@ -205,9 +260,10 @@ def on_settings_save(self, data):
del data['commandWidgetArray']
except:
pass
self._logger.info(str(data))
#self._logger.info(str(data))
octoprint.plugin.SettingsPlugin.on_settings_save(self, data)
self.cmd_commands = self._settings.get(["commandWidgetArray"])
self.updateCmds()
#FIXME: Are these still needed?
self.dht_sensor_pin = self._settings.get(["dhtSensorPin"])
self.dht_sensor_type = self._settings.get(["dhtSensorType"])
Expand Down
Loading

0 comments on commit 25025f5

Please sign in to comment.