✨ Protocol & Transport settings dialog

This commit is contained in:
Gina Häußge committed 2021-07-16 16:02:14 +02:00
1 parent bd443e2efc
commit e77f5b7385
8 files changed
+361 -111

No files matched your search

-3
View File
@@ -1,8 +1,5 @@
# General TODOs
* Settings API for transport/protocol settings
* UI for connection profiles
* UI for transport/protocol settings
* SerialTransport:
* RepRapProtocol:
* Anything missing from settings migration to profile?
+5 -6
View File
@@ -24,6 +24,7 @@ from octoprint.server.util.flask import (
with_revalidation_checking,
)
from octoprint.settings import settings
from octoprint.settings.parameters import get_param_structure
from octoprint.util import dict_merge
##~~ Connection state & commands
@@ -353,7 +354,8 @@ def _get_protocols():
{
"name": protocol.name,
"key": protocol.key,
"options": _convert_options(protocol.get_connection_options()),
"options": get_param_structure(protocol.get_connection_options()),
"settings": get_param_structure(protocol.settings),
}
)
return protocols
@@ -366,11 +368,8 @@ def _get_transports():
{
"name": transport.name,
"key": transport.key,
"options": _convert_options(transport.get_connection_options()),
"options": get_param_structure(transport.get_connection_options()),
"settings": get_param_structure(transport.settings),
}
)
return transports
def _convert_options(options):
return [option.as_dict() for option in options]
+98 -1
View File
@@ -15,6 +15,11 @@ from octoprint.server import pluginManager, userManager
from octoprint.server.api import api
from octoprint.server.util.flask import no_firstrun_access, with_revalidation_checking
from octoprint.settings import settings, valid_boolean_trues
from octoprint.settings.parameters import (
get_param_dict,
get_param_structure,
save_params_to_settings,
)
# ~~ settings
@@ -248,10 +253,12 @@ def getSettings():
"resendRatioStart": s.getInt(["serial", "resendRatioStart"]),
},
"connection": {
"protocols": _get_protocol_settings(),
"transports": _get_transport_settings(),
"log": {
"connection": s.getBoolean(["connection", "log", "connection"]),
"commdebug": s.getBoolean(["connection", "log", "commdebug"]),
}
},
},
"folder": {
"uploads": s.getBaseFolder("uploads"),
@@ -372,6 +379,50 @@ def _get_plugin_settings():
return data
def _get_protocol_settings():
from octoprint.comm.protocol import all_protocols
current = settings().get(["connection", "protocols"], merged=True)
result = {}
for protocol in all_protocols():
result[protocol.key] = get_param_dict(
current.get(protocol.key, {}), protocol.settings
)
return result
def _get_protocol_structure():
from octoprint.comm.protocol import all_protocols
result = {}
for protocol in all_protocols():
result[protocol.key] = get_param_structure(protocol.settings)
return result
def _get_transport_settings():
from octoprint.comm.transport import all_transports
current = settings().get(["connection", "transports"], merged=True)
result = {}
for transport in all_transports():
result[transport.key] = get_param_dict(
current.get(transport.key, {}), transport.settings
)
return result
def _get_transport_structure():
from octoprint.comm.transport import all_transports
result = {}
for transport in all_transports():
result[transport.key] = get_param_structure(transport.settings)
return result
@api.route("/settings", methods=["POST"])
@no_firstrun_access
@Permissions.SETTINGS.require(403)
@@ -389,6 +440,19 @@ def setSettings():
return getSettings()
@api.route("/settings/structure", methods=["GET"])
@no_firstrun_access
@Permissions.SETTINGS.require(403)
def fetchSettingsStructure():
structure = {
"connection": {
"protocols": _get_protocol_structure(),
"transports": _get_transport_structure(),
},
}
return jsonify(structure)
@api.route("/settings/templates", methods=["GET"])
@no_firstrun_access
@Permissions.SETTINGS.require(403)
@@ -959,6 +1023,12 @@ def _saveSettings(data):
toggle_log("connection", "CONNECTION")
toggle_log("commdebug", "COMMDEBUG")
if "protocols" in data["connection"]:
_set_protocol_settings(data["connection"]["protocols"])
if "transports" in data["connection"]:
_set_transport_settings(data["connection"]["transports"])
if "temperature" in data:
if "profiles" in data["temperature"]:
result = []
@@ -1134,3 +1204,30 @@ def _saveSettings(data):
)
s.save(trigger_event=True)
def _set_protocol_settings(data):
from octoprint.comm.protocol import all_protocols
base_path = ["connection", "protocols"]
for protocol in all_protocols():
if protocol.key in data:
save_params_to_settings(
protocol.settings, settings(), base_path + [protocol.key]
)
def _set_transport_settings(data):
from octoprint.comm.transport import all_transports
base_path = ["connection", "transports"]
for transport in all_transports():
if transport.key in data:
save_params_to_settings(
data[transport.key],
transport.settings,
settings(),
base_path + [transport.key],
)
+1 -1
View File
@@ -976,7 +976,7 @@ def fetch_template_data(refresh=False):
{
"template": "dialogs/settings/connection.jinja2",
"_div": "settings_connection",
"custom_bindings": False,
"custom_bindings": True,
},
),
"connectionprofiles": (
+47
View File
@@ -46,6 +46,53 @@ def get_param_dict(data, options):
return result
def get_param_structure(options):
return [option.as_dict() for option in options]
def save_params_to_settings(data, options, settings, path):
if data is None:
data = {}
for option in options:
if option.type == "group":
save_params_to_settings(
data.get(option.name), option.params, settings, path + [option.name]
)
elif option.type == "presetchoice":
save_params_to_settings(
data.get(option.group.name),
option.group.params,
settings,
path + [option.group.name],
)
elif option.type == "conditionalgroup":
save_params_to_settings(
data.get(option.name),
option.groups[option.value],
settings,
path + [option.name],
)
else:
save_param_to_settings(
data.get(option.name), option, settings, path + [option.name]
)
def save_param_to_settings(value, option, settings, path):
if value is None:
return
if option.type == "integer":
settings.setInt(path, value)
elif option.type == "float":
settings.setFloat(path, value)
elif option.type == "boolean":
settings.setBoolean(path, value)
else:
settings.set(path, value)
class ParamType:
type = "generic"
@@ -13,7 +13,7 @@ $(function () {
});
};
var convertValue = function (value, option, override) {
var convertValueFromBackend = function (value, option, override) {
/**
* Picks the right value to apply based on value, defaults and overrides.
*
@@ -34,13 +34,30 @@ $(function () {
value = option.default;
}
if (value && (option.type === "list" || option.type === "smalllist")) {
value = value.join(", ");
if (value) {
if (option.type === "list") {
value = value.join("\n");
} else if (option.type === "smalllist") {
value = value.join(",");
}
}
return value;
};
var convertValueToBackend = function (value, option) {
if (option.type === "list") {
value = splitTextToArray(value, "\n", true);
} else if (option.type === "smalllist") {
value = splitTextToArray(value, ",", true);
} else if (option.type === "integer") {
value = parseInt(value);
} else if (option.type === "float") {
value = parseFloat(value);
}
return value;
};
var setOptionValue = function (option, value, data) {
/**
* Sets the given value on the given option.
@@ -66,7 +83,11 @@ $(function () {
});
} else {
if (option.type === "presetchoice") {
value = convertValue(value, option, data ? data[option.name] : undefined);
value = convertValueFromBackend(
value,
option,
data ? data[option.name] : undefined
);
_.each(option.group.params, function (p) {
setOptionValue(
p,
@@ -77,7 +98,11 @@ $(function () {
);
});
} else if (option.type === "conditionalgroup") {
value = convertValue(value, option, data ? data[option.name] : undefined);
value = convertValueFromBackend(
value,
option,
data ? data[option.name] : undefined
);
option.expertParameters = {};
_.each(option.groups, function (group, key) {
_.each(group, function (p) {
@@ -88,7 +113,11 @@ $(function () {
});
});
} else {
value = convertValue(value, option, data ? data[option.name] : undefined);
value = convertValueFromBackend(
value,
option,
data ? data[option.name] : undefined
);
}
option.value(value);
@@ -107,17 +136,11 @@ $(function () {
} else if (parameter.type === "conditionalgroup") {
value = parameter.value();
result = Object.assign(result, toOptions(parameter.groups[value]));
} else if (parameter.type === "list" || parameter.type === "smalllist") {
value = splitTextToArray(parameter.value(), ",", true);
} else if (parameter.type === "integer") {
value = parseInt(parameter.value());
} else if (parameter.type === "float") {
value = parseFloat(parameter.value());
} else {
value = parameter.value();
value = convertValueToBackend(parameter.value(), parameter);
}
// and now set the value on the result, if it differs from the default
// set the value on the result, if it differs from the default
if (parameter.modified()) {
result[parameter.name] = value;
}
@@ -421,8 +444,17 @@ $(function () {
self.selectedProtocol = ko.observable();
self.preferredProtocol = ko.observable();
self.protocolParameters = ko.observable();
self.advancedProtocolParameters = ko.observable();
self.expertProtocolParameters = ko.observable();
self.advancedProtocolParameters = ko.pureComputed(function () {
return _.any(self.protocolParameters(), function (p) {
return p.advanced;
});
});
self.expertProtocolParameters = ko.pureComputed(function () {
return _.any(self.protocolParameters(), function (p) {
return p.expert;
});
});
self.showAdvancedProtocolOptions = function () {
$("#connection_protocol_dialog").modal("show");
};
@@ -441,17 +473,7 @@ $(function () {
if (protocol) {
protocolOptions = protocol.options;
}
self.advancedProtocolParameters(
_.any(protocolOptions, function (p) {
return p.advanced;
})
);
self.expertProtocolParameters(
_.any(protocolOptions, function (p) {
return p.expert;
})
);
self.protocolParameters(protocolOptions);
var connectionProfile = self.selectedConnectionProfile();
self.sychronizeSettings(
@@ -471,8 +493,16 @@ $(function () {
self.selectedTransport = ko.observable();
self.preferredTransport = ko.observable();
self.transportParameters = ko.observableArray();
self.advancedTransportParameters = ko.observable();
self.expertTransportParameters = ko.observable();
self.advancedTransportParameters = ko.pureComputed(function () {
return _.any(self.transportParameters(), function (p) {
return p.advanced;
});
});
self.expertTransportParameters = ko.pureComputed(function () {
return _.any(self.transportParameters(), function (p) {
return p.expert;
});
});
self.showAdvancedTransportOptions = function () {
$("#connection_transport_dialog").modal("show");
};
@@ -487,22 +517,11 @@ $(function () {
self.selectedTransport.subscribe(function () {
var transport = self.selectedTransport();
var transportOptions = [];
if (transport) {
self.transportParameters(transport.options);
} else {
self.transportParameters([]);
transportOptions = transport.options;
}
self.advancedTransportParameters(
_.any(self.transportParameters(), function (p) {
return p.advanced;
})
);
self.expertTransportParameters(
_.any(self.transportParameters(), function (p) {
return p.expert;
})
);
self.transportParameters(transportOptions);
var connectionProfile = self.selectedConnectionProfile();
self.sychronizeSettings(
@@ -605,10 +624,14 @@ $(function () {
};
self.fromOptionsResponse = function (options) {
var extendOption = function (option) {
var extendOption = function (option, valueNode) {
if (!valueNode) {
valueNode = {};
}
if (option.type === "group") {
_.each(option.params, function (param) {
extendOption(param);
extendOption(param, valueNode[option.name]);
});
option.modified = ko.pureComputed(function () {
@@ -630,7 +653,7 @@ $(function () {
} else {
if (option.type === "presetchoice") {
_.each(option.group.params, function (option) {
extendOption(option);
extendOption(option, valueNode[option.name]); // TODO
});
}
@@ -638,7 +661,7 @@ $(function () {
option.expertParameters = {};
_.each(option.groups, function (group, key) {
_.each(group, function (option) {
extendOption(option);
extendOption(option, valueNode[option.name]); // TODO
});
option.expertParameters[key] = _.any(group, function (p) {
return p.expert;
@@ -646,12 +669,28 @@ $(function () {
});
}
option.value = ko.observable(option.default);
option.defaultValue = ko.observable(option.default);
option.reset = function () {
option.value(option.defaultValue());
};
var defaultValue = convertValueFromBackend(option.default, option);
option.defaultValue = ko.observable(defaultValue);
if (valueNode && _.isFunction(valueNode)) {
option.value = ko.pureComputed({
read: function () {
return convertValueFromBackend(valueNode(), option);
},
write: function (value) {
valueNode(convertValueToBackend(value, option));
},
owner: this
});
option.reset = function () {
valueNode(defaultValue);
};
} else {
option.value = ko.observable(defaultValue);
option.reset = function () {
option.value(defaultValue);
};
}
option.modified = ko.pureComputed(function () {
// noinspection EqualityComparisonWithCoercionJS
return option.value() != option.defaultValue();
@@ -680,7 +719,7 @@ $(function () {
if (option.defaults[choice.value]) {
var d = option.defaults[choice.value][p.name];
if (d !== undefined) {
p.defaultValue(convertValue(d, p));
p.defaultValue(convertValueFromBackend(d, p));
if (!keepValue) {
p.value(p.defaultValue());
}
@@ -697,11 +736,17 @@ $(function () {
}
};
var extendOptions = function (input) {
var extendOptions = function (input, settings) {
if (!settings) settings = {};
_.each(input, function (i) {
var node = settings[i.key];
_.each(i.options, function (option) {
extendOption(option);
});
_.each(i.settings, function (option) {
extendOption(option, node ? node[option.name] : {});
});
});
};
@@ -720,12 +765,12 @@ $(function () {
// protocols
var protocols = options.protocols;
extendOptions(protocols);
extendOptions(protocols, self.settings.settings.connection.protocols);
self.availableProtocols(protocols);
// transports
var transports = options.transports;
extendOptions(transports);
extendOptions(transports, self.settings.settings.connection.transports);
self.availableTransports(transports);
// connection profiles
@@ -757,6 +802,9 @@ $(function () {
var connection = _.find(self.availableConnectionProfiles(), function (c) {
return c.id === connectionKey;
});
// this will also set printer profile, protocol and transport through
// subscriptions
self.selectedConnectionProfile(connection);
} else {
self.adjustConnectionParameters(true);
@@ -781,33 +829,40 @@ $(function () {
return p.key === protocolKey;
});
self.selectedProtocol(protocol);
var protocolParameters = self.protocolParameters();
_.each(protocolParameters, function (option) {
extendOption(
option,
current.protocolOptions[option.name],
current.protocolOptions
);
});
self.protocolParameters(protocolParameters);
} else {
self.selectedProtocol(self.availableProtocols()[0]);
}
var protocolParameters = self.protocolParameters();
_.each(protocolParameters, function (option) {
setOptionValue(
option,
current.protocolOptions[option.name],
current.protocolOptions
);
});
self.protocolParameters(protocolParameters);
console.log(protocolParameters);
var transportKey = current.transport;
if (transportKey) {
var transport = _.find(self.availableTransports(), function (t) {
return t.key === transportKey;
});
self.selectedTransport(transport);
var transportParameters = self.transportParameters();
_.each(transportParameters, function (option) {
extendOption(
option,
current.transportOptions[option.name],
current.transportOptions
);
});
self.transportParameters(transportParameters);
} else {
self.selectedProtocol(self.availableProtocols()[0]);
}
var transportParameters = self.transportParameters();
_.each(transportParameters, function (option) {
setOptionValue(
option,
current.transportOptions[option.name],
current.transportOptions
);
});
self.transportParameters(transportParameters);
};
self.fromHistoryData = function (data) {
@@ -963,6 +1018,7 @@ $(function () {
],
elements: [
"#connection_wrapper",
"#settings_connection",
"#connection_protocol_dialog",
"#connection_transport_dialog",
"#connectionprofiles_editor_dialog"
@@ -4,9 +4,11 @@
<h3>{{ _('Protocol Parameters') }}</h3>
</div>
<div class="modal-body">
<!--ko let: { profile: { protocolParameters: protocolParameters, expertProtocolParameters:expertProtocolParameters } }-->
{% include 'snippets/connectionprofiles/protocolEditor.jinja2' %}
<!--/ko-->
<form class="form-horizontal">
<!--ko let: { profile: { protocolParameters: protocolParameters, expertProtocolParameters: expertProtocolParameters } }-->
{% include 'snippets/connectionprofiles/protocolEditor.jinja2' %}
<!--/ko-->
</form>
</div>
<div class="modal-footer">
<a href="javascript:void(0)" class="btn" data-dismiss="modal" aria-hidden="true">{{ _('Close') }}</a>
@@ -19,9 +21,11 @@
<h3>{{ _('Transport Parameters') }}</h3>
</div>
<div class="modal-body">
<!--ko let: { profile: { transportParameters: transportParameters, expertTransportParameters:expertProtocolParameters } }-->
{% include 'snippets/connectionprofiles/transportEditor.jinja2' %}
<!--/ko-->
<form class="form-horizontal" action="javascript:void(0)">
<!--ko let: { profile: { transportParameters: transportParameters, expertTransportParameters: expertTransportParameters } }-->
{% include 'snippets/connectionprofiles/transportEditor.jinja2' %}
<!--/ko-->
</form>
</div>
<div class="modal-footer">
<a href="javascript:void(0)" class="btn" data-dismiss="modal" aria-hidden="true">{{ _('Close') }}</a>
@@ -1,20 +1,70 @@
<form class="form-horizontal">
<fieldset id="settings_serial_general_logging">
<legend>{{ _('Logging') }}</legend>
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox" data-bind="checked: connection_log_connection" id="settings-connectionLogConnection"> {{ _('Log communication to <code>connection.log</code>') }} <span class="label label-important">{{ _('Warning') }}</span>
<span class="help-block">{{ _('While this can negatively impact performance, a <code>connection.log</code> can be incredibly useful for debugging any issues observed in the communication between OctoPrint and your printer.') }}</span>
</label>
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox" data-bind="checked: connection_log_commdebug" id="settings-connectionLogCommdebug"> {{ _('Log debug information about communication to <code>commdebug.log</code>') }} <span class="label label-important">{{ _('Warning') }}</span>
</label>
</div>
</div>
</fieldset>
</form>
<ul class="nav nav-pills">
<li id="settings_accesscontrol_users_link" class="active">
<a href="#settings_connection_general" data-toggle="tab">{{ _('General') }}</a>
</li>
<li id="settings_accesscontrol_groups_link">
<a href="#settings_connection_protocols" data-toggle="tab">{{ _('Protocols') }}</a>
</li>
<li id="settings_accesscontrol_groups_link">
<a href="#settings_connection_transports" data-toggle="tab">{{ _('Transports') }}</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="settings_connection_general">
<form class="form-horizontal">
<fieldset id="settings_connection_general_logging">
<legend>{{ _('Logging') }}</legend>
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox" data-bind="checked: settings.connection_log_connection" id="settings-connectionLogConnection"> {{ _('Log communication to <code>connection.log</code>') }} <span class="label label-important">{{ _('Warning') }}</span>
<span class="help-block">{{ _('While this can negatively impact performance, a <code>connection.log</code> can be incredibly useful for debugging any issues observed in the communication between OctoPrint and your printer.') }}</span>
</label>
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox" data-bind="checked: settings.connection_log_commdebug" id="settings-connectionLogCommdebug"> {{ _('Log debug information about communication to <code>commdebug.log</code>') }} <span class="label label-important">{{ _('Warning') }}</span>
</label>
</div>
</div>
</fieldset>
</form>
</div>
<div class="tab-pane" id="settings_connection_protocols">
<form class="form-horizontal">
<!-- ko foreach: availableProtocols -->
<fieldset>
<legend data-bind="text: name"></legend>
<!-- ko if: settings.length -->
<!-- ko foreach: settings -->
<!-- ko template: { name: 'connectionprofile_editor_template_' + $data.type, data: $data } --><!-- /ko -->
<!-- /ko -->
<!-- /ko -->
<!-- ko if: settings.length === 0 -->
<p><em>{{ _('There are no overall settings for this transport.') }}</em></p>
<!-- /ko -->
</fieldset>
<!-- /ko -->
</form>
</div>
<div class="tab-pane" id="settings_connection_transports">
<form class="form-horizontal">
<!-- ko foreach: availableTransports -->
<fieldset>
<legend data-bind="text: name"></legend>
<!-- ko if: settings.length -->
<!-- ko foreach: settings -->
<!-- ko template: { name: 'connectionprofile_editor_template_' + $data.type, data: $data } --><!-- /ko -->
<!-- /ko -->
<!-- /ko -->
<!-- ko if: settings.length === 0 -->
<p></p><em>{{ _('There are no overall settings for this transport.') }}</em></p>
<!-- /ko -->
</fieldset>
<!-- /ko -->
</form>
</div>
</div>