Search in sources :

Example 1 with PointValueTimeWriter

use of com.infiniteautomation.mango.rest.v2.model.pointValue.PointValueTimeWriter in project ma-modules-public by infiniteautomation.

the class PointValueTimeStreamCsvMessageConverter method writeInternal.

@Override
protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    MediaType contentType = outputMessage.getHeaders().getContentType();
    JsonEncoding encoding = getJsonEncoding(contentType);
    try {
        PointValueTimeStream<?, ?> stream = (PointValueTimeStream<?, ?>) object;
        stream.setContentType(StreamContentType.CSV);
        JsonGenerator generator = this.objectMapper.getFactory().createGenerator(outputMessage.getBody(), encoding);
        // Set the schema
        CsvSchema.Builder builder = CsvSchema.builder();
        builder.setUseHeader(true);
        // Setup our rendering parameters
        LatestQueryInfo info = stream.getQueryInfo();
        if (stream instanceof MultiPointTimeRangeDatabaseStream || stream instanceof MultiPointLatestDatabaseStream) {
            if (info.isSingleArray()) {
                if (info.isMultiplePointsPerArray()) {
                    Map<Integer, DataPointVO> voMap = stream.getVoMap();
                    Iterator<Integer> it = voMap.keySet().iterator();
                    boolean firstTimestamp = true;
                    while (it.hasNext()) {
                        String xid = voMap.get(it.next()).getXid();
                        for (PointValueField field : info.getFields()) {
                            if (field == PointValueField.TIMESTAMP) {
                                if (firstTimestamp)
                                    field.createColumn(builder, xid);
                                firstTimestamp = false;
                            } else
                                field.createColumn(builder, xid);
                        }
                    }
                } else {
                    for (PointValueField field : info.getFields()) field.createColumn(builder, null);
                }
            } else {
                for (PointValueField field : info.getFields()) field.createColumn(builder, null);
            }
        } else if (stream instanceof MultiDataPointStatisticsQuantizerStream) {
            if (stream.getQueryInfo().isSingleArray()) {
                if (stream.getQueryInfo().isMultiplePointsPerArray()) {
                    Map<Integer, DataPointVO> voMap = stream.getVoMap();
                    Iterator<Integer> it = voMap.keySet().iterator();
                    boolean firstTimestamp = true;
                    while (it.hasNext()) {
                        String xid = voMap.get(it.next()).getXid();
                        for (PointValueField field : info.getFields()) {
                            if (field == PointValueField.TIMESTAMP) {
                                if (firstTimestamp)
                                    field.createColumn(builder, xid);
                                firstTimestamp = false;
                            } else if (field == PointValueField.VALUE) {
                                if (info.getRollup() == RollupEnum.ALL) {
                                    for (RollupEnum rollup : getAllRollups()) {
                                        builder.addColumn(xid + PointValueTimeWriter.DOT + rollup.name(), ColumnType.NUMBER_OR_STRING);
                                    }
                                } else {
                                    field.createColumn(builder, xid);
                                }
                            } else {
                                field.createColumn(builder, xid);
                            }
                        }
                    }
                } else {
                    // Single array
                    if (info.getRollup() == RollupEnum.ALL) {
                        for (RollupEnum rollup : getAllRollups()) {
                            builder.addColumn(rollup.name(), ColumnType.NUMBER_OR_STRING);
                        }
                        for (PointValueField field : info.getFields()) {
                            if (field == PointValueField.VALUE)
                                continue;
                            field.createColumn(builder, null);
                        }
                    } else {
                        for (PointValueField field : info.getFields()) field.createColumn(builder, null);
                    }
                }
            } else {
                if (info.getRollup() == RollupEnum.ALL) {
                    for (RollupEnum rollup : getAllRollups()) {
                        builder.addColumn(rollup.name(), ColumnType.NUMBER_OR_STRING);
                    }
                    for (PointValueField field : info.getFields()) {
                        if (field == PointValueField.VALUE)
                            continue;
                        field.createColumn(builder, null);
                    }
                } else {
                    for (PointValueField field : info.getFields()) field.createColumn(builder, null);
                }
            }
        }
        generator.setSchema(builder.build());
        PointValueTimeWriter writer = new PointValueTimeCsvWriter(stream.getQueryInfo(), stream.getVoMap().size(), generator);
        stream.start(writer);
        stream.streamData(writer);
        stream.finish(writer);
        generator.flush();
    } catch (JsonProcessingException ex) {
        throw new HttpMessageNotWritableException("Could not write content: " + ex.getMessage(), ex);
    }
}
Also used : PointValueTimeStream(com.infiniteautomation.mango.rest.v2.model.pointValue.PointValueTimeStream) MultiDataPointStatisticsQuantizerStream(com.infiniteautomation.mango.rest.v2.model.pointValue.quantize.MultiDataPointStatisticsQuantizerStream) LatestQueryInfo(com.infiniteautomation.mango.rest.v2.model.pointValue.query.LatestQueryInfo) CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) PointValueTimeCsvWriter(com.infiniteautomation.mango.rest.v2.model.pointValue.PointValueTimeCsvWriter) JsonEncoding(com.fasterxml.jackson.core.JsonEncoding) MultiPointTimeRangeDatabaseStream(com.infiniteautomation.mango.rest.v2.model.pointValue.query.MultiPointTimeRangeDatabaseStream) Iterator(java.util.Iterator) MediaType(org.springframework.http.MediaType) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) RollupEnum(com.serotonin.m2m2.web.mvc.rest.v1.model.time.RollupEnum) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) MultiPointLatestDatabaseStream(com.infiniteautomation.mango.rest.v2.model.pointValue.query.MultiPointLatestDatabaseStream) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) PointValueTimeWriter(com.infiniteautomation.mango.rest.v2.model.pointValue.PointValueTimeWriter) Map(java.util.Map) PointValueField(com.infiniteautomation.mango.rest.v2.model.pointValue.PointValueField)

