Search in sources :

Example 1 with PlcRequest

use of org.apache.plc4x.java.api.messages.PlcRequest in project plc4x by apache.

the class Plc4x2AdsProtocol method decode.

@Override
protected void decode(ChannelHandlerContext channelHandlerContext, AmsPacket amsPacket, List<Object> out) throws Exception {
    LOGGER.trace("(-->IN): {}, {}, {}", channelHandlerContext, amsPacket, out);
    if (amsPacket instanceof AdsDeviceNotificationRequest) {
        LOGGER.debug("Received notification {}", amsPacket);
        handleAdsDeviceNotificationRequest((AdsDeviceNotificationRequest) amsPacket);
        return;
    }
    PlcRequestContainer<InternalPlcRequest, InternalPlcResponse> plcRequestContainer = requests.remove(amsPacket.getAmsHeader().getInvokeId().getAsLong());
    if (plcRequestContainer == null) {
        LOGGER.info("Unmapped packet received {}", amsPacket);
        return;
    }
    PlcRequest request = plcRequestContainer.getRequest();
    final InternalPlcResponse response;
    // Handle the response to a read request.
    if (request instanceof PlcReadRequest) {
        if (amsPacket instanceof AdsReadResponse) {
            response = decodeReadResponse((AdsReadResponse) amsPacket, plcRequestContainer);
        } else {
            throw new PlcProtocolException("Wrong type correlated " + amsPacket);
        }
    } else if (request instanceof PlcWriteRequest) {
        if (amsPacket instanceof AdsWriteResponse) {
            response = decodeWriteResponse((AdsWriteResponse) amsPacket, plcRequestContainer);
        } else {
            throw new PlcProtocolException("Wrong type correlated " + amsPacket);
        }
    } else if (request instanceof PlcProprietaryRequest) {
        response = decodeProprietaryResponse(amsPacket, plcRequestContainer);
    } else {
        response = null;
    }
    LOGGER.debug("Plc4x response {}", response);
    // Confirm the response being handled.
    if (response != null) {
        plcRequestContainer.getResponseFuture().complete(response);
    }
}
Also used : PlcWriteRequest(org.apache.plc4x.java.api.messages.PlcWriteRequest) PlcProtocolException(org.apache.plc4x.java.api.exceptions.PlcProtocolException) PlcRequest(org.apache.plc4x.java.api.messages.PlcRequest) PlcReadRequest(org.apache.plc4x.java.api.messages.PlcReadRequest)

Example 2 with PlcRequest

use of org.apache.plc4x.java.api.messages.PlcRequest in project plc4x by apache.

the class OpcuaOptimizer method processReadRequest.

@Override
protected List<PlcRequest> processReadRequest(PlcReadRequest readRequest, DriverContext driverContext) {
    List<PlcRequest> processedRequests = new LinkedList<>();
    // List of all items in the current request.
    LinkedHashMap<String, PlcField> curFields = new LinkedHashMap<>();
    for (String fieldName : readRequest.getFieldNames()) {
        OpcuaField field = (OpcuaField) readRequest.getField(fieldName);
        curFields.put(fieldName, field);
    }
    // Create a new PlcReadRequest from the remaining field items.
    if (!curFields.isEmpty()) {
        processedRequests.add(new DefaultPlcReadRequest(((DefaultPlcReadRequest) readRequest).getReader(), curFields));
    }
    return processedRequests;
}
Also used : OpcuaField(org.apache.plc4x.java.opcua.field.OpcuaField) PlcRequest(org.apache.plc4x.java.api.messages.PlcRequest) DefaultPlcReadRequest(org.apache.plc4x.java.spi.messages.DefaultPlcReadRequest) PlcField(org.apache.plc4x.java.api.model.PlcField) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with PlcRequest

use of org.apache.plc4x.java.api.messages.PlcRequest in project plc4x by apache.

the class Plc4XDf1Protocol method decode.

