info.unterrainer.commons.opcuabrowser.parts.ValueReader Maven / Gradle / Ivy
The newest version!
package info.unterrainer.commons.opcuabrowser.parts;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
import org.eclipse.milo.opcua.stack.core.AttributeId;
import org.eclipse.milo.opcua.stack.core.BuiltinDataType;
import org.eclipse.milo.opcua.stack.core.types.builtin.DataValue;
import org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId;
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
import org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn;
import org.eclipse.milo.opcua.stack.core.types.structured.ReadResponse;
import org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId;
import info.unterrainer.commons.opcuabrowser.OpcUaClient;
import info.unterrainer.commons.opcuabrowser.dtos.ReadResult;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class ValueReader {
private final OpcUaClient client;
@Getter
private List results;
public ValueReader(final OpcUaClient client) {
super();
this.client = client;
}
public void read(final List ids) throws InterruptedException, ExecutionException {
read(ids.toArray(new String[0]));
}
public void read(final String... ids) throws InterruptedException, ExecutionException {
results = new ArrayList<>();
final List nodeIds = Arrays.asList(ids)
.stream()
.map(NodeId::parseOrNull)
.filter(Objects::nonNull)
.collect(Collectors.toList());
final List readValueIds = nodeIds.stream()
.map(nodeId -> new ReadValueId(nodeId, AttributeId.Value.uid(), null, null))
.collect(Collectors.toList());
readForReadValueIds(readValueIds);
}
public void readForReadValueIds(final ReadValueId... ids) throws InterruptedException, ExecutionException {
readForReadValueIds(Arrays.asList(ids).stream().filter(Objects::nonNull).collect(Collectors.toList()));
}
public void readForReadValueIds(final List ids) throws InterruptedException, ExecutionException {
results = new ArrayList<>();
List readValueIds = ids.stream().filter(Objects::nonNull).collect(Collectors.toList());
// Bulk read call
final ReadResponse response = client.read(0, TimestampsToReturn.Both, readValueIds);
final DataValue[] rs = response.getResults();
if (null != rs)
for (int i = 0; i < readValueIds.size(); i++) {
ReadResult rr = new ReadResult();
ReadValueId id = readValueIds.get(i);
rr.setId(id.getNodeId().toParseableString());
rr.setReadValueId(id.toString());
BuiltinDataType type = getDataTypeOf(rs[i]);
if (type != null) {
rr.setType(type.getBackingClass());
rr.setValue(rs[i].getValue().getValue());
}
results.add(rr);
}
}
public BuiltinDataType getDataTypeOf(final DataValue dataValue) {
if (dataValue.getValue() == null || dataValue.getValue().getDataType() == null)
return null;
ExpandedNodeId n = dataValue.getValue().getDataType().orElse(null);
if (n == null)
return null;
return BuiltinDataType.fromNodeId(n);
}
public void log() {
for (ReadResult r : results)
log.debug("id: [{}] readValueId: [{}] type:[{}] value: [{}]", r.getId(), r.getReadValueId(), r.getType(),
r.getValue());
}
}