Skip to content

Commit

Permalink
Merge pull request #11 from voruti/issue-5-preserve-the-profile-of-ch…
Browse files Browse the repository at this point in the history
…annel-links
  • Loading branch information
voruti committed Aug 8, 2021
2 parents 1a1a661 + 1c1b478 commit 00d15ec
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ directory. Alternatively you can adjust the program arguments to specify the fil
- The `-d <path>`/`--dir <path>`/`--directory <path>` parameters allow you to specify the directory in which to search
for *.items files.

:exclamation:Channel Link **Profiles** (value → configuration → properties → profile) currently will be lost in this
process:exclamation: (They can - of course - manually be added afterwards)

#### Other program features

When enabling both features, the converting feature will run first, so the appending feature can then use the generated
Expand Down
33 changes: 21 additions & 12 deletions src/main/java/voruti/json2config/model/json/JsonChannelLink.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
package voruti.json2config.model.json;

import com.google.gson.Gson;
import lombok.Getter;
import voruti.json2config.model.IConvertible;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@SuppressWarnings("unused")
@Getter
public class JsonChannelLink implements IConvertible {

private static final Gson GSON = new Gson();
private Value value;


@Override
public String toConfigLine(String lineBefore) {
String format = "channel=\"%s\"}";
// first channel or append:
String format = "channel=\"%s\"%s}";
if (lineBefore.endsWith("}")) {
lineBefore = lineBefore.substring(0, lineBefore.length() - 1);
format = "%s, " + format;
} else {
format = "%s {" + format;
}

return String.format(format, lineBefore, String.join(":", value.channelUID.segments)).strip();
// profile:
String propertiesString = "";
if (value.configuration.properties != null) {
String profile = value.configuration.properties.get("profile");
if (profile != null && !profile.equals("system:default")) {
propertiesString = String.format("[%s]",
value.configuration.properties.entrySet().stream()
.map(propertiesEntry -> String.format("%s=%s", propertiesEntry.getKey(), GSON.toJson(propertiesEntry.getValue())))
.collect(Collectors.joining(", "))
);
}
}

return String.format(format, lineBefore, String.join(":", value.channelUID.segments), propertiesString).strip();
}


Expand All @@ -38,15 +55,7 @@ private static class ChannelUID {
}

private static class Configuration {
private Properties properties;


private static class Properties {
private String offset;
private String sourceFormat;
private String profile;
private String function;
}
private Map<String, String> properties;
}
}
}
2 changes: 1 addition & 1 deletion src/test/resources/openhab2_multipleChannelsOneItem.items
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Switch MultiItem {channel="mqtt:topic:52b61fd6:light", channel="mqtt:topic:d589b50d:power", channel="mqtt:topic:3621578b:switch", channel="mqtt:topic:1c4c5e84:light"}
Switch MultiItem {channel="mqtt:topic:52b61fd6:light", channel="mqtt:topic:d589b50d:power"[profile="system:follow"], channel="mqtt:topic:3621578b:switch"[sourceFormat="%.1f °C", profile="transform:REGEX", function=".*\u003d(\\\\d*.\\\\d*).*"], channel="mqtt:topic:1c4c5e84:light"}

0 comments on commit 00d15ec

Please sign in to comment.