Skip to content

Instantly share code, notes, and snippets.

@rblundon
Last active December 22, 2015 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rblundon/8bf66f643f5d1c6e0fa0 to your computer and use it in GitHub Desktop.
Save rblundon/8bf66f643f5d1c6e0fa0 to your computer and use it in GitHub Desktop.
#
# Author: Ryan Blundon, copyright 2013
#
#------
# Track Arrangement
#
# ---Track 4 (Sensor 787)----|-(TO2)\
# ---Track 3 (Sensor 786)----|--------(TO1)/---|-----Track 6 (Sensor 789)-----
# ---Track 2 (Sensor 785)----|-------(TO3)/
# ---Track 1 (Sensor 784)----|-----------
#
# Yard Throat is Sensor 788
import jarray
import jmri
from time import sleep
import java
import javax.swing
#------
# The Variables
#------
sensorList = ['NS784','NS785','NS786','NS787','NS789']
#------
# The Classes
#------
# Define the sensor listener: Print some
# information on the status change.
#class SensorListener(java.beans.PropertyChangeListener):
# def propertyChange(self, event):
# if (event.propertyName == "KnownState") :
# mesg = "Sensor "+event.source.systemName
# if (event.source.userName != None) :
# mesg += " ("+event.source.userName+")"
# mesg += " from "+stateName(event.oldValue)
# mesg += " to "+stateName(event.newValue)
# print mesg
# You can also speak the message by un-commenting the next line
#java.lang.Runtime.getRuntime().exec(["speak", mesg])
# For more info on the speak command, see http://espeak.sf.net/
# return
# Define a Manager listener. When invoked, a new
# item has been added, so go through the list of items removing the
# old listener and adding a new one (works for both already registered
# and new sensors)
#class ManagerListener(java.beans.PropertyChangeListener):
# def propertyChange(self, event):
# list = event.source.getSystemNameList()
# for i in range(list.size()) :
# event.source.getSensor(list.get(i)).removePropertyChangeListener(listener)
# event.source.getSensor(list.get(i)).addPropertyChangeListener(listener)
#------
# The Definitions
#------
# Define routine to map status numbers to text
#def stateName(state) :
# if (state == ACTIVE) :
# return "ACTIVE"
# if (state == INACTIVE) :
# return "INACTIVE"
# if (state == INCONSISTENT) :
# return "INCONSISTENT"
# if (state == UNKNOWN) :
# return "UNKNOWN"
# return "(invalid)"
def currentSensorState(x):
# Sensor return codes: 2 = ACITVE; 4 = INACTIVE
return sensors.getSensor(x).getKnownState()
def getLocoInfo(s):
# create a frame to hold the button, set up for nice layout
f = javax.swing.JFrame(s) # argument is the frames title
f.contentPane.setLayout(javax.swing.BoxLayout(f.contentPane, javax.swing.BoxLayout.Y_AXIS))
# keep track of whether both fields have been changed
addressChanged = False # True means the field has changed
commandChanged = False
# Create the first text field
# Sized to show 5 characters, initially empty
# To make the field a different size, change the (5) to the desired size
address = javax.swing.JTextField(5)
# put the text field on a line preceded by a label
temppanel1 = javax.swing.JPanel()
temppanel1.add(javax.swing.JLabel("Address"))
temppanel1.add(address)
# create the second text field similarly
radioBtn1 = javax.swing.JRadioButton("East")
radioBtn2 = javax.swing.JRadioButton("West")
temppanel2 = javax.swing.JPanel()
temppanel2.add(javax.swing.JLabel("Direction"))
temppanel2.add(radioBtn1)
temppanel2.add(radioBtn2)
#rbBtnGroup = ButtonGroup()
#rbBtnGroup.add(radioBtn1)
#rbBtnGroup.add(radioBtn2)
# # have that text field enable the button when OK
# def whenAddressChanged(event) :
# global addressChanged, commandChanged
# if (address.text != "") : # address only changed if a value was entered
# addressChanged = True
# if (commandChanged and addressChanged) : # if both have been changed
# enterButton.setEnabled(True)
# return
# address.actionPerformed = whenAddressChanged # if user hit return or enter
# address.focusLost = whenAddressChanged # if user tabs away
# have that 2nd text field enable the button when OK also
# def whenCommandChanged(event) :
# global addressChanged, commandChanged
# if (command.text != "") :
# commandChanged = True
# if (commandChanged and addressChanged) :
# enterButton.setEnabled(True)
# return
#command.actionPerformed = whenCommandChanged
#command.focusLost = whenCommandChanged
# create the button
enterButton = javax.swing.JButton("Enter values")
enterButton.setEnabled(True) # button starts as grayed out (disabled)
# define what button does when clicked and attach that routine to the button
def whenMyButtonClicked(event) :
if radioBtn1.isSelected():
statusText = "Radio Button 1 is selected "
if self.radioBtn2.isSelected():
statusText = statusText + "Radio Button 2 is selected "
myaddr = address.text
#mycmd = command.text
print "clicked with address: ", address.text#, " command: ", command.text
#print myaddr, mycmd
f.dispose()
return
enterButton.actionPerformed = whenMyButtonClicked
# Put contents in frame and display
f.contentPane.add(temppanel1)
f.contentPane.add(temppanel2)
f.contentPane.add(enterButton)
f.pack()
f.show()
#------
# The Program
#------
# First, check to make sure that the Yard Throat is clear
#while (currentSensorState('NS788') != 4):
# print "Yard throat not CLEAR, Sleeping for 5 seconds."
# sleep(5)
# Read Sensors
for i in sensorList:
if (currentSensorState(i) == 2):
print "Sensor ", i, " ACTIVE, prompting for locomotive address and direction."
getLocoInfo(i)
#print i, currentSensorState(i)
#listener = SensorListener()
# Attach the sensor manager listener
#sensors.addPropertyChangeListener(ManagerListener())
# For the sensors that exist, attach a sensor listener
#list = sensors.getSystemNameList()
# sensors.getSensor(list.get(i)).addPropertyChangeListener(listener)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment