Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[things] Fix getThing doesn't return null if Thing not existent #315

Merged
merged 1 commit into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading