Skip to content

Commit

Permalink
SunSpecCodeGenerator: refactor, add 7xx models, use JSON input files (O…
Browse files Browse the repository at this point in the history
…penEMS#2324)

Based on PR OpenEMS#2304 by [nlamarti](https://github.com/nlamarti), this new PR handles the comments made there. Comparing the old and new output file `DefaultSunSpecModel.java`, the following changed:

- The descriptions "Ref 3: 8.14.3.2, Ref 4: 17" in the 12x models disappeared, because they are not in the json-files.
- The models 302-304 are now ignored. They only consist of one repeating block (which OpenEMS cannot handle as of now), and as far as I can see, no component uses them currently.
- The models 701-715 are added
- Descriptions of some points changed

Note that the length of model 701 is 155>126=maximum length of a modbus task in j2mod. As the `AbstractOpenemsSunSpecComponent` adds one task for each block, this causes an error (see issue 2311). I have fixed this already, this will be done in a seperate PR.

Furthermore, I used modern syntax in the switch-statements in `SunSpecPoint.java`, as I had to update them anyway due to the new pointn type STRING32. 

Lastly, I had to write a new method `getAsStringOrElse` in `JsonUtils.java`, and the other changes in this file are caused by the automatic code clean up.

---------

Co-authored-by: nlamarti <[email protected]>
Co-authored-by: Stefan Feilmeier <[email protected]>
  • Loading branch information
3 people authored Aug 25, 2023
1 parent 1be37b0 commit 5f207bd
Show file tree
Hide file tree
Showing 6 changed files with 5,222 additions and 1,040 deletions.
62 changes: 40 additions & 22 deletions io.openems.common/src/io/openems/common/utils/JsonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,22 @@ public class JsonUtils {
/**
* Provide a easy way to generate a JsonArray from a list using the given
* convert function to add each element.
*
*
* @param list to convert
* @param convert function to convert elements
* @param <T> type of an element from list
*
*
* @return list as JsonArray
*/
public static <T> JsonArray generateJsonArray(Collection<T> list, Function<T, JsonElement> convert) {
if (list == null) {
return null;
} else {
var jab = new JsonArrayBuilder();
list.forEach(element -> {
jab.add(convert.apply(element));
});
return jab.build();
}
var jab = new JsonArrayBuilder();
list.forEach(element -> {
jab.add(convert.apply(element));
});
return jab.build();
}

/**
Expand Down Expand Up @@ -262,10 +261,10 @@ public JsonObjectBuilder addProperty(String property, Enum<?> value) {

/**
* Add a {@link ZonedDateTime} value to the {@link JsonObject}.
*
*
* <p>
* The value gets added in the format of {@link DateTimeFormatter#ISO_INSTANT}.
*
*
* @param property the key
* @param value the value
* @return the {@link JsonObjectBuilder}
Expand Down Expand Up @@ -364,10 +363,10 @@ public JsonObjectBuilder addPropertyIfNotNull(String property, Enum<?> value) {
/**
* Add a {@link ZonedDateTime} value to the {@link JsonObject} if it is not
* null.
*
*
* <p>
* The value gets added in the format of {@link DateTimeFormatter#ISO_INSTANT}.
*
*
* @param property the key
* @param value the value
* @return the {@link JsonObjectBuilder}
Expand Down Expand Up @@ -719,6 +718,19 @@ public static Optional<String> getAsOptionalString(JsonElement jElement, String
return Optional.ofNullable(toString(toPrimitive(toSubElement(jElement, memberName))));
}

/**
* Gets the member of the {@link JsonElement} as {@link String} if it exists,
* and the alternative value otherwise.
*
* @param jElement the {@link JsonElement}
* @param memberName the name of the member
* @param alternative the alternative value
* @return the {@link String} value or the alternative value
*/
public static String getAsStringOrElse(JsonElement jElement, String memberName, String alternative) {
return getAsOptionalString(jElement, memberName).orElse(alternative);
}

/**
* Converts a {@link JsonArray} to a String Array.
*
Expand Down Expand Up @@ -1373,17 +1385,20 @@ public static JsonElement getAsJsonElement(Object value) {
* String
*/
return new JsonPrimitive((String) value);
} else if (value instanceof Boolean) {
}
if (value instanceof Boolean) {
/*
* Boolean
*/
return new JsonPrimitive((Boolean) value);
} else if (value instanceof Inet4Address) {
}
if (value instanceof Inet4Address) {
/*
* Inet4Address
*/
return new JsonPrimitive(((Inet4Address) value).getHostAddress());
} else if (value instanceof JsonElement) {
}
if (value instanceof JsonElement) {
/*
* JsonElement
*/
Expand Down Expand Up @@ -1497,17 +1512,20 @@ public static Object getAsType(Class<?> type, JsonElement j) throws NotImplement
* Asking for an Long
*/
return j.getAsLong();
} else if (Boolean.class.isAssignableFrom(type)) {
}
if (Boolean.class.isAssignableFrom(type)) {
/*
* Asking for an Boolean
*/
return j.getAsBoolean();
} else if (Double.class.isAssignableFrom(type)) {
}
if (Double.class.isAssignableFrom(type)) {
/*
* Asking for an Double
*/
return j.getAsDouble();
} else if (String.class.isAssignableFrom(type)) {
}
if (String.class.isAssignableFrom(type)) {
/*
* Asking for a String
*/
Expand Down Expand Up @@ -1731,7 +1749,7 @@ public static boolean isEmptyJsonArray(JsonElement j) {

/**
* Check if the given {@link JsonElement} is a {@link Number}.
*
*
* @param j the {@link JsonElement} to check
* @return true if the element is a {@link Number}, otherwise false
*/
Expand All @@ -1742,7 +1760,7 @@ public static boolean isNumber(JsonElement j) {
/**
* Returns a sequential stream of the {@link JsonElement JsonElements} in the
* {@link JsonArray}.
*
*
* @param jsonArray The {@link JsonArray}, assumed to be unmodified during use
* @return a Stream of the elements
*/
Expand All @@ -1764,7 +1782,7 @@ private static JsonObject toJsonObject(JsonElement jElement) {
/**
* Returns a {@link Collector} that accumulates the input elements into a new
* {@link JsonObject}.
*
*
* @param <T> the type of the input
* @param keyMapper the key mapper
* @param valueMapper the value mapper
Expand All @@ -1785,7 +1803,7 @@ private static JsonObject toJsonObject(JsonElement jElement) {

/**
* Returns a Collector that accumulates the input elements into a new JsonArray.
*
*
* @return a Collector which collects all the input elements into a JsonArray
*/
public static Collector<JsonElement, JsonUtils.JsonArrayBuilder, JsonArray> toJsonArray() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ public void testGetAsString() throws OpenemsNamedException {
// -> Optional Sub-Element
assertEquals("value", JsonUtils.getAsOptionalString(JSON_OBJECT, "String").get());
assertEquals(Optional.empty(), JsonUtils.getAsOptionalString(JSON_OBJECT, "foo"));

// -> As String or Else
assertEquals("value", JsonUtils.getAsStringOrElse(JSON_OBJECT, "String", "alternative"));
assertEquals("alternative", JsonUtils.getAsStringOrElse(JSON_OBJECT, "foo", "alternative"));
}

@Test
Expand Down
Loading

0 comments on commit 5f207bd

Please sign in to comment.