Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
voruti committed Sep 25, 2021
1 parent 7d96ced commit d868937
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 94 deletions.
19 changes: 9 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,20 @@ directory. Alternatively you can adjust the program arguments to specify the fil

### 3. Appending metadata from org.eclipse.smarthome.core.items.Metadata.json to *.items files

To append metadata to *already existing* .items files, you can use the third feature. If you already have
some `*.items` files in your configuration, it's recommended to also including these files i.e. moving them into the
same directory. By default, (when the appending feature is enabled!!) the tool will use metadata from
the `org.eclipse.smarthome.core.items.Metadata.json` file and append them to all `*.items` files in the same
directory. Alternatively you can adjust the program arguments to specify the file locations:
To append metadata to *already existing* .items files, you can use the third feature. If you already have some `*.items`
files in your configuration, it's recommended to also including these files i.e. moving them into the same directory. By
default, (when the appending feature is enabled!!) the tool will use metadata from
the `org.eclipse.smarthome.core.items.Metadata.json` file and append them to all `*.items` files in the same directory.
Alternatively you can adjust the program arguments to specify the file locations:

- The `-m`/`--metadata`/`--append-metadata` parameters enable the appending metadata feature. Without one of these, this
feature won't run!
- The `--metadata-file <path>` parameter allows you to specify the .json file location
containing the metadata.
- The `--metadata-file <path>` parameter allows you to specify the .json file location containing the metadata.
- The `-d <path>`/`--dir <path>`/`--directory <path>` parameters allow you to specify the directory in which to search
for *.items files.
**IMPORTANT NOTE:** Metadata attributes, found in item files as `[ roomHint="Living Room" ]` and in the
metadata json under the `configuration` key are not currently supported and will be ignored.

**IMPORTANT NOTE:** Metadata attributes, found in item files as `[ roomHint="Living Room" ]` and in the metadata json
under the `configuration` key are not currently supported and will be ignored.

#### Other program features

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/voruti/json2config/Starter.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ 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;
Expand All @@ -47,7 +47,7 @@ public class Starter implements Runnable {
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,7 +78,7 @@ public void run() {
channelFile = Constants.DEFAULT_V3_CHANNEL_FILE;
}
if (metadataFile.equals(Constants.DEFAULT_V2_METADATA_FILE)) {
metadataFile = Constants.DEFAULT_V3_METADATA_FILE;
metadataFile = Constants.DEFAULT_V3_METADATA_FILE;
}
}

