|
| 1 | +/***************************** BEGIN LICENSE BLOCK *************************** |
| 2 | + The contents of this file are subject to the Mozilla Public License, v. 2.0. |
| 3 | + If a copy of the MPL was not distributed with this file, You can obtain one |
| 4 | + at http://mozilla.org/MPL/2.0/. |
| 5 | +
|
| 6 | + Software distributed under the License is distributed on an "AS IS" basis, |
| 7 | + WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 8 | + for the specific language governing rights and limitations under the License. |
| 9 | +
|
| 10 | + Copyright (C) 2020-2025 Botts Innovative Research, Inc. All Rights Reserved. |
| 11 | + ******************************* END LICENSE BLOCK ***************************/ |
| 12 | +package com.sample.impl.sensor.drivername; |
| 13 | + |
| 14 | +import net.opengis.swe.v20.DataBlock; |
| 15 | +import net.opengis.swe.v20.DataComponent; |
| 16 | +import net.opengis.swe.v20.DataEncoding; |
| 17 | +import net.opengis.swe.v20.DataRecord; |
| 18 | +import org.sensorhub.api.data.DataEvent; |
| 19 | +import org.sensorhub.impl.sensor.AbstractSensorOutput; |
| 20 | +import org.vast.swe.helper.GeoPosHelper; |
| 21 | + |
| 22 | +import java.util.ArrayList; |
| 23 | + |
| 24 | +/** |
| 25 | + * Output specification and provider for {@link Sensor}. |
| 26 | + */ |
| 27 | +public class Output extends AbstractSensorOutput<Sensor> { |
| 28 | + static final String SENSOR_OUTPUT_NAME = "SensorOutput"; |
| 29 | + static final String SENSOR_OUTPUT_LABEL = "Sensor Output"; |
| 30 | + static final String SENSOR_OUTPUT_DESCRIPTION = "Sensor output data"; |
| 31 | + |
| 32 | + private static final int MAX_NUM_TIMING_SAMPLES = 10; |
| 33 | + |
| 34 | + private final ArrayList<Double> intervalHistogram = new ArrayList<>(MAX_NUM_TIMING_SAMPLES); |
| 35 | + private final Object histogramLock = new Object(); |
| 36 | + private final Object processingLock = new Object(); |
| 37 | + |
| 38 | + private DataRecord dataRecord; |
| 39 | + private DataEncoding dataEncoding; |
| 40 | + |
| 41 | + /** |
| 42 | + * Creates a new output for the sensor driver. |
| 43 | + * |
| 44 | + * @param parentSensor Sensor driver providing this output. |
| 45 | + */ |
| 46 | + Output(Sensor parentSensor) { |
| 47 | + super(SENSOR_OUTPUT_NAME, parentSensor); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Initializes the data structure for the output, defining the fields, their ordering, and data types. |
| 52 | + */ |
| 53 | + void doInit() { |
| 54 | + // Get an instance of SWE Factory suitable to build components |
| 55 | + GeoPosHelper sweFactory = new GeoPosHelper(); |
| 56 | + |
| 57 | + // Create the data record description |
| 58 | + dataRecord = sweFactory.createRecord() |
| 59 | + .name(SENSOR_OUTPUT_NAME) |
| 60 | + .label(SENSOR_OUTPUT_LABEL) |
| 61 | + .description(SENSOR_OUTPUT_DESCRIPTION) |
| 62 | + .addField("sampleTime", sweFactory.createTime() |
| 63 | + .asSamplingTimeIsoUTC() |
| 64 | + .label("Sample Time") |
| 65 | + .description("Time of data collection")) |
| 66 | + .addField("data", sweFactory.createText() |
| 67 | + .label("Example Data")) |
| 68 | + .build(); |
| 69 | + |
| 70 | + dataEncoding = sweFactory.newTextEncoding(",", "\n"); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public DataComponent getRecordDescription() { |
| 75 | + return dataRecord; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public DataEncoding getRecommendedEncoding() { |
| 80 | + return dataEncoding; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public double getAverageSamplingPeriod() { |
| 85 | + synchronized (histogramLock) { |
| 86 | + double sum = 0; |
| 87 | + for (double sample : intervalHistogram) |
| 88 | + sum += sample; |
| 89 | + |
| 90 | + return sum / intervalHistogram.size(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Sets the data for the output and publishes it. |
| 96 | + */ |
| 97 | + public void setData(long timestamp, String data) { |
| 98 | + synchronized (processingLock) { |
| 99 | + DataBlock dataBlock = latestRecord == null ? dataRecord.createDataBlock() : latestRecord.renew(); |
| 100 | + |
| 101 | + updateIntervalHistogram(); |
| 102 | + |
| 103 | + // Populate the data block |
| 104 | + dataBlock.setDoubleValue(0, timestamp / 1000d); |
| 105 | + dataBlock.setStringValue(1, data); |
| 106 | + |
| 107 | + // Publish the data block |
| 108 | + latestRecord = dataBlock; |
| 109 | + latestRecordTime = timestamp; |
| 110 | + eventHandler.publish(new DataEvent(latestRecordTime, Output.this, dataBlock)); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Updates the interval histogram with the time between the latest record and the current time |
| 116 | + * for calculating the average sampling period. |
| 117 | + */ |
| 118 | + private void updateIntervalHistogram() { |
| 119 | + synchronized (histogramLock) { |
| 120 | + if (latestRecord != null && latestRecordTime != Long.MIN_VALUE) { |
| 121 | + long interval = System.currentTimeMillis() - latestRecordTime; |
| 122 | + intervalHistogram.add(interval / 1000d); |
| 123 | + |
| 124 | + if (intervalHistogram.size() > MAX_NUM_TIMING_SAMPLES) { |
| 125 | + intervalHistogram.remove(0); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments