Modification for JSR223 addon (fix no file found & getRules() not found BUG)

Edit: add Utility Script. You can easily use script between rules.

Edit: add an action for the script. You can ask the engine to reload your triggers.
you can see the WIKI for how to implement it in rule.

I have made a small modification to the JSR223 engine. It should fix the “file not found” and “can’t found getRules()” (still need to make a permanent fix)

It seem to work for me. Feel free to test it.

https://github.com/dodger777/openhabMod/blob/1.8/compiledAddon/org.openhab.core.jsr223-1.8.2-SNAPSHOT.jar

Joel Perron

ps: you need to clic on RAW

The 1 second sleep will prevent the problem in most cases but not all. For example, a large file over a slow connection might take longer than that.

Here’s another approach…

Do you have a link to “file not found” error reports? It seems odd that a file change event would be raised for a non-existing file.

Also, the title was a bit misleading for me. If there’s no getRules function in a script then an exception is thrown. I think that scripts shouldn’t have that requirement and that utility scripts should also be allowed in the scripts directory. They currently can be put in directories outside the scripts directory but then they are not automatically reloaded when they change. Based on the title, I had thought this code modification changed that behavior, but I see it doesn’t.

Yes, I will check for a better solution.maybe if the file got an error, put it in a retry mode after x seconds.

If the file have not finish writing, you often got en error that it do not have getRules ()
And in very rare case, you also get a file not found exception.

I am almost certain it’s from the same problem.

.
here it is, it is for the regular script, but i have the same bug with the jsr223.( i must say that the FileNotFound is very rare, i got most often the error that it do not have getRules() )

What operating system are you using to run OpenHAB?

windows 7

Thanks. It sounds like a bug in the Java file event handling for Windows 7. I’ve never seen it on Linux (RPi / Java 8). It’s strange though, I haven’t been able to find any other reports of the issue on the net (in general, not just in an OpenHAB context).

i added an action to reload a rule trigger.

No need to “touch” the file anymore to reload your new triggers.

Joel

this could be a good option. I will put that on my to do list. Maybe a way to use Utility script from any other script, and that utility script could have the possibility to retain global var.

Which script language are you using? I’m using Jython.

I am using groovy. but any script language should work with my Mod.

Agreed, but there may be differing issues related to “global” variable management.

why not use a map. with 2 function. setGlobal(someMap) and getGlobal () on utility script. if file need reload, you get the old global with getGlobal (), load new script, and set the new script global var with setGlobal (oldMap)

if utility script are keep in the jsr engine then any static variable in the script will be global between use.

Hello,

i uploaded a version with Utility Script. Those script are loaded when put in the script directory.
You dont need a getRules(), but you do need

UtilitySet getUtilityScripts() {
    return new UtilitySet(new MyUtilityClass())
}

I will modify my wiki with an example.

It do not include a global variable…yet.
If you modify the file, it will reload the new script in memory, and you will loose any variable set that you would have made. But Your script will use this new utility script if you refresh your variable before using it.

Why is getUtilityScripts() needed? It doesn’t seem like it would be needed for Jython. I often create utility-like scripts with an empty getRules() (just to satisfy the script loader).

to load the script in memory, I need something to get the class in it. only the class pass in that function will be directly available. There may be another way… but i don’t know how.

this way. your utility script are manage in the engine. I would be able to retain global variable on file change.

I was able to remove the getTriggers need.

you can see the wiki for an example.

ps: it open also a possibility to compile the script on load.

It appears it’s more complicated to use utility scripts in Groovy than in Jython. With Jython, the “script” is a Python module that becomes part of the global namespace when it is loaded. Any other Python module can import that module and use the classes, functions and so on without any special OH support. In other words, for my purposes it would be fine to just remove the requirement to have a getRules() function in the script.

For “global” data I use an empty Python module that is not reloaded automatically. The other scripts can store data in this module and continue to access it after they have been reloaded.

It would be useful if a script/module could optionally define a function to be called before the script is removed or reloaded so that it can clean up global data or OH components it has registered (e.g., Python-based bindings or item providers). I’ve been able to work around not having that feature but it makes the code more complicated.

ok, but if I remove that requirement it will never tell you if you forgot to put that in regular rule script. What I could do is ignore some file base on naming.
something like MyUtilityScript.utility.jy
those script will not be load by OH. What do you think?

that should be easy enough. for script loaded by OH.

That’s a good point. Personally, I’d rather see a warning in the log file than be required to use special file naming.

Hello,

sorry for the delay, i had made a mod to remove the full error description, and its now only a warn.
I’ve broken the regular error, its now fix at the same time.

Joel