Load script with common functions
Haringat opened this issue · comments
I am trying to use the scripting capabilities, but since my topics are rather large (but structured) I would like to be able to put something like a publish() function in a script file where I only need to fill in one part that changes and require that script in all my other script to reduce boilerplate.
So it would look something like this:
common.js
function publish(topic, payload) {
mqtt.publish("device1/cmd/${topic}/fmt/json", JSON.stringify(payload), 2, false);
}
foo.js
load("./common.js");
publish("foo", {
foo: "bar"
});
bar.js
load("./common.js");
publish("bar", {
baz: "foobar"
});
sadly, nashorn's load() function doesn't seem to work. Whenever that one is in the code, it fails. Or am I doing something wrong?
Ok I have found out how it works. You have to include the file: protocol and the full path e.g. on Windows something like
load("file:///C:/Path/to/scripts/common.js");