Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: OI4 adapter can only read one sensor measurement per event #2756

Merged
merged 4 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
import org.apache.streampipes.messaging.InternalEventProcessor;
import org.apache.streampipes.model.AdapterType;
import org.apache.streampipes.model.connect.guess.GuessSchema;
import org.apache.streampipes.model.extensions.ExtensionAssetType;
import org.apache.streampipes.model.schema.EventSchema;
import org.apache.streampipes.sdk.StaticProperties;
import org.apache.streampipes.sdk.builder.adapter.AdapterConfigurationBuilder;
import org.apache.streampipes.sdk.helpers.Alternatives;
import org.apache.streampipes.sdk.helpers.Labels;
import org.apache.streampipes.sdk.helpers.Locales;
import org.apache.streampipes.sdk.utils.Assets;
import org.apache.streampipes.sdk.utils.Datatypes;

import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -60,7 +60,6 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
Expand All @@ -87,7 +86,6 @@ public class Oi4Adapter implements StreamPipesAdapter {

private List<String> selectedSensors;
private String givenSensorType;
private String appId;
protected final ObjectMapper mapper;


Expand All @@ -101,7 +99,7 @@ public IAdapterConfiguration declareConfig() {
return AdapterConfigurationBuilder
.create(ID, 0, Oi4Adapter::new)
.withLocales(Locales.EN)
.withAssets(Assets.DOCUMENTATION, Assets.ICON)
.withAssets(ExtensionAssetType.DOCUMENTATION, ExtensionAssetType.ICON)
.withCategory(AdapterType.Generic, AdapterType.Manufacturing)
.requiredTextParameter(MqttConnectUtils.getBrokerUrlLabel())
.requiredAlternatives(MqttConnectUtils.getAccessModeLabel(), MqttConnectUtils.getAlternativesOne(),
Expand Down Expand Up @@ -174,7 +172,7 @@ private void applyConfiguration(IStaticPropertyExtractor extractor) throws Adapt
OI4AdapterLabels.LABEL_KEY_SENSORS_ALTERNATIVES
);

appId = extractor.textParameter(OI4AdapterLabels.LABEL_KEY_APP_ID);
String appId = extractor.textParameter(OI4AdapterLabels.LABEL_KEY_APP_ID);

if (selectedAlternativeSelectedSensors.equals(OI4AdapterLabels.LABEL_KEY_SENSORS_ALL_ALTERNATIVE)) {
// An empty list is used to indicated that messages from all sensors should be collected
Expand All @@ -197,7 +195,7 @@ private void applyConfiguration(IStaticPropertyExtractor extractor) throws Adapt
public void onAdapterStopped(
IAdapterParameterExtractor extractor,
IAdapterRuntimeContext adapterRuntimeContext
) throws AdapterException {
) {
mqttConsumer.close();
}

Expand Down Expand Up @@ -311,40 +309,42 @@ private MqttConsumer getGuessMqttConsumer(List<byte[]> sampleMessages) {

private Map<String, Object> extractPayload(NetworkMessage message) throws ParseException {

var dataMessageOpt = findProcessDataInputMessage(message);
var dataMessages = findProcessDataInputMessage(message);

if (dataMessageOpt.isPresent()) {
if (!dataMessages.isEmpty()) {

var dataMessage = dataMessageOpt.get();
for (var dataMessage: dataMessages) {

var sensorId = getSensorIdFromSource(dataMessage.source());
var sensorId = getSensorIdFromSource(dataMessage.source());

// Verify that the message corresponds to the designated sensor type.
// This validation relies on the assumption that the source information includes the sensor type.
if (dataMessage.source()
.contains(givenSensorType)) {
// Verify that the message corresponds to the designated sensor type.
// This validation relies on the assumption that the source information includes the sensor type.
if (dataMessage.source()
.contains(givenSensorType)) {

// an empty list of selected sensors means that we want to collect data from all sensors available
if (selectedSensors.isEmpty() || selectedSensors.contains(sensorId)) {
// an empty list of selected sensors means that we want to collect data from all sensors available
if (selectedSensors.isEmpty() || selectedSensors.contains(sensorId)) {

return extractAndEnrichMessagePayload(dataMessage, sensorId);
return extractAndEnrichMessagePayload(dataMessage, sensorId);
}
}
}
}
throw new ParseException("No process data message");
}

private Optional<DataSetMessage> findProcessDataInputMessage(NetworkMessage message) {
private List<DataSetMessage> findProcessDataInputMessage(NetworkMessage message) {
return message.messages()
.stream()
.filter(msg -> msg.filter()
.equals(OI4AdapterLabels.MESSAGE_VALUE_FILTER))
.findFirst();
.stream()
.filter(msg -> msg.filter()
.equals(OI4AdapterLabels.MESSAGE_VALUE_FILTER))
.toList();
}

private Map<String, Object> extractAndEnrichMessagePayload(DataSetMessage dataSetMessage, String sensorId) {
var payload = dataSetMessage
.payload();
.payload()
.get(0);
try {
payload.put(
OI4AdapterLabels.EVENT_KEY_TIMESTAMP,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;
import java.util.Map;

/**
Expand All @@ -36,7 +37,7 @@ public record DataSetMessage(
@JsonProperty("Timestamp") String timestamp,
@JsonProperty("Filter") String filter,
@JsonProperty("Source") String source,
@JsonProperty("Payload") Map<String, Object> payload
@JsonProperty("Payload") List<Map<String, Object>> payload
) {

}
Loading