001package jmri.jmris.simpleserver;
002
003import java.io.DataInputStream;
004import java.io.DataOutputStream;
005import java.io.IOException;
006
007import jmri.InstanceManager;
008import jmri.Sensor;
009import jmri.SensorManager;
010import jmri.jmris.AbstractSensorServer;
011import jmri.jmris.JmriConnection;
012
013/**
014 * Simple Server interface between the JMRI Sensor manager and a network
015 * connection
016 *
017 * @author Paul Bender Copyright (C) 2010
018 */
019public class SimpleSensorServer extends AbstractSensorServer {
020
021    private static final String SENSOR = "SENSOR ";
022    private DataOutputStream output;
023    private JmriConnection connection;
024
025    public SimpleSensorServer(JmriConnection connection){
026        super();
027        this.connection = connection;
028    }
029
030    public SimpleSensorServer(DataInputStream inStream, DataOutputStream outStream) {
031        super();
032        output = outStream;
033    }
034
035
036    /*
037     * Protocol Specific Abstract Functions
038     */
039    @Override
040    public void sendStatus(String sensorName, int Status) throws IOException {
041        addSensorToList(sensorName);
042
043        switch (Status) {
044            case Sensor.INACTIVE:
045                this.sendMessage(SENSOR + sensorName + " INACTIVE\n");
046                break;
047            case Sensor.ACTIVE:
048                this.sendMessage(SENSOR + sensorName + " ACTIVE\n");
049                break;
050            default:
051                this.sendMessage(SENSOR + sensorName + " UNKNOWN\n");
052                break;
053        }
054    }
055
056    @Override
057    public void sendErrorStatus(String sensorName) throws IOException {
058        this.sendMessage("SENSOR ERROR\n");
059    }
060
061    @Override
062    public void parseStatus(String statusString) throws jmri.JmriException, java.io.IOException {
063        int index;
064        index = statusString.indexOf(' ') + 1;
065        if (statusString.contains("INACTIVE")) {
066            log.debug("Setting Sensor INACTIVE");
067            initSensor(statusString.substring(index, statusString.indexOf(' ' , index + 1)));
068            setSensorInactive(statusString.substring(index, statusString.indexOf(' ', index + 1)));
069        } else if (statusString.contains("ACTIVE")) {
070            log.debug("Setting Sensor ACTIVE");
071            initSensor(statusString.substring(index, statusString.indexOf(' ', index + 1)));
072            setSensorActive(statusString.substring(index, statusString.indexOf(' ', index + 1)));
073        } else {
074            // default case, return status for this sensor/
075            String sensorName = statusString.substring(index);
076            if(sensorName.contains("\n")){
077                // remove anything following the newline
078                sensorName = sensorName.substring(0,sensorName.indexOf('\n'));
079            }
080            if( sensorName.contains(" ") ){
081                // remove anything following the space.
082                sensorName = sensorName.substring(0,sensorName.indexOf(' '));
083            }
084            try {
085                Sensor sensor = InstanceManager.getDefault(SensorManager.class).provideSensor(sensorName);
086                sendStatus(sensorName, sensor.getKnownState());
087            } catch (IllegalArgumentException ex) {
088                log.warn("Failed to provide Sensor \"{}\" in sendStatus {}", sensorName, ex.getMessage());
089            }
090        }
091    }
092
093    private void sendMessage(String message) throws IOException {
094        if (this.output != null) {
095            this.output.writeBytes(message);
096        } else {
097            this.connection.sendMessage(message);
098        }
099    }
100    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(SimpleSensorServer.class);
101}