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