Skip to content

Commit 1d404cf

Browse files
committed
[items] Set event source for commands & updates
Refs openhab/openhab-core#5122. Refs openhab/openhab-core#5136. Signed-off-by: Florian Hotze <[email protected]>
1 parent 8c0f4ca commit 1d404cf

File tree

5 files changed

+55
-43
lines changed

5 files changed

+55
-43
lines changed

src/items/items.js

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ const ItemDTOMapper = Java.type('org.openhab.core.items.dto.ItemDTOMapper');
2929
const ItemDTO = Java.type('org.openhab.core.items.dto.ItemDTO');
3030
const GroupItemDTO = Java.type('org.openhab.core.items.dto.GroupItemDTO');
3131
const GroupFunctionDTO = Java.type('org.openhab.core.items.dto.GroupFunctionDTO');
32+
const AbstractEvent = Java.type('org.openhab.core.events.AbstractEvent');
33+
34+
const BusEventImplClassName = 'org.openhab.core.automation.module.script.internal.action.BusEventImpl';
3235

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

76+
let eventSource = null;
77+
78+
/**
79+
* Builds the event source for commands and updates.
80+
* Caches the computed source string.
81+
* @return {string}
82+
* @private
83+
*/
84+
function _buildEventSource () {
85+
if (eventSource) return eventSource;
86+
87+
const packageName = 'org.openhab.automation.jsscripting';
88+
const filename = globalThis['javax.script.filename']?.replace(/^.*[\\/]/, '');
89+
const ruleUID = globalThis.ruleUID;
90+
const engineIdentifier = globalThis['oh.engine-identifier'];
91+
if (filename) {
92+
eventSource = AbstractEvent.buildSource(packageName, `file:${filename}`);
93+
} else if (ruleUID) {
94+
eventSource = AbstractEvent.buildSource(packageName, `rule:${ruleUID}`);
95+
} else if (engineIdentifier.startsWith('openhab-transformation-script-')) {
96+
eventSource = AbstractEvent.buildSource(packageName,
97+
`transformation:${engineIdentifier.replaceAll('openhab-transformation-script-', '')}`);
98+
} else {
99+
eventSource = AbstractEvent.buildSource(packageName, `engine:${engineIdentifier}`);
100+
}
101+
return eventSource;
102+
}
103+
73104
/**
74105
* Class representing an openHAB Item
75106
*
@@ -354,7 +385,14 @@ class Item {
354385
}, expire.toMillis(), this, onExpire, value);
355386
cache.private.put(CACHE_KEY, timeoutId);
356387
}
357-
events.sendCommand(this.rawItem, _toOpenhabPrimitiveType(value));
388+
389+
if (Java.typeName(events.getClass()) === BusEventImplClassName) {
390+
// BusEventImpl supports providing command source
391+
events.sendCommand(this.rawItem, _toOpenhabPrimitiveType(value), _buildEventSource());
392+
} else {
393+
// old ScriptBusEventImpl doesn't support providing command source
394+
events.sendCommand(this.rawItem, _toOpenhabPrimitiveType(value));
395+
}
358396
}
359397

360398
/**
@@ -511,7 +549,13 @@ class Item {
511549
* @see sendCommand
512550
*/
513551
postUpdate (value) {
514-
events.postUpdate(this.rawItem, _toOpenhabPrimitiveType(value));
552+
if (Java.typeName(events.getClass()) === BusEventImplClassName) {
553+
// BusEventImpl supports providing update source
554+
events.postUpdate(this.rawItem, _toOpenhabPrimitiveType(value), _buildEventSource());
555+
} else {
556+
// old ScriptBusEventImpl doesn't support providing update source
557+
events.postUpdate(this.rawItem, _toOpenhabPrimitiveType(value));
558+
}
515559
}
516560

517561
/**

types/items/items.d.ts

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -143,44 +143,6 @@ export function replaceItem(itemConfig: ItemConfig): Item | null;
143143
* @returns {Item|null} the Item that has been removed or `null` if no Item has been found, or it cannot be removed
144144
*/
145145
export function removeItem(itemOrItemName: string | Item): Item | null;
146-
/**
147-
* @typedef {object} ItemConfig configuration describing an Item
148-
* @property {string} type the type of the Item
149-
* @property {string} name Item name for the Item to create
150-
* @property {string} [label] the label for the Item
151-
* @property {string} [category] the category (icon) for the Item
152-
* @property {string[]} [groups] an array of groups the Item is a member of
153-
* @property {string[]} [tags] an array of tags for the Item
154-
* @property {object} [group] group configuration, only applicable if type is `Group`
155-
* @property {string} group.type the type of the Group, such as `Switch` or `Number`
156-
* @property {string} [group.function] the group function, such as 'EQUALITY' or `AND`
157-
* @property {string[]} [group.parameters] optional parameters for the group function, e.g. `ON` and `OFF` for the `AND` function
158-
* @property {string|object} [channels] for single channel link a string or for multiple an object { channeluid: configuration }; configuration is an object
159-
* @property {*} [metadata] either object `{ namespace: value }` or `{ namespace: `{@link ItemMetadata}` }`
160-
* @property {string} [format] short form for the stateDescription metadata's pattern configuration
161-
* @property {string} [unit] short form for the unit metadata's value
162-
* @property {boolean} [autoupdate] short form for the autoupdate metadata's value
163-
*/
164-
/**
165-
* @typedef {import('./metadata').ItemMetadata} ItemMetadata
166-
* @private
167-
*/
168-
/**
169-
* @typedef {import('@js-joda/core').ZonedDateTime} ZonedDateTime
170-
* @private
171-
*/
172-
/**
173-
* @typedef {import('@js-joda/core').Instant} Instant
174-
* @private
175-
*/
176-
/**
177-
* @typedef {import('@js-joda/core').Duration} Duration
178-
* @private
179-
*/
180-
/**
181-
* @typedef {import('../quantity').Quantity} Quantity
182-
* @private
183-
*/
184146
/**
185147
* Class representing an openHAB Item
186148
*

types/items/items.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

types/quantity.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ export type Item = {
99
readonly state: string;
1010
readonly numericState: number;
1111
readonly quantityState: Quantity;
12-
readonly boolState: boolean;
12+
readonly boolState: boolean; /**
13+
* Convert this Quantity to the given unit.
14+
*
15+
* @param {string} unit
16+
* @returns {Quantity|null} a new Quantity with the given unit or `null` if conversion to this unit is not possible
17+
* @throws {QuantityError} when unit cannot be parsed because it is invalid
18+
*/
1319
readonly rawState: HostState;
1420
readonly previousState: string;
1521
readonly previousNumericState: number;

types/quantity.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)