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=", l.getUA(), l.getAddress(), n));
063                int i = 11;
064                while (n > 0) {
065                    for (int j = 0; (j < 8) && (n > 0); j++, n--) {
066                        s.append((((l.getElement(i) & 0x01) != 0) ? "1" : "0"));
067                        i++;
068                    }
069                    s.append(" ");
070                }
071                nextLine(s.append("\n").toString(), l.toString());
072            } else {
073                // this is the reply to the write command
074                StringBuilder s = new StringBuilder("Transmit Reply - ");
075                if (l.getElement(0) == 6) {
076                    s.append("ACK (OK)");
077                } else if (l.getElement(0) == 15) {
078                    s.append("NAK (error)");
079                }
080                nextLine(s.append("\n").toString(), l.toString());
081            }
082        } else {
083            nextLine("unrecognized cmd: \"" + l.toString() + "\"\n", "");
084        }
085    }
086
087    @Override
088    public synchronized void reply(SerialReply l) {  // receive a reply message and log it
089        // check for valid length
090        if (l.getNumDataElements() < 2) {
091            nextLine("Truncated reply of length " + l.getNumDataElements() + "\n", l.toString());
092        } else if (l.isRcv()) {
093            String s = "Receive node=" + l.getUA() + " IB=";
094            for (int i = 2; i < l.getNumDataElements(); i++) {
095                s = jmri.util.StringUtil.appendTwoHexFromInt(l.getElement(i), s) + " ";
096            }
097            nextLine(s + "\n", l.toString());
098        } else if (l.getElement(0) == 0x15) {
099            String s = "Negative reply " + l.toString();
100            nextLine(s + "\n", l.toString());
101        } else if (l.getElement(0) == 0x06) {
102            String s = "Positive reply " + l.toString();
103            nextLine(s + "\n", l.toString());
104        } else {
105            nextLine("unrecognized rep: \"" + l.toString() + "\"\n", "");
106        }
107    }
108
109}