com.sitewhere.microservice.api.device.ChartBuilder Maven / Gradle / Ivy
/*
* Copyright (c) SiteWhere, LLC. All rights reserved. http://www.sitewhere.com
*
* The software in this package is published under the terms of the CPAL v1.0
* license, a copy of which has been included with this distribution in the
* LICENSE.txt file.
*/
package com.sitewhere.microservice.api.device;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.sitewhere.rest.model.device.charting.ChartEntry;
import com.sitewhere.rest.model.device.charting.ChartSeries;
import com.sitewhere.spi.device.charting.IChartSeries;
import com.sitewhere.spi.device.event.IDeviceMeasurement;
/**
* Builds chart series from measurements.
*/
public class ChartBuilder {
/** Map of measurement names to series */
private Map> seriesByMeasurementName;
/**
* Process measurements into a list of charts series.
*
* @param matches
* @return
*/
public List> process(List matches, String[] measurementIds) {
seriesByMeasurementName = new HashMap>();
List mxids = null;
if ((measurementIds != null) && (measurementIds.length > 0)) {
mxids = Arrays.asList(measurementIds);
}
// Add all measurements.
for (IDeviceMeasurement mx : matches) {
addSeriesEntry(mx.getName(), mx.getValue().doubleValue(), mx.getEventDate());
}
// Sort entries by date.
List> results = new ArrayList>();
for (IChartSeries series : seriesByMeasurementName.values()) {
if ((mxids == null) || (mxids.contains(series.getMeasurementId()))) {
Collections.sort(series.getEntries());
results.add(series);
}
}
return results;
}
/**
* Add a new measurement entry. Create a new series if one does not already
* exist.
*
* @param key
* @param value
* @param date
*/
protected void addSeriesEntry(String key, Double value, Date date) {
ChartSeries series = seriesByMeasurementName.get(key);
if (series == null) {
ChartSeries newSeries = new ChartSeries();
newSeries.setMeasurementId(key);
seriesByMeasurementName.put(key, newSeries);
series = newSeries;
}
ChartEntry seriesEntry = new ChartEntry();
seriesEntry.setValue(value);
seriesEntry.setMeasurementDate(date);
series.getEntries().add(seriesEntry);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy