Skip to content

Commit

Permalink
store settings in local preferences instead of sharable metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Kriechi committed Jan 11, 2021
1 parent 56b6851 commit c0e6a48
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 148 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog of Cura-DuetRRFPlugin

## v1.2.0: 2020-XX-XX
* store settings in local preferences instead of sharable metadata

## v1.1.0: 2020-11-13
* BREAKING CHANGE: migrate settings to be printer-specific
* auto-migrate legacy settings for active printers if printer name matches
Expand Down
93 changes: 49 additions & 44 deletions DuetRRFAction.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import os
import json
import re
from typing import Dict, Type, TYPE_CHECKING, List, Optional, cast

from PyQt5.QtCore import QObject, pyqtSlot
from PyQt5.QtCore import QObject, pyqtSlot, pyqtProperty, pyqtSignal

from cura.CuraApplication import CuraApplication
from cura.MachineAction import MachineAction
Expand All @@ -13,6 +14,8 @@
from UM.i18n import i18nCatalog
catalog = i18nCatalog("cura")

from .DuetRRFSettings import delete_config, get_config, save_config


class DuetRRFAction(MachineAction):
def __init__(self, parent: QObject = None) -> None:
Expand All @@ -22,6 +25,13 @@ def __init__(self, parent: QObject = None) -> None:
self._application = CuraApplication.getInstance()

ContainerRegistry.getInstance().containerAdded.connect(self._onContainerAdded)
CuraApplication.getInstance().globalContainerStackChanged.connect(self._onGlobalContainerStackChanged)

def _onGlobalContainerStackChanged(self) -> None:
self.printerSettingsUrlChanged.emit()
self.printerSettingsDuetPasswordChanged.emit()
self.printerSettingsHTTPUserChanged.emit()
self.printerSettingsHTTPPasswordChanged.emit()

def _onContainerAdded(self, container: "ContainerInterface") -> None:
# Add this action as a supported action to all machine definitions
Expand All @@ -32,64 +42,59 @@ def _onContainerAdded(self, container: "ContainerInterface") -> None:
):
self._application.getMachineActionManager().addSupportedAction(container.getId(), self.getKey())

@pyqtSlot(result=str)
def printerSettingUrl(self):
global_container_stack = self._application.getGlobalContainerStack()
if global_container_stack:
return global_container_stack.getMetaDataEntry("duetrrf_url", "")
def _reset(self) -> None:
self.printerSettingsUrlChanged.emit()
self.printerSettingsDuetPasswordChanged.emit()
self.printerSettingsHTTPUserChanged.emit()
self.printerSettingsHTTPPasswordChanged.emit()

printerSettingsUrlChanged = pyqtSignal()
printerSettingsDuetPasswordChanged = pyqtSignal()
printerSettingsHTTPUserChanged = pyqtSignal()
printerSettingsHTTPPasswordChanged = pyqtSignal()

@pyqtProperty(str, notify=printerSettingsUrlChanged)
def printerSettingUrl(self) -> Optional[str]:
s = get_config()
if s:
return s["url"]
return ""

@pyqtSlot(result=str)
def printerSettingDuetPassword(self):
global_container_stack = self._application.getGlobalContainerStack()
if global_container_stack:
return global_container_stack.getMetaDataEntry("duetrrf_duet_password", "")
@pyqtProperty(str, notify=printerSettingsDuetPasswordChanged)
def printerSettingDuetPassword(self) -> Optional[str]:
s = get_config()
if s:
return s["duet_password"]
return ""

@pyqtSlot(result=str)
def printerSettingHTTPUser(self):
global_container_stack = self._application.getGlobalContainerStack()
if global_container_stack:
return global_container_stack.getMetaDataEntry("duetrrf_http_user", "")
@pyqtProperty(str, notify=printerSettingsHTTPUserChanged)
def printerSettingHTTPUser(self) -> Optional[str]:
s = get_config()
if s:
return s["http_user"]
return ""

@pyqtSlot(result=str)
def printerSettingHTTPPassword(self):
global_container_stack = self._application.getGlobalContainerStack()
if global_container_stack:
return global_container_stack.getMetaDataEntry("duetrrf_http_password", "")
@pyqtProperty(str, notify=printerSettingsHTTPPasswordChanged)
def printerSettingHTTPPassword(self) -> Optional[str]:
s = get_config()
if s:
return s["http_password"]
return ""

