kamilfb / mqtt-spy

Please use the new Eclipse Paho GitHub repo instead.
https://github.com/eclipse/paho.mqtt-spy/wiki
345 stars 68 forks source link

Load script with common functions #127

Closed Haringat closed 6 years ago

Haringat commented 6 years ago

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?

Haringat commented 6 years ago

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");