Search in sources :

Example 1 with WriteValue

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

the class DefaultAttributeServiceSet method onWrite.

@Override
public void onWrite(ServiceRequest service) {
    WriteRequest request = (WriteRequest) service.getRequest();
    OpcUaServer server = service.attr(ServiceAttributes.SERVER_KEY).get();
    Session session = service.attr(ServiceAttributes.SESSION_KEY).get();
    List<WriteValue> nodesToWrite = l(request.getNodesToWrite());
    if (nodesToWrite.isEmpty()) {
        service.setServiceFault(StatusCodes.Bad_NothingToDo);
        return;
    }
    if (nodesToWrite.size() > server.getConfig().getLimits().getMaxNodesPerWrite().intValue()) {
        service.setServiceFault(StatusCodes.Bad_TooManyOperations);
        return;
    }
    DiagnosticsContext<WriteValue> diagnosticsContext = new DiagnosticsContext<>();
    WriteContext context = new WriteContext(server, session, new DiagnosticsContext<>());
    server.getAddressSpaceManager().write(context, nodesToWrite);
    context.getFuture().thenAccept(values -> {
        ResponseHeader header = service.createResponseHeader();
        DiagnosticInfo[] diagnosticInfos = diagnosticsContext.getDiagnosticInfos(nodesToWrite);
        WriteResponse response = new WriteResponse(header, values.toArray(new StatusCode[0]), diagnosticInfos);
        service.setResponse(response);
    });
}
Also used : OpcUaServer(org.eclipse.milo.opcua.sdk.server.OpcUaServer) ResponseHeader(org.eclipse.milo.opcua.stack.core.types.structured.ResponseHeader) WriteRequest(org.eclipse.milo.opcua.stack.core.types.structured.WriteRequest) DiagnosticInfo(org.eclipse.milo.opcua.stack.core.types.builtin.DiagnosticInfo) WriteResponse(org.eclipse.milo.opcua.stack.core.types.structured.WriteResponse) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) WriteContext(org.eclipse.milo.opcua.sdk.server.api.services.AttributeServices.WriteContext) WriteValue(org.eclipse.milo.opcua.stack.core.types.structured.WriteValue) DiagnosticsContext(org.eclipse.milo.opcua.sdk.server.DiagnosticsContext) Session(org.eclipse.milo.opcua.sdk.server.Session)

Example 2 with WriteValue

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

the class ManagedAddressSpace method write.

@Override
public void write(WriteContext context, List<WriteValue> writeValues) {
    List<StatusCode> results = Lists.newArrayListWithCapacity(writeValues.size());
    for (WriteValue writeValue : writeValues) {
        UaServerNode node = nodeManager.get(writeValue.getNodeId());
        if (node != null) {
            try {
                node.writeAttribute(new AttributeContext(context), writeValue.getAttributeId(), writeValue.getValue(), writeValue.getIndexRange());
                results.add(StatusCode.GOOD);
                logger.debug("Wrote value {} to {} attribute of {}", writeValue.getValue().getValue(), AttributeId.from(writeValue.getAttributeId()).map(Object::toString).orElse("unknown"), node.getNodeId());
            } catch (UaException e) {
                logger.error("Unable to write value={}", writeValue.getValue(), e);
                results.add(e.getStatusCode());
            }
        } else {
            results.add(new StatusCode(StatusCodes.Bad_NodeIdUnknown));
        }
    }
    context.success(results);
}
Also used : WriteValue(org.eclipse.milo.opcua.stack.core.types.structured.WriteValue) AttributeContext(org.eclipse.milo.opcua.sdk.server.nodes.AttributeContext) UaException(org.eclipse.milo.opcua.stack.core.UaException) UaServerNode(org.eclipse.milo.opcua.sdk.server.nodes.UaServerNode) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)

Example 3 with WriteValue

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

the class AttributeServices method writeValues.

/**
 * This service is used to write to the value attribute of one or more nodes.
 *
 * @param nodeIds the {@link NodeId}s identifying the nodes to write to.
 * @param values  the {@link DataValue}s to write.
 * @return a {@link CompletableFuture} containing a list of results for the writes.
 */
default CompletableFuture<List<StatusCode>> writeValues(List<NodeId> nodeIds, List<DataValue> values) {
    if (nodeIds.size() != values.size()) {
        CompletableFuture<List<StatusCode>> failed = new CompletableFuture<>();
        failed.completeExceptionally(new IllegalArgumentException("nodeIds.size() != values.size()"));
        return failed;
    } else {
        Stream<WriteValue> stream = Streams.zip(nodeIds.stream(), values.stream(), (nodeId, value) -> new WriteValue(nodeId, uint(13), null, value));
        return write(stream.collect(Collectors.toList())).thenApply(response -> l(response.getResults()));
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) WriteValue(org.eclipse.milo.opcua.stack.core.types.structured.WriteValue) List(java.util.List)

Example 4 with WriteValue

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

the class UaNode method writeAttributeAsync.

/**
 * An asynchronous implementation of {@link #writeAttribute(AttributeId, DataValue)}.
 *
 * @return a CompletableFuture that completes successfully with the operation result or
 * completes exceptionally if a service-level error occurs.
 */
public CompletableFuture<StatusCode> writeAttributeAsync(AttributeId attributeId, DataValue value) {
    WriteValue writeValue = new WriteValue(getNodeId(), attributeId.uid(), null, value);
    CompletableFuture<WriteResponse> future = client.write(newArrayList(writeValue));
    return future.thenApply(response -> response.getResults()[0]);
}
Also used : WriteValue(org.eclipse.milo.opcua.stack.core.types.structured.WriteValue) WriteResponse(org.eclipse.milo.opcua.stack.core.types.structured.WriteResponse)

Aggregations

WriteValue (org.eclipse.milo.opcua.stack.core.types.structured.WriteValue)4 StatusCode (org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)2 WriteResponse (org.eclipse.milo.opcua.stack.core.types.structured.WriteResponse)2 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 DiagnosticsContext (org.eclipse.milo.opcua.sdk.server.DiagnosticsContext)1 OpcUaServer (org.eclipse.milo.opcua.sdk.server.OpcUaServer)1 Session (org.eclipse.milo.opcua.sdk.server.Session)1 WriteContext (org.eclipse.milo.opcua.sdk.server.api.services.AttributeServices.WriteContext)1 AttributeContext (org.eclipse.milo.opcua.sdk.server.nodes.AttributeContext)1 UaServerNode (org.eclipse.milo.opcua.sdk.server.nodes.UaServerNode)1 UaException (org.eclipse.milo.opcua.stack.core.UaException)1 DiagnosticInfo (org.eclipse.milo.opcua.stack.core.types.builtin.DiagnosticInfo)1 ResponseHeader (org.eclipse.milo.opcua.stack.core.types.structured.ResponseHeader)1 WriteRequest (org.eclipse.milo.opcua.stack.core.types.structured.WriteRequest)1