001package jmri.jmrix.qsi.qsimon;
002
003import jmri.jmrix.qsi.QsiListener;
004import jmri.jmrix.qsi.QsiMessage;
005import jmri.jmrix.qsi.QsiReply;
006import jmri.jmrix.qsi.QsiSystemConnectionMemo;
007
008/**
009 * Frame displaying (and logging) QSI command messages
010 *
011 * @author Bob Jacobsen Copyright (C) 2007, 2008
012 */
013public class QsiMonFrame extends jmri.jmrix.AbstractMonFrame implements QsiListener {
014
015    private final QsiSystemConnectionMemo _memo;
016
017    public QsiMonFrame(QsiSystemConnectionMemo memo) {
018        super();
019        _memo = memo;
020    }
021
022    @Override
023    protected String title() {
024        return "QSI Command Monitor";
025    }
026
027    @Override
028    protected void init() {
029        // connect to TrafficController
030        _memo.getQsiTrafficController().addQsiListener(this);
031    }
032
033    @Override
034    public void dispose() {
035        _memo.getQsiTrafficController().removeQsiListener(this);
036        super.dispose();
037    }
038
039    @Override
040    public synchronized void message(QsiMessage l) {  // receive a message and log it
041        int opcode = l.getElement(0);
042        String text;
043        if (l.getNumDataElements() == 1) {
044            // special case - assume this is a reply ack
045            text = "Ack of message for function " + l.getElement(0);
046        } else {
047            switch (opcode) {
048                case 9:
049                    text = "OP_REQ_READ_CV with CV=" + l.getElement(3);
050                    break;
051                case 30:
052                    text = "OP_REQ_WRITE_CV with CV=" + l.getElement(3) + " data=" + l.getElement(4);
053                    break;
054                case 17:
055                    text = "OP_REQ_CLEAR_ERROR_STATUS";
056                    break;
057                default:
058                    text = "Untranslated message with code " + opcode + ": " + l.toString(_memo.getQsiTrafficController());
059                    break;
060            }
061        }
062        nextLine("M: " + text + "\n", l.toString(_memo.getQsiTrafficController()));
063    }
064
065    @Override
066    public synchronized void reply(QsiReply l) {  // receive a reply message and log it
067        String text;
068
069        if (l.getElement(0) == 'A') {
070            text = "A: Ack of " + l.getElement(1) + " with status " + l.getElement(2);
071        } else if (l.getElement(0) == 'S') {
072            switch (l.getElement(1) & 0xFF) {
073                case 10:
074                    text = "S: OP_RPL_READ_CV status " + l.getElement(4) + " value " + l.getElement(5);
075                    break;
076                case 31:
077                    text = "S: OP_RPL_WRITE_CV status " + l.getElement(4);
078                    break;
079                default:
080                    text = "S: Response type " + l.getElement(1) + " with length " + (l.getElement(2) + 256 * l.getElement(3));
081                    break;
082            }
083
084        } else {
085            text = "U: Untranslated reply: " + l.toString(_memo.getQsiTrafficController());
086        }
087        nextLine(text + "\n", l.toString(_memo.getQsiTrafficController()));
088    }
089
090}