Keyboard Shortcuts
Likes
Search
Locked
jmri_bindings.py
#scripting
The jmri_bindings.py file has been obsolete for a while, so I¡¯m not sure what that part of your question is asking
It¡¯s easier to handle a throttle within AbstractAutomaton because there¡¯s specific code there to handle the necessary waits and callbacks. Unlike a turnout operation, which is ¡°set the command and go on with what you¡¯re doing¡±, throttle operations require back-and-forth with the hardware. That can be directly coded in a script, but it¡¯s tricky to get right. Bob On Jan 5, 2020, at 9:00 AM, James Anderson <james_anderson_999@...> wrote:-- Bob Jacobsen rgj1927@... |
Sorry to come back on this again.
I have a small script? import routes
route=routes.testRoute1()
route.setTurnoutSensor("ISTS-1",ACTIVE) and on disk I have this module called routes shown at the bottom of this note.? If i use self.sensor=sensors.provideSensor(self.turnoutSensorName) I get?NameError: global name 'sensors' is not defined but it works with jmri.InstanceManager.getDefault(jmri.SensorManager).provideSensor(self.turnoutSensorName)? Is there something else I should be loading? ?import jmri
?class automationRoutesBaseClass(jmri.jmrit.automat.AbstractAutomaton) :
? ? def init(self):
? ? ? ? return
? ? def handle(self):
? ? ? ?return 0
? ? def setTurnoutSensor(self,turnoutSensorName,state):
? ? ? ? self.turnoutSensorName=turnoutSensorName
? ? ? ? self.turnoutState=state
#? ? ? self.sensor=jmri.InstanceManager.getDefault(jmri.SensorManager).provideSensor(self.turnoutSensorName)
? ? ? ? self.sensor=sensors.provideSensor(self.turnoutSensorName)
? ? ? ? if self.sensor !=None :
? ? ? ? ? ?self.sensor.setState(self.turnoutState)
? ? ? ? else:
? ? ? ? ? print("error unable to obtain sensor object for "+str(self.turnoutSensorName))
? ? ? ? return
?#-----------------------------------------------------------------------------------
?# Class? :? define a route. Inherits methods from automationRoutesBaseClass
?#-----------------------------------------------------------------------------------
?class testRoute1(automationRoutesBaseClass) :
? ? def init(self):
? ? ? ? return
? ? def handle(self):
? ? ? ? return
? ? def __init__(self,routeDescription=None,routeName=""):
? ? ? ? self.routeDescription=routeDescription
? ? ? ? self.routeName=routeName
? ? ? ? automationRoutesBaseClass.__init__(self)
? ? ? ? return
?
|
James, By importing your module you lose the global variables. An alternate approach is to load the module instead of import and modify the route line. import jmri execfile(jmri.util.FileUtil.getExternalFilename("preference:routes.py")) route=testRoute1() route.setTurnoutSensor("ISTS-1",ACTIVE) Dave Sand ----- Original message ----- From: James Anderson <james_anderson_999@...> Subject: Re: [jmriusers] jmri_bindings.py Date: Monday, January 06, 2020 7:56 AM Sorry to come back on this again. I have a small script? import routes route=routes.testRoute1() route.setTurnoutSensor("ISTS-1",ACTIVE) and on disk I have this module called routes shown at the bottom of this note.? If i use self.sensor=sensors.provideSensor(self.turnoutSensorName) I get?NameError: global name 'sensors' is not defined but it works with jmri.InstanceManager.getDefault(jmri.SensorManager).provideSensor(self.turnoutSensorName)? Is there something else I should be loading? ?import jmri ?class automationRoutesBaseClass(jmri.jmrit.automat.AbstractAutomaton) : ? ? def init(self): ? ? ? ? return ? ? def handle(self): ? ? ? ?return 0 ? ? def setTurnoutSensor(self,turnoutSensorName,state): ? ? ? ? self.turnoutSensorName=turnoutSensorName ? ? ? ? self.turnoutState=state #? ? ? self.sensor=jmri.InstanceManager.getDefault(jmri.SensorManager).provideSensor(self.turnoutSensorName) ? ? ? ? self.sensor=sensors.provideSensor(self.turnoutSensorName) ? ? ? ? if self.sensor !=None : ? ? ? ? ? ?self.sensor.setState(self.turnoutState) ? ? ? ? else: ? ? ? ? ? print("error unable to obtain sensor object for "+str(self.turnoutSensorName)) ? ? ? ? return ?#----------------------------------------------------------------------------------- ?# Class? :? define a route. Inherits methods from automationRoutesBaseClass ?#----------------------------------------------------------------------------------- ?class testRoute1(automationRoutesBaseClass) : ? ? def init(self): ? ? ? ? return ? ? def handle(self): ? ? ? ? return ? ? def __init__(self,routeDescription=None,routeName=""): ? ? ? ? self.routeDescription=routeDescription ? ? ? ? self.routeName=routeName ? ? ? ? automationRoutesBaseClass.__init__(self) ? ? ? ? return ? |
Thanks Dave.?
unfortunately I also use this function so that I can have a dynamic name which gets in the way. Is there a way to? have testRoute1 as a string, something like route=eval("testRoute1()") ? ? ? ? def importClass(self,className):
? ? ? ? ? self.name=className
? ? ? ? ? self.components = self.name.split('.')
? ? ? ? ? self.module = __import__(self.components[0])
? ? ? ? ? for self.component in self.components[1:]:
? ? ? ? ? ? ?self.module=getattr(self.module, self.component)
? ? ? ? ? return self.module
? ? ? #---------------------------------------------------------------------------------------------------------
? ? ? # set the route for this driver
? ? ? #---------------------------------------------------------------------------------------------------------
? ? ? def setRoute(self,route,throttle):
? ? ? ? ? self.route_name="automationRoutes."+route
? ? ? ? ? print("setting route to "+self.route_name)
? ? ? ? ? self.route=self.importClass(self.route_name)
? ? ? ? ? self.route_instance=self.route
? ? ? ? ? self.route_instance.throttle=throttle
? ? ? ? ? return self.route_instance()
? |
Randall Wood
That
Put another way, if a script does:
For historical reasons, all scripts run from the Panels->Run Script... menu item, and as startup items, run in the same global state (scripts run from other triggers may or may not share that state, I don't necessarily know), so there is no assurance, other than running that script by |
Hi sorry to reopen this discussion. ? In jmri v 4.16 I had two ways I called a program ?execfile(self.cmdR) ?or globals()[self.cmdR](self.client_address,**self.args). I use execfile when developing my code because I could change the self.cmdR program and the changes would be picked up without me having to restart jmri. ?I used global in production because it was muchmuch? faster than execfile. This file is called multiple times and therefore using globals() gave ?me a significant increase in performance.? With 4.18 which I have just installed, when using the globals approach none of the global ?jmri names (sensors, blocks etc) are recognised. This I fixed by defining them long hand (sensors????? = jmri.InstanceManager.getDefault(jmri.SensorManager etc.) I am happy to do this but just want to reassure myself that by doing so I am not creating a massive problem for the future. ? ? Thanks Jim |