001package jmri.jmrix.lenz.messageformatters;
002
003import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
004import jmri.jmrix.Message;
005import jmri.jmrix.lenz.LenzCommandStation;
006import jmri.jmrix.lenz.XNetConstants;
007import jmri.jmrix.lenz.XNetMessage;
008import jmri.jmrix.lenz.XPressNetMessageFormatter;
009
010/** formatter for function group 6 operations request messages
011 *
012 * @author Paul Bender Copyright (C) 2024
013 */
014public class XNetFunctionGroup6OperateRequestMessageFormatter implements XPressNetMessageFormatter {
015
016    private static final String X_NET_MESSAGE_SET_FUNCTION_GROUP_X = "XNetMessageSetFunctionGroupX";
017    private static final String POWER_STATE_ON = "PowerStateOn";
018    private static final String POWER_STATE_OFF = "PowerStateOff";
019
020    @Override
021    public boolean handlesMessage(Message m) {
022        return m instanceof XNetMessage &&
023                m.getElement(0) == XNetConstants.LOCO_OPER_REQ &&
024                m.getElement(1) == XNetConstants.LOCO_SET_FUNC_GROUP6;
025    }
026
027    @SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST", justification = "cast is checked in handlesMessage")
028    @Override
029    public String formatMessage(Message m) {
030        if(!handlesMessage(m)) {
031            throw new IllegalArgumentException( "XNetFunction6OperateRequestMessageFormatter: message type not supported");
032        }
033        return "Mobile Decoder Operations Request: " + buildSetFunctionGroup6MonitorString((XNetMessage ) m);
034    }
035
036
037    private String buildSetFunctionGroup6MonitorString(XNetMessage m) {
038        String text = Bundle.getMessage(X_NET_MESSAGE_SET_FUNCTION_GROUP_X, 6)
039                + " " + LenzCommandStation.calcLocoAddress(m.getElement(2), m.getElement(3)) + " ";
040        int element4 = m.getElement(4);
041        if ((element4 & 0x01) != 0) {
042            text += "F29 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
043        } else {
044            text += "F29 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
045        }
046        if ((element4 & 0x02) != 0) {
047            text += "F30 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
048        } else {
049            text += "F30 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
050        }
051        if ((element4 & 0x04) != 0) {
052            text += "F31 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
053        } else {
054            text += "F31 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
055        }
056        if ((element4 & 0x08) != 0) {
057            text += "F32 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
058        } else {
059            text += "F32 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
060        }
061        if ((element4 & 0x10) != 0) {
062            text += "F33 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
063        } else {
064            text += "F33 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
065        }
066        if ((element4 & 0x20) != 0) {
067            text += "F34 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
068        } else {
069            text += "F34 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
070        }
071        if ((element4 & 0x40) != 0) {
072            text += "F35 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
073        } else {
074            text += "F35 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
075        }
076        if ((element4 & 0x80) != 0) {
077            text += "F36 " + Bundle.getMessage(POWER_STATE_ON) + "; ";
078        } else {
079            text += "F36 " + Bundle.getMessage(POWER_STATE_OFF) + "; ";
080        }
081        return text;
082    }
083
084
085}