|
| 1 | +const osgi = require('../osgi'); |
| 2 | +const utils = require('../utils'); |
| 3 | +const environment = require('../environment'); |
| 4 | +const log = require('../log')('itemchannellink'); |
| 5 | +const { _getItemName } = require('../helpers'); |
| 6 | + |
| 7 | +const itemChannelLinkRegistry = environment.useProviderRegistries() |
| 8 | + ? require('@runtime/provider').itemChannelLinkRegistry |
| 9 | + : osgi.getService('org.openhab.core.thing.link.ItemChannelLinkRegistry'); |
| 10 | +const JavaItemChannelLink = Java.type('org.openhab.core.thing.link.ItemChannelLink'); |
| 11 | +const ChannelUID = Java.type('org.openhab.core.thing.ChannelUID'); |
| 12 | +const Configuration = Java.type('org.openhab.core.config.core.Configuration'); |
| 13 | + |
| 14 | +/** |
| 15 | + * Item channel link namespace. |
| 16 | + * This namespace provides access to Item channel links. |
| 17 | + * |
| 18 | + * @namespace items.itemChannelLink |
| 19 | + */ |
| 20 | + |
| 21 | +/** |
| 22 | + * @typedef {import('./items').Item} Item |
| 23 | + * @private |
| 24 | + */ |
| 25 | + |
| 26 | +/** |
| 27 | + * Class representing an openHAB Item -> channel link. |
| 28 | + * |
| 29 | + * @memberof items.itemChannelLink |
| 30 | + * @hideconstructor |
| 31 | + */ |
| 32 | +class ItemChannelLink { |
| 33 | + itemName; |
| 34 | + channelUID; |
| 35 | + configuration; |
| 36 | + |
| 37 | + /** |
| 38 | + * @param {*} rawItemChannelLink {@link https://www.openhab.org/javadoc/latest/org/openhab/core/thing/link/itemchannellink org.openhab.core.thing.link.ItemChannelLink} |
| 39 | + */ |
| 40 | + constructor (rawItemChannelLink) { |
| 41 | + this.itemName = rawItemChannelLink.getItemName().toString(); |
| 42 | + this.channelUID = rawItemChannelLink.getLinkedUID().toString(); |
| 43 | + this.configuration = utils.javaMapToJsObj(rawItemChannelLink.getConfiguration().getProperties()); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Gets a channel link of from an Item. |
| 49 | + * |
| 50 | + * @memberof items.itemChannelLink |
| 51 | + * @param {Item|string} itemOrName {@link Item} or the name of the Item |
| 52 | + * @param {string} channelUID |
| 53 | + * @returns {ItemChannelLink|null} the ItemChannelLink or `null` if none exists |
| 54 | + */ |
| 55 | +function getItemChannelLink (itemOrName, channelUID) { |
| 56 | + const itemName = _getItemName(itemOrName); |
| 57 | + log.debug(`Getting ItemChannelLink ${itemName} -> ${channelUID} from registry.`); |
| 58 | + const itemChannelLink = itemChannelLinkRegistry.get(itemName + ' -> ' + channelUID); |
| 59 | + if (itemChannelLink === null) return null; |
| 60 | + return new ItemChannelLink(itemChannelLink); |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Creates a new ItemChannelLink object. |
| 65 | + * This ItemChannelLink is not registered with any provider and therefore cannot be accessed. |
| 66 | + * |
| 67 | + * @private |
| 68 | + * @param {string} itemName the name of the Item |
| 69 | + * @param {string} channelUID |
| 70 | + * @param {object} [conf] channel configuration |
| 71 | + * @returns {JavaItemChannelLink} ItemChannelLink object |
| 72 | + */ |
| 73 | +function _createItemChannelLink (itemName, channelUID, conf) { |
| 74 | + log.debug(`Creating ItemChannelLink ${itemName} -> ${channelUID}`); |
| 75 | + if (typeof conf === 'object') { |
| 76 | + log.debug(` with configuration: ${JSON.stringify(conf)}`); |
| 77 | + return new JavaItemChannelLink(itemName, new ChannelUID(channelUID), new Configuration(conf)); |
| 78 | + } else { |
| 79 | + return new JavaItemChannelLink(itemName, new ChannelUID(channelUID)); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Adds a new channel link to an Item. |
| 85 | + * |
| 86 | + * If this is called from file-based scripts, the Item -> channel link is registered with the ScriptedItemChannelLinkProvider and shares the same lifecycle as the script. |
| 87 | + * You can still persist the Item -> channel link permanently in this case by setting the `persist` parameter to `true`. |
| 88 | + * If this is called from UI-based scripts, the Item -> channel link is stored to the ManagedItemChannelLinkProvider and independent of the script's lifecycle. |
| 89 | + * |
| 90 | + * @memberOf items.itemChannelLink |
| 91 | + * @param {Item|string} itemOrName {@link Item} or the name of the Item |
| 92 | + * @param {string} channelUID |
| 93 | + * @param {object} [configuration] channel configuration |
| 94 | + * @param {boolean} [persist=false] whether to persist the Item -> channel link permanently (only respected for file-based scripts) |
| 95 | + * @returns {ItemChannelLink} the ItemChannelLink |
| 96 | + * @throws {Error} if the Item -> channel link already exists |
| 97 | + */ |
| 98 | +function addItemChannelLink (itemOrName, channelUID, configuration, persist = false) { |
| 99 | + const itemName = _getItemName(itemOrName); |
| 100 | + log.debug(`Adding ItemChannelLink ${itemName} -> ${channelUID} to registry...`); |
| 101 | + let itemChannelLink = _createItemChannelLink(itemName, channelUID, configuration); |
| 102 | + try { |
| 103 | + itemChannelLink = (persist && environment.useProviderRegistries()) ? itemChannelLinkRegistry.addPermanent(itemChannelLink) : itemChannelLinkRegistry.add(itemChannelLink); |
| 104 | + } catch (e) { |
| 105 | + if (e instanceof Java.type('java.lang.IllegalArgumentException')) { |
| 106 | + throw new Error(`Cannot add ItemChannelLink ${itemName} -> ${channelUID}: already exists`); |
| 107 | + } else { |
| 108 | + throw e; // re-throw other errors |
| 109 | + } |
| 110 | + } |
| 111 | + return new ItemChannelLink(itemChannelLink); |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Updates a channel link of an Item. |
| 116 | + * |
| 117 | + * @private |
| 118 | + * @param {string} itemName the name of the Item |
| 119 | + * @param {string} channelUID |
| 120 | + * @param {object} [configuration] channel configuration |
| 121 | + * @returns {ItemChannelLink|null} the old ItemChannelLink or `null` if none exists |
| 122 | + */ |
| 123 | +function _updateItemChannelLink (itemName, channelUID, configuration) { |
| 124 | + log.debug(`Updating ItemChannelLink ${itemName} -> ${channelUID} in registry...`); |
| 125 | + let itemChannelLink = _createItemChannelLink(itemName, channelUID, configuration); |
| 126 | + itemChannelLink = itemChannelLinkRegistry.update(itemChannelLink); |
| 127 | + if (itemChannelLink === null) return null; |
| 128 | + return new ItemChannelLink(itemChannelLink); |
| 129 | +} |
| 130 | + |
| 131 | +/** |
| 132 | + * Adds or updates a channel link of an Item. |
| 133 | + * If you use this in file-based scripts, better use {@link addItemChannelLink} to provide channel links. |
| 134 | + * |
| 135 | + * If an Item -> channel link is not provided by this script or the ManagedItemChannelLinkProvider, it is not editable and a warning is logged. |
| 136 | + * |
| 137 | + * @memberof items.itemChannelLink |
| 138 | + * @param {Item|string} itemOrName {@link Item} or the name of the Item |
| 139 | + * @param {string} channelUID |
| 140 | + * @param {object} [configuration] channel configuration |
| 141 | + * @returns {ItemChannelLink|null} the old ItemChannelLink or `null` if it did not exist |
| 142 | + */ |
| 143 | +function replaceItemChannelLink (itemOrName, channelUID, configuration) { |
| 144 | + const itemName = _getItemName(itemOrName); |
| 145 | + const itemChannelLink = getItemChannelLink(itemName, channelUID); |
| 146 | + return (itemChannelLink === null) ? addItemChannelLink(itemName, channelUID, configuration) : _updateItemChannelLink(itemName, channelUID, configuration); |
| 147 | +} |
| 148 | + |
| 149 | +/** |
| 150 | + * Removes a channel link from an Item. |
| 151 | + * |
| 152 | + * @memberof items.itemChannelLink |
| 153 | + * @param {Item|string} itemOrName {@link Item} or the name of the Item |
| 154 | + * @param {string} channelUID |
| 155 | + * @returns {ItemChannelLink|null} the removed ItemChannelLink or `null` if none exists, or it cannot be removed |
| 156 | + */ |
| 157 | +function removeItemChannelLink (itemOrName, channelUID) { |
| 158 | + const itemName = _getItemName(itemOrName); |
| 159 | + log.debug(`Removing ItemChannelLink ${itemName} -> ${channelUID} from registry...`); |
| 160 | + const itemChannelLink = itemChannelLinkRegistry.remove(itemName + ' -> ' + channelUID); |
| 161 | + if (itemChannelLink === null) return null; |
| 162 | + return new ItemChannelLink(itemChannelLink); |
| 163 | +} |
| 164 | + |
| 165 | +/** |
| 166 | + * Removes all channel links from the given Item. |
| 167 | + * |
| 168 | + * @memberof items.itemChannelLink |
| 169 | + * @param {string} itemName the name of the Item |
| 170 | + * @returns {number} number of links removed |
| 171 | + */ |
| 172 | +function removeLinksForItem (itemName) { |
| 173 | + return itemChannelLinkRegistry.removeLinksForItem(itemName); |
| 174 | +} |
| 175 | + |
| 176 | +/** |
| 177 | + * Removes all orphaned (Item or channel missing) links. |
| 178 | + * |
| 179 | + * @memberof items.itemChannelLink |
| 180 | + * @returns {number} number of links removed |
| 181 | + */ |
| 182 | +function removeOrphanedItemChannelLinks () { |
| 183 | + return itemChannelLinkRegistry.purge(); |
| 184 | +} |
| 185 | + |
| 186 | +module.exports = { |
| 187 | + getItemChannelLink, |
| 188 | + addItemChannelLink, |
| 189 | + replaceItemChannelLink, |
| 190 | + removeItemChannelLink, |
| 191 | + removeLinksForItem, |
| 192 | + removeOrphanedItemChannelLinks, |
| 193 | + ItemChannelLink |
| 194 | +}; |
0 commit comments