001package jmri.jmrit.ussctc; 002 003import jmri.*; 004 005/** 006 * Drive the interactions of a code button and code light on the panel. 007 * <p> 008 * Primary interactions are with the common {@link CodeLine} and 009 * specific {@link Station} object. 010 * 011 * @author Bob Jacobsen Copyright (C) 2007, 2017 012 */ 013public class CodeButton { 014 015 /** 016 * Create and configure 017 * 018 * @param buttonSensor Name for Sensor that shows button press 019 * @param panelIndicator Name of Turnout that lights panel indicator 020 */ 021 public CodeButton(String buttonSensor, String panelIndicator) { 022 NamedBeanHandleManager hm = InstanceManager.getDefault(NamedBeanHandleManager.class); 023 TurnoutManager tm = InstanceManager.getDefault(TurnoutManager.class); 024 SensorManager sm = InstanceManager.getDefault(SensorManager.class); 025 026 hButtonSensor = hm.getNamedBeanHandle(buttonSensor, sm.provideSensor(buttonSensor)); 027 hPanelIndicator = hm.getNamedBeanHandle(panelIndicator, tm.provideTurnout(panelIndicator)); 028 029 sm.provideSensor(buttonSensor).addPropertyChangeListener((java.beans.PropertyChangeEvent e) -> {layoutSensorChanged(e);}); 030 } 031 032 /** 033 * Configure the Station connection for this CodeButton. 034 * <p> 035 * Note that {@link Station} normally invokes this automatically 036 * as part of its construction 037 * 038 * @param station A Station instance for this panel 039 * @return This CodeButton object to permit call linking 040 */ 041 CodeButton addStation(Station<?,?> station) { 042 this.station = station; 043 return this; 044 } 045 046 Station<?,?> station; 047 048 NamedBeanHandle<Sensor> hButtonSensor; 049 NamedBeanHandle<Turnout> hPanelIndicator; 050 051 void layoutSensorChanged(java.beans.PropertyChangeEvent e) { 052 if (e.getPropertyName().equals("KnownState") && e.getNewValue().equals(Sensor.ACTIVE) && !e.getOldValue().equals(Sensor.ACTIVE)) 053 codeButtonPressed(); 054 } 055 056 void codeButtonPressed() { 057 log.debug("Code button pressed"); 058 log.debug("Code light on - codeButtonPressed"); 059 hPanelIndicator.getBean().setCommandedState(Turnout.THROWN); 060 station.codeSendRequest(); 061 } 062 063 /** 064 * Code sequence done, turn off code light 065 */ 066 void codeValueDelivered() { 067 log.debug("Code light off - codeValueDelivered"); 068 hPanelIndicator.getBean().setCommandedState(Turnout.CLOSED); 069 } 070 071 /** 072 * Indication sequence starting, turn on code light 073 */ 074 void indicationStart() { 075 log.debug("Code light on - indicationStart"); 076 hPanelIndicator.getBean().setCommandedState(Turnout.THROWN); 077 } 078 079 /** 080 * Indication sequence done, turn off code light 081 */ 082 void indicationComplete() { 083 log.debug("Code light off - indicationComplete"); 084 hPanelIndicator.getBean().setCommandedState(Turnout.CLOSED); 085 } 086 087 088 private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CodeButton.class); 089}