001package jmri.jmrix.jmriclient;
002
003import jmri.implementation.AbstractReporter;
004import org.slf4j.Logger;
005import org.slf4j.LoggerFactory;
006
007/**
008 * JMRIClient implementation of the Reporter interface.
009 * <p>
010 *
011 * extend jmri.AbstractReporter for JMRIClient layouts
012 *
013 * @author Bob Jacobsen Copyright (C) 2001, 2008
014 * @author Paul Bender Copyright (C) 2010
015 */
016public class JMRIClientReporter extends AbstractReporter implements JMRIClientListener {
017
018    // data members
019    private int _number;   // reporter number
020    private JMRIClientTrafficController tc = null;
021    private String transmitName = null;
022
023    /**
024     * JMRIClient reporters use the reporter number on the remote host.
025     * @param number reporter number.
026     * @param memo system connection.
027     */
028    public JMRIClientReporter(int number, JMRIClientSystemConnectionMemo memo) {
029        super(memo.getSystemPrefix() + "R" + number);
030        transmitName = memo.getTransmitPrefix() + "R" + number;
031        _number = number;
032        tc = memo.getJMRIClientTrafficController();
033        // At construction, register for messages
034        tc.addJMRIClientListener(this);
035        // Then request status.
036        requestUpdateFromLayout();
037    }
038
039    public int getNumber() {
040        return _number;
041    }
042
043    public void requestUpdateFromLayout() {
044        // get the message text
045        String text = "REPORTER " + transmitName + "\n";
046
047        // create and send the message itself
048        tc.sendJMRIClientMessage(new JMRIClientMessage(text), this);
049    }
050
051    // to listen for status changes from JMRIClient system
052    @Override
053    public void reply(JMRIClientReply m) {
054        String message = m.toString();
055        log.debug("Message Received: {}", m);
056        log.debug("length {}", message.length());
057        if (!message.contains(transmitName + " ") &&
058            !message.contains(transmitName + "\n") &&
059            !message.contains(transmitName + "\r") ) {
060            return; // not for us
061        } else {
062            String text = "REPORTER " + transmitName + "\n";
063            if (!message.equals(text)) {
064                String report = message.substring(text.length());
065                log.debug("setting report to {}", report);
066                setReport(report);  // this is an update of the report.
067            } else {
068                log.debug("setting report to null");
069                setReport(null); // this is an update, but it is just 
070                // telling us the transient current 
071                // report is no longer valid.
072            }
073        }
074    }
075
076    @Override
077    public void message(JMRIClientMessage m) {
078    }
079
080    private int state = UNKNOWN;
081
082    @Override
083    public void setState(int s) {
084        state = s;
085    }
086
087    @Override
088    public int getState() {
089        return state;
090    }
091
092    private final static Logger log = LoggerFactory.getLogger(JMRIClientReporter.class);
093
094}