001package jmri.jmrix.loconet.streamport;
002
003import java.io.DataInputStream;
004import java.io.DataOutputStream;
005import jmri.jmrix.loconet.LnCommandStationType;
006import jmri.jmrix.loconet.LnConnectionTypeList;
007import jmri.jmrix.loconet.LocoNetSystemConnectionMemo;
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010
011/**
012 * Base for classes representing a LocoNet communications port connected via
013 * streams.
014 *
015 * @author Bob Jacobsen Copyright (C) 2001, 2002
016 * @author Paul Bender Copyright (C) 2018
017 */
018public class LnStreamPortController extends jmri.jmrix.AbstractStreamPortController {
019
020    public LnStreamPortController(LocoNetSystemConnectionMemo connectionMemo, DataInputStream in, DataOutputStream out, String pname) {
021        super(connectionMemo, in, out, pname);
022        setManufacturer(LnConnectionTypeList.DIGITRAX);
023    }
024
025    public LnStreamPortController(DataInputStream in, DataOutputStream out, String pname) {
026        super(new LocoNetSystemConnectionMemo(), in, out, pname);
027        setManufacturer(LnConnectionTypeList.DIGITRAX);
028    }
029
030    public LnStreamPortController() {
031        super(new LocoNetSystemConnectionMemo());
032        setManufacturer(LnConnectionTypeList.DIGITRAX);
033    }
034
035    /**
036     * Check that this object is ready to operate. This is a question of
037     * configuration, not transient hardware status.
038     */
039    @Override
040    public boolean status(){
041       return true;
042    }
043
044    /**
045     * Set up all of the other objects to operate with a LocoNet connected via
046     * this class.
047     */
048    @Override
049    public void configure() {
050
051        // hardcode options for now
052        setCommandStationType(LnCommandStationType.COMMAND_STATION_STANDALONE);
053        setTurnoutHandling("");
054        // connect to a packetizing traffic controller
055        LnStreamPortPacketizer packets = new LnStreamPortPacketizer(this.getSystemConnectionMemo());
056        packets.connectPort(this);
057
058        // setup memo
059        this.getSystemConnectionMemo().setLnTrafficController(packets);
060        // do the common manager config
061        this.getSystemConnectionMemo().configureCommandStation(commandStationType,
062                mTurnoutNoRetry, mTurnoutExtraSpace, false, false, false); // never transponding or XPSlots
063        this.getSystemConnectionMemo().configureManagers();
064
065        // start operation
066        packets.startThreads();
067    }
068
069    /**
070     * Can the port accept additional characters? This might go false for short
071     * intervals, but it might also stick off if something goes wrong.
072     * <p>
073     * Provide a default implementation for the MS100, etc, in which this is
074     * _always_ true, as we rely on the queueing in the port itself.
075     * @return always true.
076     */
077    public boolean okToSend() {
078        return true;
079    }
080
081    protected LnCommandStationType commandStationType = null;
082
083    protected boolean mTurnoutNoRetry = false;
084    protected boolean mTurnoutExtraSpace = false;
085    protected boolean mInterrogateAtStart = false;
086
087    /**
088     * Set config info from the Command Station type enum.
089     * @param value Command Station Type, can be null while switching protocols. 
090     */
091    public void setCommandStationType(LnCommandStationType value) {
092        if (value == null) {
093            return;  // can happen while switching protocols
094        }
095        log.debug("setCommandStationType: {}", value); // NOI18N
096        commandStationType = value;
097    }
098
099    public void setTurnoutHandling(String value) {
100        if (value.equals("One Only") || value.equals(Bundle.getMessage("HandleOneOnly"))
101                || value.equals("Both") || value.equals(Bundle.getMessage("HandleBoth"))) {
102            mTurnoutNoRetry = true;
103        }
104        log.debug("turnout no retry: {}", mTurnoutNoRetry); // NOI18N
105        if (value.equals("Spread") || value.equals(Bundle.getMessage("HandleSpread"))
106                || value.equals("Both") || value.equals(Bundle.getMessage("HandleBoth"))) {
107            mTurnoutExtraSpace = true;
108        }
109        log.debug("turnout extra space: {}", mTurnoutExtraSpace); // NOI18N
110    }
111
112    /**
113     * Set whether to interrogate at startup
114     *
115     * @param value either yes or no
116     */
117    public void setInterrogateOnStart(String value) {
118        // default (most common state) is on, so just check for No
119        mInterrogateAtStart = !(value.equals("No") || value.equals(Bundle.getMessage("ButtonNo")));
120        log.debug("Interrogate at StartUp: {}", mInterrogateAtStart); // NOI18N
121    }
122
123    @Override
124    public LocoNetSystemConnectionMemo getSystemConnectionMemo() {
125        return (LocoNetSystemConnectionMemo) super.getSystemConnectionMemo();
126    }
127
128    private final static Logger log = LoggerFactory.getLogger(LnStreamPortController.class);
129
130}