Skip to content

Commit

Permalink
adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
sbholmes committed Sep 19, 2021
1 parent e2cf94b commit 34c27e4
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 24 deletions.
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -42,9 +42,26 @@ 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.

### 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 <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 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
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/voruti/json2config/Starter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/voruti/json2config/model/json/JsonMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import java.util.List;

import com.google.gson.Gson;

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

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/voruti/json2config/service/Appender.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private Appender() {
public static List<String> 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());
Expand All @@ -51,7 +51,7 @@ public static boolean appendToItemInFile(IAppendable appendable, String fileName
List<String> originalLines = Arrays.asList(SharedService.openFileToString(fileName).split("\n"));
List<String> 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);
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/voruti/json2config/service/ChannelAppender.java
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down
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 @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
36 changes: 31 additions & 5 deletions src/test/resources/openhab2_example1.Metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 6 additions & 6 deletions src/test/resources/openhab2_example1.items
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Group FF_Toilet "Toilet"
Group GF "Ground Floor" <groundfloor> (Home) ["GroundFloor"]
Group GF_Bathroom "Bathroom" <bath> (Home, GF) ["Bathroom"]
Group GF_Bedroom "Bedroom" <bedroom> (Home, GF) ["Bedroom"]
Group GF_Corridor "Corridor" <corridor> (Home, GF) ["Corridor"]
Group GF_Corridor "Corridor" <corridor> (Home, GF) ["Corridor"] {ga="Light"}
Group GF_Kitchen "Kitchen" <kitchen> (Home, GF) ["Kitchen"]
Group GF_LivingRoom "Living Room" <sofa> (Home, GF) ["LivingRoom"]
Group Home "Our Home" <house> ["Building"]
Expand All @@ -21,16 +21,16 @@ Group:Switch:OR(ON,OFF) gPower "Power O

Switch FF_Corridor_Light "Light" <light> (FF_Corridor, gLight) ["Lighting", "Switchable"]
Switch FF_FamilyRoom_Light "Light" <light> (FF_FamilyRoom, gLight) ["Lighting", "Switchable"]
Switch FF_FamilyRoom_Motion "Motion Sensor" <motion> (FF_FamilyRoom, gMotion) ["MotionDetector", "Switchable"] {channel="mqtt:topic:3621578b:switch"}
Switch FF_FamilyRoom_Motion "Motion Sensor" <motion> (FF_FamilyRoom, gMotion) ["MotionDetector", "Switchable"] {channel="mqtt:topic:3621578b:switch", ga="Sensor"}
Switch FF_GuestRoom_Light "Light" <light> (FF_GuestRoom, gLight) ["Lighting", "Switchable"]
Switch FF_KidsRoom_Light "Light" <light> (FF_KidsRoom, gLight) ["Lighting", "Switchable"]
Switch FF_KidsRoom_Power "Power Outlet" <poweroutlet> (FF_KidsRoom, gPower) ["Switch", "Switchable"] {channel="mqtt:topic:d589b50d:power"}
Switch FF_Library_Light "Light" <light> (FF_Library, gLight) ["Lighting", "Switchable"] {channel="mqtt:topic:1c4c5e84:light"}
Switch FF_Library_Power "Power Outlet" <poweroutlet> (FF_Library, gPower) ["Switch", "Switchable"]
Switch FF_Toilet_Light "Light" <light> (FF_Toilet, gLight) ["Lighting", "Switchable"] {channel="mqtt:topic:52b61fd6:light"}
Switch GF_Bathroom_Light "Light" <light> (GF_Bathroom, gLight) ["Lighting", "Switchable"]
Switch GF_Bedroom_Light "Light" <light> (GF_Bedroom, gLight) ["Lighting", "Switchable"]
Switch GF_Corridor_Light "Light" <light> (GF_Corridor, gLight) ["Lighting", "Switchable"] {ga="Light"}
Switch GF_Kitchen_Light "Light" <light> (GF_Kitchen, gLight) ["Lighting", "Switchable"] {channel="mqtt:topic:40d4c19b:light", ga="Light"}
Switch GF_Bedroom_Light "Light" <light> (GF_Bedroom, gLight) ["Lighting", "Switchable"] {alexa="Lighting", ga="Light"}
Switch GF_Corridor_Light "Light" <light> (GF_Corridor, gLight) ["Lighting", "Switchable"]
Switch GF_Kitchen_Light "Light" <light> (GF_Kitchen, gLight) ["Lighting", "Switchable"] {channel="mqtt:topic:40d4c19b:light"}
Switch GF_LivingRoom_Light "Light" <light> (GF_LivingRoom, gLight) ["Lighting", "Switchable"]
Switch GF_LivingRoom_Power "Power Outlet" <poweroutlet> (GF_LivingRoom, gPower) ["Switch", "Switchable"]
Switch GF_LivingRoom_Power "Power Outlet" <poweroutlet> (GF_LivingRoom, gPower) ["Switch", "Switchable"] {ga="Outlet"}

0 comments on commit 34c27e4

Please sign in to comment.