|
| 1 | +package io.openems.common.jsonrpc.request; |
| 2 | + |
| 3 | +import java.time.ZoneId; |
| 4 | +import java.time.ZoneOffset; |
| 5 | +import java.time.ZonedDateTime; |
| 6 | +import java.time.format.DateTimeFormatter; |
| 7 | +import java.util.TreeSet; |
| 8 | +import java.util.UUID; |
| 9 | + |
| 10 | +import com.google.gson.JsonArray; |
| 11 | +import com.google.gson.JsonElement; |
| 12 | +import com.google.gson.JsonObject; |
| 13 | + |
| 14 | +import io.openems.common.exceptions.OpenemsError.OpenemsNamedException; |
| 15 | +import io.openems.common.exceptions.OpenemsException; |
| 16 | +import io.openems.common.jsonrpc.base.GenericJsonrpcRequest; |
| 17 | +import io.openems.common.jsonrpc.base.JsonrpcRequest; |
| 18 | +import io.openems.common.types.ChannelAddress; |
| 19 | +import io.openems.common.utils.JsonUtils; |
| 20 | + |
| 21 | +/** |
| 22 | + * Represents a JSON-RPC Request for 'queryHistoricTimeseriesData'. |
| 23 | + * |
| 24 | + * <pre> |
| 25 | + * { |
| 26 | + * "jsonrpc": "2.0", |
| 27 | + * "id": "UUID", |
| 28 | + * "method": "queryHistoricTimeseriesEnergy", |
| 29 | + * "params": { |
| 30 | + * "timezone": Number, |
| 31 | + * "fromDate": YYYY-MM-DD, |
| 32 | + * "toDate": YYYY-MM-DD, |
| 33 | + * "channels": ChannelAddress[] |
| 34 | + * } |
| 35 | + * } |
| 36 | + * </pre> |
| 37 | + */ |
| 38 | + |
| 39 | +public class QueryHistoricTimeseriesEnergyRequest extends JsonrpcRequest { |
| 40 | + |
| 41 | + public final static String METHOD = "queryHistoricTimeseriesEnergy"; |
| 42 | + |
| 43 | + private final static DateTimeFormatter FORMAT = DateTimeFormatter.ISO_LOCAL_DATE; |
| 44 | + |
| 45 | + public static QueryHistoricTimeseriesEnergyRequest from(JsonrpcRequest r) throws OpenemsNamedException { |
| 46 | + JsonObject p = r.getParams(); |
| 47 | + int timezoneDiff = JsonUtils.getAsInt(p, "timezone"); |
| 48 | + ZoneId timezone = ZoneId.ofOffset("", ZoneOffset.ofTotalSeconds(timezoneDiff * -1)); |
| 49 | + ZonedDateTime fromDate = JsonUtils.getAsZonedDateTime(p, "fromDate", timezone); |
| 50 | + ZonedDateTime toDate = JsonUtils.getAsZonedDateTime(p, "toDate", timezone).plusDays(1); |
| 51 | + QueryHistoricTimeseriesEnergyRequest result = new QueryHistoricTimeseriesEnergyRequest(r.getId(), fromDate, |
| 52 | + toDate); |
| 53 | + JsonArray channels = JsonUtils.getAsJsonArray(p, "channels"); |
| 54 | + for (JsonElement channel : channels) { |
| 55 | + ChannelAddress address = ChannelAddress.fromString(JsonUtils.getAsString(channel)); |
| 56 | + result.addChannel(address); |
| 57 | + } |
| 58 | + return result; |
| 59 | + } |
| 60 | + |
| 61 | + public static QueryHistoricTimeseriesEnergyRequest from(JsonObject j) throws OpenemsNamedException { |
| 62 | + return from(GenericJsonrpcRequest.from(j)); |
| 63 | + } |
| 64 | + |
| 65 | + private final int timezoneDiff; |
| 66 | + private final ZonedDateTime fromDate; |
| 67 | + private final ZonedDateTime toDate; |
| 68 | + private final TreeSet<ChannelAddress> channels = new TreeSet<>(); |
| 69 | + |
| 70 | + public QueryHistoricTimeseriesEnergyRequest(UUID id, ZonedDateTime fromDate, ZonedDateTime toDate) |
| 71 | + throws OpenemsNamedException { |
| 72 | + super(id, METHOD); |
| 73 | + |
| 74 | + this.timezoneDiff = ZoneOffset.from(fromDate).getTotalSeconds(); |
| 75 | + if (timezoneDiff != ZoneOffset.from(toDate).getTotalSeconds()) { |
| 76 | + throw new OpenemsException("FromDate and ToDate need to be in the same timezone!"); |
| 77 | + } |
| 78 | + |
| 79 | + this.fromDate = fromDate; |
| 80 | + this.toDate = toDate; |
| 81 | + } |
| 82 | + |
| 83 | + public QueryHistoricTimeseriesEnergyRequest(ZonedDateTime fromDate, ZonedDateTime toDate) |
| 84 | + throws OpenemsNamedException { |
| 85 | + this(UUID.randomUUID(), fromDate, toDate); |
| 86 | + } |
| 87 | + |
| 88 | + private void addChannel(ChannelAddress address) { |
| 89 | + this.channels.add(address); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public JsonObject getParams() { |
| 94 | + JsonArray channels = new JsonArray(); |
| 95 | + for (ChannelAddress address : this.channels) { |
| 96 | + channels.add(address.toString()); |
| 97 | + } |
| 98 | + return JsonUtils.buildJsonObject().addProperty("timezone", this.timezoneDiff) // |
| 99 | + .addProperty("fromDate", FORMAT.format(this.fromDate)) // |
| 100 | + .addProperty("toDate", FORMAT.format(this.toDate)) // |
| 101 | + .add("channels", channels) // |
| 102 | + .build(); |
| 103 | + } |
| 104 | + |
| 105 | + public ZonedDateTime getFromDate() { |
| 106 | + return fromDate; |
| 107 | + } |
| 108 | + |
| 109 | + public ZonedDateTime getToDate() { |
| 110 | + return toDate; |
| 111 | + } |
| 112 | + |
| 113 | + public TreeSet<ChannelAddress> getChannels() { |
| 114 | + return channels; |
| 115 | + } |
| 116 | +} |
0 commit comments