Search in sources :

Example 1 with HistoryReadResponse

use of org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadResponse in project milo by eclipse.

the class HistoryReadExampleProsys method run.

@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
    client.connect().get();
    HistoryReadDetails historyReadDetails = new ReadRawModifiedDetails(false, DateTime.MIN_VALUE, DateTime.now(), uint(0), true);
    HistoryReadValueId historyReadValueId = new HistoryReadValueId(new NodeId(3, "Counter"), null, QualifiedName.NULL_VALUE, ByteString.NULL_VALUE);
    List<HistoryReadValueId> nodesToRead = new ArrayList<>();
    nodesToRead.add(historyReadValueId);
    HistoryReadResponse historyReadResponse = client.historyRead(historyReadDetails, TimestampsToReturn.Both, false, nodesToRead).get();
    HistoryReadResult[] historyReadResults = historyReadResponse.getResults();
    if (historyReadResults != null) {
        HistoryReadResult historyReadResult = historyReadResults[0];
        StatusCode statusCode = historyReadResult.getStatusCode();
        if (statusCode.isGood()) {
            HistoryData historyData = (HistoryData) historyReadResult.getHistoryData().decode(client.getStaticSerializationContext());
            List<DataValue> dataValues = l(historyData.getDataValues());
            dataValues.forEach(v -> System.out.println("value=" + v));
        } else {
            System.out.println("History read failed: " + statusCode);
        }
    }
    future.complete(client);
}
Also used : HistoryData(org.eclipse.milo.opcua.stack.core.types.structured.HistoryData) HistoryReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadValueId) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) HistoryReadDetails(org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadDetails) 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) HistoryReadResponse(org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadResponse) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId)

Example 2 with HistoryReadResponse

use of org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadResponse in project milo by eclipse.

the class DefaultAttributeHistoryServiceSet method onHistoryRead.

@Override
public void onHistoryRead(ServiceRequest service) {
    historyReadMetric.record(service);
    HistoryReadRequest request = (HistoryReadRequest) service.getRequest();
    OpcUaServer server = service.attr(ServiceAttributes.SERVER_KEY).get();
    Session session = service.attr(ServiceAttributes.SESSION_KEY).get();
    List<HistoryReadValueId> nodesToRead = l(request.getNodesToRead());
    if (nodesToRead.isEmpty()) {
        service.setServiceFault(StatusCodes.Bad_NothingToDo);
        return;
    }
    if (nodesToRead.size() > server.getConfig().getLimits().getMaxNodesPerRead().longValue()) {
        service.setServiceFault(StatusCodes.Bad_TooManyOperations);
        return;
    }
    if (request.getTimestampsToReturn() == null) {
        service.setServiceFault(StatusCodes.Bad_TimestampsToReturnInvalid);
        return;
    }
    DiagnosticsContext<HistoryReadValueId> diagnosticsContext = new DiagnosticsContext<>();
    HistoryReadContext context = new HistoryReadContext(server, session, diagnosticsContext);
    HistoryReadDetails details = (HistoryReadDetails) request.getHistoryReadDetails().decode(server.getSerializationContext());
    server.getAddressSpaceManager().historyRead(context, details, request.getTimestampsToReturn(), nodesToRead);
    context.getFuture().thenAccept(values -> {
        ResponseHeader header = service.createResponseHeader();
        DiagnosticInfo[] diagnosticInfos = diagnosticsContext.getDiagnosticInfos(nodesToRead);
        HistoryReadResponse response = new HistoryReadResponse(header, a(values, HistoryReadResult.class), diagnosticInfos);
        service.setResponse(response);
    });
}
Also used : OpcUaServer(org.eclipse.milo.opcua.sdk.server.OpcUaServer) HistoryReadRequest(org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadRequest) ResponseHeader(org.eclipse.milo.opcua.stack.core.types.structured.ResponseHeader) HistoryReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadValueId) DiagnosticInfo(org.eclipse.milo.opcua.stack.core.types.builtin.DiagnosticInfo) HistoryReadDetails(org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadDetails) HistoryReadContext(org.eclipse.milo.opcua.sdk.server.api.services.AttributeHistoryServices.HistoryReadContext) HistoryReadResult(org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadResult) DiagnosticsContext(org.eclipse.milo.opcua.sdk.server.DiagnosticsContext) HistoryReadResponse(org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadResponse) Session(org.eclipse.milo.opcua.sdk.server.Session)

Example 3 with HistoryReadResponse

use of org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadResponse 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 4 with HistoryReadResponse

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

the class OpcUaClientServiceImpl method retrieveGenericHistoryData.

/**
 * retrieve history data.
 *
 * @param historyReadValueId historyReadValueId
 * @return history data value
 * @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 List<DataValue> retrieveGenericHistoryData(HistoryReadValueId historyReadValueId) throws UnrecoverableKeyException, UaException, CertificateException, IOException, KeyStoreException, NoSuchAlgorithmException, ExecutionException, InterruptedException {
    OpcUaClient opcClient = getOpcUaClient();
    try {
        HistoryReadResponse historyReadResponse = opcClient.historyRead(new ReadRawModifiedDetails(false, DateTime.MIN_VALUE, DateTime.now(), uint(0), true), TimestampsToReturn.Both, false, Collections.singletonList(historyReadValueId)).get();
        // parse result
        HistoryReadResult[] historyReadResults = historyReadResponse.getResults();
        if (historyReadResults != null) {
            HistoryReadResult historyReadResult = historyReadResults[0];
            StatusCode statusCode = historyReadResult.getStatusCode();
            if (statusCode.isGood()) {
                HistoryData historyData = (HistoryData) historyReadResult.getHistoryData().decode(opcClient.getStaticSerializationContext());
                return l(historyData.getDataValues());
            } else {
                log.error("History read failed: {}", statusCode);
            }
        }
        return new ArrayList<>();
    } finally {
        opcClient.disconnect();
    }
}
Also used : HistoryData(org.eclipse.milo.opcua.stack.core.types.structured.HistoryData) HistoryReadResponse(org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadResponse) ArrayList(java.util.ArrayList) ReadRawModifiedDetails(org.eclipse.milo.opcua.stack.core.types.structured.ReadRawModifiedDetails) OpcUaClient(org.eclipse.milo.opcua.sdk.client.OpcUaClient) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) HistoryReadResult(org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadResult)

Aggregations

HistoryReadResponse (org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadResponse)4 HistoryReadResult (org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadResult)4 ArrayList (java.util.ArrayList)3 StatusCode (org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)3 HistoryData (org.eclipse.milo.opcua.stack.core.types.structured.HistoryData)3 HistoryReadValueId (org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadValueId)3 ReadRawModifiedDetails (org.eclipse.milo.opcua.stack.core.types.structured.ReadRawModifiedDetails)3 OpcUaClient (org.eclipse.milo.opcua.sdk.client.OpcUaClient)2 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 Key (java.security.Key)1 KeyPair (java.security.KeyPair)1 KeyStore (java.security.KeyStore)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 PrivateKey (java.security.PrivateKey)1