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] Add Thing class & Get Thing(s) #132

Merged
merged 4 commits into from
Jun 4, 2022
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
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ binding](https://www.openhab.org/addons/automation/jsscripting/).
- [Paths](#paths)
- [Standard Library](#standard-library)
- [Items](#items)
- [Things](#things)
- [Actions](#actions)
- [Cache](#cache)
- [Log](#log)
Expand Down Expand Up @@ -393,6 +394,43 @@ var item = items.getItem("KitchenDimmer");
console.log("KitchenDimmer averageSince", item.history.averageSince(yesterday));
```

### Things

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>
* .getItem(uid, nullIfMissing) ⇒ <code>Thing</code>
* .Things() ⇒ <code>Array.&lt;Thing&gt;</code>

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

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

* Thing : <code>object</code>
* .bridgeUID ⇒ <code>String</code>
* .label ⇒ <code>String</code>
* .location ⇒ <code>String</code>
* .status ⇒ <code>String</code>
* .statusInfo ⇒ <code>String</code>
* .thingTypeUID ⇒ <code>String</code>
* .uid ⇒ <code>String</code>
* .isEnabled ⇒ <code>Boolean</code>
* .setLabel(label)
* .setLocation(location)
* .setProperty(name, value)
* .setEnabled(enabled)

```javascript
const thing = things.getThing('astro:moon:home');
console.log('Thing label: ' + thing.label);
// Set Thing location
thing.setLocation('living room');
// Disable Thing
thing.setEnabled(false);
```

### Actions

The actions namespace allows interactions with openHAB actions.
Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
/**
* @typedef {Object} HostTrigger Native Jave openHAB Trigger (instance of {@link https://www.openhab.org/javadoc/latest/org/openhab/core/automation/trigger org.openhab.core.automation.Trigger})
*/
/**
* @typedef {Object} HostThing Native Java openHAB Thing (instance of {@link https://www.openhab.org/javadoc/latest/org/openhab/core/thing/thing org.openhab.core.thing.Thing})
*/

// lazy getters to avoid any reference loading all submodules
module.exports = {
Expand Down
163 changes: 162 additions & 1 deletion things/things.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
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');

const JavaThingBuilder = Java.type('org.openhab.core.thing.binding.builder.ThingBuilder');
const ThingTypeUID = Java.type('org.openhab.core.thing.ThingTypeUID');
const JavaChannelBuilder = Java.type('org.openhab.core.thing.binding.builder.ChannelBuilder');
Expand All @@ -7,6 +14,13 @@ const ChannelKind = Java.type('org.openhab.core.thing.type.ChannelKind');
const ChannelTypeUID = Java.type('org.openhab.core.thing.type.ChannelTypeUID');
const Configuration = Java.type('org.openhab.core.config.core.Configuration');

/**
* Things namespace.
* This namespace handles querying and editing openHAB Things.
*
* @namespace things
*/

class OHThing {
constructor (rawThing) {
this.rawThing = rawThing;
Expand Down Expand Up @@ -95,7 +109,154 @@ class ChannelBuilder {
}
}

/**
* Class representing an openHAB Thing
*
* @memberof things
*/
class Thing {
/**
* Create an Thing, wrapping a native Java openHAB Thing. Don't use this constructor, instead call {@link getThing}.
* @param {HostThing} rawThing Java Thing from Host
* @hideconstructor
*/
constructor (rawThing) {
if (typeof rawThing === 'undefined') {
throw Error('Supplied Thing is undefined');
}
this.rawThing = rawThing;
}

/**
* Thing's bridge UID as `String`
*/
get bridgeUID () {
try {
return this.rawThing.getBridgeUID().getID();
} catch (error) {
// Thing has no bridge
}
}

/**
* label as `String`
*/
get label () {
return this.rawThing.getLabel();
}

/**
* physical location as `String`
*/
get location () {
return this.rawThing.getLocation();
}

/**
* status as `String`
*/
get status () {
return this.rawThing.getStatus().toString();
}

/**
* status info (more detailed status text) as `String`
*/
get statusInfo () {
return this.rawThing.getStatusInfo().toString();
}

/**
* Thing type UID as `String`
*/
get thingTypeUID () {
return this.rawThing.getThingTypeUID().toString();
}

/**
* Thing UID as `String`
*/
get uid () {
return this.rawThing.getUID().toString();
}

/**
* whether the Thing is enabled or not (`Boolean`)
*/
get isEnabled () {
return this.rawThing.isEnabled();
}

/**
* Set the label.
* @param {String} label Thing label
*/
setLabel (label) {
this.rawThing.setLabel(label);
}

/**
* Sets the physical location.
* @param {String} location physical location of the Thing
*/
setLocation (location) {
this.rawThing.setLocation(location);
}

/**
* Sets the property value for the property identified by the given name.
* @param {String} name name of the property
* @param {String} value value for the property
*/
setProperty (name, value) {
this.rawThing.setProperty(name, value);
}

/**
* Sets the enabled status of the Thing.
* @param {Boolean} enabled whether the Thing is enabled or not
*/
setEnabled (enabled) {
thingMgr.setEnabled(this.rawThing.getUID(), enabled);
}
}

/**
* Gets an openHAB Thing.
*
* @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 {things.Thing} the Thing
*/
const getThing = function (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;
}
}
};

/**
* Gets all openHAB Things.
*
* @memberof things
* @returns {things.Thing[]} all Things
*/
const getThings = function () {
return utils.javaSetToJsArray(thingRegistry.getAll()).map(i => new Thing(i));
};

module.exports = {
newThingBuilder: (thingTypeUID, id, bridgeUID) => new ThingBuilder(thingTypeUID, id, bridgeUID),
newChannelBuilder: (thingUID, channelId, acceptedItemType) => new ChannelBuilder(thingUID, channelId, acceptedItemType)
newChannelBuilder: (thingUID, channelId, acceptedItemType) => new ChannelBuilder(thingUID, channelId, acceptedItemType),
Thing,
getThing,
getThings
};