Search in sources :

Example 1 with Point

use of com.influxdb.client.write.Point in project solarthing by wildmountainfarms.

the class ConstantMeasurementPacketPoint2Creator method createBuilder.

@Override
public Point createBuilder(Packet packet) {
    Point r = Point.measurement(measurement);
    r.addTags(PointUtil.getTags(packet));
    return r;
}
Also used : Point(com.influxdb.client.write.Point)

Example 2 with Point

use of com.influxdb.client.write.Point in project solarthing by wildmountainfarms.

the class InfluxDb2PacketSaver method handle.

@Override
public void handle(PacketCollection packetCollection) throws PacketHandleException {
    final InstancePacketGroup packetGroup = PacketGroups.parseToInstancePacketGroup(packetCollection, DefaultInstanceOptions.REQUIRE_NO_DEFAULTS);
    DefaultInstanceOptions.requireNoDefaults(packetGroup);
    Organization organization = findOrCreateOrg();
    Bucket bucket = findOrCreateBucket(bucketNameGetter.getName(packetGroup), organization);
    final long time = packetCollection.getDateMillis();
    List<Point> points = new ArrayList<>();
    for (Packet packet : packetGroup.getPackets()) {
        Point point = pointCreator.createBuilder(packet).time(time, WritePrecision.MS);
        Collection<String> tagKeys = PointUtil.getTagKeys(packet.getClass());
        ObjectNode json = OBJECT_MAPPER.valueToTree(packet);
        for (Map.Entry<String, ValueNode> entry : PointUtil.flattenJsonObject(json)) {
            String key = entry.getKey();
            ValueNode prim = entry.getValue();
            if (tagKeys.contains(key)) {
                point.addTag(key, prim.asText());
            }
            if (prim.isNumber()) {
                // always store as float datatype because you can never change the type from int to float easily
                final Number value;
                if (prim.isBigDecimal()) {
                    DecimalNode decimal = (DecimalNode) prim;
                    value = decimal.decimalValue();
                } else {
                    value = prim.asDouble();
                }
                point.addField(key, value);
            } else if (prim.isTextual() || prim.isBinary()) {
                point.addField(key, prim.asText());
            } else if (prim.isBoolean()) {
                point.addField(key, prim.asBoolean());
            } else
                throw new AssertionError("This primitive isn't a number, string/binary or boolean! It's: " + prim + " class: " + prim.getClass() + " text=" + prim.asText());
        }
        points.add(point);
    }
    try {
        client.getWriteApiBlocking().writePoints(bucket.getName(), bucket.getOrgID(), points);
    } catch (InfluxException exception) {
        throw new PacketHandleException("Could not write points", exception);
    }
}
Also used : InstancePacketGroup(me.retrodaredevil.solarthing.packets.collection.InstancePacketGroup) Packet(me.retrodaredevil.solarthing.packets.Packet) Organization(com.influxdb.client.domain.Organization) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) Point(com.influxdb.client.write.Point) Bucket(com.influxdb.client.domain.Bucket) ValueNode(com.fasterxml.jackson.databind.node.ValueNode) InfluxException(com.influxdb.exceptions.InfluxException) DecimalNode(com.fasterxml.jackson.databind.node.DecimalNode) Map(java.util.Map) PacketHandleException(me.retrodaredevil.solarthing.packets.handling.PacketHandleException)

Aggregations

Point (com.influxdb.client.write.Point)2 DecimalNode (com.fasterxml.jackson.databind.node.DecimalNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ValueNode (com.fasterxml.jackson.databind.node.ValueNode)1 Bucket (com.influxdb.client.domain.Bucket)1 Organization (com.influxdb.client.domain.Organization)1 InfluxException (com.influxdb.exceptions.InfluxException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Packet (me.retrodaredevil.solarthing.packets.Packet)1 InstancePacketGroup (me.retrodaredevil.solarthing.packets.collection.InstancePacketGroup)1 PacketHandleException (me.retrodaredevil.solarthing.packets.handling.PacketHandleException)1