use of io.openems.common.types.ChannelAddress in project openems by OpenEMS.
the class InfluxdbQueryWrapper method queryData.
private static JsonArray queryData(InfluxDB influxdb, Optional<Integer> fems, ZonedDateTime fromDate, ZonedDateTime toDate, JsonObject channels, int resolution, String dbName) throws OpenemsException {
// Prepare query string
StringBuilder query = new StringBuilder("SELECT ");
query.append(toChannelAddressList(channels));
query.append(" FROM data WHERE ");
if (fems.isPresent()) {
query.append("fems = '");
query.append(fems.get());
query.append("' AND ");
}
query.append("time > ");
query.append(String.valueOf(fromDate.toEpochSecond()));
query.append("s");
query.append(" AND time < ");
query.append(String.valueOf(toDate.toEpochSecond()));
query.append("s");
query.append(" GROUP BY time(");
query.append(resolution);
query.append("s) fill(previous)");
QueryResult queryResult = executeQuery(influxdb, query.toString(), dbName);
JsonArray j = new JsonArray();
for (Result result : queryResult.getResults()) {
List<Series> seriess = result.getSeries();
if (seriess != null) {
for (Series series : seriess) {
// create thing/channel index
ArrayList<ChannelAddress> addressIndex = new ArrayList<>();
for (String column : series.getColumns()) {
if (column.equals("time")) {
continue;
}
addressIndex.add(ChannelAddress.fromString(column));
}
// first: create empty timestamp objects
for (List<Object> values : series.getValues()) {
JsonObject jTimestamp = new JsonObject();
// get timestamp
Instant timestampInstant = Instant.ofEpochMilli((long) ((Double) values.get(0)).doubleValue());
ZonedDateTime timestamp = ZonedDateTime.ofInstant(timestampInstant, fromDate.getZone());
String timestampString = timestamp.format(DateTimeFormatter.ISO_INSTANT);
jTimestamp.addProperty("time", timestampString);
// add empty channels by copying "channels" parameter
JsonObject jChannels = new JsonObject();
for (Entry<String, JsonElement> entry : channels.entrySet()) {
String thingId = entry.getKey();
JsonObject jThing = new JsonObject();
JsonArray channelIds = JsonUtils.getAsJsonArray(entry.getValue());
for (JsonElement channelElement : channelIds) {
String channelId = JsonUtils.getAsString(channelElement);
jThing.add(channelId, JsonNull.INSTANCE);
}
jChannels.add(thingId, jThing);
}
jTimestamp.add("channels", jChannels);
j.add(jTimestamp);
}
// then: add all data
for (int columnIndex = 1; columnIndex < series.getColumns().size(); columnIndex++) {
for (int timeIndex = 0; timeIndex < series.getValues().size(); timeIndex++) {
Double value = (Double) series.getValues().get(timeIndex).get(columnIndex);
ChannelAddress address = addressIndex.get(columnIndex - 1);
j.get(timeIndex).getAsJsonObject().get("channels").getAsJsonObject().get(address.getThingId()).getAsJsonObject().addProperty(address.getChannelId(), value);
}
}
}
}
}
return j;
}
use of io.openems.common.types.ChannelAddress in project openems by OpenEMS.
the class InfluxdbUtils method queryHistoricData.
public static JsonArray queryHistoricData(InfluxDB influxdb, String database, Optional<Integer> influxIdOpt, ZonedDateTime fromDate, ZonedDateTime toDate, JsonObject channels, int resolution) throws OpenemsException {
// Prepare query string
StringBuilder query = new StringBuilder("SELECT ");
query.append(toChannelAddressList(channels));
query.append(" FROM data WHERE ");
if (influxIdOpt.isPresent()) {
query.append("fems = '" + influxIdOpt.get() + "' AND ");
}
query.append("time > ");
query.append(String.valueOf(fromDate.toEpochSecond()));
query.append("s");
query.append(" AND time < ");
query.append(String.valueOf(toDate.toEpochSecond()));
query.append("s");
query.append(" GROUP BY time(");
query.append(resolution);
query.append("s) fill(null)");
QueryResult queryResult = executeQuery(influxdb, database, query.toString());
JsonArray j = new JsonArray();
for (Result result : queryResult.getResults()) {
List<Series> seriess = result.getSeries();
if (seriess != null) {
for (Series series : seriess) {
// create thing/channel index
ArrayList<ChannelAddress> addressIndex = new ArrayList<>();
for (String column : series.getColumns()) {
if (column.equals("time")) {
continue;
}
addressIndex.add(ChannelAddress.fromString(column));
}
// first: create empty timestamp objects
for (List<Object> values : series.getValues()) {
JsonObject jTimestamp = new JsonObject();
// get timestamp
Instant timestampInstant = Instant.ofEpochMilli((long) ((Double) values.get(0)).doubleValue());
ZonedDateTime timestamp = ZonedDateTime.ofInstant(timestampInstant, fromDate.getZone());
String timestampString = timestamp.format(DateTimeFormatter.ISO_INSTANT);
jTimestamp.addProperty("time", timestampString);
// add empty channels by copying "channels" parameter
JsonObject jChannels = new JsonObject();
for (Entry<String, JsonElement> entry : channels.entrySet()) {
String thingId = entry.getKey();
JsonObject jThing = new JsonObject();
JsonArray channelIds = JsonUtils.getAsJsonArray(entry.getValue());
for (JsonElement channelElement : channelIds) {
String channelId = JsonUtils.getAsString(channelElement);
jThing.add(channelId, JsonNull.INSTANCE);
}
jChannels.add(thingId, jThing);
}
jTimestamp.add("channels", jChannels);
j.add(jTimestamp);
}
// then: add all data
for (int columnIndex = 1; columnIndex < series.getColumns().size(); columnIndex++) {
for (int timeIndex = 0; timeIndex < series.getValues().size(); timeIndex++) {
Double value = (Double) series.getValues().get(timeIndex).get(columnIndex);
ChannelAddress address = addressIndex.get(columnIndex - 1);
j.get(timeIndex).getAsJsonObject().get("channels").getAsJsonObject().get(address.getThingId()).getAsJsonObject().addProperty(address.getChannelId(), value);
}
}
}
}
}
return j;
}
use of io.openems.common.types.ChannelAddress in project openems by OpenEMS.
the class CurrentDataWorker method getSubscribedData.
/**
* Gets a json object with all subscribed channels
*
* @return
*/
private JsonObject getSubscribedData() {
JsonObject jData = new JsonObject();
for (String thingId : this.channels.keys()) {
JsonObject jThingData = new JsonObject();
for (String channelId : this.channels.get(thingId)) {
ChannelAddress channelAddress = new ChannelAddress(thingId, channelId);
Optional<JsonElement> jValueOpt = this.getChannelValue(channelAddress);
if (jValueOpt.isPresent()) {
jThingData.add(channelId, jValueOpt.get());
}
}
jData.add(thingId, jThingData);
}
return jData;
}
use of io.openems.common.types.ChannelAddress in project openems by OpenEMS.
the class DefaultMessages method timestampedData.
/**
* <pre>
* {
* timedata: {
* timestamp (Long): {
* channel: String,
* value: String | Number
* }
* }
* }
* </pre>
*
* @param token
* @return
*/
public static JsonObject timestampedData(long timestamp, HashMap<ChannelAddress, FieldValue<?>> queue) {
JsonObject jTimestamp = new JsonObject();
for (Entry<ChannelAddress, FieldValue<?>> entry : queue.entrySet()) {
String address = entry.getKey().toString();
FieldValue<?> fieldValue = entry.getValue();
if (fieldValue instanceof NumberFieldValue) {
jTimestamp.addProperty(address, ((NumberFieldValue) fieldValue).value);
} else if (fieldValue instanceof StringFieldValue) {
jTimestamp.addProperty(address, ((StringFieldValue) fieldValue).value);
}
}
JsonObject jTimedata = new JsonObject();
jTimedata.add(String.valueOf(timestamp), jTimestamp);
JsonObject j = new JsonObject();
j.add("timedata", jTimedata);
return j;
}
use of io.openems.common.types.ChannelAddress in project openems by OpenEMS.
the class ModbusTcpApiController method updateChannelMapping.
protected void updateChannelMapping(Optional<JsonObject> jMappingOpt) {
processImage.clearMapping();
ThingRepository thingRepository = ThingRepository.getInstance();
if (jMappingOpt.isPresent()) {
JsonObject jMapping = jMappingOpt.get();
for (Entry<String, JsonElement> entry : jMapping.entrySet()) {
try {
int ref = Integer.parseInt(entry.getKey());
ChannelAddress channelAddress = ChannelAddress.fromString(JsonUtils.getAsString(entry.getValue()));
Optional<ChannelDoc> channelDocOpt = thingRepository.getChannelDoc(channelAddress);
if (channelDocOpt.isPresent()) {
processImage.addMapping(ref, channelAddress, channelDocOpt.get());
} else {
Optional<Channel> channelOpt = thingRepository.getChannel(channelAddress);
if (channelOpt.isPresent()) {
throw new OpenemsException("ChannelDoc for channel [" + channelAddress + "] is not available.");
} else {
throw new OpenemsException("Channel [" + channelAddress + "] does not exist.");
}
}
} catch (Exception e) {
log.error("Unable to add channel mapping: " + e.getMessage());
}
}
}
}
Aggregations