001package jmri.jmrix.ecos; 002 003import jmri.JmriException; 004import jmri.managers.AbstractPowerManager; 005 006import org.slf4j.Logger; 007import org.slf4j.LoggerFactory; 008 009/** 010 * PowerManager implementation for controlling ECoS layout power. 011 * 012 * @author Bob Jacobsen Copyright (C) 2001, 2008 013 */ 014public class EcosPowerManager extends AbstractPowerManager<EcosSystemConnectionMemo> implements EcosListener { 015 016 private EcosTrafficController tc; 017 018 public EcosPowerManager(EcosTrafficController etc) { 019 super(etc.adaptermemo); 020 // connect to the TrafficManager 021 tc = etc; 022 init(); 023 } 024 025 private void init() { 026 tc.addEcosListener(this); 027 028 // ask to be notified 029 EcosMessage m = new EcosMessage("request(1, view)"); 030 tc.sendEcosMessage(m, this); 031 032 // get initial state 033 m = new EcosMessage("get(1, status)"); 034 tc.sendEcosMessage(m, this); 035 036 } 037 038 @Override 039 public void setPower(int v) throws JmriException { 040 int old = power; 041 power = UNKNOWN; // while waiting for reply 042 checkTC(); 043 if (v == ON) { 044 // send message to turn on 045 EcosMessage l = new EcosMessage("set(1, go)"); 046 tc.sendEcosMessage(l, this); 047 } else if (v == OFF) { 048 // send message to turn off 049 EcosMessage l = new EcosMessage("set(1, stop)"); 050 tc.sendEcosMessage(l, this); 051 } 052 firePowerPropertyChange(old, power); 053 } 054 055 // to free resources when no longer used 056 @Override 057 public void dispose() throws JmriException { 058 tc.removeEcosListener(this); 059 tc = null; 060 } 061 062 private void checkTC() throws JmriException { 063 if (tc == null) { 064 throw new JmriException("attempt to use EcosPowerManager after dispose"); 065 } 066 } 067 068 // to listen for status changes from Ecos system 069 @Override 070 public void reply(EcosReply m) { 071 // power message? 072 String msg = m.toString(); 073 if (msg.contains("<EVENT 1>") || msg.contains("REPLY get(1,") || msg.contains("REPLY set(1,")) { 074 int old = power; 075 if (msg.contains("status[GO]") || msg.contains("et(1, go)")) { 076 log.debug("POWER ON DETECTED"); 077 power = ON; 078 } else if (msg.contains("status[STOP]") || msg.contains("et(1, stop)")) { 079 log.debug("POWER OFF DETECTED"); 080 power = OFF; 081 } 082 firePowerPropertyChange(old, power); 083 } 084 } 085 086 @Override 087 public void message(EcosMessage m) { 088 // messages are ignored 089 } 090 091 private final static Logger log = LoggerFactory.getLogger(EcosPowerManager.class); 092 093}