So a while back I was working on a generic MQTT client app/driver that could create child devices on the hubitat hub for MQTT subscriptions. I got busy, so set it to the side, but pulled it out and was looking at it tonight.
What I did was have it load a CSV file that had the MQTT info in it - one child device per line (format is: mqtt path, child device type, name). Side note, it wouldn't have to be CSV, could be JSON or something else. I just stuck to what I know.
Is there a better/worse way to read in a file like a CSV and parse it? I did below (no laughing), which works, but seems really clunky?
byte[] filebytes = downloadHubFile(mqttFileName)
if(filebytes) {
//convert the bytes to a string
str1 = new String(filebytes);
String[] str2;
String[] str3;
//Split the string blob back into rows
str2 = str1.split('\n');
//Parse each row and split apart the comma separated values
for( String values : str2 ) {
log.debug "str2: " + values;
str3 = values.split(',');
log.debug "subs: " + str3[0];
log.debug "type: " + str3[1];
log.debug "name: " + str3[2];
// MQTT Subscribe
mqttDriver.subscribe(str3[0])
pauseExecution(100)
}
}
```