001package jmri.jmrix.zimo.swing.monitor;
002
003import java.util.Date;
004import javax.swing.JCheckBox;
005import jmri.jmrix.zimo.Mx1Listener;
006import jmri.jmrix.zimo.Mx1Message;
007import jmri.jmrix.zimo.Mx1SystemConnectionMemo;
008import jmri.jmrix.zimo.swing.Mx1PanelInterface;
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012/**
013 * Swing action to create and register a MonFrame object.
014 *
015 * @author Bob Jacobsen Copyright (C) 2001, 2008
016 * @author kcameron Copyright (C) 2011 copied from SerialMonPane.java
017 * @author Daniel Boudreau Copyright (C) 2012 added human readable format
018 */
019public class Mx1MonPanel extends jmri.jmrix.AbstractMonPane implements Mx1Listener, Mx1PanelInterface {
020
021    public Mx1MonPanel() {
022        super();
023    }
024
025    @Override
026    public String getHelpTarget() {
027        return "package.jmri.jmrix.zimo.swing.monitor.Mx1MonPanel";
028    } // NOI18N
029
030    @Override
031    public String getTitle() {
032        StringBuilder x = new StringBuilder();
033        if (memo != null) {
034            x.append(memo.getUserName());
035        } else {
036            x.append("Mx1_"); // NOI18N
037        }
038        x.append(": "); // NOI18N
039        x.append("Command Monitor"); // I18N
040        return x.toString();
041    }
042
043    @Override
044    public void dispose() {
045        if (memo.getMx1TrafficController() != null) {
046            memo.getMx1TrafficController().removeMx1Listener(~0, this);
047        }
048        // and unwind swing
049        super.dispose();
050    }
051
052    @Override
053    public void init() {
054    }
055
056    Mx1SystemConnectionMemo memo;
057
058    @Override
059    public void initContext(Object context) {
060        if (context instanceof Mx1SystemConnectionMemo) {
061            initComponents((Mx1SystemConnectionMemo) context);
062        }
063    }
064
065    JCheckBox includePoll = new JCheckBox("Include Poll Messages"); // NOI18N
066
067    @Override
068    public void initComponents(Mx1SystemConnectionMemo memo) {
069        this.memo = memo;
070        // connect to the LnTrafficController
071        if (memo.getMx1TrafficController() == null) {
072            log.error("No traffic controller is available"); // NOI18N
073            return;
074        }
075        memo.getMx1TrafficController().addMx1Listener(~0, this);
076    }
077
078    Date previousTimeStamp;
079
080    public synchronized void notifyXmit(Date timestamp, Mx1Message m) {
081        logMessage(timestamp, m, "Tx:");
082    }
083
084    public synchronized void notifyFailedXmit(Date timestamp, Mx1Message m) {
085
086        logMessage(timestamp, m, "FAILED:");
087    }
088
089    public synchronized void notifyRcv(Date timestamp, Mx1Message m) {
090
091        String prefix = "Rx:"; // NOI18N
092        logMessage(timestamp, m, prefix);
093    }
094
095    @Override
096    public synchronized void message(Mx1Message l) {  // receive a MX-1 message and log it
097        // display the raw data if requested
098        StringBuilder raw = new StringBuilder("packet: ");
099        if (rawCheckBox.isSelected()) {
100            int len = l.getNumDataElements();
101            for (int i = 0; i < len; i++) {
102                raw.append(Integer.toHexString(l.getElement(i))).append(" ");
103            }
104        }
105
106        // display the decoded data
107        nextLine(l.getStringMsg() + "\n", raw.toString());
108    }
109
110    private void logMessage(Date timestamp, Mx1Message m, String src) { // receive a Mrc message and log it
111        StringBuilder raw = new StringBuilder("");
112        for (int i = 0; i < m.getNumDataElements(); i++) {
113            if (i > 0) {
114                raw.append(" ");
115            }
116            raw.append(jmri.util.StringUtil.twoHexFromInt(m.getElement(i) & 0xFF));
117        }
118
119        // display the decoded data
120        // we use Mx1Message to format
121        nextLineWithTime(timestamp, src + " " + m.toString() + "\n", raw.toString());
122    }
123
124    private final static Logger log = LoggerFactory.getLogger(Mx1MonPanel.class);
125
126}