diff --git a/index.js b/index.js index a607bf053..2c75087b2 100644 --- a/index.js +++ b/index.js @@ -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 = { diff --git a/things/things.js b/things/things.js index 2154ff80c..5a65242b4 100644 --- a/things/things.js +++ b/things/things.js @@ -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'); @@ -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; @@ -95,6 +108,117 @@ 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)