@Override
protected void decode(ChannelHandlerContext ctx, DF1Symbol msg, List<Object> out) throws Exception {
    logger.debug("Received DF1 Command incoming {}", msg);
    if (msg instanceof DF1SymbolMessageFrameNAK) {
        logger.warn("Received a response NAK, notify all requests");
        for (Map.Entry<Integer, PlcRequestContainer> entry : requests.entrySet()) {
            entry.getValue().getResponseFuture().complete(new DefaultPlcReadResponse((PlcReadRequest) entry.getValue().getRequest(), Collections.singletonMap("erster", new ResponseItem<>(PlcResponseCode.INTERNAL_ERROR, new PlcDINT(-1)))));
        }
        return;
    } else if (msg instanceof DF1SymbolMessageFrameACK) {
        logger.warn("Received a response ACK :D");
        return;
    }
    assert msg instanceof DF1SymbolMessageFrame;
    DF1Command command = ((DF1SymbolMessageFrame) msg).getCommand();
    int transactionId = command.getTransactionCounter();
    if (!requests.containsKey(transactionId)) {
        logger.warn("Received a response to unknown transaction id {}", transactionId);
        ctx.fireExceptionCaught(new RuntimeException("Received a response to unknown transaction id"));
        ctx.close();
        return;
    }
    // As every response has a matching request, get this request based on the tpdu.
    PlcRequestContainer requestContainer = requests.remove(transactionId);
    PlcRequest request = requestContainer.getRequest();
    // Handle the response.
    PlcResponse response = null;
    if (request instanceof PlcReadRequest) {
        /*
            Things to do
            - check response code (if there is something like that?
            - cast the bytes to right datatype
            - create Response
             */
        // We can do this as we have only one fieldName in DF1
        final String fieldName = ((PlcReadRequest) request).getFieldNames().iterator().next();
        // TODO can there be another code than ok?
        final PlcResponseCode responseCode = PlcResponseCode.OK;
        // TODO maybe check for different status bytes
        final Df1Field field = (Df1Field) ((PlcReadRequest) request).getField(fieldName);
        // Cast byte and create response item
        PlcValue responseItem = null;
        byte[] data = ((DF1UnprotectedReadResponse) command).getData();
        switch(field.getDataType()) {
            case BIT:
                break;
            case INTEGER:
                // TODO: type conversion is untested
                responseItem = new PlcDINT((int) data[0] + ((int) data[1] << 8));
                break;
            case FLOAT:
                break;
            case BIT_STRING:
                break;
            case ARRAY:
                break;
            // TODO add all other cases here...
            default:
                throw new NotImplementedException("The DataType " + field.getDataType() + " is currently not implemented!");
        }
        response = new DefaultPlcReadResponse(((PlcReadRequest) request), Collections.singletonMap(fieldName, new ResponseItem<>(responseCode, responseItem)));
    } else if (request instanceof PlcWriteRequest) {
        logger.warn("Writing is currently not implemented but received a write response?!");
        ctx.close();
        throw new NotImplementedException("This is currently not implemented!");
    }
    // Confirm the response being handled.
    if (response != null) {
        requestContainer.getResponseFuture().complete(response);
    }
}
Also used : PlcRequestContainer(org.apache.plc4x.java.spi.messages.PlcRequestContainer) PlcDINT(org.apache.plc4x.java.spi.values.PlcDINT) PlcRequest(org.apache.plc4x.java.api.messages.PlcRequest) NotImplementedException(org.apache.commons.lang3.NotImplementedException) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PlcValue(org.apache.plc4x.java.api.value.PlcValue) PlcResponse(org.apache.plc4x.java.api.messages.PlcResponse) PlcWriteRequest(org.apache.plc4x.java.api.messages.PlcWriteRequest) DefaultPlcReadResponse(org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse) PlcReadRequest(org.apache.plc4x.java.api.messages.PlcReadRequest) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) Df1Field(org.apache.plc4x.java.df1.field.Df1Field)

Example 4 with PlcRequest

use of org.apache.plc4x.java.api.messages.PlcRequest in project plc4x by apache.

the class Plc4xAbEthProtocol method decode.

