001package jmri.jmrix.roco.z21.messageformatters;
002
003import jmri.jmrix.lenz.XPressNetMessageFormatter;
004import jmri.jmrix.lenz.messageformatters.XNetLocoInfoReplyUtilities;
005import jmri.jmrix.roco.z21.Z21XNetReply;
006
007/**
008 * Format Z21XNetReply containg Z21 Specific Loco results for display in the XpressNet monitor.
009 *
010 * @author Paul Bender Copyright (C) 2025
011 */
012public class Z21XNetLocoReplyFormatter implements XPressNetMessageFormatter {
013
014    public boolean handlesMessage(jmri.jmrix.Message m) {
015        return m instanceof Z21XNetReply &&
016        (m.getElement(0)&0xE0)==0xE0 && ((m.getElement(0)&0x0f) >= 7 && (m.getElement(0)&0x0f) <=15 );
017    }
018
019    public String formatMessage(jmri.jmrix.Message m) {
020        if (!handlesMessage(m)) {
021            throw new IllegalArgumentException("Message is not a Z21XNetReply");
022        }
023        //Data Byte 0 and 1 contain the locomotive address
024        int messageaddress = ((m.getElement(1) & 0x3F) << 8) + (m.getElement(2) & 0xff);
025        String text = "Z21 Mobile decoder info reply for address " + messageaddress + ":";
026        //The message is for this throttle.
027        int b2 = m.getElement(3) & 0xff;
028        int b3 = m.getElement(4) & 0xff;
029        int b4 = m.getElement(5) & 0xff;
030        int b5 = m.getElement(6) & 0xff;
031        int b6 = m.getElement(7) & 0xff;
032        int b7 = m.getElement(8) & 0xff;
033        // byte 2 contains the speed step mode and availability
034        // information.
035        // byte 3 contains the direction and the speed information
036        text += " " + XNetLocoInfoReplyUtilities.parseSpeedAndDirection(b2, b3);
037        // byte 4 contains flags for whether or not the locomotive
038        // is in a double header and for smart search.  These aren't used
039        // here.
040
041        // byte 4 and 5 contain function information for F0-F12
042        text += " " + XNetLocoInfoReplyUtilities.parseFunctionStatus(b4, b5);
043        // byte 6 and 7 contain function information for F13-F28
044        text += " " + XNetLocoInfoReplyUtilities.parseFunctionHighStatus(b6, b7);
045        return text;
046    }
047}