001package jmri.jmrix.lenz.messageformatters;
002
003import jmri.jmrix.Message;
004import jmri.jmrix.lenz.XNetConstants;
005import jmri.jmrix.lenz.XNetReply;
006import jmri.jmrix.lenz.XPressNetMessageFormatter;
007
008/**
009 * Format replies for XPressNet Command Station reply for Status.
010 *
011 * @author Paul Bender Copyright (C) 2025
012 */
013public class XNetCSStatusReplyFormatter implements XPressNetMessageFormatter {
014
015    @Override
016    public boolean handlesMessage(Message m) {
017        return m instanceof XNetReply &&
018                m.getElement(0) == XNetConstants.CS_REQUEST_RESPONSE &&
019                m.getElement(1) == XNetConstants.CS_STATUS_RESPONSE; 
020    }
021
022    @Override
023    public String formatMessage(Message m) {
024        if(!handlesMessage(m)) { 
025            throw new IllegalArgumentException("Message is not supported");
026        }
027        StringBuilder text = new StringBuilder(Bundle.getMessage("XNetReplyCSStatus") + " ");
028        int statusByte = m.getElement(2);
029        if ((statusByte & 0x01) == 0x01) {
030            // Command station is in Emergency Off Mode
031            text.append(Bundle.getMessage("XNetCSStatusEmergencyOff")).append("; ");
032        }
033            if ((statusByte & 0x02) == 0x02) {
034                // Command station is in Emergency Stop Mode
035                text.append(Bundle.getMessage("XNetCSStatusEmergencyStop")).append("; ");
036            }
037            if ((statusByte & 0x08) == 0x08) {
038                // Command station is in Service Mode
039                text.append(Bundle.getMessage("XNetCSStatusServiceMode")).append("; ");
040            }
041            if ((statusByte & 0x40) == 0x40) {
042                // Command station is in Power Up Mode
043                text.append(Bundle.getMessage("XNetCSStatusPoweringUp")).append("; ");
044            }
045            if ((statusByte & 0x04) == 0x04) {
046                text.append(Bundle.getMessage("XNetCSStatusPowerModeAuto")).append("; ");
047            } else {
048                text.append(Bundle.getMessage("XNetCSStatusPowerModeManual")).append("; ");
049            }
050            if ((statusByte & 0x80) == 0x80) {
051                // Command station has a experienced a ram check error
052                text.append(Bundle.getMessage("XNetCSStatusRamCheck"));
053            }
054            return text.toString();
055    }
056
057}