Skip to content

Commit

Permalink
Merge pull request #10 from voruti/issue-4-handle-multiple-channel-li…
Browse files Browse the repository at this point in the history
…nks-on-a-single-item
  • Loading branch information
voruti authored Aug 7, 2021
2 parents 01f4d73 + c5ba5b5 commit 1a1a661
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 22 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = 'voruti'
version = '1.4'
version = '1.5'
description = 'json2config'

repositories {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/voruti/json2config/Starter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* @author voruti
*/
@Command(name = "java -jar json2config.jar", mixinStandardHelpOptions = true, version = "1.4",
@Command(name = "java -jar json2config.jar", mixinStandardHelpOptions = true, version = "1.5",
description = "Converts openHAB Items from JsonDB Storage files.")
public class Starter implements Runnable {

Expand Down
12 changes: 11 additions & 1 deletion src/main/java/voruti/json2config/model/json/JsonChannelLink.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ public class JsonChannelLink implements IConvertible {

@Override
public String toConfigLine(String lineBefore) {
return String.format("%s {channel=\"%s\"}", lineBefore, String.join(":", value.channelUID.segments)).strip();
String format = "channel=\"%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();
}


Expand All @@ -35,7 +43,9 @@ private static class Configuration {

private static class Properties {
private String offset;
private String sourceFormat;
private String profile;
private String function;
}
}
}
Expand Down
39 changes: 20 additions & 19 deletions src/test/java/voruti/json2config/IntegrationTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package voruti.json2config;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -14,16 +15,22 @@ class IntegrationTest {
private static final String RESOURCES = "build/resources/test/";
private static final String TEMPORARY = "build/tmp/test/";

@Test
void openhab2_example1() throws IOException {

private String openFile(String file) throws IOException {
Path path = Paths.get(file);
return String.join("\n", Files.readAllLines(path))
.replaceAll("[\\h\\t ]{2,}", " ");
}


@ParameterizedTest
@ValueSource(strings = {"openhab2_example1", "openhab2_multipleChannelsOneItem"})
void itemsAndChannels(String testName) throws IOException {
// arrange:
final String testName = "openhab2_example1";
final String generatedItemsFile = TEMPORARY + testName + "_result.items";

// load template .items file:
Path pathTemplate = Paths.get(RESOURCES + testName + ".items");
String template = String.join("\n", Files.readAllLines(pathTemplate))
.replaceAll("[\\h\\t ]{2,}", " ");
String template = openFile(RESOURCES + testName + ".items");

// act:
Starter.main(new String[]{"-i", RESOURCES + testName + ".Item.json",
Expand All @@ -34,33 +41,27 @@ void openhab2_example1() throws IOException {

// assert:
// load generated .items file:
Path pathGenerated = Paths.get(generatedItemsFile);
String generated = String.join("\n", Files.readAllLines(pathGenerated))
.replaceAll("[\\h\\t ]{2,}", " ");
String generated = openFile(generatedItemsFile);

assertEquals(template, generated);
}

@Test
void openhab2_exampleDimensions() throws IOException {
@ParameterizedTest
@ValueSource(strings = {"openhab2_exampleDimensions"})
void onlyItems(String testName) throws IOException {
// arrange:
final String testName = "openhab2_exampleDimensions";
final String generatedItemsFile = TEMPORARY + testName + "_result.items";

// load template .items file:
Path pathTemplate = Paths.get(RESOURCES + testName + ".items");
String template = String.join("\n", Files.readAllLines(pathTemplate))
.replaceAll("[\\h\\t ]{2,}", " ");
String template = openFile(RESOURCES + testName + ".items");

// act:
Starter.main(new String[]{"-i", RESOURCES + testName + ".Item.json",
"-o", generatedItemsFile});

// assert:
// load generated .items file:
Path pathGenerated = Paths.get(generatedItemsFile);
String generated = String.join("\n", Files.readAllLines(pathGenerated))
.replaceAll("[\\h\\t ]{2,}", " ");
String generated = openFile(generatedItemsFile);

assertEquals(template, generated);
}
Expand Down
10 changes: 10 additions & 0 deletions src/test/resources/openhab2_multipleChannelsOneItem.Item.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"MultiItem": {
"class": "org.eclipse.smarthome.core.items.ManagedItemProvider$PersistedItem",
"value": {
"groupNames": [],
"itemType": "Switch",
"tags": []
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
"MultiItem -\u003e mqtt:topic:52b61fd6:light": {
"class": "org.eclipse.smarthome.core.thing.link.ItemChannelLink",
"value": {
"channelUID": {
"segments": [
"mqtt",
"topic",
"52b61fd6",
"light"
]
},
"configuration": {
"properties": {
"profile": "system:default"
}
},
"itemName": "MultiItem"
}
},
"MultiItem -\u003e mqtt:topic:d589b50d:power": {
"class": "org.eclipse.smarthome.core.thing.link.ItemChannelLink",
"value": {
"channelUID": {
"segments": [
"mqtt",
"topic",
"d589b50d",
"power"
]
},
"configuration": {
"properties": {
"profile": "system:follow"
}
},
"itemName": "MultiItem"
}
},
"MultiItem -\u003e mqtt:topic:3621578b:switch": {
"class": "org.eclipse.smarthome.core.thing.link.ItemChannelLink",
"value": {
"channelUID": {
"segments": [
"mqtt",
"topic",
"3621578b",
"switch"
]
},
"configuration": {
"properties": {
"sourceFormat": "%.1f °C",
"profile": "transform:REGEX",
"function": ".*\u003d(\\\\d*.\\\\d*).*"
}
},
"itemName": "MultiItem"
}
},
"MultiItem -\u003e mqtt:topic:1c4c5e84:light": {
"class": "org.eclipse.smarthome.core.thing.link.ItemChannelLink",
"value": {
"channelUID": {
"segments": [
"mqtt",
"topic",
"1c4c5e84",
"light"
]
},
"configuration": {
"properties": {
"profile": "system:default"
}
},
"itemName": "MultiItem"
}
}
}
1 change: 1 addition & 0 deletions src/test/resources/openhab2_multipleChannelsOneItem.items
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Switch MultiItem {channel="mqtt:topic:52b61fd6:light", channel="mqtt:topic:d589b50d:power", channel="mqtt:topic:3621578b:switch", channel="mqtt:topic:1c4c5e84:light"}

0 comments on commit 1a1a661

Please sign in to comment.