You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm not sure where else to ask this question so please forgive if this is the wrong place.
I'm trying to modify a script I found https://github.com/kamilfb/mqtt-spy/issues/116 for bulk clearing retained messages. I would like to modify it to load a list of topics from a text file but I'm running into errors with the examples I'm finding online. I understand mqtt-spy uses Nashorn JS but I get a "readFully" is not defined error or fs.readFileSync is not a function. It works when I use the array. Here's what I have.
// Delete a bunch of retained messages
// Wrap the script in a method, so that you can do "return false;" in case of an error or stop request
function publish() {
var Thread = Java.type("java.lang.Thread");
var fs = "";
//var text = readFully("topiclist.txt");
var text = fs.readFileSync("D:/Matt/Dropbox/MQTT-Spy/mqtt-spy-userprofile/topiclist.txt").toString('utf-8');
var topics = text.split("\n")
// Define an array for topics we want to process
// var topics = [
// 'test/msg',
// 'test/status'
// ];
// Loop through each topic
topics.forEach(function(topic) {
// Publish blank payload with retain false
// removes retained messages
mqttspy.publish(topic, 'test', 0, false); // topic, payload, qos, retained
// Add some sleepy time and allow
// mqtt_spy to interrupt if needed
try {
Thread.sleep(100);
} catch(err) {
return false;
}
});
// This means all OK, script has completed without any issues and as expected
// return false if needed
return true;
}
publish();
//EOF
The text was updated successfully, but these errors were encountered:
// Delete a bunch of retained messages
// Wrap the script in a method, so that you can do "return false;" in case of an error or stop request
function publish() {
var Thread = Java.type("java.lang.Thread");
var File = Java.type("java.io.File");
var FileUtils = Java.type("org.apache.commons.io.FileUtils");
var topics = FileUtils.readLines(new File("/home/kamil/topics.txt"), "utf-8");
// Loop through each topic
topics.forEach(function(topic) {
// Publish blank payload with retain false
// removes retained messages
mqttspy.publish(topic, 'test', 0, false); // topic, payload, qos, retained
// Add some sleepy time and allow
// mqtt_spy to interrupt if needed
try {
Thread.sleep(100);
} catch(err) {
return false;
}
});
// This means all OK, script has completed without any issues and as expected
// return false if needed
return true;
}
publish();
//EOF
I'm not sure where else to ask this question so please forgive if this is the wrong place.
I'm trying to modify a script I found https://github.com/kamilfb/mqtt-spy/issues/116 for bulk clearing retained messages. I would like to modify it to load a list of topics from a text file but I'm running into errors with the examples I'm finding online. I understand mqtt-spy uses Nashorn JS but I get a "readFully" is not defined error or fs.readFileSync is not a function. It works when I use the array. Here's what I have.
The text was updated successfully, but these errors were encountered: