001package jmri.jmrix.dccpp;
002
003import org.slf4j.Logger;
004import org.slf4j.LoggerFactory;
005
006/**
007 * Base for classes representing a DCCpp communications port
008 *
009 * @author Kevin Dickerson Copyright (C) 2011
010 * @author Mark Underwoodn Copyright (C) 2015
011 *
012 * Based o LnNetworkPortController by Kevin Dickerson
013 */
014public abstract class DCCppNetworkPortController extends jmri.jmrix.AbstractNetworkPortController implements DCCppPortController {
015    // base class. Implementations will provide InputStream and OutputStream
016    // objects to LnTrafficController classes, who in turn will deal in messages.
017
018    private final static Logger log = LoggerFactory.getLogger(DCCppNetworkPortController.class);
019    
020    protected DCCppNetworkPortController() {
021        super(new DCCppSystemConnectionMemo());
022        setManufacturer(DCCppConnectionTypeList.DCCPP);
023        allowConnectionRecovery = true;
024    }
025    
026    protected DCCppNetworkPortController(DCCppSystemConnectionMemo connectionMemo) {
027        super(connectionMemo);
028        setManufacturer(DCCppConnectionTypeList.DCCPP);
029    }
030
031    protected int commandStationType = 0;
032
033    protected boolean mTurnoutNoRetry = false;
034    protected boolean mTurnoutExtraSpace = false;
035
036    protected int[] commandStationTypes = {
037        DCCppConstants.DCCPP_UNO_1_0,
038        DCCppConstants.DCCPP_ARDUINO_1_1
039    };
040    
041    protected String[] commandStationNames;
042    
043    {
044        commandStationNames = new String[commandStationTypes.length];
045        for (int i = 0; i < commandStationTypes.length; i++) {
046            commandStationNames[i] = DCCppConstants.CommandStationNames[i];
047        }
048    }
049    
050    /**
051     * Set config info from a name, which needs to be one of the valid ones.
052     * @param name exact name of command station type.
053     */
054    public void setCommandStationType(String name) {
055        for (int i = 0; i < commandStationNames.length; i++) {
056            if (commandStationNames[i].matches(name)) {
057                commandStationType = i;
058                return;
059            }
060        }
061        log.error("CommandStation Type not found: {}", name);
062        commandStationType = 0;
063    }
064    
065    /**
066     * Set config info from the command station type enum.
067     * @param value command station type.
068     */
069    public void setCommandStationType(int value) {
070        log.debug("setCommandStationType: {}", Integer.toString(value));
071        commandStationType = value;
072    }
073    
074    @Override
075    public DCCppSystemConnectionMemo getSystemConnectionMemo() {
076        return (DCCppSystemConnectionMemo) super.getSystemConnectionMemo();
077    }
078
079    public void setTurnoutHandling(String value) {
080        if (value.equals("One Only") || value.equals("Both")) {
081            mTurnoutNoRetry = true;
082        }
083        if (value.equals("Spread") || value.equals("Both")) {
084            mTurnoutExtraSpace = true;
085        }
086        log.debug("turnout no retry: {}", mTurnoutNoRetry);
087        log.debug("turnout extra space: {}", mTurnoutExtraSpace);
088    }
089
090    /**
091     * Set the third port option. Only to be used after construction, but before
092     * the openPort call
093     */
094    @Override
095    public void configureOption3(String value) {
096        super.configureOption3(value);
097        log.debug("configureOption3: {}", value);
098        setTurnoutHandling(value);
099    }
100
101
102     /**
103     * Check that this object is ready to operate. This is a question of
104     * configuration, not transient hardware status.
105     */
106    @Override
107    public abstract boolean status();
108
109    /**
110     * Can the port accept additional characters? This might go false for short
111     * intervals, but it might also stick off if something goes wrong.
112     */
113    @Override
114    public abstract boolean okToSend();
115
116    @Override
117    public void setOutputBufferEmpty(boolean s) {
118    } // Maintained for compatibility with DCCpptPortController. Simply ignore calls !!!
119
120    /**
121     * Customizable method to deal with resetting a system connection after a
122     * successful recovery of a connection.
123     */
124    @Override
125    protected void resetupConnection() {
126        this.getSystemConnectionMemo().getDCCppTrafficController().connectPort(this);
127    }
128
129}
130
131
132