001package jmri.jmrix.loconet.alm.almi;
002
003import jmri.jmrix.loconet.LnConstants;
004import jmri.jmrix.loconet.LocoNetMessage;
005import jmri.jmrix.loconet.messageinterp.LocoNetMessageInterpret;
006import jmri.jmrix.loconet.alm.Alm;
007
008
009/**
010 * Provides displayable interpretation of various ALM messages.
011 *
012 * @author Bob Milhaupt  Copyright (C) 2022
013 */
014
015public class Almi {
016    private static final String EMPTY = "";
017    private Almi () {
018        throw new IllegalStateException("Utility class"); // NOI18N
019    }
020
021    public static String interpretAlm(LocoNetMessage l) {
022        if ((l.getOpCode() != LnConstants.OPC_ALM_READ) &&
023                (l.getOpCode() != LnConstants.OPC_ALM_WRITE)) {
024            return EMPTY;
025        }
026        if ((l.getNumDataElements() != 16) || (l.getElement(1) != 0x10)) {
027            return EMPTY;
028        }
029
030        String ret;
031        ret = dealWithAlmAliases(l);
032        if (ret.length() > 1) {
033            return ret;
034        }
035        ret = Almir.interpretAlmRoutes(l);
036        if (ret.length() > 1) {
037            return ret;
038        }
039        return EMPTY;
040    }
041
042    private static String dealWithAlmAliases(LocoNetMessage l) {
043        if (l.getElement(2) != 0) {
044            return EMPTY;
045        }
046        if ((l.getElement(3) == 0)
047                && (l.getElement(6) == 0)) {
048            return Bundle.getMessage("LN_MSG_QUERY_ALIAS_INFO");
049        }
050        if ((l.getElement(3) == 0)
051                && (l.getElement(6) == 0x0b)) {
052            return Bundle.getMessage("LN_MSG_ALIAS_INFO_REPORT", l.getElement(4) * 2);
053        }
054        if ((l.getElement(3) == 2) && (l.getElement(6) == 0xf)
055                && (l.getElement(14) == 0)) {
056            // Alias read and write messages
057            String message;
058            if (l.getOpCode() == LnConstants.OPC_ALM_WRITE) {
059                return Bundle.getMessage("LN_MSG_QUERY_ALIAS", l.getElement(4));
060            }
061
062            message = "LN_MSG_REPORT_ALIAS_2_ALIASES"; // NOI18N
063
064            String longAddr = LocoNetMessageInterpret.convertToMixed(l.getElement(7), l.getElement(8));
065            int shortAddr = l.getElement(9);
066            String longAddr2 = LocoNetMessageInterpret.convertToMixed(l.getElement(11), l.getElement(12));
067            int shortAddr2 = l.getElement(13);
068            int pair = l.getElement(4);
069            return Bundle.getMessage(message, pair,
070                    longAddr, shortAddr, longAddr2, shortAddr2);
071        }
072        if ((l.getElement(3) == 0x43)) {
073            String longAddr = LocoNetMessageInterpret.convertToMixed(l.getElement(7), l.getElement(8));
074            int shortAddr = l.getElement(9);
075            String longAddr2 = LocoNetMessageInterpret.convertToMixed(l.getElement(11), l.getElement(12));
076            int shortAddr2 = l.getElement(13);
077            int pair = l.getElement(4);
078            return Bundle.getMessage("LN_MSG_SET_ALIAS_2_ALIASES",
079                    pair, longAddr, shortAddr, longAddr2, shortAddr2);
080        } else if ((l.getElement(6) == 0)
081                && (l.getElement(14) == 0)) {
082            return Bundle.getMessage("LN_MSG_QUERY_ALIAS", l.getElement(4));
083        }
084        return EMPTY;
085    }
086
087}