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 // class only supplies static methods 012 private MarklinMon(){} 013 014 public static String displayReply(MarklinReply r) { 015 016 StringBuilder sb = new StringBuilder(); 017 appendPriority(r, sb); 018 appendCommand(r, sb); 019 appendResponse(r, sb); 020 appendAddress(r, sb); 021 appendCanHex(r, sb); 022 return sb.toString(); 023 } 024 025 private static void appendPriority(MarklinReply r, StringBuilder sb) { 026 sb.append("Priority "); 027 switch (r.getPriority()) { 028 case MarklinConstants.PRIO_1: 029 sb.append("1, Stop/Go/Short"); 030 break; 031 case MarklinConstants.PRIO_2: 032 sb.append("2, Feedback"); 033 break; 034 case MarklinConstants.PRIO_3: 035 sb.append("3, Engine Stop"); 036 break; 037 case MarklinConstants.PRIO_4: 038 sb.append("4, Engine/accessory command"); 039 break; 040 default: 041 sb.append(Bundle.getMessage("StateUnknown")); 042 } 043 } 044 045 private static void appendCommand(MarklinReply r, StringBuilder sb) { 046 sb.append(" Command: "); 047 int command = r.getCommand(); 048 if (command == MarklinConstants.SYSCOMMANDSTART) { 049 sb.append("System"); 050 } else if (command >= MarklinConstants.MANCOMMANDSTART && command <= MarklinConstants.MANCOMMANDEND) { 051 switch (r.getCommand()) { 052 case MarklinConstants.LOCODIRECTION: 053 sb.append("Change of direction ").append(r.getElement(9)); 054 break; 055 case MarklinConstants.LOCOSPEED: 056 sb.append("Change of speed ").append((r.getElement(9) & 0xff << 8) + (r.getElement(10) & 0xff)); 057 break; 058 case MarklinConstants.LOCOFUNCTION: 059 sb.append("Function: ").append(r.getElement(9)).append(" state: ").append(r.getElement(10)); 060 break; 061 default: 062 sb.append("Management"); 063 } 064 } else if (command >= MarklinConstants.ACCCOMMANDSTART && command <= MarklinConstants.ACCCOMMANDEND) { 065 sb.append("Accessory"); 066 switch (r.getElement(9)) { 067 case 0x00: 068 sb.append(Bundle.getMessage("SetTurnoutState", Bundle.getMessage("TurnoutStateThrown"))); 069 break; 070 case 0x01: 071 sb.append(Bundle.getMessage("SetTurnoutState", Bundle.getMessage("TurnoutStateClosed"))); 072 break; 073 default: 074 sb.append("Unknown state command ").append(r.getElement(9)); 075 } 076 } else if (command >= MarklinConstants.SOFCOMMANDSTART && command <= MarklinConstants.SOFCOMMANDEND) { 077 sb.append("Software"); 078 } else if (command >= MarklinConstants.GUICOMMANDSTART && command <= MarklinConstants.GUICOMMANDEND) { 079 sb.append("GUI"); 080 } else if (command >= MarklinConstants.AUTCOMMANDSTART && command <= MarklinConstants.AUTCOMMANDEND) { 081 sb.append("Automation"); 082 } else if (command >= MarklinConstants.FEECOMMANDSTART && command <= MarklinConstants.FEECOMMANDEND) { 083 sb.append("Feedback"); 084 } 085 } 086 087 private static void appendResponse(MarklinReply r, StringBuilder sb) { 088 if (r.isResponse()) { 089 sb.append(" ").append( Bundle.getMessage("ReplyMessage")); 090 } else { 091 sb.append(" ").append( Bundle.getMessage("RequestMessage")); 092 } 093 } 094 095 private static void appendAddress(MarklinReply r, StringBuilder sb) { 096 long addr = r.getAddress(); 097 if (addr >= MarklinConstants.MM1START && addr <= MarklinConstants.MM1END) { 098 if (addr == 0) { 099 sb.append(" Broadcast"); 100 } else { 101 sb.append(" ").append( Bundle.getMessage("MonTrafToLocoAddress", addr)); 102 } 103 } else if (addr >= MarklinConstants.MM1FUNCTSTART && addr <= MarklinConstants.MM1FUNCTEND) { 104 addr -= MarklinConstants.MM1FUNCTSTART; 105 sb.append(" to MM Function decoder ").append(addr); 106 } else if (addr >= MarklinConstants.MM1LOCOSTART && addr <= MarklinConstants.MM1LOCOEND) { 107 addr -= MarklinConstants.MM1LOCOSTART; 108 sb.append(" ").append( Bundle.getMessage("MonTrafToLocoAddress", addr)); 109 } else if (addr >= MarklinConstants.SX1START && addr <= MarklinConstants.SX1END) { 110 addr -= MarklinConstants.SX1START; 111 sb.append(" to SX Address ").append( addr); 112 } else if (addr >= MarklinConstants.SX1ACCSTART && addr <= MarklinConstants.SX1ACCEND) { 113 addr -= MarklinConstants.SX1ACCSTART; 114 sb.append(" to SX Accessory Address ").append(addr); 115 } else if (addr >= MarklinConstants.MM1ACCSTART && addr <= MarklinConstants.MM1ACCEND) { 116 addr -= MarklinConstants.MM1ACCSTART; 117 sb.append(" to MM Accessory Address ").append( addr); 118 } else if (addr >= MarklinConstants.DCCACCSTART && addr <= MarklinConstants.DCCACCEND) { 119 addr -= MarklinConstants.DCCACCSTART; 120 sb.append(" to DCC Accessory Address ").append( addr); 121 } else if (addr >= MarklinConstants.MFXSTART && addr <= MarklinConstants.MFXEND) { 122 addr -= MarklinConstants.MFXSTART; 123 sb.append(" to MFX Address ").append( addr); 124 } else if (addr >= MarklinConstants.SX2START && addr <= MarklinConstants.SX2END) { 125 addr -= MarklinConstants.SX2START; 126 sb.append(" to SX2 Address ").append( addr); 127 } else if (addr >= MarklinConstants.DCCSTART && addr <= MarklinConstants.DCCEND) { 128 addr -= MarklinConstants.DCCSTART; 129 sb.append(" to DCC Address ").append( addr); 130 } 131 } 132 133 private static void appendCanHex(MarklinReply r, StringBuilder sb) { 134 sb.append("0x").append( Integer.toHexString(r.getCanData()[0])); 135 for (int i = 1; i < r.getCanData().length; i++) { 136 sb.append(", 0x").append(Integer.toHexString(r.getCanData()[i])); 137 } 138 } 139 140}