@Override
protected void decode(ChannelHandlerContext ctx, CIPEncapsulationPacket packet, List<Object> out) throws Exception {
    logger.trace("Received {}, decoding...", packet);
    if (packet instanceof CIPEncapsulationConnectionResponse) {
        CIPEncapsulationConnectionResponse connectionResponse = (CIPEncapsulationConnectionResponse) packet;
        // Save the session handle
        sessionHandle = connectionResponse.getSessionHandle();
        // Tell Netty we're finished connecting
        ctx.channel().pipeline().fireUserEventTriggered(new ConnectedEvent());
    } else {
        // We're currently just expecting responses.
        if (!(packet instanceof CIPEncapsulationReadResponse)) {
            return;
        }
        CIPEncapsulationReadResponse cipResponse = (CIPEncapsulationReadResponse) packet;
        int transactionCounter = cipResponse.getResponse().getTransactionCounter();
        if (!requests.containsKey(transactionCounter)) {
            ctx.fireExceptionCaught(new PlcProtocolException("Couldn't find request for response with transaction counter " + transactionCounter));
            return;
        }
        PlcRequestContainer requestContainer = requests.remove(transactionCounter);
        PlcRequest request = requestContainer.getRequest();
        PlcResponse response = null;
        if (request instanceof PlcReadRequest) {
            response = decodeReadResponse(cipResponse, requestContainer);
        } else {
            ctx.fireExceptionCaught(new PlcProtocolException("Unsupported request type " + request.getClass().getName()));
        }
        // Confirm the response being handled.
        if (response != null) {
            requestContainer.getResponseFuture().complete(response);
        }
    }
}
Also used : PlcRequestContainer(org.apache.plc4x.java.spi.messages.PlcRequestContainer) PlcResponse(org.apache.plc4x.java.api.messages.PlcResponse) PlcProtocolException(org.apache.plc4x.java.api.exceptions.PlcProtocolException) PlcRequest(org.apache.plc4x.java.api.messages.PlcRequest) ConnectedEvent(org.apache.plc4x.java.spi.events.ConnectedEvent) PlcReadRequest(org.apache.plc4x.java.api.messages.PlcReadRequest)

Example 5 with PlcRequest

use of org.apache.plc4x.java.api.messages.PlcRequest in project plc4x by apache.

the class SingleFieldOptimizer method processWriteRequest.

@Override
protected List<PlcRequest> processWriteRequest(PlcWriteRequest writeRequest, DriverContext driverContext) {
    if (writeRequest.getNumberOfFields() == 1) {
        return Collections.singletonList(writeRequest);
    }
    List<PlcRequest> subRequests = new ArrayList<>(writeRequest.getNumberOfFields());
    for (String fieldName : writeRequest.getFieldNames()) {
        PlcField field = writeRequest.getField(fieldName);
        PlcValue value = writeRequest.getPlcValue(fieldName);
        PlcWriteRequest subRequest = new DefaultPlcWriteRequest(((DefaultPlcWriteRequest) writeRequest).getWriter(), new LinkedHashMap<>(Collections.singletonMap(fieldName, new FieldValueItem(field, value))));
        subRequests.add(subRequest);
    }
    return subRequests;
}
Also used : PlcValue(org.apache.plc4x.java.api.value.PlcValue) PlcWriteRequest(org.apache.plc4x.java.api.messages.PlcWriteRequest) DefaultPlcWriteRequest(org.apache.plc4x.java.spi.messages.DefaultPlcWriteRequest) FieldValueItem(org.apache.plc4x.java.spi.messages.utils.FieldValueItem) PlcRequest(org.apache.plc4x.java.api.messages.PlcRequest) ArrayList(java.util.ArrayList) PlcField(org.apache.plc4x.java.api.model.PlcField) DefaultPlcWriteRequest(org.apache.plc4x.java.spi.messages.DefaultPlcWriteRequest)

Aggregations

PlcRequest (org.apache.plc4x.java.api.messages.PlcRequest)8 PlcReadRequest (org.apache.plc4x.java.api.messages.PlcReadRequest)6 PlcProtocolException (org.apache.plc4x.java.api.exceptions.PlcProtocolException)4 PlcWriteRequest (org.apache.plc4x.java.api.messages.PlcWriteRequest)4 PlcField (org.apache.plc4x.java.api.model.PlcField)4 ArrayList (java.util.ArrayList)2 PlcResponse (org.apache.plc4x.java.api.messages.PlcResponse)2 PlcValue (org.apache.plc4x.java.api.value.PlcValue)2 DefaultPlcReadRequest (org.apache.plc4x.java.spi.messages.DefaultPlcReadRequest)2 PlcRequestContainer (org.apache.plc4x.java.spi.messages.PlcRequestContainer)2 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 NotImplementedException (org.apache.commons.lang3.NotImplementedException)1 AbEthField (org.apache.plc4x.java.abeth.field.AbEthField)1 PlcResponseCode (org.apache.plc4x.java.api.types.PlcResponseCode)1 Df1Field (org.apache.plc4x.java.df1.field.Df1Field)1 OpcuaField (org.apache.plc4x.java.opcua.field.OpcuaField)1