From 34c27e4e319ddc36722671bec55894c0c03e5d77 Mon Sep 17 00:00:00 2001 From: Sam Holmes Date: Sun, 19 Sep 2021 23:12:39 +1200 Subject: [PATCH] adjustments --- README.md | 21 +++++++++-- src/main/java/voruti/json2config/Starter.java | 3 ++ .../json2config/model/json/JsonMetadata.java | 2 -- .../voruti/json2config/service/Appender.java | 4 +-- .../json2config/service/ChannelAppender.java | 9 ++--- .../voruti/json2config/service/Constants.java | 1 + .../json2config/service/MetadataAppender.java | 1 - .../resources/openhab2_example1.Metadata.json | 36 ++++++++++++++++--- src/test/resources/openhab2_example1.items | 12 +++---- 9 files changed, 65 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index c006cd6..36a2123 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ You can run the tool with Java: java -jar json2config-XXX.jar [arguments] ``` -The program has two main features. By default, only the first one will be executed. +The program has three main features. By default, only the first one will be executed. ### 1. Converting org.eclipse.smarthome.core.items.Item.json into a *.items file @@ -42,9 +42,26 @@ directory. Alternatively you can adjust the program arguments to specify the fil - The `-d `/`--dir `/`--directory ` parameters allow you to specify the directory in which to search for *.items files. +### 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: + +- The `-m`/`--metadata`/`--append-metadata` parameters enable the appending metadata feature. Without one of these, this + feature won't run! +- The `--metadata-file ` parameter allows you to specify the .json file location + containing the metadata. +- The `-d `/`--dir `/`--directory ` parameters allow you to specify the directory in which to search + for *.items files. + +**IMPORTANT NOTE:** Metadata properties are not currently supported. + #### Other program features -When enabling both features, the converting feature will run first, so the appending feature can then use the generated +When enabling all features, the converting feature will run first, so the appending features can then use the generated .items file to append the channel links. With the `-3`/`--v3`/`--openhab3` parameters default values used since openHAB version 3.X are set. Additional diff --git a/src/main/java/voruti/json2config/Starter.java b/src/main/java/voruti/json2config/Starter.java index 1e6a3b6..e2b0daf 100644 --- a/src/main/java/voruti/json2config/Starter.java +++ b/src/main/java/voruti/json2config/Starter.java @@ -77,6 +77,9 @@ public void run() { if (channelFile.equals(Constants.DEFAULT_V2_CHANNEL_FILE)) { channelFile = Constants.DEFAULT_V3_CHANNEL_FILE; } + if (metadataFile.equals(Constants.DEFAULT_V2_METADATA_FILE)) { + metadataFile = Constants.DEFAULT_V3_METADATA_FILE; + } } // start Converter: diff --git a/src/main/java/voruti/json2config/model/json/JsonMetadata.java b/src/main/java/voruti/json2config/model/json/JsonMetadata.java index 269ac0a..ea39c06 100644 --- a/src/main/java/voruti/json2config/model/json/JsonMetadata.java +++ b/src/main/java/voruti/json2config/model/json/JsonMetadata.java @@ -2,8 +2,6 @@ import java.util.List; -import com.google.gson.Gson; - import lombok.Getter; import voruti.json2config.model.IAppendable; diff --git a/src/main/java/voruti/json2config/service/Appender.java b/src/main/java/voruti/json2config/service/Appender.java index 5811f08..2edf23d 100644 --- a/src/main/java/voruti/json2config/service/Appender.java +++ b/src/main/java/voruti/json2config/service/Appender.java @@ -26,7 +26,7 @@ private Appender() { public static List getItemNamesFromFile(String fileName) { try { return Arrays.stream(SharedService.openFileToString(fileName).split("\n")) - .filter(line -> !line.isEmpty() && !line.toLowerCase().startsWith("group")) + .filter(line -> !line.isEmpty()) .map(Appender::searchNameInLine) .filter(itemName -> !itemName.isEmpty()) .collect(Collectors.toList()); @@ -51,7 +51,7 @@ public static boolean appendToItemInFile(IAppendable appendable, String fileName List originalLines = Arrays.asList(SharedService.openFileToString(fileName).split("\n")); List modifiedLines = originalLines.stream() .map(line -> { - if (!line.isEmpty() && !line.toLowerCase().startsWith("group")) { + if (!line.isEmpty()) { String readItemName = searchNameInLine(line); if (!readItemName.isEmpty() && readItemName.equals(appendable.getItemName())) { return appendable.toConfigLine(line); diff --git a/src/main/java/voruti/json2config/service/ChannelAppender.java b/src/main/java/voruti/json2config/service/ChannelAppender.java index f185c5d..4fec199 100644 --- a/src/main/java/voruti/json2config/service/ChannelAppender.java +++ b/src/main/java/voruti/json2config/service/ChannelAppender.java @@ -1,16 +1,13 @@ package voruti.json2config.service; -import lombok.extern.slf4j.Slf4j; -import voruti.json2config.model.json.JsonChannelLink; - -import java.io.File; import java.io.IOException; -import java.util.Arrays; import java.util.Collection; import java.util.List; -import java.util.Objects; import java.util.stream.Collectors; +import lombok.extern.slf4j.Slf4j; +import voruti.json2config.model.json.JsonChannelLink; + /** * @author voruti */ diff --git a/src/main/java/voruti/json2config/service/Constants.java b/src/main/java/voruti/json2config/service/Constants.java index a013074..a0bc0f0 100644 --- a/src/main/java/voruti/json2config/service/Constants.java +++ b/src/main/java/voruti/json2config/service/Constants.java @@ -11,6 +11,7 @@ public final class Constants { 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"; + public static final String DEFAULT_V3_METADATA_FILE = "org.openhab.core.items.Metadata.json"; private Constants() { diff --git a/src/main/java/voruti/json2config/service/MetadataAppender.java b/src/main/java/voruti/json2config/service/MetadataAppender.java index 1bce1ff..aa34b98 100644 --- a/src/main/java/voruti/json2config/service/MetadataAppender.java +++ b/src/main/java/voruti/json2config/service/MetadataAppender.java @@ -6,7 +6,6 @@ import java.util.stream.Collectors; import lombok.extern.slf4j.Slf4j; -import voruti.json2config.model.json.JsonChannelLink; import voruti.json2config.model.json.JsonMetadata; /** diff --git a/src/test/resources/openhab2_example1.Metadata.json b/src/test/resources/openhab2_example1.Metadata.json index 169daef..0e010a3 100644 --- a/src/test/resources/openhab2_example1.Metadata.json +++ b/src/test/resources/openhab2_example1.Metadata.json @@ -12,26 +12,52 @@ "configuration": {} } }, - "ga:GF_Corridor_Light": { + "ga:FF_FamilyRoom_Motion": { "class": "org.eclipse.smarthome.core.items.Metadata", "value": { "key": { "segments": [ "ga", - "GF_Corridor_Light" + "FF_FamilyRoom_Motion" ] }, - "value": "Light", + "value": "Sensor", + "configuration": {} + } + }, + "ga:GF_LivingRoom_Power": { + "class": "org.eclipse.smarthome.core.items.Metadata", + "value": { + "key": { + "segments": [ + "ga", + "GF_LivingRoom_Power" + ] + }, + "value": "Outlet", + "configuration": {} + } + }, + "alexa:GF_Bedroom_Light": { + "class": "org.eclipse.smarthome.core.items.Metadata", + "value": { + "key": { + "segments": [ + "alexa", + "GF_Bedroom_Light" + ] + }, + "value": "Lighting", "configuration": {} } }, - "ga:GF_Kitchen_Light": { + "ga:GF_Bedroom_Light": { "class": "org.eclipse.smarthome.core.items.Metadata", "value": { "key": { "segments": [ "ga", - "GF_Kitchen_Light" + "GF_Bedroom_Light" ] }, "value": "Light", diff --git a/src/test/resources/openhab2_example1.items b/src/test/resources/openhab2_example1.items index a5688ff..0440a4a 100644 --- a/src/test/resources/openhab2_example1.items +++ b/src/test/resources/openhab2_example1.items @@ -10,7 +10,7 @@ Group FF_Toilet "Toilet" Group GF "Ground Floor" (Home) ["GroundFloor"] Group GF_Bathroom "Bathroom" (Home, GF) ["Bathroom"] Group GF_Bedroom "Bedroom" (Home, GF) ["Bedroom"] -Group GF_Corridor "Corridor" (Home, GF) ["Corridor"] +Group GF_Corridor "Corridor" (Home, GF) ["Corridor"] {ga="Light"} Group GF_Kitchen "Kitchen" (Home, GF) ["Kitchen"] Group GF_LivingRoom "Living Room" (Home, GF) ["LivingRoom"] Group Home "Our Home" ["Building"] @@ -21,7 +21,7 @@ Group:Switch:OR(ON,OFF) gPower "Power O Switch FF_Corridor_Light "Light" (FF_Corridor, gLight) ["Lighting", "Switchable"] Switch FF_FamilyRoom_Light "Light" (FF_FamilyRoom, gLight) ["Lighting", "Switchable"] -Switch FF_FamilyRoom_Motion "Motion Sensor" (FF_FamilyRoom, gMotion) ["MotionDetector", "Switchable"] {channel="mqtt:topic:3621578b:switch"} +Switch FF_FamilyRoom_Motion "Motion Sensor" (FF_FamilyRoom, gMotion) ["MotionDetector", "Switchable"] {channel="mqtt:topic:3621578b:switch", ga="Sensor"} Switch FF_GuestRoom_Light "Light" (FF_GuestRoom, gLight) ["Lighting", "Switchable"] Switch FF_KidsRoom_Light "Light" (FF_KidsRoom, gLight) ["Lighting", "Switchable"] Switch FF_KidsRoom_Power "Power Outlet" (FF_KidsRoom, gPower) ["Switch", "Switchable"] {channel="mqtt:topic:d589b50d:power"} @@ -29,8 +29,8 @@ Switch FF_Library_Light "Light" Switch FF_Library_Power "Power Outlet" (FF_Library, gPower) ["Switch", "Switchable"] Switch FF_Toilet_Light "Light" (FF_Toilet, gLight) ["Lighting", "Switchable"] {channel="mqtt:topic:52b61fd6:light"} Switch GF_Bathroom_Light "Light" (GF_Bathroom, gLight) ["Lighting", "Switchable"] -Switch GF_Bedroom_Light "Light" (GF_Bedroom, gLight) ["Lighting", "Switchable"] -Switch GF_Corridor_Light "Light" (GF_Corridor, gLight) ["Lighting", "Switchable"] {ga="Light"} -Switch GF_Kitchen_Light "Light" (GF_Kitchen, gLight) ["Lighting", "Switchable"] {channel="mqtt:topic:40d4c19b:light", ga="Light"} +Switch GF_Bedroom_Light "Light" (GF_Bedroom, gLight) ["Lighting", "Switchable"] {alexa="Lighting", ga="Light"} +Switch GF_Corridor_Light "Light" (GF_Corridor, gLight) ["Lighting", "Switchable"] +Switch GF_Kitchen_Light "Light" (GF_Kitchen, gLight) ["Lighting", "Switchable"] {channel="mqtt:topic:40d4c19b:light"} Switch GF_LivingRoom_Light "Light" (GF_LivingRoom, gLight) ["Lighting", "Switchable"] -Switch GF_LivingRoom_Power "Power Outlet" (GF_LivingRoom, gPower) ["Switch", "Switchable"] +Switch GF_LivingRoom_Power "Power Outlet" (GF_LivingRoom, gPower) ["Switch", "Switchable"] {ga="Outlet"}