@pyqtSlot(str, str, str, str)
def testAndSave(self, url, duet_password, http_user, http_password):
def saveConfig(self, url, duet_password, http_user, http_password):
if not url.endswith('/'):
url += '/'

global_container_stack = self._application.getGlobalContainerStack()
if not global_container_stack:
Logger.log("e", "failed to save config: global_container_stack is missing")
return

global_container_stack.setMetaDataEntry("duetrrf", True)
global_container_stack.setMetaDataEntry("duetrrf_url", url)
global_container_stack.setMetaDataEntry("duetrrf_duet_password", duet_password)
global_container_stack.setMetaDataEntry("duetrrf_http_user", http_user)
global_container_stack.setMetaDataEntry("duetrrf_http_password", http_password)
Logger.log("d", "config saved: " + global_container_stack.getName())
save_config(url, duet_password, http_user, http_password)
Logger.log("d", "config saved")

@pyqtSlot()
def deleteConfig(self):
global_container_stack = self._application.getGlobalContainerStack()
if not global_container_stack:
Logger.log("e", "failed to delete config: global_container_stack is missing")
return

global_container_stack.removeMetaDataEntry("duetrrf")
global_container_stack.removeMetaDataEntry("duetrrf_url")
global_container_stack.removeMetaDataEntry("duetrrf_duet_password")
global_container_stack.removeMetaDataEntry("duetrrf_http_user")
global_container_stack.removeMetaDataEntry("duetrrf_http_password")
Logger.log("d", "config deleted: " + global_container_stack.getName())
if delete_config():
Logger.log("d", "config deleted")
else:
Logger.log("d", "no config to delete")

@pyqtSlot(str, result=bool)
def validUrl(self, newUrl):
Expand Down
24 changes: 12 additions & 12 deletions DuetRRFOutputDevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,17 @@ class DuetRRFDeviceType(Enum):


class DuetRRFOutputDevice(OutputDevice):
def __init__(self, name_id, device_type):
self._name_id = name_id
super().__init__(name_id)
def __init__(self, settings, device_type):
self._name_id = "duetrrf-{}".format(device_type)
self._url = settings["url"]
self._duet_password = settings["duet_password"]
self._http_user = settings["http_user"]
self._http_password = settings["http_password"]

self._application = CuraApplication.getInstance()
global_container_stack = self._application.getGlobalContainerStack()
super().__init__(self._name_id)

application = CuraApplication.getInstance()
global_container_stack = application.getGlobalContainerStack()
self._name = global_container_stack.getName()

self._device_type = device_type
Expand All @@ -69,11 +74,6 @@ def __init__(self, name_id, device_type):
self._stream = None
self._message = None

self._url = global_container_stack.getMetaDataEntry("duetrrf_url", "")
self._duet_password = global_container_stack.getMetaDataEntry("duetrrf_duet_password", "")
self._http_user = global_container_stack.getMetaDataEntry("duetrrf_http_user", "")
self._http_password = global_container_stack.getMetaDataEntry("duetrrf_http_password", "")

self._use_rrf_http_api = True # by default we try to connect to the RRF HTTP API via rr_connect

Logger.log("d",
Expand Down Expand Up @@ -177,7 +177,7 @@ def onFilenameAccepted(self):
self.writeStarted.emit(self)

# show a progress message
self._message = Message(catalog.i18nc("@info:progress", "Uploading to {}").format(self._name), 0, False, -1)
self._message = Message(catalog.i18nc("@info:progress", "Uploading to {}...").format(self._name), 0, False, -1)
self._message.show()

Logger.log("d", "Loading gcode...")
Expand Down Expand Up @@ -320,7 +320,7 @@ def onPrintStarted(self, reply):
self._message.hide()
self._message = None

text = "Print started on {} with file {}".format(self._name, self._fileName)
text = "Print started on {} with file {}.".format(self._name, self._fileName)
self._message = Message(catalog.i18nc("@info:status", text), 0, False)
self._message.addAction("open_browser", catalog.i18nc("@action:button", "Open Browser"), "globe", catalog.i18nc("@info:tooltip", "Open browser to DuetWebControl."))
self._message.actionTriggered.connect(self._onMessageActionTriggered)
Expand Down
Loading

0 comments on commit c0e6a48

Please sign in to comment.