Example 2 with PointValueTimeWriter

use of com.infiniteautomation.mango.rest.v2.model.pointValue.PointValueTimeWriter in project ma-modules-public by infiniteautomation.

the class PointValueTimeStreamJsonSerializer method serialize.

/* (non-Javadoc)
     * @see com.fasterxml.jackson.databind.JsonSerializer#serialize(java.lang.Object, com.fasterxml.jackson.core.JsonGenerator, com.fasterxml.jackson.databind.SerializerProvider)
     */
@Override
public void serialize(PointValueTimeStream<T, INFO> value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
    PointValueTimeWriter writer = new PointValueTimeJsonWriter(value.getQueryInfo(), jgen);
    value.setContentType(StreamContentType.JSON);
    value.start(writer);
    value.streamData(writer);
    value.finish(writer);
    jgen.flush();
}
Also used : PointValueTimeWriter(com.infiniteautomation.mango.rest.v2.model.pointValue.PointValueTimeWriter) PointValueTimeJsonWriter(com.infiniteautomation.mango.rest.v2.model.pointValue.PointValueTimeJsonWriter)

Example 3 with PointValueTimeWriter

use of com.infiniteautomation.mango.rest.v2.model.pointValue.PointValueTimeWriter in project ma-modules-public by infiniteautomation.

the class MultiPointSimplifyTimeRangeDatabaseStream method finish.

/* (non-Javadoc)
     * @see com.infiniteautomation.mango.rest.v2.model.pointValue.query.MultiPointLatestDatabaseStream#finish(com.infiniteautomation.mango.rest.v2.model.pointValue.PointValueTimeWriter)
     */
@Override
public void finish(PointValueTimeWriter writer) throws IOException {
    // Write out the values after simplifying
    Iterator<Integer> it = valuesMap.keySet().iterator();
    if (info.isSingleArray() && voMap.size() > 1) {
        List<DataPointVOPointValueTimeBookend> sorted = new ArrayList<>();
        while (it.hasNext()) {
            Integer id = it.next();
            BookendPair pair = bookendMap.get(id);
            if (pair != null && pair.startBookend != null)
                sorted.add(pair.startBookend);
            sorted.addAll(simplify(valuesMap.get(id)));
            if (// Can be null bookend if limit is hit
            pair != null && pair.endBookend != null)
                sorted.add(pair.endBookend);
        }
        // Sort the Sorted List
        Collections.sort(sorted, new Comparator<DataPointVOPointValueTimeBookend>() {

            @Override
            public int compare(DataPointVOPointValueTimeBookend o1, DataPointVOPointValueTimeBookend o2) {
                return o1.getPvt().compareTo(o2.getPvt());
            }
        });
        for (DataPointVOPointValueTimeBookend value : sorted) super.writeValue(value);
    } else {
        while (it.hasNext()) {
            Integer id = it.next();
            List<DataPointVOPointValueTimeBookend> values = simplify(valuesMap.get(id));
            BookendPair pair = bookendMap.get(id);
            if (pair != null && pair.startBookend != null)
                super.writeValue(pair.startBookend);
            for (DataPointVOPointValueTimeBookend value : values) super.writeValue(value);
            if (// Can be null bookend if limit is hit
            pair != null && pair.endBookend != null)
                super.writeValue(pair.endBookend);
        }
    }
    super.finish(writer);
}
Also used : ArrayList(java.util.ArrayList) DataPointVOPointValueTimeBookend(com.infiniteautomation.mango.rest.v2.model.pointValue.DataPointVOPointValueTimeBookend)

