Search in sources :

Example 1 with ChannelAddress

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;
}
Also used : Instant(java.time.Instant) ArrayList(java.util.ArrayList) ChannelAddress(io.openems.common.types.ChannelAddress) JsonObject(com.google.gson.JsonObject) QueryResult(org.influxdb.dto.QueryResult) Result(org.influxdb.dto.QueryResult.Result) JsonArray(com.google.gson.JsonArray) Series(org.influxdb.dto.QueryResult.Series) QueryResult(org.influxdb.dto.QueryResult) ZonedDateTime(java.time.ZonedDateTime) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject)

Example 2 with ChannelAddress

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;
}
Also used : Instant(java.time.Instant) ArrayList(java.util.ArrayList) ChannelAddress(io.openems.common.types.ChannelAddress) JsonObject(com.google.gson.JsonObject) QueryResult(org.influxdb.dto.QueryResult) Result(org.influxdb.dto.QueryResult.Result) JsonArray(com.google.gson.JsonArray) Series(org.influxdb.dto.QueryResult.Series) QueryResult(org.influxdb.dto.QueryResult) ZonedDateTime(java.time.ZonedDateTime) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject)

Example 3 with ChannelAddress

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;
}
Also used : JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) ChannelAddress(io.openems.common.types.ChannelAddress)

Example 4 with ChannelAddress

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;
}
Also used : StringFieldValue(io.openems.common.types.StringFieldValue) JsonObject(com.google.gson.JsonObject) ChannelAddress(io.openems.common.types.ChannelAddress) NumberFieldValue(io.openems.common.types.NumberFieldValue) NumberFieldValue(io.openems.common.types.NumberFieldValue) FieldValue(io.openems.common.types.FieldValue) StringFieldValue(io.openems.common.types.StringFieldValue)

Example 5 with ChannelAddress

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());
            }
        }
    }
}
Also used : ConfigChannel(io.openems.api.channel.ConfigChannel) Channel(io.openems.api.channel.Channel) JsonObject(com.google.gson.JsonObject) ChannelAddress(io.openems.common.types.ChannelAddress) OpenemsException(io.openems.common.exceptions.OpenemsException) ChannelDoc(io.openems.api.doc.ChannelDoc) OpenemsException(io.openems.common.exceptions.OpenemsException) ModbusException(com.ghgande.j2mod.modbus.ModbusException) ThingRepository(io.openems.core.ThingRepository) JsonElement(com.google.gson.JsonElement)

Aggregations

JsonObject (com.google.gson.JsonObject)5 ChannelAddress (io.openems.common.types.ChannelAddress)5 JsonElement (com.google.gson.JsonElement)4 JsonArray (com.google.gson.JsonArray)2 Instant (java.time.Instant)2 ZonedDateTime (java.time.ZonedDateTime)2 ArrayList (java.util.ArrayList)2 QueryResult (org.influxdb.dto.QueryResult)2 Result (org.influxdb.dto.QueryResult.Result)2 Series (org.influxdb.dto.QueryResult.Series)2 ModbusException (com.ghgande.j2mod.modbus.ModbusException)1 Channel (io.openems.api.channel.Channel)1 ConfigChannel (io.openems.api.channel.ConfigChannel)1 ChannelDoc (io.openems.api.doc.ChannelDoc)1 OpenemsException (io.openems.common.exceptions.OpenemsException)1 FieldValue (io.openems.common.types.FieldValue)1 NumberFieldValue (io.openems.common.types.NumberFieldValue)1 StringFieldValue (io.openems.common.types.StringFieldValue)1 ThingRepository (io.openems.core.ThingRepository)1