001package jmri.jmrix.mrc.swing.monitor;
002
003import java.util.Date;
004import javax.swing.JCheckBox;
005import jmri.jmrix.mrc.MrcInterface;
006import jmri.jmrix.mrc.MrcMessage;
007import jmri.jmrix.mrc.MrcSystemConnectionMemo;
008import jmri.jmrix.mrc.MrcTrafficListener;
009import jmri.jmrix.mrc.swing.MrcPanelInterface;
010import org.slf4j.Logger;
011import org.slf4j.LoggerFactory;
012
013/**
014 * Swing action to create and register a MonFrame object
015 *
016 * @author Bob Jacobsen Copyright (C) 2001, 2008
017 * @author kcameron Copyright (C) 2011 copied from SerialMonPane.java
018 * @author Daniel Boudreau Copyright (C) 2012 added human readable format
019 */
020public class MrcMonPanel extends jmri.jmrix.AbstractMonPane implements MrcTrafficListener, MrcPanelInterface {
021
022    public MrcMonPanel() {
023        super();
024    }
025
026    @Override
027    public String getHelpTarget() {
028        return "package.jmri.jmrix.mrc.swing.monitor.MrcMonPanel";
029    }// NOI18N
030
031    @Override
032    public String getTitle() {
033        return(Bundle.getMessage("MrcMonPanelName"));
034    }
035
036    @Override
037    public void dispose() {
038        if (memo.getMrcTrafficController() != null) {
039            memo.getMrcTrafficController().removeTrafficListener(trafficFilter, this);
040        }
041        // and unwind swing
042        super.dispose();
043    }
044
045    @Override
046    public void init() {
047    }
048
049    MrcSystemConnectionMemo memo;
050
051    @Override
052    public void initContext(Object context) {
053        if (context instanceof MrcSystemConnectionMemo) {
054            initComponents((MrcSystemConnectionMemo) context);
055        }
056    }
057
058    JCheckBox includePoll = new JCheckBox(Bundle.getMessage("MrcMonPanelCheckIncPoll")); // NOI18N
059
060    private int trafficFilter = MrcInterface.ALL;
061
062    @Override
063    public void initComponents(MrcSystemConnectionMemo memo) {
064        this.memo = memo;
065        add(includePoll);
066        // connect to the LnTrafficController
067        if (memo.getMrcTrafficController() == null) {
068            log.error("No traffic controller is available"); // NOI18N
069            return;
070        }
071        memo.getMrcTrafficController().addTrafficListener(trafficFilter, this);
072    }
073
074    MrcMessage previousPollMessage;
075    Date previousTimeStamp;
076
077    @Override
078    public synchronized void notifyXmit(Date timestamp, MrcMessage m) {
079        if (!includePoll.isSelected() && (m.getMessageClass() & MrcInterface.POLL) == MrcInterface.POLL) {
080            return;
081        }
082
083        logMessage(timestamp, m, Bundle.getMessage("MrcMonPanelTextXmit")); // NOI18N
084    }
085
086    @Override
087    public synchronized void notifyFailedXmit(Date timestamp, MrcMessage m) {
088
089        logMessage(timestamp, m, Bundle.getMessage("MrcMonPanelTextFailed"));   // NOI18N
090    }
091
092    @Override
093    public synchronized void notifyRcv(Date timestamp, MrcMessage m) {
094
095        String prefix = Bundle.getMessage("MrcMonPanelTextRecv"); // NOI18N
096        if (!includePoll.isSelected() && (m.getMessageClass() & MrcInterface.POLL) == MrcInterface.POLL && m.getElement(1) == 0x01) {
097            //Do not show poll messages
098            previousPollMessage = m;
099            return;
100        } else if (previousPollMessage != null) {
101            if ((m.getMessageClass() & MrcInterface.POLL) == MrcInterface.POLL) {
102                previousPollMessage = null;
103                return;
104            }
105            prefix = Bundle.getMessage("MrcMonPanelTextRxCab", Integer.toString(previousPollMessage.getElement(0))); // NOI18N
106            previousPollMessage = null;
107        }
108        logMessage(timestamp, m, prefix);
109    }
110
111    private void logMessage(Date timestamp, MrcMessage m, String src) {  // receive an Mrc message and log it
112        String raw = "";
113        for (int i = 0; i < m.getNumDataElements(); i++) {
114            if (i > 0) {
115                raw += " ";
116            }
117            raw = jmri.util.StringUtil.appendTwoHexFromInt(m.getElement(i) & 0xFF, raw);
118        }
119
120        // display the decoded data
121        // we use MrcPackets to format
122        nextLineWithTime(timestamp, src + " " + m.toString() + "\n", raw);
123    }
124
125    /**
126     * Nested class to create one of these using old-style defaults
127     */
128    static public class Default extends jmri.jmrix.mrc.swing.MrcNamedPaneAction {
129
130        public Default() {
131            super("Mrc Command Monitor",
132                    new jmri.util.swing.sdi.JmriJFrameInterface(),
133                    MrcMonPanel.class.getName(),
134                    jmri.InstanceManager.getDefault(MrcSystemConnectionMemo.class)); // NOI18N
135        }
136    }
137
138    private final static Logger log = LoggerFactory.getLogger(MrcMonPanel.class);
139
140}