Skip to content

Update fact during runtime (OPC UA), then trigger rule evaluation #252

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

Closed
mikakaraila opened this issue Feb 17, 2021 · 6 comments
Closed

Comments

@mikakaraila
Copy link

I would like to use OPC UA and as I got new value I am trying to update fact first.

Then I want to run actual rules. So it will be more event based.

For some reason this snippet does not call actual addFact:

monitoredItemGroup.on("changed", (monitoredItem, dataValue, index) => {
          var tag = items[index].nodeId.toString();
          var val = dataValue.value.value.toString();
          if (dataValue.value.dataType === 10) {
              val = dataValue.value.value.toFixed(2).toString();
          }
          // update facts!!
          console.log("update facts:" + tag + " value: " + val);
          engine.addFact('pump-information', function (params, almanac) {
            console.log('Updating live values...' + JSON.stringify(params) + " " + JSON.stringify(almanac));
            return almanac.factValue('nodeId')
              .then((tag) => {
                console.log("Updating facts for nodeId: " + tag + " value:" + val);
                return { value: val }
              })
          });
      });
@CacheControl
Copy link
Owner

hey @mikakaraila sorry you're having problems. I wasn't familiar with OPC UA, it looks really neat.

I assume this is on node-red? We did have one issue filed a few years back on node-red, in which the following was mentioned:

Node-Red runs user-supplied code inside a JavaScript VM which upsets json-rules-engine in a few ways. The solution is to use a third-party unsafe code node for rules engine ops.

Any chance that means anything to you? Sorry, I can't be much use here. If you're able to zero in on what's going on, I'm happy to accept a PR.

@mikakaraila
Copy link
Author

I am not using node-red, this is just small experiment how to bind real-time data to facts.
I will refactor code and try other way. I will keep you updated as I found a way or propose PR.

@mikakaraila
Copy link
Author

I started to read code. Do you have example how to use runtimeFacts?
It is in the Almanac constructor. I suppose I need to turn on also allowUndefined facts.

@mikakaraila
Copy link
Author

OK, problem seems to be missing contructor for Almanac.

// Just to make instance is not working (cannot be constructed):
let testFact = new Fact({factId: "ns=1;s=TEST", value:{} });
let almanac = new Almanac( new Map(), testFact, {allowUndefinedFacts: true} );

image

Usage to update fact value:
// update facts!!
console.log("update facts:" + nodeId + " value: " + value);
almanac.addRuntimeFact({factId: nodeId, value: value});

@mikakaraila
Copy link
Author

mikakaraila commented Feb 18, 2021

I took copy and modified code a bit, now I have DYNAMIC facts. Have to study bit more how to use these:

image

@CacheControl
Copy link
Owner

Closing due to inactivity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants