001package jmri.jmrix.oaktree.serialmon;
002
003import jmri.jmrix.oaktree.SerialListener;
004import jmri.jmrix.oaktree.SerialMessage;
005import jmri.jmrix.oaktree.SerialReply;
006import jmri.jmrix.oaktree.OakTreeSystemConnectionMemo;
007
008/**
009 * Frame displaying (and logging) serial command messages.
010 *
011 * @author Bob Jacobsen Copyright (C) 2001, 2006
012 */
013public class SerialMonFrame extends jmri.jmrix.AbstractMonFrame implements SerialListener {
014
015    private OakTreeSystemConnectionMemo _memo = null;
016
017    public SerialMonFrame(OakTreeSystemConnectionMemo memo) {
018        super();
019        _memo = memo;
020    }
021
022    @Override
023    protected String title() {
024        return Bundle.getMessage("MonitorXTitle", "OakTree");
025    }
026
027    @Override
028    protected void init() {
029        // connect to TrafficController
030        _memo.getTrafficController().addSerialListener(this);
031    }
032
033    @Override
034    public void dispose() {
035        _memo.getTrafficController().removeSerialListener(this);
036        super.dispose();
037    }
038
039    @Override
040    public synchronized void message(SerialMessage l) {  // receive a message and log it
041        // check for valid length
042        if (l.getNumDataElements() < 5) {
043            nextLine("Truncated message of length " + l.getNumDataElements() + "\n", // TODO I18N
044                    l.toString());
045        } else if (l.isPoll()) {
046            nextLine("Poll addr=" + l.getAddr() + "\n", l.toString());
047        } else if (l.isXmt()) {
048            String s = "Transmit addr=" + l.getAddr()
049                    + " byte " + (l.getElement(2) & 0x000000ff)
050                    + " data = " + Integer.toHexString(l.getElement(3) & 0xff);
051            nextLine(s + "\n", l.toString());
052        } else {
053            nextLine("Unrecognized cmd: \"" + l.toString() + "\"\n", l.toString());
054        }
055    }
056
057    @Override
058    public synchronized void reply(SerialReply l) {  // receive a reply message and log it
059        // check for valid length
060        if (l.getNumDataElements() == 1) {
061            if (l.getElement(0) == 0) {
062                nextLine("NACK\n", l.toString());
063            } else {
064                nextLine("Ack from node " + l.getElement(0) + "\n", l.toString());
065            }
066        } else if (l.getNumDataElements() != 5) {
067            nextLine("Truncated reply of length " + l.getNumDataElements() + ":" + l.toString() + "\n",
068                    l.toString());
069        } else { // must be data reply
070            StringBuilder s = new StringBuilder(String.format("Receive addr=%d IB=", l.getAddr()));
071            for (int i = 2; i < 4; i++) {
072                s.append(Integer.toHexString(l.getElement(i))).append(" ");
073            }
074            nextLine(s.append("\n").toString(), l.toString());
075        }
076    }
077
078}