openhab / openhab-js

openHAB JavaScript Library for JavaScript Scripting Automation
https://www.openhab.org/addons/automation/jsscripting/
Eclipse Public License 2.0
38 stars 31 forks source link

[rules] Add original input to event for file-based rules #334

Closed rkoshak closed 4 weeks ago

rkoshak commented 4 weeks ago

[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