Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rules] Fix null-access when getting event information #296

Merged
merged 2 commits into from
Sep 17, 2023
Merged
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
33 changes: 18 additions & 15 deletions rules/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,18 +357,6 @@ function _getTriggeredData (input) {
const event = input.get('event');
const data = {};

// Always
data.eventClass = Java.typeName(event.getClass());
try {
if (event.getPayload()) {
data.payload = JSON.parse(event.getPayload());
log.debug('Extracted event payload {}', data.payload);
}
} catch (e) {
log.warn('Failed to extract payload: {}', e.message);
}
_addFromHashMap(input, 'module', data);

// Dynamically added properties, depending on their availability

// Item triggers
Expand All @@ -385,10 +373,23 @@ function _getTriggeredData (input) {
_addFromHashMap(input, 'newStatus', data);
_addFromHashMap(input, 'status', data);

// The source code of the trigger handlers provide an insight into the respective events,
// see https://github.com/openhab/openhab-core/tree/main/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/handler
// Properties added if event is available

if (event) {
switch (Java.typeName(event.getClass())) {
data.eventClass = Java.typeName(event.getClass());

try {
if (event.getPayload()) {
data.payload = JSON.parse(event.getPayload());
log.debug('Extracted event payload {}', data.payload);
}
} catch (e) {
log.warn('Failed to extract payload: {}', e.message);
}

// The source code of the trigger handlers provide an insight into the respective events,
// see https://github.com/openhab/openhab-core/tree/main/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/handler
switch (data.eventClass) {
case 'org.openhab.core.automation.events.ExecutionEvent':
data.eventType = event.toString().split(' ').pop();
break;
Expand Down Expand Up @@ -465,6 +466,8 @@ function _getTriggeredData (input) {
}
}

_addFromHashMap(input, 'module', data);

return data;
}

Expand Down
Loading