Search in sources :

Example 1 with DF1Symbol

use of org.apache.plc4x.java.df1.readwrite.DF1Symbol 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 2 with DF1Symbol

use of org.apache.plc4x.java.df1.readwrite.DF1Symbol in project plc4x by apache.

the class Df1Protocol method decode.

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    DF1Symbol resp;
    // do {
    in.markReaderIndex();
    short size = 0x00;
    // Yes, it's a little complicated, but we need to find out if we've got enough data.
    if (in.readableBytes() > 1) {
        if (in.getUnsignedByte(0) != (short) 0x10) {
            logger.warn("Expecting DF1 magic number: {}", 0x10);
            if (logger.isDebugEnabled()) {
                logger.debug("Got Data: {}", ByteBufUtil.hexDump(in));
            }
            exceptionCaught(ctx, new PlcProtocolException(String.format("Expecting DF1 magic number: %02X", 0x10)));
            return;
        }
        short symbolType = in.getUnsignedByte(1);
        switch(symbolType) {
            case (short) 0x02:
                {
                    if (in.readableBytes() < 5) {
                        return;
                    }
                    short commandType = in.getUnsignedByte(4);
                    switch(commandType) {
                        case (short) 0x01:
                            {
                                if (in.readableBytes() < 11) {
                                    return;
                                }
                                break;
                            }
                        case (short) 0x41:
                            {
                                // TODO: Let's just assume all is good for now ...
                                break;
                            }
                    }
                    break;
                }
            case (short) 0x03:
                {
                    if (in.readableBytes() < 4) {
                        return;
                    }
                    break;
                }
        }
    }
    // Parse the message received from the DF1 device
    byte[] data = new byte[in.readableBytes()];
    in.readBytes(data);
    ReadBuffer readBuffer = new ReadBufferByteBased(data);
    resp = DF1Symbol.staticParse(readBuffer);
    // } while (readWasSucessfull);
    // // TODO if unableto read
    // in.resetReaderIndex();
    // Add the received message to the output
    out.add(resp);
}
Also used : ReadBuffer(org.apache.plc4x.java.spi.generation.ReadBuffer) PlcProtocolException(org.apache.plc4x.java.api.exceptions.PlcProtocolException) ReadBufferByteBased(org.apache.plc4x.java.spi.generation.ReadBufferByteBased) DF1Symbol(org.apache.plc4x.java.df1.readwrite.DF1Symbol)

Aggregations

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 PlcProtocolException (org.apache.plc4x.java.api.exceptions.PlcProtocolException)1 PlcReadRequest (org.apache.plc4x.java.api.messages.PlcReadRequest)1 PlcRequest (org.apache.plc4x.java.api.messages.PlcRequest)1 PlcResponse (org.apache.plc4x.java.api.messages.PlcResponse)1 PlcWriteRequest (org.apache.plc4x.java.api.messages.PlcWriteRequest)1 PlcResponseCode (org.apache.plc4x.java.api.types.PlcResponseCode)1 PlcValue (org.apache.plc4x.java.api.value.PlcValue)1 Df1Field (org.apache.plc4x.java.df1.field.Df1Field)1 DF1Symbol (org.apache.plc4x.java.df1.readwrite.DF1Symbol)1 ReadBuffer (org.apache.plc4x.java.spi.generation.ReadBuffer)1 ReadBufferByteBased (org.apache.plc4x.java.spi.generation.ReadBufferByteBased)1 DefaultPlcReadResponse (org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse)1 PlcRequestContainer (org.apache.plc4x.java.spi.messages.PlcRequestContainer)1 PlcDINT (org.apache.plc4x.java.spi.values.PlcDINT)1