001package jmri.jmrix.maple.serialmon;
002
003import jmri.jmrix.maple.SerialListener;
004import jmri.jmrix.maple.SerialMessage;
005import jmri.jmrix.maple.SerialReply;
006import jmri.jmrix.maple.MapleSystemConnectionMemo;
007
008/**
009 * Frame displaying (and logging) serial command messages.
010 *
011 * @author Bob Jacobsen Copyright (C) 2001
012 */
013public class SerialMonFrame extends jmri.jmrix.AbstractMonFrame implements SerialListener {
014
015    private MapleSystemConnectionMemo _memo = null;
016
017    public SerialMonFrame(MapleSystemConnectionMemo memo) {
018        super();
019        _memo = memo;
020    }
021
022    @Override
023    protected String title() {
024        return "Maple Serial Command Monitor";
025    } // TODO I18N
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    /**
040     * Define system-specific help item
041     */
042    @Override
043    protected void setHelp() {
044        addHelpMenu("package.jmri.jmrix.maple.serialmon.SerialMonFrame", true);
045    }
046
047    @Override
048    public synchronized void message(SerialMessage l) { // receive a message and log it
049        // check for valid length
050        if (l.getNumDataElements() < 2) {
051            nextLine("Truncated message of length " + l.getNumDataElements() + "\n", l.toString());
052        } else if (l.isPoll()) {
053            if ((l.getNumDataElements() <= 6) && (l.getElement(0) == 15)) {
054                nextLine("Poll Reply - NAK (error)", l.toString()); // TODO I18N
055            } else {
056                nextLine("Poll node " + l.getUA() + "\n", l.toString());
057            }
058        } else if (l.isXmt()) {
059            if (l.getNumDataElements() > 12) {
060                // this is the write command
061                int n = l.getNumItems();
062                StringBuilder s = new StringBuilder(String.format("Transmit node=%d ADDR = %d N = %d OB=",
063                    l.getUA(), l.getAddress(), n));
064                int i = 11;
065                while (n > 0) {
066                    for (int j = 0; (j < 8) && (n > 0); j++, n--) {
067                        s.append((((l.getElement(i) & 0x01) != 0) ? "1" : "0"));
068                        i++;
069                    }
070                    s.append(" ");
071                }
072                nextLine(s.append("\n").toString(), l.toString());
073            } else {
074                // this is the reply to the write command
075                StringBuilder s = new StringBuilder("Transmit Reply - ");
076                if (l.getElement(0) == 6) {
077                    s.append("ACK (OK)");
078                } else if (l.getElement(0) == 15) {
079                    s.append("NAK (error)");
080                }
081                nextLine(s.append("\n").toString(), l.toString());
082            }
083        } else {
084            nextLine("unrecognized cmd: \"" + l.toString() + "\"\n", "");
085        }
086    }
087
088    @Override
089    public synchronized void reply(SerialReply l) {  // receive a reply message and log it
090        // check for valid length
091        if (l.getNumDataElements() < 2) {
092            nextLine("Truncated reply of length " + l.getNumDataElements() + "\n", l.toString());
093        } else if (l.isRcv()) {
094            String s = "Receive node=" + l.getUA() + " IB=";
095            for (int i = 2; i < l.getNumDataElements(); i++) {
096                s = jmri.util.StringUtil.appendTwoHexFromInt(l.getElement(i), s) + " ";
097            }
098            nextLine(s + "\n", l.toString());
099        } else if (l.getElement(0) == 0x15) {
100            String s = "Negative reply " + l.toString();
101            nextLine(s + "\n", l.toString());
102        } else if (l.getElement(0) == 0x06) {
103            String s = "Positive reply " + l.toString();
104            nextLine(s + "\n", l.toString());
105        } else {
106            nextLine("unrecognized rep: \"" + l.toString() + "\"\n", "");
107        }
108    }
109
110}