-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split TimestampedDatanotification into AggregatedDataNotification, Ti…
…mestampedDataNotification and ResendDataNotification. Added/enhanced some utilities classes. (#2297) Thank you for providing us your code and for review.
- Loading branch information
Showing
9 changed files
with
482 additions
and
64 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
io.openems.common/src/io/openems/common/channel/PersistencePriority.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
io.openems.common/src/io/openems/common/function/ThrowingTriConsumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.openems.common.function; | ||
|
||
/** | ||
* This interface is similar to the java.util interface | ||
* {@link ThrowingBiConsumer}. Difference is, that it allows to pass to the | ||
* apply() method one more parameter. | ||
* | ||
* @param <T> the apply methods first argument type | ||
* @param <U> the apply methods second argument type | ||
* @param <S> the apply methods third argument type | ||
* @param <E> the exception type | ||
*/ | ||
@FunctionalInterface | ||
public interface ThrowingTriConsumer<T, U, S, E extends Exception> { | ||
|
||
/** | ||
* Applies this function to the given arguments. | ||
* | ||
* @param t the first function argument | ||
* @param u the second function argument | ||
* @param s the third function argument | ||
* @throws E on error | ||
*/ | ||
public void accept(T t, U u, S s) throws E; | ||
|
||
} |
95 changes: 95 additions & 0 deletions
95
io.openems.common/src/io/openems/common/jsonrpc/notification/AbstractDataNotification.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package io.openems.common.jsonrpc.notification; | ||
|
||
import java.util.Map; | ||
|
||
import com.google.common.collect.TreeBasedTable; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
|
||
import io.openems.common.exceptions.OpenemsError.OpenemsNamedException; | ||
import io.openems.common.jsonrpc.base.JsonrpcNotification; | ||
import io.openems.common.utils.JsonUtils; | ||
|
||
/** | ||
* Represents a JSON-RPC Notification for timestamped or aggregated data sent | ||
* from Edge to Backend. | ||
* | ||
* <pre> | ||
* { | ||
* "jsonrpc": "2.0", | ||
* "method": "timestampedData | aggregatedData", | ||
* "params": { | ||
* [timestamp: epoch in milliseconds]: { | ||
* [channelAddress]: T | ||
* } | ||
* } | ||
* } | ||
* </pre> | ||
*/ | ||
// TODO change to sealed class | ||
public abstract class AbstractDataNotification extends JsonrpcNotification { | ||
|
||
private final TreeBasedTable<Long, String, JsonElement> data; | ||
|
||
protected static TreeBasedTable<Long, String, JsonElement> parseParams(// | ||
final JsonObject params // | ||
) throws OpenemsNamedException { | ||
var data = TreeBasedTable.<Long, String, JsonElement>create(); | ||
for (var e1 : params.entrySet()) { | ||
var timestamp = Long.parseLong(e1.getKey()); | ||
var jTime = JsonUtils.getAsJsonObject(e1.getValue()); | ||
for (var e2 : jTime.entrySet()) { | ||
data.put(timestamp, e2.getKey(), e2.getValue()); | ||
} | ||
} | ||
return data; | ||
} | ||
|
||
protected AbstractDataNotification(String method, TreeBasedTable<Long, String, JsonElement> data) { | ||
super(method); | ||
this.data = data; | ||
} | ||
|
||
/** | ||
* Add timestamped data. | ||
* | ||
* @param timestamp the timestamp epoch in milliseconds | ||
* @param data a map of Channel-Address to {@link JsonElement} value | ||
*/ | ||
public void add(long timestamp, Map<String, JsonElement> data) { | ||
for (var entry : data.entrySet()) { | ||
this.add(timestamp, entry.getKey(), entry.getValue()); | ||
} | ||
} | ||
|
||
/** | ||
* Add a timestamped value. | ||
* | ||
* @param timestamp the timestamp epoch in milliseconds | ||
* @param address the Channel-Address | ||
* @param value the {@link JsonElement} value | ||
*/ | ||
public void add(long timestamp, String address, JsonElement value) { | ||
this.data.put(timestamp, address, value); | ||
} | ||
|
||
@Override | ||
public JsonObject getParams() { | ||
var p = new JsonObject(); | ||
for (var e1 : this.data.rowMap().entrySet()) { | ||
var jTime = new JsonObject(); | ||
for (var e2 : e1.getValue().entrySet()) { | ||
var address = e2.getKey(); | ||
var value = e2.getValue(); | ||
jTime.add(address, value); | ||
} | ||
p.add(e1.getKey().toString(), jTime); | ||
} | ||
return p; | ||
} | ||
|
||
public TreeBasedTable<Long, String, JsonElement> getData() { | ||
return this.data; | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
io.openems.common/src/io/openems/common/jsonrpc/notification/AggregatedDataNotification.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package io.openems.common.jsonrpc.notification; | ||
|
||
import com.google.common.collect.TreeBasedTable; | ||
import com.google.gson.JsonElement; | ||
|
||
import io.openems.common.exceptions.OpenemsError.OpenemsNamedException; | ||
import io.openems.common.jsonrpc.base.JsonrpcNotification; | ||
|
||
/** | ||
* Represents a JSON-RPC Notification for aggregatedData data sent from Edge to | ||
* Backend. | ||
* | ||
* <pre> | ||
* { | ||
* "jsonrpc": "2.0", | ||
* "method": "aggregatedData", | ||
* "params": { | ||
* [timestamp: epoch in milliseconds]: { | ||
* [channelAddress]: {@link JsonElement} | ||
* } | ||
* } | ||
* } | ||
* </pre> | ||
*/ | ||
public class AggregatedDataNotification extends AbstractDataNotification { | ||
|
||
public static final String METHOD = "aggregatedData"; | ||
|
||
/** | ||
* Parses a {@link JsonrpcNotification} to a {@link AggregatedDataNotification}. | ||
* | ||
* @param notification the {@link JsonrpcNotification} | ||
* @return the {@link AggregatedDataNotification} | ||
* @throws OpenemsNamedException on error | ||
*/ | ||
public static AggregatedDataNotification from(JsonrpcNotification notification) throws OpenemsNamedException { | ||
return new AggregatedDataNotification(parseParams(notification.getParams())); | ||
} | ||
|
||
public AggregatedDataNotification(TreeBasedTable<Long, String, JsonElement> data) { | ||
super(AggregatedDataNotification.METHOD, data); | ||
} | ||
|
||
public AggregatedDataNotification() { | ||
super(AggregatedDataNotification.METHOD, TreeBasedTable.create()); | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
io.openems.common/src/io/openems/common/jsonrpc/notification/ResendDataNotification.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package io.openems.common.jsonrpc.notification; | ||
|
||
import com.google.common.collect.TreeBasedTable; | ||
import com.google.gson.JsonElement; | ||
|
||
import io.openems.common.exceptions.OpenemsError.OpenemsNamedException; | ||
import io.openems.common.jsonrpc.base.JsonrpcNotification; | ||
|
||
/** | ||
* Represents a JSON-RPC Notification for resending aggregated data. | ||
* | ||
* <pre> | ||
* { | ||
* "jsonrpc": "2.0", | ||
* "method": "resendData", | ||
* "params": { | ||
* [channelAddress]: string | number | ||
* } | ||
* } | ||
* </pre> | ||
*/ | ||
public class ResendDataNotification extends AbstractDataNotification { | ||
|
||
public static final String METHOD = "resendData"; | ||
|
||
/** | ||
* Parses a {@link JsonrpcNotification} to a {@link ResendDataNotification}. | ||
* | ||
* @param notification the {@link JsonrpcNotification} | ||
* @return the {@link ResendDataNotification} | ||
* @throws OpenemsNamedException on error | ||
*/ | ||
public static ResendDataNotification from(JsonrpcNotification notification) throws OpenemsNamedException { | ||
return new ResendDataNotification(parseParams(notification.getParams())); | ||
} | ||
|
||
public ResendDataNotification(TreeBasedTable<Long, String, JsonElement> data) { | ||
super(ResendDataNotification.METHOD, data); | ||
} | ||
|
||
} |
Oops, something went wrong.