private void discoverProtocol()

in plc4j/tools/ui/backend/src/main/java/org/apache/plc4x/java/tools/ui/service/DriverService.java [80:117]


    private void discoverProtocol(String protocolCode) {
        try {
            PlcDriver driver = driverManager.getDriver(protocolCode);
            if (!driver.getMetadata().isDiscoverySupported()) {
                throw new RuntimeException("Driver doesn't support discovery");
            } else {
                PlcDiscoveryRequest request = driver.discoveryRequestBuilder().addQuery("all", "*").build();
                // Execute the discovery request and have all connections found be added as connections.
                request.executeWithHandler(discoveryItem -> {
                    // Create the new device.
                    Device device = new Device();
                    device.setName(discoveryItem.getName());
                    device.setProtocolCode(discoveryItem.getProtocolCode());
                    device.setTransportCode(discoveryItem.getTransportCode());
                    device.setTransportUrl(discoveryItem.getTransportUrl());
                    device.setOptions(discoveryItem.getOptions());
                    Map<String, String> attributes = new HashMap<>();
                    for (String attributeName : discoveryItem.getAttributes().keySet()) {
                        String attributeValue = discoveryItem.getAttributes().get(attributeName).getString();
                        attributes.put(attributeName, attributeValue);
                    }
                    device.setAttributes(attributes);

                    // Save the found device in the database, if this is a new device,
                    // that is not stored in our system before.
                    if(deviceService.isNewDevice(device)) {
                        deviceService.createDevice(device);
                    }
                }).whenComplete((plcDiscoveryResponse, throwable) -> {
                    if(throwable != null) {
                        throw new RuntimeException("Error executing discovery", throwable);
                    }
                });
            }
        } catch (PlcConnectionException e) {
            throw new RuntimeException("Error getting driver", e);
        }
    }