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