Skip to content

Commit

Permalink
first cut
Browse files Browse the repository at this point in the history
  • Loading branch information
sbholmes committed Sep 17, 2021
1 parent 4e1a9ed commit e2cf94b
Show file tree
Hide file tree
Showing 16 changed files with 353 additions and 105 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ plugins {
id 'java'
id 'application'
id 'com.github.johnrengelman.shadow' version '7.0.0'
id "io.freefair.lombok" version "6.1.0-m3"
// id "io.freefair.lombok" version "6.1.0-m3"
}

group = 'voruti'
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/voruti/json2config/Starter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import voruti.json2config.service.ChannelAppender;
import voruti.json2config.service.Constants;
import voruti.json2config.service.Converter;
import voruti.json2config.service.MetadataAppender;
import voruti.json2config.service.Type;

/**
Expand All @@ -22,6 +23,10 @@ public class Starter implements Runnable {
@Option(names = {"-c", "--channel", "--channel-link", "--create-channels", "--create-channel-links"},
description = "enable the appending feature")
private boolean doChannelLinks;

@Option(names = {"-m", "--metadata", "--append-metadata"},
description = "enable the metadata appending feature")
private boolean doMetadata;

@Option(names = {"-3", "--openhab3", "--v3", "--openhab-v3", "--openhabv3", "--openhab-3"},
description = "set default file names used since openHAB version 3.X")
Expand All @@ -38,6 +43,11 @@ public class Starter implements Runnable {
description = "specify the .json file location containing the channel links")
private String channelFile;

@Option(names = {"--metadata-file"},
defaultValue = Constants.DEFAULT_V2_METADATA_FILE,
description = "specify the .json file location containing the metadata")
private String metadataFile;

@Option(names = {"-o", "--out", "--items"},
defaultValue = "json.items",
description = "specify the output file")
Expand Down Expand Up @@ -78,5 +88,10 @@ public void run() {
if (doChannelLinks) {
ChannelAppender.start(channelFile, directory);
}

// start MetadataAppender:
if (doMetadata) {
MetadataAppender.start(metadataFile, directory);
}
}
}
11 changes: 11 additions & 0 deletions src/main/java/voruti/json2config/model/IAppendable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package voruti.json2config.model;

public interface IAppendable extends IConvertible {

/**
*
* @return
*/
String getItemName();

}
16 changes: 11 additions & 5 deletions src/main/java/voruti/json2config/model/json/JsonChannelLink.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
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;

import com.google.gson.Gson;

import lombok.Getter;
import voruti.json2config.model.IAppendable;

@Getter
public class JsonChannelLink implements IConvertible {
public class JsonChannelLink implements IAppendable {

private static final Gson GSON = new Gson();
private Value value;
Expand Down Expand Up @@ -58,4 +59,9 @@ private static class Configuration {
private Map<String, String> properties;
}
}

@Override
public String getItemName() {
return value.itemName;
}
}
44 changes: 44 additions & 0 deletions src/main/java/voruti/json2config/model/json/JsonMetadata.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package voruti.json2config.model.json;

import java.util.List;

import com.google.gson.Gson;

import lombok.Getter;
import voruti.json2config.model.IAppendable;

@Getter
public class JsonMetadata implements IAppendable {

private Value value;

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

@Getter
public static class Value {
private Key key;
private String value;

private static class Key {
private List<String> segments;
}

}

@Override
public String getItemName() {
return value.key.segments.get(1);
}

}
103 changes: 103 additions & 0 deletions src/main/java/voruti/json2config/service/Appender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package voruti.json2config.service;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;
import voruti.json2config.model.IAppendable;

@Slf4j
public class Appender {

private Appender() {
}

/**
* Returns a {@link List} of Strings containing the names of all items in
* {@code fileName}.
*
* @param fileName the file(-Name) to open and search for items
* @return a {@link List} containing the names of the items
*/
public static List<String> getItemNamesFromFile(String fileName) {
try {
return Arrays.stream(SharedService.openFileToString(fileName).split("\n"))
.filter(line -> !line.isEmpty() && !line.toLowerCase().startsWith("group"))
.map(Appender::searchNameInLine)
.filter(itemName -> !itemName.isEmpty())
.collect(Collectors.toList());
} catch (IOException e) {
log.error(Constants.LOG_CANT_OPEN_FILE, fileName);
}

return List.of();
}

/**
* Appends the {@code appendable} after the item in {@code fileName}.
*
* @param appendable the data to append after the item
* @param fileName the file to search for the item
* @return {@code true} if the {@code appendable} could be appended, {@code false} otherwise
*/
public static boolean appendToItemInFile(IAppendable appendable, String fileName) {
boolean successful = false;

try {
List<String> originalLines = Arrays.asList(SharedService.openFileToString(fileName).split("\n"));
List<String> modifiedLines = originalLines.stream()
.map(line -> {
if (!line.isEmpty() && !line.toLowerCase().startsWith("group")) {
String readItemName = searchNameInLine(line);
if (!readItemName.isEmpty() && readItemName.equals(appendable.getItemName())) {
return appendable.toConfigLine(line);
}
}
// return unmodified line:
return line;
})
.collect(Collectors.toList());

if (!originalLines.equals(modifiedLines)) {
successful = SharedService.writeLinesToFile(modifiedLines, fileName);
}
} catch (IOException e) {
log.error(Constants.LOG_CANT_OPEN_FILE, fileName);
}

return successful;
}

/**
* Searches for an item name in the {@code line}.
*
* @param line the line to search in
* @return name of the item if found, {@code null} otherwise
*/
public static String searchNameInLine(String line) {
String[] arr = line.strip().split("\\s+");

String itemName = "";
if (arr.length >= 2) {
itemName = arr[1];
}

return itemName;
}

/**
* Searches for ".items" files in the {@code directory}.
*
* @param directory the directory to search in
* @return a {@link List} with all file paths of ".items" files
*/
public static List<String> findItemsFilesInDir(String directory) {
return Arrays.stream(Objects.requireNonNull(new File(directory).listFiles((dir, filename) -> filename.endsWith(".items"))))
.map(File::getAbsolutePath)
.collect(Collectors.toList());
}
}
90 changes: 3 additions & 87 deletions src/main/java/voruti/json2config/service/ChannelAppender.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public static void start(String channelLinkFile, String directory) {
log.trace("channelLinkList={}", channelLinkList);

// search items files:
List<String> itemsFiles = findItemsFilesInDir(directory);
List<String> itemsFiles = Appender.findItemsFilesInDir(directory);
// get names of all items:
List<String> itemNamesList = itemsFiles.stream()
.map(ChannelAppender::getItemNamesFromFile)
.map(Appender::getItemNamesFromFile)
.flatMap(Collection::stream)
.collect(Collectors.toList());
log.info("Found {} items", itemNamesList.size());
Expand All @@ -63,7 +63,7 @@ public static void start(String channelLinkFile, String directory) {
int count = 0;
for (JsonChannelLink channel : relevantChannelLinkList) {
for (String iFile : itemsFiles) {
if (setChannelToItemInFile(channel, iFile)) {
if (Appender.appendToItemInFile(channel, iFile)) {
count++;
}
}
Expand All @@ -76,88 +76,4 @@ public static void start(String channelLinkFile, String directory) {
}
}

/**
* Returns a {@link List} of Strings containing the names of all items in
* {@code fileName}.
*
* @param fileName the file(-Name) to open and search for items
* @return a {@link List} containing the names of the items
*/
public static List<String> getItemNamesFromFile(String fileName) {
try {
return Arrays.stream(SharedService.openFileToString(fileName).split("\n"))
.filter(line -> !line.isEmpty() && !line.toLowerCase().startsWith("group"))
.map(ChannelAppender::searchNameInLine)
.filter(itemName -> !itemName.isEmpty())
.collect(Collectors.toList());
} catch (IOException e) {
log.error(Constants.LOG_CANT_OPEN_FILE, fileName);
}

return List.of();
}

/**
* Appends the {@code channelLink} after the item in {@code fileName}.
*
* @param channelLink the channel link to append after the item
* @param fileName the file to search for the item
* @return {@code true} if the {@code channelLink} could be appended, {@code false} otherwise
*/
public static boolean setChannelToItemInFile(JsonChannelLink channelLink, String fileName) {
boolean successful = false;

try {
List<String> originalLines = Arrays.asList(SharedService.openFileToString(fileName).split("\n"));
List<String> modifiedLines = originalLines.stream()
.map(line -> {
if (!line.isEmpty() && !line.toLowerCase().startsWith("group")) {
String readItemName = searchNameInLine(line);
if (!readItemName.isEmpty() && readItemName.equals(channelLink.getValue().getItemName())) {
return channelLink.toConfigLine(line);
}
}
// return unmodified line:
return line;
})
.collect(Collectors.toList());

if (!originalLines.equals(modifiedLines)) {
successful = SharedService.writeLinesToFile(modifiedLines, fileName);
}
} catch (IOException e) {
log.error(Constants.LOG_CANT_OPEN_FILE, fileName);
}

return successful;
}

/**
* Searches for an item name in the {@code line}.
*
* @param line the line to search in
* @return name of the item if found, {@code null} otherwise
*/
public static String searchNameInLine(String line) {
String[] arr = line.strip().split("\\s+");

String itemName = "";
if (arr.length >= 2) {
itemName = arr[1];
}

return itemName;
}

/**
* Searches for ".items" files in the {@code directory}.
*
* @param directory the directory to search in
* @return a {@link List} with all file paths of ".items" files
*/
public static List<String> findItemsFilesInDir(String directory) {
return Arrays.stream(Objects.requireNonNull(new File(directory).listFiles((dir, filename) -> filename.endsWith(".items"))))
.map(File::getAbsolutePath)
.collect(Collectors.toList());
}
}
1 change: 1 addition & 0 deletions src/main/java/voruti/json2config/service/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public final class Constants {
public static final String DEFAULT_V3_JSON_FILE = "org.openhab.core.items.Item.json";
public static final String DEFAULT_V2_CHANNEL_FILE = "org.eclipse.smarthome.core.thing.link.ItemChannelLink.json";
public static final String DEFAULT_V3_CHANNEL_FILE = "org.openhab.core.thing.link.ItemChannelLink.json";
public static final String DEFAULT_V2_METADATA_FILE = "org.eclipse.smarthome.core.items.Metadata.json";


private Constants() {
Expand Down
Loading

0 comments on commit e2cf94b

Please sign in to comment.