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?
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.
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).
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
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.
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.
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.
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.