Skip to content

Commit

Permalink
feat(plc4j/api): Added a getArrayInfo() method to the PlcBrowseItem t…
Browse files Browse the repository at this point in the history
…o provide information over array dimensions.
  • Loading branch information
chrisdutz committed Sep 11, 2022
1 parent 4663ff9 commit cf2f2f5
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ public interface PlcBrowseItem {
*/
PlcValueType getPlcValueType();

/**
* @return returns the array info for this element
* (this is usually null, but for lists, it contains the array sizes)
*/
default List<PlcBrowseItemArrayInfo> getArrayInfo() {
return null;
}

/**
* @return returns 'true' if we can read this variable.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* https://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 org.apache.plc4x.java.api.messages;

public interface PlcBrowseItemArrayInfo {

/**
* @return returns the size of the array
*/
default long getSize() {
return getUpperBound() - getLowerBound();
}

/**
* @return returns the lower bound of the array
*/
long getLowerBound();

/**
* @return returns the upper bound of the array
*/
long getUpperBound();

}
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,20 @@ public CompletableFuture<PlcBrowseResponse> browse(PlcBrowseRequest browseReques
options.put("offset", new PlcUDINT(symbol.getOffset()));
options.put("size-in-bytes", new PlcUDINT(symbol.getSize()));

// Add the type itself.
values.add(new DefaultPlcBrowseItem(symbol.getName(), itemName, plc4xPlcValueType, true,
!symbol.getFlagReadOnly(), true, children, options));
if(plc4xPlcValueType == org.apache.plc4x.java.api.types.PlcValueType.List) {
List<PlcBrowseItemArrayInfo> arrayInfo = new ArrayList<>();
for (AdsDataTypeArrayInfo adsDataTypeArrayInfo : dataType.getArrayInfo()) {
arrayInfo.add(new DefaultBrowseItemArrayInfo(
adsDataTypeArrayInfo.getLowerBound(), adsDataTypeArrayInfo.getUpperBound()));
}
// Add the type itself.
values.add(new DefaultListPlcBrowseItem(symbol.getName(), itemName, plc4xPlcValueType, arrayInfo,
true, !symbol.getFlagReadOnly(), true, children, options));
} else {
// Add the type itself.
values.add(new DefaultPlcBrowseItem(symbol.getName(), itemName, plc4xPlcValueType, true,
!symbol.getFlagReadOnly(), true, children, options));
}
}
DefaultPlcBrowseResponse response = new DefaultPlcBrowseResponse(browseRequest, PlcResponseCode.OK, values);
future.complete(response);
Expand Down Expand Up @@ -372,9 +383,20 @@ protected List<PlcBrowseItem> getBrowseItems(String basePath, long baseGroupId,
options.put("offset", new PlcUDINT(baseOffset + child.getOffset()));
options.put("size-in-bytes", new PlcUDINT(childDataType.getSize()));

// Add the type itself.
values.add(new DefaultPlcBrowseItem(basePath + "." + child.getPropertyName(), itemName, plc4xPlcValueType,
true, parentWritable, true, children, options));
if(plc4xPlcValueType == org.apache.plc4x.java.api.types.PlcValueType.List) {
List<PlcBrowseItemArrayInfo> arrayInfo = new ArrayList<>();
for (AdsDataTypeArrayInfo adsDataTypeArrayInfo : childDataType.getArrayInfo()) {
arrayInfo.add(new DefaultBrowseItemArrayInfo(
adsDataTypeArrayInfo.getLowerBound(), adsDataTypeArrayInfo.getUpperBound()));
}
// Add the type itself.
values.add(new DefaultListPlcBrowseItem(basePath + "." + child.getPropertyName(), itemName,
plc4xPlcValueType, arrayInfo,true, parentWritable, true, children, options));
} else {
// Add the type itself.
values.add(new DefaultPlcBrowseItem(basePath + "." + child.getPropertyName(), itemName,
plc4xPlcValueType,true, parentWritable, true, children, options));
}
}
return values;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* https://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 org.apache.plc4x.java.spi.messages;

import org.apache.plc4x.java.api.messages.PlcBrowseItemArrayInfo;

