Skip to content
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

Advanced scripting help - reading file into array #51

Closed
m-elias opened this issue Mar 13, 2018 · 2 comments
Closed

Advanced scripting help - reading file into array #51

m-elias opened this issue Mar 13, 2018 · 2 comments
Assignees
Labels

Comments

@m-elias
Copy link

m-elias commented Mar 13, 2018

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
@kamilfb kamilfb self-assigned this Mar 24, 2018
@kamilfb
Copy link
Contributor

kamilfb commented Mar 24, 2018

@m-elias, try this:

// 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

@m-elias
Copy link
Author

m-elias commented Apr 17, 2018

Thanks @kamilfb! It works!

@m-elias m-elias closed this as completed Apr 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants