001package jmri.jmrix.ecos.utilities; 002 003import jmri.jmrix.ecos.EcosListener; 004import jmri.jmrix.ecos.EcosMessage; 005import jmri.jmrix.ecos.EcosReply; 006import jmri.jmrix.ecos.EcosTrafficController; 007 008public class RemoveObjectFromEcos implements EcosListener { 009 010 public RemoveObjectFromEcos() { 011 } 012 013 private String _ecosObject; 014 private int ecosretry; 015 016 private EcosTrafficController tc; 017 018 //Need to deal with the fact this method has a contructor name. 019 public void removeObjectFromEcos(String ecosObject, EcosTrafficController etc) { 020 tc = etc; 021 _ecosObject = ecosObject; 022 log.debug("Call to delete Object {} from the Ecos", ecosObject); 023 tc = etc; 024 String message = "request(" + _ecosObject + ", control, view)"; 025 EcosMessage m = new EcosMessage(message); 026 tc.sendEcosMessage(m, this); 027 } 028 029 @Override 030 public void reply(EcosReply m) { 031 032 String msg = m.toString(); 033 String[] lines = msg.split("\n"); 034 if(m.getResultCode()==0){ 035 if (lines[0].startsWith("<REPLY request(" + _ecosObject + ",")) { 036 deleteObject(); 037 } 038 } else if (m.getResultCode()==25){ 039 /** 040 * This section deals with no longer having control over the ecos 041 * loco object. we try three times to request control, on the fourth 042 * attempt we try a forced control, if that fails we inform the user 043 * and reset the counter to zero. 044 */ 045 log.info("We have no control over the ecos object {}Retry Counter = {}", _ecosObject, ecosretry); 046 retryControl(); 047 } 048 } 049 050 private void retryControl() { 051 if (ecosretry < 3) { 052 //It might be worth adding in a sleep/pause of description between retries. 053 ecosretry++; 054 055 String message = "request(" + _ecosObject + ", control)"; 056 EcosMessage ms = new EcosMessage(message); 057 tc.sendEcosMessage(ms, this); 058 log.error("JMRI has no control over the ecos object {}. Retrying Attempt {}", _ecosObject, ecosretry); 059 } //We do not want to do a force control over an object when we are trying to delete it, bad things might happen on the layout if we do this! 060 else { 061 jmri.util.swing.JmriJOptionPane.showMessageDialog(null, Bundle.getMessage("DeleteFromEcosWarning"), 062 Bundle.getMessage("WarningTitle"), jmri.util.swing.JmriJOptionPane.WARNING_MESSAGE); 063 ecosretry = 0; 064 } 065 } 066 067 private void deleteObject() { 068 EcosMessage m; 069 String message = "delete(" + _ecosObject + ")"; 070 m = new EcosMessage(message); 071 tc.sendEcosMessage(m, this); 072 } 073 074 @Override 075 public void message(EcosMessage m) { 076 // messages are ignored 077 } 078 079 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(RemoveObjectFromEcos.class); 080 081}