Search in sources :

Example 1 with PlcStruct

use of org.apache.plc4x.java.spi.values.PlcStruct in project plc4x by apache.

the class KnxNetIpProtocolLogic method processCemiData.

protected void processCemiData(KnxAddress sourceAddress, byte[] destinationGroupAddress, byte firstByte, byte[] restBytes) throws ParseException {
    // The first byte is actually just 6 bit long, but we'll treat it as a full one.
    // So here we create a byte array containing the first and all the following bytes.
    byte[] payload = new byte[1 + restBytes.length];
    payload[0] = firstByte;
    System.arraycopy(restBytes, 0, payload, 1, restBytes.length);
    // Decode the group address depending on the project settings.
    ReadBuffer addressBuffer = new ReadBufferByteBased(destinationGroupAddress);
    final KnxGroupAddress knxGroupAddress = KnxGroupAddress.staticParse(addressBuffer, knxNetIpDriverContext.getGroupAddressType());
    final String destinationAddress = toString(knxGroupAddress);
    // If there is an ETS5 model provided, continue decoding the payload.
    if (knxNetIpDriverContext.getEts5Model() != null) {
        final Ets5Model ets5Model = knxNetIpDriverContext.getEts5Model();
        final GroupAddress groupAddress = ets5Model.getGroupAddresses().get(destinationAddress);
        final String areaName = ets5Model.getTopologyName(destinationAddress.substring(0, destinationAddress.indexOf('/')));
        final String lineName = ets5Model.getTopologyName(destinationAddress.substring(0, destinationAddress.indexOf('/', destinationAddress.indexOf('/') + 1)));
        if ((groupAddress != null) && (groupAddress.getType() != null)) {
            LOGGER.trace(String.format("Message from: '%s' to: '%s'", toString(sourceAddress), destinationAddress));
            // Parse the payload depending on the type of the group-address.
            ReadBuffer rawDataReader = new ReadBufferByteBased(payload);
            final PlcValue value = KnxDatapoint.staticParse(rawDataReader, groupAddress.getType());
            // Assemble the plc4x return data-structure.
            Map<String, PlcValue> dataPointMap = new HashMap<>();
            dataPointMap.put("sourceAddress", new PlcSTRING(toString(sourceAddress)));
            dataPointMap.put("targetAddress", new PlcSTRING(groupAddress.getGroupAddress()));
            if (groupAddress.getFunction() != null) {
                dataPointMap.put("location", new PlcSTRING(groupAddress.getFunction().getSpaceName()));
                dataPointMap.put("function", new PlcSTRING(groupAddress.getFunction().getName()));
            } else {
                dataPointMap.put("location", null);
                dataPointMap.put("function", null);
            }
            if (areaName != null) {
                dataPointMap.put("area", new PlcSTRING(areaName));
            }
            if (lineName != null) {
                dataPointMap.put("line", new PlcSTRING(lineName));
            }
            dataPointMap.put("description", new PlcSTRING(groupAddress.getName()));
            dataPointMap.put("unitOfMeasurement", new PlcSTRING(groupAddress.getType().getName()));
            dataPointMap.put("value", value);
            final PlcStruct dataPoint = new PlcStruct(dataPointMap);
            // Send the data-structure.
            publishEvent(groupAddress, dataPoint);
        } else {
            LOGGER.warn(String.format("Message from: '%s' to unknown group address: '%s'%n payload: '%s'", toString(sourceAddress), destinationAddress, Hex.encodeHexString(payload)));
        }
    } else // Else just output the raw payload.
    {
        LOGGER.info(String.format("Raw Message: '%s' to: '%s'%n payload: '%s'", KnxNetIpProtocolLogic.toString(sourceAddress), destinationAddress, Hex.encodeHexString(payload)));
    }
}
Also used : Ets5Model(org.apache.plc4x.java.knxnetip.ets5.model.Ets5Model) KnxGroupAddress(org.apache.plc4x.java.knxnetip.readwrite.KnxGroupAddress) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PlcStruct(org.apache.plc4x.java.spi.values.PlcStruct) GroupAddress(org.apache.plc4x.java.knxnetip.ets5.model.GroupAddress) KnxGroupAddress(org.apache.plc4x.java.knxnetip.readwrite.KnxGroupAddress) PlcSTRING(org.apache.plc4x.java.spi.values.PlcSTRING)

Example 2 with PlcStruct

use of org.apache.plc4x.java.spi.values.PlcStruct in project plc4x by apache.

the class DefaultPlcReadResponse method getAsPlcValue.

@Override
@JsonIgnore
public PlcValue getAsPlcValue() {
    Map<String, PlcValue> structMap = new HashMap<>();
    for (String fieldName : request.getFieldNames()) {
        PlcValue plcValue = getPlcValue(fieldName);
        structMap.put(fieldName, plcValue);
    }
    return new PlcStruct(structMap);
}
Also used : PlcValue(org.apache.plc4x.java.api.value.PlcValue) PlcStruct(org.apache.plc4x.java.spi.values.PlcStruct)

Example 3 with PlcStruct

use of org.apache.plc4x.java.spi.values.PlcStruct in project plc4x by apache.

the class PassiveBacNetIpDriverManual method main.

