Search in sources :

Example 96 with ExtensionObject

use of org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject in project milo by eclipse.

the class OpcUaBinaryStreamEncoder method writeValue.

// endregion
private void writeValue(Object value, int typeId, boolean structure, boolean enumeration) {
    if (structure) {
        UaStructure struct = (UaStructure) value;
        ExtensionObject extensionObject = ExtensionObject.encode(context, struct);
        writeBuiltinType(typeId, extensionObject);
    } else if (enumeration) {
        writeBuiltinType(typeId, ((UaEnumeration) value).getValue());
    } else {
        writeBuiltinType(typeId, value);
    }
}
Also used : ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject)

Example 97 with ExtensionObject

use of org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject in project tech-pdai-spring-demos by realpdai.

the class ExampleNamespace method addCustomUnionTypeVariable.

private void addCustomUnionTypeVariable(UaFolderNode rootFolder) throws Exception {
    NodeId dataTypeId = CustomUnionType.TYPE_ID.toNodeIdOrThrow(getServer().getNamespaceTable());
    NodeId binaryEncodingId = CustomUnionType.BINARY_ENCODING_ID.toNodeIdOrThrow(getServer().getNamespaceTable());
    UaVariableNode customUnionTypeVariable = UaVariableNode.builder(getNodeContext()).setNodeId(newNodeId("HelloWorld/CustomUnionTypeVariable")).setAccessLevel(AccessLevel.READ_WRITE).setUserAccessLevel(AccessLevel.READ_WRITE).setBrowseName(newQualifiedName("CustomUnionTypeVariable")).setDisplayName(LocalizedText.english("CustomUnionTypeVariable")).setDataType(dataTypeId).setTypeDefinition(Identifiers.BaseDataVariableType).build();
    CustomUnionType value = CustomUnionType.ofBar("hello");
    ExtensionObject xo = ExtensionObject.encodeDefaultBinary(getServer().getSerializationContext(), value, binaryEncodingId);
    customUnionTypeVariable.setValue(new DataValue(new Variant(xo)));
    getNodeManager().addNode(customUnionTypeVariable);
    customUnionTypeVariable.addReference(new Reference(customUnionTypeVariable.getNodeId(), Identifiers.Organizes, rootFolder.getNodeId().expanded(), false));
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) UaVariableNode(org.eclipse.milo.opcua.sdk.server.nodes.UaVariableNode) CustomUnionType(tech.pdai.opcua.milo.server.types.CustomUnionType) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject) Reference(org.eclipse.milo.opcua.sdk.core.Reference) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId)

Example 98 with ExtensionObject

use of org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject in project tech-pdai-spring-demos by realpdai.

the class ExampleNamespace method addCustomStructTypeVariable.

private void addCustomStructTypeVariable(UaFolderNode rootFolder) throws Exception {
    NodeId dataTypeId = CustomStructType.TYPE_ID.toNodeIdOrThrow(getServer().getNamespaceTable());
    NodeId binaryEncodingId = CustomStructType.BINARY_ENCODING_ID.toNodeIdOrThrow(getServer().getNamespaceTable());
    UaVariableNode customStructTypeVariable = UaVariableNode.builder(getNodeContext()).setNodeId(newNodeId("HelloWorld/CustomStructTypeVariable")).setAccessLevel(AccessLevel.READ_WRITE).setUserAccessLevel(AccessLevel.READ_WRITE).setBrowseName(newQualifiedName("CustomStructTypeVariable")).setDisplayName(LocalizedText.english("CustomStructTypeVariable")).setDataType(dataTypeId).setTypeDefinition(Identifiers.BaseDataVariableType).build();
    CustomStructType value = new CustomStructType("foo", uint(42), true);
    ExtensionObject xo = ExtensionObject.encodeDefaultBinary(getServer().getSerializationContext(), value, binaryEncodingId);
    customStructTypeVariable.setValue(new DataValue(new Variant(xo)));
    getNodeManager().addNode(customStructTypeVariable);
    customStructTypeVariable.addReference(new Reference(customStructTypeVariable.getNodeId(), Identifiers.Organizes, rootFolder.getNodeId().expanded(), false));
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) UaVariableNode(org.eclipse.milo.opcua.sdk.server.nodes.UaVariableNode) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject) Reference(org.eclipse.milo.opcua.sdk.core.Reference) CustomStructType(tech.pdai.opcua.milo.server.types.CustomStructType) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId)

Example 99 with ExtensionObject

use of org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject in project tech-pdai-spring-demos by realpdai.

the class OpcUaClientServiceImpl method retrieveCustomHistoryData.

/**
 * retrieve custom type history data.
 *
 * @param historyReadValueId node id
 * @return data
 * @throws java.security.UnrecoverableKeyException       UnrecoverableKey Exception
 * @throws org.eclipse.milo.opcua.stack.core.UaException Ua Exception
 * @throws java.security.cert.CertificateException       Certificate Exception
 * @throws java.io.IOException                           IO Exception
 * @throws java.security.KeyStoreException               KeyStore Exception
 * @throws java.security.NoSuchAlgorithmException        NoSuchAlgorithm Exception
 * @throws java.util.concurrent.ExecutionException       Execution Exception
 * @throws InterruptedException                          Interrupted Exception
 */
@Override
public List<CustomStructType> retrieveCustomHistoryData(HistoryReadValueId historyReadValueId) throws UnrecoverableKeyException, UaException, CertificateException, IOException, KeyStoreException, NoSuchAlgorithmException, ExecutionException, InterruptedException {
    log.info("trying to get custom data from node {}", historyReadValueId);
    OpcUaClient opcClient = getOpcUaClient();
    try {
        // connect
        log.info("connect to opc server");
        opcClient.addSessionInitializer(new DataTypeDictionarySessionInitializer(new GenericBsdParser()));
        opcClient.connect().get();
        // read history
        HistoryReadResponse historyReadResponse = opcClient.historyRead(new ReadRawModifiedDetails(false, DateTime.MIN_VALUE, DateTime.now(), uint(0), true), TimestampsToReturn.Both, false, Collections.singletonList(historyReadValueId)).get();
        // register custom codec
        log.info("register custom codec");
        registerCustomCodec(opcClient);
        // parse result
        log.info("parse history data result");
        HistoryReadResult[] historyReadResults = historyReadResponse.getResults();
        if (historyReadResults != null) {
            HistoryReadResult historyReadResult = historyReadResults[0];
            StatusCode statusCode = historyReadResult.getStatusCode();
            // check if status code is good
            if (statusCode.isGood()) {
                HistoryData historyData = (HistoryData) historyReadResult.getHistoryData().decode(opcClient.getStaticSerializationContext());
                // parse node value to custom type
                return l(historyData.getDataValues()).stream().map(value -> {
                    log.info("Value={}", value);
                    Variant variant = value.getValue();
                    ExtensionObject xo = (ExtensionObject) variant.getValue();
                    // decode value
                    CustomStructType decoded = (CustomStructType) xo.decode(opcClient.getStaticSerializationContext());
                    log.info("Decoded={}", decoded);
                    return decoded;
                }).collect(Collectors.toList());
            } else {
                log.error("History read failed: {}", statusCode);
            }
        }
        return new ArrayList<>();
    } finally {
        opcClient.disconnect();
    }
}
Also used : X509Certificate(java.security.cert.X509Certificate) KeyPair(java.security.KeyPair) Arrays(java.util.Arrays) KeyStoreException(java.security.KeyStoreException) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject) DateTime(org.eclipse.milo.opcua.stack.core.types.builtin.DateTime) DefaultClientCertificateValidator(org.eclipse.milo.opcua.stack.client.security.DefaultClientCertificateValidator) DataTypeDictionarySessionInitializer(org.eclipse.milo.opcua.sdk.client.dtd.DataTypeDictionarySessionInitializer) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) HistoryReadResult(org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadResult) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) Path(java.nio.file.Path) GenericBsdParser(org.eclipse.milo.opcua.binaryschema.GenericBsdParser) TimestampsToReturn(org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) KeyStore(java.security.KeyStore) Collectors(java.util.stream.Collectors) OpcUaProperties(tech.pdai.opcua.milo.client.properties.OpcUaProperties) Key(java.security.Key) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) OpcUaSSLConfig(tech.pdai.opcua.milo.client.config.OpcUaSSLConfig) Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) PrivateKey(java.security.PrivateKey) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CustomStructType(tech.pdai.opcua.milo.client.entity.CustomStructType) UaVariableNode(org.eclipse.milo.opcua.sdk.client.nodes.UaVariableNode) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) OpcUaClient(org.eclipse.milo.opcua.sdk.client.OpcUaClient) ReadRawModifiedDetails(org.eclipse.milo.opcua.stack.core.types.structured.ReadRawModifiedDetails) ArrayList(java.util.ArrayList) ConversionUtil.l(org.eclipse.milo.opcua.stack.core.util.ConversionUtil.l) Service(org.springframework.stereotype.Service) UnrecoverableKeyException(java.security.UnrecoverableKeyException) SecurityPolicy(org.eclipse.milo.opcua.stack.core.security.SecurityPolicy) HistoryReadResponse(org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadResponse) Files(java.nio.file.Files) LocalizedText(org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText) IOException(java.io.IOException) PublicKey(java.security.PublicKey) CertificateException(java.security.cert.CertificateException) File(java.io.File) HistoryData(org.eclipse.milo.opcua.stack.core.types.structured.HistoryData) ExecutionException(java.util.concurrent.ExecutionException) OpcUaClientConfig(tech.pdai.opcua.milo.client.config.OpcUaClientConfig) Paths(java.nio.file.Paths) DefaultTrustListManager(org.eclipse.milo.opcua.stack.core.security.DefaultTrustListManager) UaException(org.eclipse.milo.opcua.stack.core.UaException) IOpcUaService(tech.pdai.opcua.milo.client.service.impl.IOpcUaService) Collections(java.util.Collections) InputStream(java.io.InputStream) HistoryReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadValueId) HistoryData(org.eclipse.milo.opcua.stack.core.types.structured.HistoryData) GenericBsdParser(org.eclipse.milo.opcua.binaryschema.GenericBsdParser) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject) CustomStructType(tech.pdai.opcua.milo.client.entity.CustomStructType) ArrayList(java.util.ArrayList) ReadRawModifiedDetails(org.eclipse.milo.opcua.stack.core.types.structured.ReadRawModifiedDetails) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) HistoryReadResult(org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadResult) Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) DataTypeDictionarySessionInitializer(org.eclipse.milo.opcua.sdk.client.dtd.DataTypeDictionarySessionInitializer) HistoryReadResponse(org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadResponse) OpcUaClient(org.eclipse.milo.opcua.sdk.client.OpcUaClient)

