001package jmri.jmrix.lenz.hornbyelite.messageformatters;
002
003import jmri.jmrix.Message;
004import jmri.jmrix.lenz.LenzCommandStation;
005import jmri.jmrix.lenz.XNetConstants;
006import jmri.jmrix.lenz.XNetReply;
007import jmri.jmrix.lenz.XPressNetMessageFormatter;
008import jmri.jmrix.lenz.messageformatters.XNetLocoInfoReplyUtilities;
009
010/**
011 * Formatter for Hornby Elite specific Multiple Unit Info Reply messages.
012 *
013 * @author Paul Bender Copyright (C) 2025
014 */
015public class HornbyEliteMultiUnitInfoReplyFormatter implements XPressNetMessageFormatter {
016
017    @Override
018    public boolean handlesMessage(Message m) {
019        return m instanceof XNetReply &&
020                ((XNetReply) m).getElement(0) == XNetConstants.LOCO_INFO_MUED_UNIT && (
021                ((XNetReply) m).getElement(1) == 0xF8 ||  // only handle the two Hornby Elite specific messages
022                ((XNetReply) m).getElement(1) == 0xF9 );
023    }
024
025    @Override
026    public String formatMessage(Message m) {
027        if(!handlesMessage(m)){
028            throw new IllegalArgumentException("Message is not supported");
029        }
030        StringBuilder text = new StringBuilder();
031        if (m.getElement(1) == 0xF8) {
032            // This message is a Hornby addition to the protocol
033            // indicating the speed and direction of a locomoitve
034            // controlled by the elite's built in throttles
035            text = new StringBuilder(Bundle.getMessage("XNetReplyLocoEliteSLabel") + " ");
036            text.append(LenzCommandStation.calcLocoAddress(m.getElement(2), m.getElement(3)));
037            text.append(",").append(XNetLocoInfoReplyUtilities.parseSpeedAndDirection(m.getElement(4), m.getElement(5))).append(" ");
038        } else if (m.getElement(1) == 0xF9) {
039            // This message is a Hornby addition to the protocol
040            // indicating the function on/off status of a locomoitve
041            // controlled by the elite's built in throttles
042            text = new StringBuilder(Bundle.getMessage("XNetReplyLocoEliteFLabel") + " ");
043            text.append(LenzCommandStation.calcLocoAddress(m.getElement(2), m.getElement(3))).append(" ");
044            // message byte 5, contains F0,F1,F2,F3,F4
045            int element4 = m.getElement(4);
046            // message byte 5, contains F12,F11,F10,F9,F8,F7,F6,F5
047            int element5 = m.getElement(5);
048            text.append(XNetLocoInfoReplyUtilities.parseFunctionStatus(element4, element5));
049        }
050        return text.toString();
051    }
052
053}