Skip to content

Commit

Permalink
Refactor RPC handling (#2610)
Browse files Browse the repository at this point in the history
Removed endpoints from ControllerApiBackend (edge --> backend):
 - UpdateUserLanguage (Threw error NotImplemented)
 - UpdateUserSettings (Threw error NotImplemented)

Added endpoint to RestApi:
 - queryHistoricTimeseriesEnergyPerPeriod
 - queryHistoricTimeseriesExportXlxs

Reviewed-by: Hueseyin Sahutoglu <34771592+huseyinsaht@users.noreply.github.com>
Co-authored-by: Michael Grill <59126309+michaelgrill@users.noreply.github.com>
  • Loading branch information
sfeilmeier and michaelgrill committed Apr 28, 2024
1 parent c78334d commit 348f59d
Show file tree
Hide file tree
Showing 167 changed files with 6,463 additions and 2,459 deletions.
2 changes: 1 addition & 1 deletion cnf/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
<message key="name.invalidPattern" value="Method type name ''{0}'' must match pattern ''{1}''."/>
</module>
<module name="InterfaceTypeParameterName">
<property name="format" value="(^[A-Z][0-9]?)$|([A-Z][a-zA-Z0-9]*[T]$)"/>
<property name="format" value="(^[A-Z][0-9]?)$|([A-Z][a-zA-Z0-9]$)"/>
<message key="name.invalidPattern" value="Interface type name ''{0}'' must match pattern ''{1}''."/>
</module>
<module name="NoFinalizer"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.openems.common.exceptions;

public class OpenemsRuntimeException extends RuntimeException {

private static final long serialVersionUID = -4509666272212124910L;

public OpenemsRuntimeException() {
super();
}

public OpenemsRuntimeException(String message, Throwable cause, boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}

public OpenemsRuntimeException(String message, Throwable cause) {
super(message, cause);
}

public OpenemsRuntimeException(String message) {
super(message);
}

public OpenemsRuntimeException(Throwable cause) {
super(cause);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.openems.common.jsonrpc.serialization;

import java.util.List;
import java.util.function.Function;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

public interface JsonArrayPath extends JsonPath {

/**
* Gets the elements as a list parsed to the object.
*
* @param <T> the type of the objects
* @param mapper the {@link JsonElement} to object mapper
* @return the list with the parsed values
*/
public <T> List<T> getAsList(Function<JsonElementPath, T> mapper);

/**
* Gets the elements as a list parsed to the object.
*
* @param <T> the type of the objects
* @param serializer the {@link JsonSerializer} to deserialize the elements
* @return the list with the parsed values
*/
public default <T> List<T> getAsList(JsonSerializer<T> serializer) {
return this.getAsList(serializer::deserializePath);
}

/**
* Gets the current element of the path.
*
* @return the {@link JsonObject}
*/
public JsonArray get();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.openems.common.jsonrpc.serialization;

import java.util.List;
import java.util.function.Function;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;

import io.openems.common.exceptions.OpenemsRuntimeException;
import io.openems.common.utils.JsonUtils;

public class JsonArrayPathActual implements JsonArrayPath {

private final JsonArray object;

public JsonArrayPathActual(JsonElement object) {
if (!object.isJsonArray()) {
throw new OpenemsRuntimeException(object + " is not a JsonArray!");
}
this.object = object.getAsJsonArray();
}

@Override
public <T> List<T> getAsList(Function<JsonElementPath, T> mapper) {
return JsonUtils.stream(this.object) //
.map(JsonElementPathActual::new) //
.map(mapper) //
.toList();
}

@Override
public JsonArray get() {
return this.object;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.openems.common.jsonrpc.serialization;

import static java.util.Collections.emptyList;

import java.util.List;
import java.util.function.Function;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;

import io.openems.common.utils.JsonUtils;

public class JsonArrayPathDummy implements JsonArrayPath, JsonPathDummy {

private JsonPathDummy elementType;

@Override
public <T> List<T> getAsList(Function<JsonElementPath, T> mapper) {
final var path = new JsonElementPathDummy();
mapper.apply(path);
this.elementType = path;
return emptyList();
}

@Override
public JsonArray get() {
return new JsonArray();
}

@Override
public JsonElement buildPath() {
return JsonUtils.buildJsonObject() //
.addProperty("type", "array") //
.onlyIf(this.elementType != null, t -> t.add("elementType", this.elementType.buildPath())) //
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.openems.common.jsonrpc.serialization;

import com.google.gson.JsonElement;

public interface JsonElementPath extends JsonPath {

/**
* Gets the current {@link JsonElementPath} as a {@link JsonObjectPath}.
*
* @return the current element as a {@link JsonObjectPath}
*/
public JsonObjectPath getAsJsonObjectPath();

/**
* Gets the current {@link JsonElementPath} as a {@link JsonArrayPath}.
*
* @return the current element as a {@link JsonArrayPath}
*/
public JsonArrayPath getAsJsonArrayPath();

/**
* Gets the current {@link JsonElementPath} as a {@link StringPath}.
*
* @return the current element as a {@link StringPath}
*/
public StringPath getAsStringPath();

/**
* Gets the current {@link JsonElementPath} as a {@link String}.
*
* @return the current element as a {@link String}
*/
public default String getAsString() {
return this.getAsStringPath().get();
}

/**
* Gets the current {@link JsonElementPath} as a Object serialized with the
* provided {@link JsonSerializer}.
*
* @param <O> the type of the final object
* @param serializer the {@link JsonSerializer} to deserialize the
* {@link JsonElement} to the object
* @return the current element as a {@link StringPath}
*/
public <O> O getAsObject(JsonSerializer<O> serializer);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.openems.common.jsonrpc.serialization;

import com.google.gson.JsonElement;

public class JsonElementPathActual implements JsonElementPath {
private final JsonElement element;

public JsonElementPathActual(JsonElement element) {
this.element = element;
}

@Override
public JsonArrayPath getAsJsonArrayPath() {
return new JsonArrayPathActual(this.element);
}

@Override
public JsonObjectPath getAsJsonObjectPath() {
return new JsonObjectPathActual(this.element);
}

@Override
public StringPath getAsStringPath() {
return new StringPathActual(this.element);
}

@Override
public <O> O getAsObject(JsonSerializer<O> deserializer) {
return deserializer.deserializePath(new JsonElementPathActual(this.element));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.openems.common.jsonrpc.serialization;

import com.google.gson.JsonElement;
import com.google.gson.JsonNull;

public class JsonElementPathDummy implements JsonElementPath, JsonPathDummy {

private JsonPathDummy dummyPath;

@Override
public JsonArrayPath getAsJsonArrayPath() {
return this.withDummyPath(new JsonArrayPathDummy());
}

@Override
public JsonObjectPath getAsJsonObjectPath() {
return this.withDummyPath(new JsonObjectPathDummy());
}

@Override
public StringPath getAsStringPath() {
return this.withDummyPath(new StringPathDummy());
}

@Override
public <O> O getAsObject(JsonSerializer<O> deserializer) {
final var dummyPath = new JsonElementPathDummy();
this.withDummyPath(dummyPath);
return deserializer.deserializePath(dummyPath);
}

private <T extends JsonPathDummy> T withDummyPath(T path) {
if (this.dummyPath != null) {
throw new RuntimeException("Path already set");
}
this.dummyPath = path;
return path;
}

public JsonPathDummy getDummyPath() {
return this.dummyPath;
}

@Override
public JsonElement buildPath() {
return this.dummyPath == null ? JsonNull.INSTANCE : this.dummyPath.buildPath();
}

}

0 comments on commit 348f59d

Please sign in to comment.