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] Add original input to event for file-based rules #334

Merged
merged 2 commits into from
May 31, 2024

Conversation

rkoshak
Copy link
Contributor

@rkoshak rkoshak commented May 31, 2024

[JSRule] Add original input from the event that triggered a rule to the JS event Object passed to file based rules

Description

There are circumstances where one may need to access the raw Java Objects from the event passed to a rule or to access variables passed into the rule by a calling rule. This PR adds the original contents of all the input passed to the rule as a raw property on the event Object passed into file based rules. Managed script actions and script condition always get the original and not the converted JS event Object.

For a usage example, to access the property foo passed from a calling rule, use event.raw.foo or event.raw['foo'].

Testing

To test I created a JS Rule that dumps the event Object.

rules.JSRule({
  name: 'Dump event Object',
  description: 'Logs out the event Object',
  triggers: [],
  execute: (event) => {
    utils.dumpObject(event, true);
  },
  id: 'dumpEvent',
  tags: ['Script']
});

I then tested by manually running the following from the -Scratchpad- Script:

rules.runRule('testEvent', {'foo':'bar', 'baz':'bim'}, true);

and verified that event.raw.foo is "bar" and event.raw.baz is "bim".

Next I exercised it from a rule that uses Thing trigger by instantiating an instance of https://community.openhab.org/t/thing-status-reporting-4-0-0-0-4-9-9-9/143180/ that calls this rule when ever a Thing changes status with a number of properties passed in. I then verified that the event.raw contains thingID, oldStatus, oldDetail, newStatus, newDetail and thing and each is the expected value. This second test was to ensure that the data gets passed both when a rule is called from a manually triggered rule and from an event triggered rule.

Solves #333

@rkoshak rkoshak requested a review from a team as a code owner May 31, 2024 19:59
Copy link
Contributor

@florian-h05 florian-h05 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

I will update the JSDoc and regenerate type defs and merge afterwards.-

Signed-off-by: Florian Hotze <[email protected]>
@florian-h05 florian-h05 added the enhancement New feature or request label May 31, 2024
@florian-h05 florian-h05 added this to the to be released milestone milestone May 31, 2024
@florian-h05 florian-h05 changed the title Add original input to event for file based rules [rules] Add original input to event for file based rules May 31, 2024
@florian-h05 florian-h05 changed the title [rules] Add original input to event for file based rules [rules] Add original input to event for file-based rules May 31, 2024
@florian-h05 florian-h05 merged commit d4ee454 into openhab:main May 31, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants