openhab-scripters / openhab-helper-libraries

Scripts and modules for use with openHAB
Eclipse Public License 1.0
88 stars 70 forks source link

timer is called inside a custom module the scope is lost #106

Closed lukicsl closed 5 years ago

lukicsl commented 5 years ago

Bug Description When a rule call function A in a custom module, function A sets up a Timer with a function B of the same custom module, function B accesses scope, an exception is thrown "No JSR223 scope is available"

To Reproduce custom module

from core.jsr223 import scope
from threading import Timer
import core

def executeTimed():
    try:
        itmState= scope.ir.getItem("ProxySwitch").state
    except:    
        core.JythonExtensionProvider = None
        import traceback
        log.error(traceback.format_exc())

def setTimer(timeout):
    Timer(timeout, lambda: executeTimed()).start()

rule (empty, good enough for run once)

import personal.myLib
reload(personal.myLib)
from  personal.myLib import setTimer

setTimer(2)

Expected behavior no exception is thrown

log

2019-05-17 21:09:15.779 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: personal/testScope.py
2019-05-17 21:09:17.831 [ERROR] [jsr223.jython.this.is.my.log        ] - Traceback (most recent call last):
  File "/openhab/conf/automation/lib/python/personal/myLib.py", line 45, in executeTimed
    itmState= scope.ir.getItem("ProxySwitch").state
  File "/openhab/conf/automation/lib/python/core/jsr223.py", line 28, in __getattr__
    scope = get_scope()
  File "/openhab/conf/automation/lib/python/core/jsr223.py", line 14, in get_scope
    raise EnvironmentError("No JSR223 scope is available")

Environment (please complete the following information):

5iver commented 5 years ago

Thank yiu for reporting this, but it is not an issue with the Jython helper libraries. I've commented in the forum with a solution...

https://community.openhab.org/t/global-name-events-is-not-defined-in-script/74554/9?u=5iver