开云体育

Re: How to edit script, re-exec without restarting? #scripting


 

I don't know the best way to remove old listeners, but I have tested this and it seems to work.

sen = sensors.getSensor('LNT-Jython')

# See if the listener has been created.
try:
??? appListener
except NameError:
??? appListener = None

if appListener:
??? # remove listener from objects
??? sen.removePropertyChangeListener(appListener)

appListener = SomeListenerClass()

print 'Listener = {}'.format(appListener)

# add new listener to objects
sen.addPropertyChangeListener(appListener)

Jython variables that are not within a class are global for the duration of the JMRI session.

The first run will create the listener. ?Subsequent runs will remove the old listener and add a new one.

Dave Sand


----- Original message -----
From: stevemac <steveo@...>
Subject: [jmriusers] How to edit script, re-exec without restarting? #scripting
Date: Wednesday, March 19, 2025 11:06 AM

I'm working on a script to facilitate virtual block handling (time before unoccupied, etc) by considering time entered and exited and block length. Since every car being pulled draws power I can estimate how long it will be in a virtual block (assuming same speed).
?
The script is loaded at startup and adds property change listeners.
?
Since I'm learning JMRI I have to make incremental changes and every time I have to restart since the old listener is still active.
?
Maybe I could save my chained listener objects in a memory and check if it's there if I re-execute the script but wanted to check with experts first.
Is there any advice for avoiding a restart?

Join [email protected] to automatically receive all group messages.