Example 4 with PointValueTimeWriter

use of com.infiniteautomation.mango.rest.v2.model.pointValue.PointValueTimeWriter in project ma-modules-public by infiniteautomation.

the class MultiPointSimplifyLatestDatabaseStream method finish.

/* (non-Javadoc)
     * @see com.infiniteautomation.mango.rest.v2.model.pointValue.query.MultiPointLatestDatabaseStream#finish(com.infiniteautomation.mango.rest.v2.model.pointValue.PointValueTimeWriter)
     */
@Override
public void finish(PointValueTimeWriter writer) throws IOException {
    // Write out the values after simplifying
    Iterator<Integer> it = valuesMap.keySet().iterator();
    if (info.isSingleArray() && voMap.size() > 1) {
        List<DataPointVOPointValueTimeBookend> sorted = new ArrayList<>();
        while (it.hasNext()) sorted.addAll(simplify(valuesMap.get(it.next())));
        // Sort the Sorted List
        Collections.sort(sorted, new Comparator<DataPointVOPointValueTimeBookend>() {

            @Override
            public int compare(DataPointVOPointValueTimeBookend o1, DataPointVOPointValueTimeBookend o2) {
                return o1.getPvt().compareTo(o2.getPvt());
            }
        });
        for (DataPointVOPointValueTimeBookend value : sorted) super.writeValue(value);
    } else {
        while (it.hasNext()) {
            List<DataPointVOPointValueTimeBookend> values = simplify(valuesMap.get(it.next()));
            for (DataPointVOPointValueTimeBookend value : values) super.writeValue(value);
        }
    }
    super.finish(writer);
}
Also used : ArrayList(java.util.ArrayList) DataPointVOPointValueTimeBookend(com.infiniteautomation.mango.rest.v2.model.pointValue.DataPointVOPointValueTimeBookend)

Aggregations

DataPointVOPointValueTimeBookend (com.infiniteautomation.mango.rest.v2.model.pointValue.DataPointVOPointValueTimeBookend)2 PointValueTimeWriter (com.infiniteautomation.mango.rest.v2.model.pointValue.PointValueTimeWriter)2 ArrayList (java.util.ArrayList)2 JsonEncoding (com.fasterxml.jackson.core.JsonEncoding)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 CsvSchema (com.fasterxml.jackson.dataformat.csv.CsvSchema)1 PointValueField (com.infiniteautomation.mango.rest.v2.model.pointValue.PointValueField)1 PointValueTimeCsvWriter (com.infiniteautomation.mango.rest.v2.model.pointValue.PointValueTimeCsvWriter)1 PointValueTimeJsonWriter (com.infiniteautomation.mango.rest.v2.model.pointValue.PointValueTimeJsonWriter)1 PointValueTimeStream (com.infiniteautomation.mango.rest.v2.model.pointValue.PointValueTimeStream)1 MultiDataPointStatisticsQuantizerStream (com.infiniteautomation.mango.rest.v2.model.pointValue.quantize.MultiDataPointStatisticsQuantizerStream)1 LatestQueryInfo (com.infiniteautomation.mango.rest.v2.model.pointValue.query.LatestQueryInfo)1 MultiPointLatestDatabaseStream (com.infiniteautomation.mango.rest.v2.model.pointValue.query.MultiPointLatestDatabaseStream)1 MultiPointTimeRangeDatabaseStream (com.infiniteautomation.mango.rest.v2.model.pointValue.query.MultiPointTimeRangeDatabaseStream)1 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)1 RollupEnum (com.serotonin.m2m2.web.mvc.rest.v1.model.time.RollupEnum)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 MediaType (org.springframework.http.MediaType)1