001package jmri.jmrix.jmriclient; 002 003import jmri.JmriException; 004import jmri.PowerManager; 005import jmri.managers.AbstractPowerManager; 006 007/** 008 * PowerManager implementation for controlling layout power 009 * 010 * @author Bob Jacobsen Copyright (C) 2001, 2008 011 * @author Paul Bender Copyright (C) 2010 012 */ 013public class JMRIClientPowerManager extends AbstractPowerManager<JMRIClientSystemConnectionMemo> implements JMRIClientListener { 014 015 JMRIClientTrafficController tc = null; 016 017 public JMRIClientPowerManager(JMRIClientSystemConnectionMemo memo) { 018 super(memo); 019 // connect to the TrafficManager 020 tc = memo.getJMRIClientTrafficController(); 021 tc.addJMRIClientListener(this); 022 } 023 024 @Override 025 public void setPower(int v) throws JmriException { 026 int old = power; 027 power = UNKNOWN; // while waiting for reply 028 checkTC(); 029 if (v == ON) { 030 // send "Enable main track" 031 JMRIClientMessage l = JMRIClientMessage.getEnableMain(); 032 tc.sendJMRIClientMessage(l, this); 033 } else if (v == OFF) { 034 // send "Kill main track" 035 JMRIClientMessage l = JMRIClientMessage.getKillMain(); 036 tc.sendJMRIClientMessage(l, this); 037 } 038 firePowerPropertyChange(old, power); 039 } 040 041 // to free resources when no longer used 042 @Override 043 public void dispose() throws JmriException { 044 tc.removeJMRIClientListener(this); 045 tc = null; 046 } 047 048 private void checkTC() throws JmriException { 049 if (tc == null) { 050 throw new JmriException("attempt to use JMRIClientPowerManager after dispose"); 051 } 052 } 053 054 // to listen for status changes from JMRIClient system 055 @Override 056 public void reply(JMRIClientReply m) { 057 int old = power; 058 if (m.toString().contains("ON")) { 059 power = PowerManager.ON; 060 } else { 061 power = PowerManager.OFF; 062 } 063 firePowerPropertyChange(old, power); 064 } 065 066 @Override 067 public void message(JMRIClientMessage m) { 068 } 069 070}