|
| 1 | +package io.openems.common.jsonrpc.notification; |
| 2 | + |
| 3 | +import java.util.Map; |
| 4 | + |
| 5 | +import com.google.common.collect.TreeBasedTable; |
| 6 | +import com.google.gson.JsonElement; |
| 7 | +import com.google.gson.JsonObject; |
| 8 | + |
| 9 | +import io.openems.common.exceptions.OpenemsError.OpenemsNamedException; |
| 10 | +import io.openems.common.jsonrpc.base.JsonrpcNotification; |
| 11 | +import io.openems.common.utils.JsonUtils; |
| 12 | + |
| 13 | +/** |
| 14 | + * Represents a JSON-RPC Notification for timestamped or aggregated data sent |
| 15 | + * from Edge to Backend. |
| 16 | + * |
| 17 | + * <pre> |
| 18 | + * { |
| 19 | + * "jsonrpc": "2.0", |
| 20 | + * "method": "timestampedData | aggregatedData", |
| 21 | + * "params": { |
| 22 | + * [timestamp: epoch in milliseconds]: { |
| 23 | + * [channelAddress]: T |
| 24 | + * } |
| 25 | + * } |
| 26 | + * } |
| 27 | + * </pre> |
| 28 | + */ |
| 29 | +// TODO change to sealed class |
| 30 | +public abstract class AbstractDataNotification extends JsonrpcNotification { |
| 31 | + |
| 32 | + private final TreeBasedTable<Long, String, JsonElement> data; |
| 33 | + |
| 34 | + protected static TreeBasedTable<Long, String, JsonElement> parseParams(// |
| 35 | + final JsonObject params // |
| 36 | + ) throws OpenemsNamedException { |
| 37 | + var data = TreeBasedTable.<Long, String, JsonElement>create(); |
| 38 | + for (var e1 : params.entrySet()) { |
| 39 | + var timestamp = Long.parseLong(e1.getKey()); |
| 40 | + var jTime = JsonUtils.getAsJsonObject(e1.getValue()); |
| 41 | + for (var e2 : jTime.entrySet()) { |
| 42 | + data.put(timestamp, e2.getKey(), e2.getValue()); |
| 43 | + } |
| 44 | + } |
| 45 | + return data; |
| 46 | + } |
| 47 | + |
| 48 | + protected AbstractDataNotification(String method, TreeBasedTable<Long, String, JsonElement> data) { |
| 49 | + super(method); |
| 50 | + this.data = data; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Add timestamped data. |
| 55 | + * |
| 56 | + * @param timestamp the timestamp epoch in milliseconds |
| 57 | + * @param data a map of Channel-Address to {@link JsonElement} value |
| 58 | + */ |
| 59 | + public void add(long timestamp, Map<String, JsonElement> data) { |
| 60 | + for (var entry : data.entrySet()) { |
| 61 | + this.add(timestamp, entry.getKey(), entry.getValue()); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Add a timestamped value. |
| 67 | + * |
| 68 | + * @param timestamp the timestamp epoch in milliseconds |
| 69 | + * @param address the Channel-Address |
| 70 | + * @param value the {@link JsonElement} value |
| 71 | + */ |
| 72 | + public void add(long timestamp, String address, JsonElement value) { |
| 73 | + this.data.put(timestamp, address, value); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public JsonObject getParams() { |
| 78 | + var p = new JsonObject(); |
| 79 | + for (var e1 : this.data.rowMap().entrySet()) { |
| 80 | + var jTime = new JsonObject(); |
| 81 | + for (var e2 : e1.getValue().entrySet()) { |
| 82 | + var address = e2.getKey(); |
| 83 | + var value = e2.getValue(); |
| 84 | + jTime.add(address, value); |
| 85 | + } |
| 86 | + p.add(e1.getKey().toString(), jTime); |
| 87 | + } |
| 88 | + return p; |
| 89 | + } |
| 90 | + |
| 91 | + public TreeBasedTable<Long, String, JsonElement> getData() { |
| 92 | + return this.data; |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments