Skip to content

Commit

Permalink
Merge branch 'feature/27616-mappings-extraction' of github.com:hivemq…
Browse files Browse the repository at this point in the history
…/hivemq-edge into feature/27616-mappings-extraction
  • Loading branch information
codepitbull committed Nov 26, 2024
2 parents 8c8092a + eb80bfc commit d95efb7
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2019-present HiveMQ GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hivemq.api.resources.examples;

public interface FieldMappingsExamples {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public FromEdgeMappingEntity() {
messageHandlingOptions = MessageHandlingOptions.MQTTMessagePerTag;
includeTagNames = false;
includeTimestamp = true;
maxQoS = 2;
maxQoS = 1;
userProperties = new ArrayList<>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package com.hivemq.edge.adapters.file.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hivemq.adapter.sdk.api.config.MqttUserProperty;
import com.hivemq.adapter.sdk.api.factories.ProtocolAdapterFactoryInput;
import com.hivemq.configuration.entity.HiveMQConfigEntity;
import com.hivemq.configuration.entity.adapter.ProtocolAdapterEntity;
Expand Down Expand Up @@ -70,7 +69,7 @@ public void convertConfigObject_fullConfig_valid() throws Exception {

assertThat(protocolAdapterConfig.getFromEdgeMappings()).satisfiesExactly(mapping -> {
assertThat(mapping.getMqttTopic()).isEqualTo("my/topic");
assertThat(mapping.getMqttQos()).isEqualTo(0);
assertThat(mapping.getMqttQos()).isEqualTo(1);
assertThat(mapping.getMessageHandlingOptions()).isEqualTo(MQTTMessagePerSubscription);
assertThat(mapping.getIncludeTimestamp()).isFalse();
assertThat(mapping.getIncludeTagNames()).isTrue();
Expand All @@ -85,7 +84,7 @@ public void convertConfigObject_fullConfig_valid() throws Exception {
});
}, mapping -> {
assertThat(mapping.getMqttTopic()).isEqualTo("my/topic/2");
assertThat(mapping.getMqttQos()).isEqualTo(0);
assertThat(mapping.getMqttQos()).isEqualTo(1);
assertThat(mapping.getMessageHandlingOptions()).isEqualTo(MQTTMessagePerSubscription);
assertThat(mapping.getIncludeTimestamp()).isFalse();
assertThat(mapping.getIncludeTagNames()).isTrue();
Expand Down Expand Up @@ -119,7 +118,7 @@ public void convertConfigObject_defaults_valid() throws Exception {

assertThat(protocolAdapterConfig.getFromEdgeMappings()).satisfiesExactly(subscription -> {
assertThat(subscription.getMqttTopic()).isEqualTo("my/topic");
assertThat(subscription.getMqttQos()).isEqualTo(0);
assertThat(subscription.getMqttQos()).isEqualTo(1);
assertThat(subscription.getMessageHandlingOptions()).isEqualTo(MQTTMessagePerTag);
assertThat(subscription.getIncludeTimestamp()).isTrue();
assertThat(subscription.getIncludeTagNames()).isFalse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

import static com.hivemq.adapter.sdk.api.state.ProtocolAdapterState.ConnectionStatus.CONNECTED;

Expand Down Expand Up @@ -101,13 +100,14 @@ public void poll(final @NotNull PollingInput pollingInput, @NotNull final Pollin
final String tagName = plc4xToMqttMapping.getTagName();

findTag(tagName).ifPresentOrElse(def -> tempConnection.read(plc4xToMqttMapping)
.thenApply(response -> processReadResponse((Plc4xToMqttMapping) pollingInput.getPollingContext(),
response))
.thenApply(response -> processReadResponse(pollingInput.getPollingContext(), response))
.thenApply(data -> captureDataSample(data, (Plc4xTag) def))
.whenComplete((sample, t) -> handleDataAndExceptions(sample, t, pollingOutput)),
() -> pollingOutput.fail("Polling for protocol adapter failed because the used tag '" +
tagName +
"' was not found. For the polling to work the tag must be created via REST API or the UI."));
} else {
pollingOutput.fail("Polling failed for adapter '" + adapterId + "' because the connection was null.");
}
}

Expand Down Expand Up @@ -139,6 +139,7 @@ public void start(
@NotNull final ProtocolAdapterStartInput input, @NotNull final ProtocolAdapterStartOutput output) {
try {
// we do not subscribe anymore as no current adapter type supports it anyway
initConnection();
output.startedSuccessfully();
} catch (final Exception e) {
output.failStart(e, null);
Expand Down Expand Up @@ -249,8 +250,7 @@ private Plc4xConnection<T> initConnection() {
* Default: tagAddress:expectedDataType eg. "0%20:BOOL"
*/
protected @NotNull String createTagAddressForSubscription(
final @NotNull PollingContext subscription,
final @NotNull Plc4xTag tag) {
final @NotNull PollingContext subscription, final @NotNull Plc4xTag tag) {
final String tagAddress = tag.getDefinition().getTagAddress();
return String.format("%s%s%s", tagAddress, TAG_ADDRESS_TYPE_SEP, tag.getDefinition().getDataType());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,30 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.hivemq.adapter.sdk.api.annotations.ModuleConfigField;
import com.hivemq.adapter.sdk.api.config.AdapterConfigWithPollingContexts;
import com.hivemq.adapter.sdk.api.config.PollingContext;
import com.hivemq.edge.adapters.plc4x.config.Plc4XSpecificAdapterConfig;
import com.hivemq.edge.adapters.plc4x.config.Plc4xToMqttConfig;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Objects;

public class S7SpecificAdapterConfig extends Plc4XSpecificAdapterConfig<Plc4xToMqttConfig> {
public class S7SpecificAdapterConfig extends Plc4XSpecificAdapterConfig<Plc4xToMqttConfig>
implements AdapterConfigWithPollingContexts {

private static final int PORT_MIN = 1;
private static final int PORT_MAX = 65535;

@Override
public @NotNull List<? extends PollingContext> getPollingContexts() {
if (s7ToMqttConfig == null) {
return List.of();
}
return s7ToMqttConfig.getMappings();
}

public enum ControllerType {
S7_300,
S7_400,
Expand Down

0 comments on commit d95efb7

Please sign in to comment.