|
| 1 | +/** |
| 2 | + * Functions for creating rules |
| 3 | + * |
| 4 | + * Copyright (c) 2019 Contributors to the openHAB Scripters project |
| 5 | + * |
| 6 | + * @author Jonathan Gilbert - initial contribution |
| 7 | + */ |
| 8 | + |
| 9 | +var OPENHAB_CONF = Java.type("java.lang.System").getenv("OPENHAB_CONF"); |
| 10 | +load(OPENHAB_CONF+'/automation/lib/javascript/core/osgi.js'); |
| 11 | + |
| 12 | + |
| 13 | +(function(context) { |
| 14 | + 'use strict'; |
| 15 | + |
| 16 | +var itemBuilderFactory = get_service( |
| 17 | + "org.openhab.core.items.ItemBuilderFactory" |
| 18 | + ) || get_service( |
| 19 | + "org.eclipse.smarthome.core.items.ItemBuilderFactory" |
| 20 | + ) |
| 21 | + |
| 22 | +var managedItemProvider = get_service( |
| 23 | + "org.openhab.core.items.ManagedItemProvider" |
| 24 | + ) || get_service( |
| 25 | + "org.eclipse.smarthome.core.items.ManagedItemProvider" |
| 26 | + ) |
| 27 | + |
| 28 | +var HashSet = Java.type("java.util.HashSet");; |
| 29 | + |
| 30 | + |
| 31 | + /* |
| 32 | + itemName (str): Item name for the Item to create |
| 33 | + itemType (str): (optional) the type of the Item |
| 34 | + category (str): (optional) the category (icon) for the Item |
| 35 | + groups (str): (optional) a list of groups the Item is a member of |
| 36 | + label (str): (optional) the label for the Item |
| 37 | + tags (list): (optional) a list of tags for the Item |
| 38 | + giBaseType (str): (optional) the group Item base type for the Item |
| 39 | + groupFunction (GroupFunction): (optional) the group function used by the Item |
| 40 | + |
| 41 | + returns: the item object, or undefined otherwise |
| 42 | + */ |
| 43 | +context.addItem = function(itemName, itemType, category, groups, label, tags, giBaseType, groupFunction) { |
| 44 | + var baseItem; |
| 45 | + if(itemType !== 'Group' && typeof(giBaseType) !== 'undefined') { |
| 46 | + baseItem = itemBuilderFactory.newItemBuilder(giBaseType, itemName + "_baseItem").build() |
| 47 | + } |
| 48 | + if(itemType !== 'Group') { |
| 49 | + groupFunction = undefined; |
| 50 | + } |
| 51 | + |
| 52 | + var builder = itemBuilderFactory.newItemBuilder(itemType, itemName). |
| 53 | + withCategory(category). |
| 54 | + withLabel(label). |
| 55 | + withGroups(groups); |
| 56 | + |
| 57 | + builder = builder.withTags(new HashSet(tags)); |
| 58 | + |
| 59 | + if(typeof baseItem !== 'undefined') { |
| 60 | + builder = builder.withBaseItem(baseItem); |
| 61 | + } |
| 62 | + if(typeof groupFunction !== 'undefined') { |
| 63 | + builder = builder.withGroupFunction(groupFunction); |
| 64 | + } |
| 65 | + |
| 66 | + var item = builder.build(); |
| 67 | + managedItemProvider.add(item); |
| 68 | + logDebug("Item added: " + item); |
| 69 | + return item; |
| 70 | +} |
| 71 | + |
| 72 | +context.removeItem = function(itemOrItemName) { |
| 73 | + var itemName; |
| 74 | + |
| 75 | + if(typeof itemOrItemName === 'string') { |
| 76 | + itemName = itemOrItemName; |
| 77 | + } else if(itemOrItemName.hasOwnProperty('name')) { |
| 78 | + itemName = itemOrItemName.name; |
| 79 | + } else { |
| 80 | + logWarn('Item not registered so cannot be removed'); |
| 81 | + return null; |
| 82 | + } |
| 83 | + |
| 84 | + if(getItem(itemName) === null) { |
| 85 | + logWarn('Item not registered so cannot be removed'); |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + managedItemProvider.remove(itemName); |
| 90 | + |
| 91 | + if(getItem(itemName) === null) { |
| 92 | + logDebug("Item removed: " + itemName); |
| 93 | + return itemName; |
| 94 | + } else { |
| 95 | + logWarn("Failed to remove item: " + itemName); |
| 96 | + return null; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +})(this); |
0 commit comments