public static void main(String[] args) throws Exception {
    final BacNetIpDriver driver = new BacNetIpDriver();
    final PlcConnection connection = driver.getConnection("bacnet-ip:pcap:///Users/christofer.dutz/Projects/Apache/PLC4X-Documents/BacNET/Merck/Captures/BACnet.pcapng?ede-directory-path=/Users/christofer.dutz/Projects/Apache/PLC4X-Documents/BacNET/Merck/EDE-Files");
    connection.connect();
    final PlcSubscriptionResponse subscriptionResponse = connection.subscriptionRequestBuilder().addEventField("Hurz", "*/*/*").build().execute().get();
    subscriptionResponse.getSubscriptionHandle("Hurz").register(plcSubscriptionEvent -> {
        PlcStruct plcStruct = (PlcStruct) ((DefaultPlcSubscriptionEvent) plcSubscriptionEvent).getValues().get("event").getValue();
        System.out.println(plcStruct);
    });
}
Also used : PlcSubscriptionResponse(org.apache.plc4x.java.api.messages.PlcSubscriptionResponse) PlcStruct(org.apache.plc4x.java.spi.values.PlcStruct) PlcConnection(org.apache.plc4x.java.api.PlcConnection)

Example 4 with PlcStruct

use of org.apache.plc4x.java.spi.values.PlcStruct in project plc4x by apache.

the class PassiveBacNetIpDriver2Manual method main.

public static void main(String[] args) throws Exception {
    String tempDirectory = FileUtils.getTempDirectoryPath();
    File pcapFile = FileSystems.getDefault().getPath(tempDirectory, "bacnet-stack-services.cap").toFile();
    if (!pcapFile.exists())
        FileUtils.copyURLToFile(new URL("https://wiki.wireshark.org/Protocols/bacnet?action=AttachFile&do=get&target=bacnet-stack-services.cap"), pcapFile);
    final BacNetIpDriver driver = new BacNetIpDriver();
    final PlcConnection connection = driver.getConnection("bacnet-ip:pcap://" + pcapFile.getAbsolutePath());
    connection.connect();
    final PlcSubscriptionResponse subscriptionResponse = connection.subscriptionRequestBuilder().addEventField("Hurz", "*/*/*").build().execute().get();
    subscriptionResponse.getSubscriptionHandle("Hurz").register(plcSubscriptionEvent -> {
        PlcStruct plcStruct = (PlcStruct) ((DefaultPlcSubscriptionEvent) plcSubscriptionEvent).getValues().get("event").getValue();
        System.out.println(plcStruct);
    });
}
Also used : PlcSubscriptionResponse(org.apache.plc4x.java.api.messages.PlcSubscriptionResponse) PlcStruct(org.apache.plc4x.java.spi.values.PlcStruct) File(java.io.File) URL(java.net.URL) PlcConnection(org.apache.plc4x.java.api.PlcConnection)

Example 5 with PlcStruct

use of org.apache.plc4x.java.spi.values.PlcStruct in project plc4x by apache.

the class PassiveBacNetIpDriver3Manual method main.

public static void main(String[] args) throws Exception {
    String tempDirectory = FileUtils.getTempDirectoryPath();
    File pcapFile = FileSystems.getDefault().getPath(tempDirectory, "bo_command_failure_original.pcap").toFile();
    if (!pcapFile.exists())
        FileUtils.copyURLToFile(new URL("http://kargs.net/captures/bo_command_failure_original.pcap"), pcapFile);
    final BacNetIpDriver driver = new BacNetIpDriver();
    try (final PlcConnection connection = driver.getConnection("bacnet-ip:pcap://" + pcapFile.getAbsolutePath() + "?filter=udp%20port%2047808")) {
        connection.connect();
        final PlcSubscriptionResponse subscriptionResponse = connection.subscriptionRequestBuilder().addEventField("Hurz", "*/*/*").build().execute().get();
        subscriptionResponse.getSubscriptionHandle("Hurz").register(plcSubscriptionEvent -> {
            PlcStruct plcStruct = (PlcStruct) ((DefaultPlcSubscriptionEvent) plcSubscriptionEvent).getValues().get("event").getValue();
            System.out.println(plcStruct);
        });
        TimeUnit.SECONDS.sleep(3);
    }
}
Also used : PlcSubscriptionResponse(org.apache.plc4x.java.api.messages.PlcSubscriptionResponse) PlcStruct(org.apache.plc4x.java.spi.values.PlcStruct) File(java.io.File) URL(java.net.URL) PlcConnection(org.apache.plc4x.java.api.PlcConnection)

Aggregations

PlcStruct (org.apache.plc4x.java.spi.values.PlcStruct)6 PlcConnection (org.apache.plc4x.java.api.PlcConnection)3 PlcSubscriptionResponse (org.apache.plc4x.java.api.messages.PlcSubscriptionResponse)3 File (java.io.File)2 URL (java.net.URL)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 PlcValue (org.apache.plc4x.java.api.value.PlcValue)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Consumer (java.util.function.Consumer)1 PlcSubscriptionEvent (org.apache.plc4x.java.api.messages.PlcSubscriptionEvent)1 PlcSubscriptionHandle (org.apache.plc4x.java.api.model.PlcSubscriptionHandle)1 CANOpenPDOField (org.apache.plc4x.java.canopen.field.CANOpenPDOField)1 CANOpenHeartbeatPayload (org.apache.plc4x.java.canopen.readwrite.CANOpenHeartbeatPayload)1 CANOpenNetworkPayload (org.apache.plc4x.java.canopen.readwrite.CANOpenNetworkPayload)1 CANOpenPDOPayload (org.apache.plc4x.java.canopen.readwrite.CANOpenPDOPayload)1 NMTState (org.apache.plc4x.java.canopen.readwrite.NMTState)1 NMTStateRequest (org.apache.plc4x.java.canopen.readwrite.NMTStateRequest)1 Ets5Model (org.apache.plc4x.java.knxnetip.ets5.model.Ets5Model)1