Skip to content

Commit

Permalink
[things] Fix getThing doesn't return null if Thing not existent (#315)
Browse files Browse the repository at this point in the history
Fixes #314.

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
  • Loading branch information
florian-h05 authored Dec 25, 2023
1 parent b30c794 commit ed1062d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 21 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -555,12 +555,12 @@ The Things namespace allows to interact with openHAB Things.
See [openhab-js : things](https://openhab.github.io/openhab-js/things.html) for full API documentation.

- things : <code>object</code>
- .getThing(uid, nullIfMissing) ⇒ <code>Thing</code>
- .getThings() ⇒ <code>Array.&lt;Thing&gt;</code>
- .getThing(uid) ⇒ <code>Thing|null</code>
- .getThings() ⇒ <code>Array[Thing]</code>

#### `getThing(uid, nullIfMissing)`

Calling `getThing(...)` returns a `Thing` object with the following properties:
Calling `getThing(uid)` returns a `Thing` object with the following properties:

- Thing : <code>object</code>
- .bridgeUID ⇒ <code>String</code>
Expand Down
21 changes: 6 additions & 15 deletions things.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const osgi = require('./osgi');
const utils = require('./utils');
const log = require('./log')('things'); // eslint-disable-line no-unused-vars

const thingRegistry = osgi.getService('org.openhab.core.thing.ThingRegistry');
const thingMgr = osgi.getService('org.openhab.core.thing.ThingManager');
Expand Down Expand Up @@ -148,24 +147,16 @@ class Thing {

/**
* Gets an openHAB Thing.
* Returns `null` if no Thing with the given UID exists.
*
* @memberof things
* @param {string} uid UID of the thing
* @param {boolean} [nullIfMissing] whether to return null if the Thing cannot be found (default is to throw an exception)
* @returns {Thing} {@link things.Thing}
*/
function getThing (uid, nullIfMissing) {
try {
if (typeof uid === 'string' || uid instanceof String) {
return new Thing(thingRegistry.get(new ThingUID(uid)));
}
} catch (e) {
if (nullIfMissing) {
return null;
} else {
throw e;
}
}
function getThing (uid) {
const rawThing = thingRegistry.get(new ThingUID(uid));
if (rawThing === null) return null;
return new Thing(rawThing);
}

/**
Expand All @@ -175,7 +166,7 @@ function getThing (uid, nullIfMissing) {
* @returns {Thing[]} {@link things.Thing}[]: all Things
*/
function getThings () {
return utils.javaSetToJsArray(thingRegistry.getAll()).map(i => new Thing(i));
return utils.javaSetToJsArray(thingRegistry.getAll()).map(t => new Thing(t));
}

module.exports = {
Expand Down
4 changes: 2 additions & 2 deletions types/things.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ export class Thing {
}
/**
* Gets an openHAB Thing.
* Returns `null` if no Thing with the given UID exists.
*
* @memberof things
* @param {string} uid UID of the thing
* @param {boolean} [nullIfMissing] whether to return null if the Thing cannot be found (default is to throw an exception)
* @returns {Thing} {@link things.Thing}
*/
export function getThing(uid: string, nullIfMissing?: boolean): Thing;
export function getThing(uid: string): Thing;
/**
* Gets all openHAB Things.
*
Expand Down
2 changes: 1 addition & 1 deletion types/things.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ed1062d

Please sign in to comment.