Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EdgeConfig: try to parse JSON configurations #1705

Merged
merged 1 commit into from
Jan 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

import io.openems.common.OpenemsConstants;
import io.openems.common.channel.Level;
import io.openems.common.exceptions.OpenemsError.OpenemsNamedException;
import io.openems.common.types.EdgeConfig;
import io.openems.common.types.EdgeConfig.Component;
import io.openems.common.types.EdgeConfig.Component.Channel.ChannelDetail;
Expand All @@ -67,6 +68,8 @@ public class EdgeConfigWorker extends ComponentManagerWorker {

private static final int CYCLE_TIME = 300_000; // in ms

private static final Logger LOG = LoggerFactory.getLogger(EdgeConfigWorker.class);

private final Logger log = LoggerFactory.getLogger(EdgeConfigWorker.class);
private final Queue<ConfigurationEvent> events = new ArrayDeque<ConfigurationEvent>();

Expand Down Expand Up @@ -560,15 +563,25 @@ private String[] getNatures(Bundle bundle, Manifest manifest, String factoryPid)
* @return the value as JsonElement
*/
private static JsonElement getPropertyAsJsonElement(Dictionary<String, Object> properties, String key) {
Object valueObj = properties.get(key);
var valueObj = properties.get(key);
if (valueObj != null && valueObj instanceof String) {
String value = (String) valueObj;
var value = ((String) valueObj).trim();
// find boolean
if (value.equalsIgnoreCase("true")) {
return new JsonPrimitive(true);
} else if (value.equalsIgnoreCase("false")) {
return new JsonPrimitive(false);
}
// find JSON
if ((value.startsWith("{") && value.endsWith("}")) /* JsonObject */
|| (value.startsWith("[") && value.endsWith("]")) /* JsonObject */
) {
try {
return JsonUtils.parse(value);
} catch (OpenemsNamedException e) {
LOG.warn(e.getMessage());
}
}
}
// fallback to JsonUtils
return JsonUtils.getAsJsonElement(valueObj);
Expand Down