pitschr / knx-core

KNX Core - A Java library for KNX Net/IP communication
GNU General Public License v3.0
28 stars 12 forks source link

Hello author, I have some questions for you #18

Closed thhbbx closed 1 year ago

thhbbx commented 2 years ago

I am a junior programmer in China, and now I am working on a project on the Internet of Things, one requirement is to periodically read the state of the device, we put your code example encapsulated into a different way, timed tasks by invoking the different method to obtain the data to the device address different, but multiple calls method after the method of "DefaultKnxClient. CreateStarted (" gateway IP");" Content Frequent connection to the gateway results in slow response and disconnected calls. How do I solve the problem? Here is one of the methods invoked after the scheduled task is executed, this method is called every 30 seconds or so to get the data, but about ten minutes after running the code to connect to the gateway will appear the connection failure problem, do you need to use long connections to solve this problem?  Can the author provide an example for us, millions of thanks!

The above words are machine translation,Please forgive me if there is something wrong. public String getDeviceState(String individualAddress, String stateGroupAddress) { final var readGroupAddress = GroupAddress.of(stateGroupAddress); final var client = DefaultKnxClient.createStarted(individualAddress); client.readRequest(readGroupAddress); DPT1Value value = client.getStatusPool().getValue(readGroupAddress, DPT1.SWITCH); if (value == null) { throw new ServiceException("Group address error"); } else { if (value.getValue()){ return "ON"; }else { return "OFF"; } } }

pitschr commented 1 year ago

Hello.

The KNX communication is designed to connect with the KNX Net/IP device (either a router or a tunneling device) and the connection will stay open until one of the peer closes the connection for any reasons. A KNX router usually has only few free connection slots; the exact number of max accepted connections in parallel may vary on KNX device / model / manufacturer.

So keep in mind about it, in case you open too much connections in short time and those are not released fast enough, then subsequent connection attempts will fail and that is most probably you faced.

Given your implementation I see, that you are trying to open a new connection, send a request (asynchronously) and then fetch for value from your group address. And this should happen every 30 seconds. When I got your idea, then I would suggest following implementation below:

The example below will poll the state of lamp (in my case, the group address 1/2/113) every 30 seconds. The connection will remain open until the client application is terminated or the connection has been stopped for any reasons (e.g. network issue, power outage, ...).

import li.pitschmann.knx.core.address.GroupAddress;
import li.pitschmann.knx.core.communication.DefaultKnxClient;
import li.pitschmann.knx.core.communication.KnxClient;
import li.pitschmann.knx.core.datapoint.DPT1;
import li.pitschmann.knx.core.datapoint.value.DPT1Value;
import li.pitschmann.knx.core.utils.Sleeper;

public final class Main {
    public static String getDeviceState(KnxClient client, GroupAddress readGroupAddress) {
        client.readRequest(readGroupAddress);
        DPT1Value value = client.getStatusPool().getValue(readGroupAddress, DPT1.SWITCH);
        if (value == null) {
            throw new RuntimeException("Group address error");
        } else {
            if (value.getValue()) { return "ON"; } else { return "OFF"; }
        }
    }

    public static void main(final String[] args) {
        var readGroupAddress = GroupAddress.of("1/2/113");

        try (final var client = DefaultKnxClient.createStarted()) {
            System.out.println("KNX Connection opened");
            do {
                System.out.println("Status: " + getDeviceState(client, readGroupAddress));
                Sleeper.seconds(30);
            } while (client.isRunning());  // poll as long the KNX client is running
        } finally {
            System.out.println("KNX Connection closed");
        }
    }
}
thhbbx commented 1 year ago

Thank you for your help

pitschr commented 1 year ago

Welcome