Search in sources :

Example 1 with Builder

use of org.influxdb.dto.Point.Builder in project openems by OpenEMS.

the class Influx method writeData.

private void writeData(int influxId, TreeBasedTable<Long, String, Object> data) throws OpenemsException {
    InfluxDB influxDB = this.getInfluxDbConnection();
    BatchPoints batchPoints = // 
    BatchPoints.database(database).tag("fems", // 
    String.valueOf(influxId)).build();
    for (Entry<Long, Map<String, Object>> entry : data.rowMap().entrySet()) {
        Long timestamp = entry.getKey();
        Builder builder = // this builds an InfluxDB record ("point") for a
        Point.measurement(this.measurement).time(timestamp, TimeUnit.MILLISECONDS).fields(entry.getValue());
        batchPoints.point(builder.build());
    }
    // write to DB
    influxDB.write(batchPoints);
}
Also used : BatchPoints(org.influxdb.dto.BatchPoints) InfluxDB(org.influxdb.InfluxDB) Builder(org.influxdb.dto.Point.Builder) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 2 with Builder

use of org.influxdb.dto.Point.Builder in project openems by OpenEMS.

the class Influx method writeDataToOldMiniMonitoring.

/**
 * Writes data to old database for old Mini monitoring
 *
 * XXX remove after full migration
 *
 * @param device
 * @param data
 * @throws OpenemsException
 */
private void writeDataToOldMiniMonitoring(Edge edge, int influxId, TreeBasedTable<Long, String, Object> data) throws OpenemsException {
    InfluxDB influxDB = getInfluxDbConnection();
    BatchPoints batchPoints = // 
    BatchPoints.database(database).tag("fems", // 
    String.valueOf(influxId)).build();
    for (Entry<Long, Map<String, Object>> entry : data.rowMap().entrySet()) {
        Long timestamp = entry.getKey();
        Builder builder = Point.measurement(TMP_MINI_MEASUREMENT).time(timestamp, TimeUnit.MILLISECONDS);
        Map<String, Object> fields = new HashMap<>();
        for (Entry<String, Object> valueEntry : entry.getValue().entrySet()) {
            String channel = valueEntry.getKey();
            Object valueObj = valueEntry.getValue();
            if (valueObj instanceof Number) {
                Long value = ((Number) valueObj).longValue();
                // convert channel ids to old identifiers
                if (channel.equals("ess0/Soc")) {
                    fields.put("Stack_SOC", value);
                    edge.setSoc(value.intValue());
                } else if (channel.equals("meter0/ActivePower")) {
                    fields.put("PCS_Grid_Power_Total", value * -1);
                } else if (channel.equals("meter1/ActivePower")) {
                    fields.put("PCS_PV_Power_Total", value);
                } else if (channel.equals("meter2/ActivePower")) {
                    fields.put("PCS_Load_Power_Total", value);
                }
                // from here value needs to be divided by 10 for backwards compatibility
                value = value / 10;
                if (channel.equals("meter2/Energy")) {
                    fields.put("PCS_Summary_Consumption_Accumulative_cor", value);
                    fields.put("PCS_Summary_Consumption_Accumulative", value);
                } else if (channel.equals("meter0/BuyFromGridEnergy")) {
                    fields.put("PCS_Summary_Grid_Buy_Accumulative_cor", value);
                    fields.put("PCS_Summary_Grid_Buy_Accumulative", value);
                } else if (channel.equals("meter0/SellToGridEnergy")) {
                    fields.put("PCS_Summary_Grid_Sell_Accumulative_cor", value);
                    fields.put("PCS_Summary_Grid_Sell_Accumulative", value);
                } else if (channel.equals("meter1/EnergyL1")) {
                    fields.put("PCS_Summary_PV_Accumulative_cor", value);
                    fields.put("PCS_Summary_PV_Accumulative", value);
                }
            }
        }
        if (fields.size() > 0) {
            builder.fields(fields);
            batchPoints.point(builder.build());
        }
    }
    // write to DB
    influxDB.write(batchPoints);
}
Also used : BatchPoints(org.influxdb.dto.BatchPoints) HashMap(java.util.HashMap) InfluxDB(org.influxdb.InfluxDB) Builder(org.influxdb.dto.Point.Builder) JsonObject(com.google.gson.JsonObject) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 3 with Builder

use of org.influxdb.dto.Point.Builder in project openems by OpenEMS.

the class InfluxdbPersistence method forever.

@Override
protected void forever() {
    // Prepare DB connection
    Optional<InfluxDB> _influxdb = getInfluxDB();
    if (!_influxdb.isPresent()) {
        synchronized (queue) {
            // Clear queue if we don't have a valid influxdb connection. This is necessary to avoid filling the
            // memory in case of no available DB connection
            queue.clear();
        }
    }
    InfluxDB influxDB = _influxdb.get();
    /*
		 * Convert FieldVales in queue to Points
		 */
    BatchPoints batchPoints = // 
    BatchPoints.database(database.valueOptional().orElse("db")).tag("fems", // 
    String.valueOf(fems.valueOptional().get())).build();
    synchronized (queue) {
        queue.asMap().forEach((timestamp, fieldValues) -> {
            Builder builder = // 
            Point.measurement("data").time(timestamp, TimeUnit.MILLISECONDS);
            fieldValues.forEach(fieldValue -> {
                if (fieldValue instanceof NumberFieldValue) {
                    builder.addField(fieldValue.field, ((NumberFieldValue) fieldValue).value);
                } else if (fieldValue instanceof StringFieldValue) {
                    builder.addField(fieldValue.field, ((StringFieldValue) fieldValue).value);
                }
            });
            batchPoints.point(builder.build());
        });
        queue.clear();
    }
    // write to DB
    try {
        influxDB.write(batchPoints);
        log.debug("Wrote [" + batchPoints.getPoints().size() + "] points to InfluxDB");
    } catch (RuntimeException e) {
        log.error("Error writing to InfluxDB: " + e);
    }
}
Also used : BatchPoints(org.influxdb.dto.BatchPoints) InfluxDB(org.influxdb.InfluxDB) Builder(org.influxdb.dto.Point.Builder)

Aggregations

InfluxDB (org.influxdb.InfluxDB)3 BatchPoints (org.influxdb.dto.BatchPoints)3 Builder (org.influxdb.dto.Point.Builder)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 JsonObject (com.google.gson.JsonObject)1