001package jmri.jmris.simpleserver;
002
003import java.io.DataInputStream;
004import java.io.DataOutputStream;
005import java.io.IOException;
006
007import jmri.InstanceManager;
008import jmri.Turnout;
009import jmri.jmris.AbstractTurnoutServer;
010import jmri.jmris.JmriConnection;
011import org.slf4j.Logger;
012import org.slf4j.LoggerFactory;
013
014/**
015 * Simple Server interface between the JMRI turnout manager and a network
016 * connection
017 *
018 * @author Paul Bender Copyright (C) 2010
019 */
020public class SimpleTurnoutServer extends AbstractTurnoutServer {
021
022    private static final String TURNOUT = "TURNOUT ";
023    private DataOutputStream output;
024    private JmriConnection connection;
025
026    public SimpleTurnoutServer(JmriConnection connection){
027        this.connection = connection;
028    }
029
030    public SimpleTurnoutServer(DataInputStream inStream,DataOutputStream outStream){
031        output = outStream;
032    }
033
034
035    /*
036     * Protocol Specific Abstract Functions
037     */
038    @Override
039    public void sendStatus(String turnoutName, int Status) throws IOException {
040        addTurnoutToList(turnoutName);
041        if (Status == Turnout.THROWN) {
042            this.sendMessage(TURNOUT + turnoutName + " THROWN\n");
043        } else if (Status == Turnout.CLOSED) {
044            this.sendMessage(TURNOUT + turnoutName + " CLOSED\n");
045        } else {
046            //  unknown state
047            this.sendMessage(TURNOUT + turnoutName + " UNKNOWN\n");
048        }
049    }
050
051    @Override
052    public void sendErrorStatus(String turnoutName) throws IOException {
053        this.sendMessage("TURNOUT ERROR\n");
054    }
055
056    @Override
057    public void parseStatus(String statusString) throws jmri.JmriException, java.io.IOException {        
058        
059        String turnoutName = statusString.split(" ")[1];
060        log.debug("status: {}", statusString);
061        if (statusString.contains("THROWN")) {
062            if (log.isDebugEnabled()) {
063                log.debug("Setting Turnout THROWN");
064            }
065            // create turnout if it does not exist since throwTurnout() no longer does so
066            this.initTurnout(turnoutName);
067            throwTurnout(turnoutName);
068        } else if (statusString.contains("CLOSED")) {
069            if (log.isDebugEnabled()) {
070                log.debug("Setting Turnout CLOSED");
071            }
072            // create turnout if it does not exist since closeTurnout() no longer does so
073            this.initTurnout(turnoutName);
074            closeTurnout(turnoutName);
075        } else {
076            // default case, return status for this turnout
077            try {
078                sendStatus(turnoutName,
079                    InstanceManager.turnoutManagerInstance().provideTurnout(turnoutName).getKnownState());
080            } catch (IllegalArgumentException ex) {
081                log.warn("Failed to provide Turnout \"{}\" in parseStatus", turnoutName);
082            }
083        }
084    }
085
086    private void sendMessage(String message) throws IOException {
087        if (this.output != null) {
088            this.output.writeBytes(message);
089        } else {
090            this.connection.sendMessage(message);
091        }
092    }
093
094    private static final Logger log = LoggerFactory.getLogger(SimpleTurnoutServer.class);
095}