public class DefaultBrowseItemArrayInfo implements PlcBrowseItemArrayInfo {

private final long lowerBound;
private final long upperBound;

public DefaultBrowseItemArrayInfo(long lowerBound, long upperBound) {
this.lowerBound = lowerBound;
this.upperBound = upperBound;
}

@Override
public long getLowerBound() {
return lowerBound;
}

@Override
public long getUpperBound() {
return upperBound;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* https://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 org.apache.plc4x.java.spi.messages;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.apache.plc4x.java.api.messages.PlcBrowseItem;
import org.apache.plc4x.java.api.messages.PlcBrowseItemArrayInfo;
import org.apache.plc4x.java.api.types.PlcValueType;
import org.apache.plc4x.java.api.value.PlcValue;
import org.apache.plc4x.java.spi.generation.SerializationException;
import org.apache.plc4x.java.spi.generation.WriteBuffer;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "className")
public class DefaultListPlcBrowseItem extends DefaultPlcBrowseItem {

private final List<PlcBrowseItemArrayInfo> arrayInfo;

@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public DefaultListPlcBrowseItem(@JsonProperty("address") String address,
@JsonProperty("name") String name,
@JsonProperty("dataType") PlcValueType dataType,
@JsonProperty("arrayInfo") List<PlcBrowseItemArrayInfo> arrayInfo,
@JsonProperty("readable") boolean readable,
@JsonProperty("writable") boolean writable,
@JsonProperty("subscribable") boolean subscribable,
@JsonProperty("children") List<PlcBrowseItem> children,
@JsonProperty("options") Map<String, PlcValue> options) {
super(address, name, dataType, readable, writable, subscribable, children, options);
this.arrayInfo = arrayInfo;
}

@Override
public List<PlcBrowseItemArrayInfo> getArrayInfo() {
return arrayInfo;
}

@Override
public void serialize(WriteBuffer writeBuffer) throws SerializationException {
writeBuffer.pushContext(getClass().getSimpleName());
writeBuffer.writeString("address", getAddress().getBytes(StandardCharsets.UTF_8).length * 8, StandardCharsets.UTF_8.name(), getAddress());
writeBuffer.writeString("name", getName().getBytes(StandardCharsets.UTF_8).length * 8, StandardCharsets.UTF_8.name(), getName());
// TODO: Find out how to serialize an enum.
//writeBuffer.writeString("dataType", dataType.getBytes(StandardCharsets.UTF_8).length * 8, StandardCharsets.UTF_8.name(), dataType);
if(getChildren() != null && !getChildren().isEmpty()) {
writeBuffer.pushContext("children");
for (PlcBrowseItem child : getChildren()) {
writeBuffer.pushContext("child");
((DefaultListPlcBrowseItem) child).serialize(writeBuffer);
writeBuffer.popContext("child");
}
writeBuffer.popContext("children");
}
if(getOptions() != null && !getOptions().isEmpty()) {
writeBuffer.pushContext("options");
for (Map.Entry<String, PlcValue> optionEntry : getOptions().entrySet()) {
writeBuffer.pushContext("option");
writeBuffer.writeString("name", optionEntry.getKey().getBytes(StandardCharsets.UTF_8).length * 8, StandardCharsets.UTF_8.name(), optionEntry.getKey());
// TODO: Find out how to serialize a PlcValue
//writeBuffer.writeString("value", optionEntry.getValue().getBytes(StandardCharsets.UTF_8).length * 8, StandardCharsets.UTF_8.name(), optionEntry.getValue());
((DefaultListPlcBrowseItem) optionEntry).serialize(writeBuffer);
writeBuffer.popContext("option");
}
writeBuffer.popContext("options");
}
writeBuffer.popContext(getClass().getSimpleName());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@
import javafx.scene.input.MouseEvent;
import org.apache.plc4x.java.api.PlcConnection;
import org.apache.plc4x.java.api.messages.PlcBrowseItem;
import org.apache.plc4x.java.api.messages.PlcBrowseItemArrayInfo;
import org.apache.plc4x.java.api.messages.PlcBrowseResponse;
import org.apache.plc4x.java.api.types.PlcValueType;
import org.kordamp.ikonli.javafx.FontIcon;
import org.kordamp.ikonli.materialdesign.MaterialDesign;

import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ExecutionException;

public class ConnectionTabController {
Expand Down Expand Up @@ -83,7 +86,11 @@ void setConnection(String connectionName, PlcConnection connection) {
rootItem.setGraphic(new FontIcon(MaterialDesign.MDI_FOLDER));
rootItem.setExpanded(true);

for (PlcBrowseItem value : browseResponse.getValues()) {
// Sort the entries first.
List<PlcBrowseItem> values = browseResponse.getValues();
values.sort(new PlcBrowseItemComparator());
// Then add the elements to the tree.
for (PlcBrowseItem value : values) {
rootItem.getChildren().add(getTreeItemForBrowseItem(value));
}

Expand All @@ -95,11 +102,22 @@ void setConnection(String connectionName, PlcConnection connection) {
}

private TreeItem<ConnectionTabController.TreeEntry> getTreeItemForBrowseItem(PlcBrowseItem browseItem) {
StringBuilder addressSuffix = new StringBuilder();
if ((browseItem.getPlcValueType() == PlcValueType.List) && (browseItem.getArrayInfo() != null)){
addressSuffix.append(" ");
for (PlcBrowseItemArrayInfo arrayInfo : browseItem.getArrayInfo()) {
addressSuffix.append("[").append(arrayInfo.getLowerBound()).append(" .. ").append(arrayInfo.getUpperBound()).append("]");
}
}
TreeItem<ConnectionTabController.TreeEntry> treeItem = new TreeItem<>(new ConnectionTabController.TreeEntry(
browseItem.getAddress(), browseItem.getName(), browseItem.getPlcValueType(),
browseItem.getAddress() + addressSuffix, browseItem.getName(), browseItem.getPlcValueType(),
browseItem.isReadable(), browseItem.isWritable(), browseItem.isSubscribable()));
if(!browseItem.getChildren().isEmpty()) {
for (PlcBrowseItem child : browseItem.getChildren()) {
// Sort the entries first.
List<PlcBrowseItem> values = browseItem.getChildren();
// Then add the elements to the tree.
values.sort(new PlcBrowseItemComparator());
for (PlcBrowseItem child : values) {
treeItem.getChildren().add(getTreeItemForBrowseItem(child));
}
}
Expand Down Expand Up @@ -159,4 +177,11 @@ public boolean isSubscribable() {
}
}

class PlcBrowseItemComparator implements Comparator<PlcBrowseItem> {
@Override
public int compare(PlcBrowseItem o1, PlcBrowseItem o2) {
return o1.getAddress().compareTo(o2.getAddress());
}
}

}

0 comments on commit cf2f2f5

Please sign in to comment.