Skip to content

Commit

Permalink
[things] Add Thing class
Browse files Browse the repository at this point in the history
The Thing class represents an openHAB Thing.

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
  • Loading branch information
florian-h05 committed Jun 3, 2022
1 parent bcc3505 commit eb0d8c8
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
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
125 changes: 125 additions & 0 deletions 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,12 @@ 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,6 +108,118 @@ 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().getId();
}

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

/**
* 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);
}
}

module.exports = {
newThingBuilder: (thingTypeUID, id, bridgeUID) => new ThingBuilder(thingTypeUID, id, bridgeUID),
newChannelBuilder: (thingUID, channelId, acceptedItemType) => new ChannelBuilder(thingUID, channelId, acceptedItemType)
Expand Down

0 comments on commit eb0d8c8

Please sign in to comment.