Example 100 with ExtensionObject

use of org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject in project tech-pdai-spring-demos by realpdai.

the class OpcUaClientServiceImpl method retrieveCustomData.

/**
 * retrieve custom type data.
 *
 * @param nodeId node id
 * @return data
 * @throws java.security.UnrecoverableKeyException UnrecoverableKey Exception
 * @throws UaException                             Ua Exception
 * @throws java.security.cert.CertificateException Certificate Exception
 * @throws java.io.IOException                     IO Exception
 * @throws java.security.KeyStoreException         KeyStore Exception
 * @throws java.security.NoSuchAlgorithmException  NoSuchAlgorithm Exception
 * @throws java.util.concurrent.ExecutionException Execution Exception
 * @throws InterruptedException                    Interrupted Exception
 */
@Override
public CustomStructType retrieveCustomData(NodeId nodeId) throws UnrecoverableKeyException, UaException, CertificateException, IOException, KeyStoreException, NoSuchAlgorithmException, ExecutionException, InterruptedException {
    log.info("trying to get custom data from node {}", nodeId);
    OpcUaClient opcClient = getOpcUaClient();
    try {
        // connect
        log.info("connect to opc server");
        opcClient.addSessionInitializer(new DataTypeDictionarySessionInitializer(new GenericBsdParser()));
        opcClient.connect().get();
        // register custom codec
        log.info("register custom codec");
        registerCustomCodec(opcClient);
        // synchronous read request via VariableNode
        UaVariableNode node = opcClient.getAddressSpace().getVariableNode(nodeId);
        log.info("DataType={}", node.getDataType());
        // Read the current value
        log.info("trying to read value form NodeId");
        DataValue value = node.readValue();
        log.info("Value={}", value);
        Variant variant = value.getValue();
        ExtensionObject xo = (ExtensionObject) variant.getValue();
        // decode value
        CustomStructType decoded = (CustomStructType) xo.decode(opcClient.getStaticSerializationContext());
        log.info("Decoded={}", decoded);
        return decoded;
    } finally {
        opcClient.disconnect();
    }
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) DataTypeDictionarySessionInitializer(org.eclipse.milo.opcua.sdk.client.dtd.DataTypeDictionarySessionInitializer) UaVariableNode(org.eclipse.milo.opcua.sdk.client.nodes.UaVariableNode) GenericBsdParser(org.eclipse.milo.opcua.binaryschema.GenericBsdParser) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject) CustomStructType(tech.pdai.opcua.milo.client.entity.CustomStructType) OpcUaClient(org.eclipse.milo.opcua.sdk.client.OpcUaClient)

Aggregations

ExtensionObject (org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject)191 Variant (org.eclipse.milo.opcua.stack.core.types.builtin.Variant)178 DataValue (org.eclipse.milo.opcua.stack.core.types.builtin.DataValue)98 NodeId (org.eclipse.milo.opcua.stack.core.types.builtin.NodeId)22 PropertyTypeNode (org.eclipse.milo.opcua.sdk.client.model.nodes.variables.PropertyTypeNode)16 UaException (org.eclipse.milo.opcua.stack.core.UaException)12 Identifiers (org.eclipse.milo.opcua.stack.core.Identifiers)11 UInteger (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger)11 AttributeId (org.eclipse.milo.opcua.stack.core.AttributeId)10 Unsigned.uint (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint)10 OpcUaServer (org.eclipse.milo.opcua.sdk.server.OpcUaServer)9 List (java.util.List)8 QualifiedName (org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName)8 Reference (org.eclipse.milo.opcua.sdk.core.Reference)7 LoggerFactory (org.slf4j.LoggerFactory)7 ArrayList (java.util.ArrayList)6 NoSuchElementException (java.util.NoSuchElementException)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 AbstractLifecycle (org.eclipse.milo.opcua.sdk.server.AbstractLifecycle)6 Session (org.eclipse.milo.opcua.sdk.server.Session)6