001package jmri.jmrix.marklin.swing.monitor;
002
003import jmri.jmrix.marklin.MarklinConstants;
004import jmri.jmrix.marklin.MarklinReply;
005
006/**
007 * Class to convert Marklin Can bus messages to a human readable form
008 */
009public class MarklinMon {
010
011    public static String displayReply(MarklinReply r) {
012
013        StringBuffer sb = new StringBuffer();
014        sb.append("Priority ");
015        switch (r.getPriority()) {
016            case MarklinConstants.PRIO_1:
017                sb.append("1, Stop/Go/Short");
018                break;
019            case MarklinConstants.PRIO_2:
020                sb.append("2, Feedback");
021                break;
022            case MarklinConstants.PRIO_3:
023                sb.append("3, Engine Stop");
024                break;
025            case MarklinConstants.PRIO_4:
026                sb.append("4, Engine/accessory command");
027                break;
028            default:
029                sb.append(Bundle.getMessage("StateUnknown"));
030        }
031        sb.append(" Command: ");
032        int command = r.getCommand();
033        if (command == MarklinConstants.SYSCOMMANDSTART) {
034            sb.append("System");
035        } else if (command >= MarklinConstants.MANCOMMANDSTART && command <= MarklinConstants.MANCOMMANDEND) {
036            switch (r.getCommand()) {
037                case MarklinConstants.LOCODIRECTION:
038                    sb.append("Change of direction " + r.getElement(9));
039                    break;
040                case MarklinConstants.LOCOSPEED:
041                    sb.append("Change of speed " + ((r.getElement(9) & 0xff << 8) + (r.getElement(10) & 0xff)));
042                    break;
043                case MarklinConstants.LOCOFUNCTION:
044                    sb.append("Function: " + r.getElement(9) + " state: " + r.getElement(10));
045                    break;
046                default:
047                    sb.append("Management");
048            }
049        } else if (command >= MarklinConstants.ACCCOMMANDSTART && command <= MarklinConstants.ACCCOMMANDEND) {
050            sb.append("Accessory");
051            switch (r.getElement(9)) {
052                case 0x00:
053                    sb.append(Bundle.getMessage("SetTurnoutState", Bundle.getMessage("TurnoutStateThrown")));
054                    break;
055                case 0x01:
056                    sb.append(Bundle.getMessage("SetTurnoutState", Bundle.getMessage("TurnoutStateClosed")));
057                    break;
058                default:
059                    sb.append("Unknown state command " + r.getElement(9));
060            }
061        } else if (command >= MarklinConstants.SOFCOMMANDSTART && command <= MarklinConstants.SOFCOMMANDEND) {
062            sb.append("Software");
063        } else if (command >= MarklinConstants.GUICOMMANDSTART && command <= MarklinConstants.GUICOMMANDEND) {
064            sb.append("GUI");
065        } else if (command >= MarklinConstants.AUTCOMMANDSTART && command <= MarklinConstants.AUTCOMMANDEND) {
066            sb.append("Automation");
067        } else if (command >= MarklinConstants.FEECOMMANDSTART && command <= MarklinConstants.FEECOMMANDEND) {
068            sb.append("Feedback");
069        }
070        if (r.isResponse()) {
071            sb.append(" " + Bundle.getMessage("ReplyMessage"));
072        } else {
073            sb.append(" " + Bundle.getMessage("RequestMessage"));
074        }
075        long addr = r.getAddress();
076        if (addr >= MarklinConstants.MM1START && addr <= MarklinConstants.MM1END) {
077            if (addr == 0) {
078                sb.append(" Broadcast");
079            } else {
080                sb.append(" " + Bundle.getMessage("MonTrafToLocoAddress", addr));
081            }
082        } else if (addr >= MarklinConstants.MM1FUNCTSTART && addr <= MarklinConstants.MM1FUNCTEND) {
083            addr = addr - MarklinConstants.MM1FUNCTSTART;
084            sb.append(" to MM Function decoder " + addr);
085        } else if (addr >= MarklinConstants.MM1LOCOSTART && addr <= MarklinConstants.MM1LOCOEND) {
086            addr = addr - MarklinConstants.MM1LOCOSTART;
087            sb.append(" " + Bundle.getMessage("MonTrafToLocoAddress", addr));
088        } else if (addr >= MarklinConstants.SX1START && addr <= MarklinConstants.SX1END) {
089            addr = addr - MarklinConstants.SX1START;
090            sb.append(" to SX Address " + addr);
091        } else if (addr >= MarklinConstants.SX1ACCSTART && addr <= MarklinConstants.SX1ACCEND) {
092            addr = addr - MarklinConstants.SX1ACCSTART;
093            sb.append(" to SX Accessory Address " + addr);
094        } else if (addr >= MarklinConstants.MM1ACCSTART && addr <= MarklinConstants.MM1ACCEND) {
095            addr = addr - MarklinConstants.MM1ACCSTART;
096            sb.append(" to MM Accessory Address " + addr);
097        } else if (addr >= MarklinConstants.DCCACCSTART && addr <= MarklinConstants.DCCACCEND) {
098            addr = addr - MarklinConstants.DCCACCSTART;
099            sb.append(" to DCC Accessory Address " + addr);
100        } else if (addr >= MarklinConstants.MFXSTART && addr <= MarklinConstants.MFXEND) {
101            addr = addr - MarklinConstants.MFXSTART;
102            sb.append(" to MFX Address " + addr);
103        } else if (addr >= MarklinConstants.SX2START && addr <= MarklinConstants.SX2END) {
104            addr = addr - MarklinConstants.SX2START;
105            sb.append(" to SX2 Address " + addr);
106        } else if (addr >= MarklinConstants.DCCSTART && addr <= MarklinConstants.DCCEND) {
107            addr = addr - MarklinConstants.DCCSTART;
108            sb.append(" to DCC Address " + addr);
109        }
110        //StringBuffer buf = new StringBuffer();
111        sb.append("0x" + Integer.toHexString(r.getCanData()[0]));
112        for (int i = 1; i < r.getCanData().length; i++) {
113            sb.append(", 0x" + Integer.toHexString(r.getCanData()[i]));
114        }
115
116        return sb.toString();
117    }
118
119}