So I put this in a DSL script and even executing the above line i get following error
[ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'testReadScript' failed: ___ import ___ java.io.File
try{
logInfo('Info', 'Info')
}
catch(Exception e){
logInfo('Error', 'Error')
}
1. The method or field import is undefined; line 1, column 0, length 6
2. This expression is not allowed in this context, since it doesn't cause any side effects.; line 1, column 7, length 12
I have tried the following
Use ECMA Script type i get a whole different error
Changed my JDK from AdoptOpenJDK to Azul Zulu
Restarts system, openhab service
Run in console (command prompt) the error is consistent.
Also don’t want to use the html static folder workaround and use http binding to read the file as it will be exposed to the http server.
I feel I am making some basic mistake as even using only import statement is giving error. Am i missing any automation / scripting add-ons. Can you please help on getting the script for reading of file in windows setup working.
If you are working in UI rules, you can’t do this with Rules DSL in this way because you can’t import anything. You have to import stuff before the rule and in a UI Script, there is no before the rule.
If you want to use UI rules with Rules DSL, you’ll have to be clever. If you are on Linux you can use cat.
val myFileContents = executeCommandLine(java.time.Duration.ofSeconds(1), 'cat /path/to/file')
Since I have a similar need I experimented a bit with writing a file from Javascript (ECMAScript-2021).
Create a script (or a rule) in MainUI and try this code:
// Creating a test file
var FileWriter = Java.type('java.io.FileWriter');
var BufferedWriter = Java.type('java.io.BufferedWriter');
var outFile = new FileWriter("deleteme.txt");
var out = new BufferedWriter(outFile);
out.write("Some text");
out.newLine();
out.flush();
//out.close();
out.append('more text');
out.newLine();
out.append('even more text');
out.close();
In my tests the file was created in the ‘userdata’ directory (since I did not explicitly specify a directory).
If you do not want to use the exec solution, this may help getting started with a plain JavaScript solution.
Thanks @Fleck this is what i needed. I have modified the code to read a file instead of writing.
Abstract code is as below in the ECMA Script in the Main UI
var scanner = Java.type('java.util.Scanner');
var file =Java.type('java.io.File');
var myFile = new file('..\\conf\\scripts\\new.txt'); //relative path to userdata folder
var myScanner = new scanner(myFile);
var line = "";
var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);
while (myScanner.hasNextLine()) {
line = myScanner.nextLine();
var fields = line.split("=");
logger.info(fields[0]);//Just got the first value
}