Expand All @@ -91,7 +91,7 @@ public void run() {
if (doChannelLinks) {
ChannelAppender.start(channelFile, directory);
}

// start MetadataAppender:
if (doMetadata) {
MetadataAppender.start(metadataFile, directory);
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/voruti/json2config/model/IAppendable.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
public interface IAppendable extends IConvertible {

/**
* Provides a way to get the item name from the channel or metadata so it can be appended to the right item
*
* Provides a way to get the item name from the channel or metadata, so it can be appended to the right item.
*
* @return the name of the item
*/
String getItemName();

}
18 changes: 8 additions & 10 deletions src/main/java/voruti/json2config/model/json/JsonChannelLink.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package voruti.json2config.model.json;

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;

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

@Getter
public class JsonChannelLink implements IAppendable {

Expand Down Expand Up @@ -43,6 +42,10 @@ public String toConfigLine(String lineBefore) {
return String.format(format, lineBefore, String.join(":", value.channelUID.segments), propertiesString).strip();
}

@Override
public String getItemName() {
return value.itemName;
}

@Getter
public static class Value {
Expand All @@ -59,9 +62,4 @@ private static class Configuration {
private Map<String, String> properties;
}
}

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

import java.util.List;

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

import java.util.List;

@Getter
public class JsonMetadata implements IAppendable {

private Value value;
private Value value;


@Override
public String toConfigLine(String lineBefore) {
@Override
public String toConfigLine(String lineBefore) {
// first metadata or append:
String format = "%s=\"%s\"}";
if (lineBefore.endsWith("}")) {
Expand All @@ -21,22 +22,21 @@ public String toConfigLine(String lineBefore) {
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;
@Override
public String getItemName() {
return value.key.segments.get(1);
}

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

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

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

private static class Key {
private List<String> segments;
}
}
}
38 changes: 19 additions & 19 deletions src/main/java/voruti/json2config/service/Appender.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package voruti.json2config.service;

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

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
Expand All @@ -8,28 +11,25 @@
import java.util.Objects;
import java.util.stream.Collectors;

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

/**
* Common code for appending channel links and metadata to items
*
* @author sbholmes
*
* @author sbholmes
*/
@Slf4j
public class Appender {

private Appender() {
}

/**
* Appends data found in {@code appendableList} onto the end of items in the {@code directory}
*
* @param directory the directory in which to search for ".items" files
* @param appendableList the list of data that needs appending to items
*/
public static void searchAndAppend(String directory, List<IAppendable> appendableList) {

private Appender() {
}


/**
* Appends data found in {@code appendableList} onto the end of items in the {@code directory}
*
* @param directory the directory in which to search for ".items" files
* @param appendableList the list of data that needs appending to items
*/
public static void searchAndAppend(String directory, List<IAppendable> appendableList) {
// search items files:
List<String> itemsFiles = Appender.findItemsFilesInDir(directory);
// get names of all items:
Expand Down Expand Up @@ -60,8 +60,8 @@ public static void searchAndAppend(String directory, List<IAppendable> appendabl
log.info("Successfully appended {} channels/metadata!", count);

log.warn("Warning: You might need to manually fix some converting mistakes (double channels, etc.)");
}
}

/**
* Returns a {@link List} of Strings containing the names of all items in
* {@code fileName}.
Expand All @@ -87,7 +87,7 @@ public static List<String> getItemNamesFromFile(String fileName) {
* 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
* @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) {
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/voruti/json2config/service/ChannelAppender.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package voruti.json2config.service;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

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

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author voruti
*/
Expand All @@ -17,6 +17,7 @@ public class ChannelAppender {
private ChannelAppender() {
}


/**
* Appends the channel links from {@code channelLinkFile} to all ".items" files
* in the {@code directory}.
Expand All @@ -38,10 +39,8 @@ public static void start(String channelLinkFile, String directory) {
log.trace("channelLinkList={}", channelLinkList);

Appender.searchAndAppend(directory, channelLinkList);

} catch (IOException e) {
log.error(Constants.LOG_CANT_OPEN_FILE, channelLinkFile);
}
}

}
25 changes: 12 additions & 13 deletions src/main/java/voruti/json2config/service/MetadataAppender.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
package voruti.json2config.service;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

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

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author sbholmes
*/
@Slf4j
public class MetadataAppender {

private MetadataAppender() {
}

/**

private MetadataAppender() {
}


/**
* Appends the metadata from {@code metadataFile} to all ".items" files
* in the {@code directory}.
*
* @param metadataFile path to the file which contains the metadata in
* JSON format
* @param directory the directory in which to search for ".items" files
* JSON format
* @param directory the directory in which to search for ".items" files
*/
public static void start(String metadataFile, String directory) {
log.debug("Starting MetadataAppender with metadataFile={}, directory={}", metadataFile, directory);
Expand All @@ -39,10 +40,8 @@ public static void start(String metadataFile, String directory) {
log.trace("metadataList={}", metadataList);

Appender.searchAndAppend(directory, metadataList);

} catch (IOException e) {
log.error(Constants.LOG_CANT_OPEN_FILE, metadataFile);
}
}

}
20 changes: 10 additions & 10 deletions src/main/java/voruti/json2config/service/SharedService.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package voruti.json2config.service;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import lombok.extern.slf4j.Slf4j;
import voruti.json2config.model.IConvertible;
import voruti.json2config.model.json.JsonChannelLink;
import voruti.json2config.model.json.JsonItem;
import voruti.json2config.model.json.JsonMetadata;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;

@Slf4j
public final class SharedService {

Expand Down Expand Up @@ -63,9 +62,10 @@ public static Map<String, IConvertible> jsonToConvertibleMap(String json, Type t
}.getType();
break;
case METADATA:
//noinspection DuplicateBranchesInSwitch
mapType = new TypeToken<Map<String, JsonMetadata>>() {
}.getType();
break;
break;
}

return GSON.fromJson(json, mapType);
Expand Down

0 comments on commit d868937

Please sign in to comment.