[SOLVED] Discovery Implementation - Textual added things/Items

Hi guys

So my binding is now up and running and in use by a few users. A really irritating thing though is discovery finding things that have already been added via text files.
Is there anyway to combat this directly in the coding or is it just one of them put up with it things?

Discovery finds Things, not Items. They are two totally different concepts in openHAB.

Wrong terminology. ‘Things’, but you get my gist
I’m assuming it’s because the last part of the identifier is changed by a user in a text file so it’s not in the registry

For instance This is discovered:

2020-02-10 22:58:27.388 [home.event.InboxAddedEvent] - Discovery Result with UID 'lightwaverf:s22:benfleet:2' has been added.

But my text item is:

lightwaverf:s22:benfleet:Socket2

The issue, I think, is that the Thing UID for the thing defined in the .things file is different than the Thing UID for the discovered thing. Hence they are viewed as different. I don’t know of any way around this (although I agree with you that it would be nice if it could be avoided).

1 Like

So could this be done:
Start discovery
Query the binding for all things already present/initialised
Loop through the configurations to get say an iPaddress or and Id.
Then only discover the thing if it’s not already present

I can do the 2nd part but wouldn’t know how to query already added devices

The REST API?

Can it not be done internally or can the api be accessed as a package?

Ie:
For each thing getConfiguration().get(“DeviceId”)
Then loop through

I’m still learning what can and can’t be done, I only started with this 2 weeks ago having no prior java knowledge, so learning the different packages/methods is the part I’m still getting to grips with

The easiest way from the browser is to install the REST Documentation under User Interfaces. That gives a pretty self explanatory web interface to the API.

This doesn’t seem to me to be the best option to be using in backend coding. Seems a little arse about face as the api must query the internal packages anyhow.

OK, you can query the REST API using https and cURL. It is a standard JSON based API. It looks like to get a list of all things, it is http://[ip address]:8080/api/things or
curl -X GET --header "Accept: application/json" "http://[ip address:8080/rest/things"

I got that information from the REST Documentation.

Try this on your setup to see if it’s true. If it is you can document In the help docs a suggestion on naming?

I know for the hue and tradfri binding the condition you site is true. Once I realized this I fixed my thing file and emptied my inbox.

I’ve checked my code and yes it’s true.
The auto discovery uses the deviceId discovered from the api, where as my manual config is any name you want to choose.
Since the deviceId is present in the auto discovery class where I’m looping if I can access ‘Allthings’ configuration parameter then I can check if it’s already present and not add it to the inbox.

1 Like

Got it, get list of things from the bridge added to the top of the discovery method:

List<Thing> things = accountHandler.getThing().getThings();

Private Variable added:

private Boolean deviceAdded;

Then when i query the api:

for (int s = 0; s < structures.size(); s++) {
    sId = structures.get(s).getGroupId();
    sName = structures.get(s).getName();
    logger.debug("Structure Id {} with name {} Found", sId,sName);
    for (int d = 0; d < structures.get(s).getDevices().size(); d++) {
        dType = structures.get(s).getDevices().get(d).getCat().toString();
        dId = structures.get(s).getDevices().get(d).getDeviceId().toString();
        array1 = dId.split("-");
        sdId = array1[1].toString();
        deviceAdded = false;
        for (int i = 0; i < things.size(); i++) {
            String Id = things.get(i).getConfiguration().get("sdId").toString();
            if(Id.equals(sdId)) {
                deviceAdded = true;
            }
        }
        if (deviceAdded == false) {
        \\add the thing here
        }
    }
}

This was all I needed to know (how to query the already added things):

List<Thing> things = accountHandler.getThing().getThings();

Hopefully it will help anyone else diving in to the realms of development.

3 Likes

Hi @delid4ve

Do you mind providing a link to the code in the git repo where you have implemented that so I can view it within the whole context? I have checked out 2.5.x but I cannot find the above code anywhere.

TIA
Stefan

@delid4ve (thanks) provided the link in the meantime to me. So just for the record, here is where it can be found:

delid4ve/lightwaverf/blob/master/src/main/java/org/openhab/binding/lightwaverf/internal/LWDiscoveryService.java

and the important piece of the code is

and the piece of code is this:

Boolean deviceAdded = false;
                                for (int i = 0; i < things.size(); i++) {
                                    String id = things.get(i).getConfiguration().get("sdId").toString();
                                    if(id.equals(sdId)) {
                                        deviceAdded = true;
                                    }
                                }
                                if (!deviceAdded) {