Skip to content
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
48 changes: 46 additions & 2 deletions src/items/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ const ItemDTOMapper = Java.type('org.openhab.core.items.dto.ItemDTOMapper');
const ItemDTO = Java.type('org.openhab.core.items.dto.ItemDTO');
const GroupItemDTO = Java.type('org.openhab.core.items.dto.GroupItemDTO');
const GroupFunctionDTO = Java.type('org.openhab.core.items.dto.GroupFunctionDTO');
const AbstractEvent = Java.type('org.openhab.core.events.AbstractEvent');

const BusEventImplClassName = 'org.openhab.core.automation.module.script.internal.action.BusEventImpl';

// typedefs need to be global for TypeScript to fully work
/**
Expand Down Expand Up @@ -70,6 +73,34 @@ const GroupFunctionDTO = Java.type('org.openhab.core.items.dto.GroupFunctionDTO'
* @private
*/

let eventSource = null;

/**
* Builds the event source for commands and updates.
* Caches the computed source string.
* @private
* @return {string}
*/
function _buildEventSource () {
if (eventSource) return eventSource;

const packageName = 'org.openhab.automation.jsscripting';
const filename = globalThis['javax.script.filename']?.replace(/^.*[\\/]/, '');
const ruleUID = globalThis.ruleUID;
const engineIdentifier = globalThis['oh.engine-identifier'];
if (filename) {
eventSource = AbstractEvent.buildSource(packageName, `file:${filename}`);
} else if (ruleUID) {
eventSource = AbstractEvent.buildSource(packageName, `rule:${ruleUID}`);
} else if (engineIdentifier.startsWith('openhab-transformation-script-')) {
eventSource = AbstractEvent.buildSource(packageName,
`transformation:${engineIdentifier.replaceAll('openhab-transformation-script-', '')}`);
} else {
eventSource = AbstractEvent.buildSource(packageName, `engine:${engineIdentifier}`);
}
return eventSource;
}

/**
* Class representing an openHAB Item
*
Expand Down Expand Up @@ -354,7 +385,14 @@ class Item {
}, expire.toMillis(), this, onExpire, value);
cache.private.put(CACHE_KEY, timeoutId);
}
events.sendCommand(this.rawItem, _toOpenhabPrimitiveType(value));

if (Java.typeName(events.getClass()) === BusEventImplClassName) {
// BusEventImpl supports providing command source
events.sendCommand(this.rawItem, _toOpenhabPrimitiveType(value), _buildEventSource());
} else {
// old ScriptBusEventImpl doesn't support providing command source
events.sendCommand(this.rawItem, _toOpenhabPrimitiveType(value));
}
}

/**
Expand Down Expand Up @@ -511,7 +549,13 @@ class Item {
* @see sendCommand
*/
postUpdate (value) {
events.postUpdate(this.rawItem, _toOpenhabPrimitiveType(value));
if (Java.typeName(events.getClass()) === BusEventImplClassName) {
// BusEventImpl supports providing update source
events.postUpdate(this.rawItem, _toOpenhabPrimitiveType(value), _buildEventSource());
} else {
// old ScriptBusEventImpl doesn't support providing update source
events.postUpdate(this.rawItem, _toOpenhabPrimitiveType(value));
}
}

/**
Expand Down
38 changes: 0 additions & 38 deletions types/items/items.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,44 +143,6 @@ export function replaceItem(itemConfig: ItemConfig): Item | null;
* @returns {Item|null} the Item that has been removed or `null` if no Item has been found, or it cannot be removed
*/
export function removeItem(itemOrItemName: string | Item): Item | null;
/**
* @typedef {object} ItemConfig configuration describing an Item
* @property {string} type the type of the Item
* @property {string} name Item name for the Item to create
* @property {string} [label] the label for the Item
* @property {string} [category] the category (icon) for the Item
* @property {string[]} [groups] an array of groups the Item is a member of
* @property {string[]} [tags] an array of tags for the Item
* @property {object} [group] group configuration, only applicable if type is `Group`
* @property {string} group.type the type of the Group, such as `Switch` or `Number`
* @property {string} [group.function] the group function, such as 'EQUALITY' or `AND`
* @property {string[]} [group.parameters] optional parameters for the group function, e.g. `ON` and `OFF` for the `AND` function
* @property {string|object} [channels] for single channel link a string or for multiple an object { channeluid: configuration }; configuration is an object
* @property {*} [metadata] either object `{ namespace: value }` or `{ namespace: `{@link ItemMetadata}` }`
* @property {string} [format] short form for the stateDescription metadata's pattern configuration
* @property {string} [unit] short form for the unit metadata's value
* @property {boolean} [autoupdate] short form for the autoupdate metadata's value
*/
/**
* @typedef {import('./metadata').ItemMetadata} ItemMetadata
* @private
*/
/**
* @typedef {import('@js-joda/core').ZonedDateTime} ZonedDateTime
* @private
*/
/**
* @typedef {import('@js-joda/core').Instant} Instant
* @private
*/
/**
* @typedef {import('@js-joda/core').Duration} Duration
* @private
*/
/**
* @typedef {import('../quantity').Quantity} Quantity
* @private
*/
/**
* Class representing an openHAB Item
*
Expand Down
2 changes: 1 addition & 1 deletion types/items/items.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.