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 10 momentary request messages
011 *
012 * @author Paul Bender Copyright (C) 2024
013 */
014public class XNetFunctionGroup10MomentaryRequestMessageFormatter implements XPressNetMessageFormatter {
015
016    private static final String X_NET_MESSAGE_SET_FUNCTION_GROUP_X_MOMENTARY = "XNetMessageSetFunctionGroupXMomentary";
017    private static final String FUNCTION_CONTINUOUS = "FunctionContinuous";
018    private static final String FUNCTION_MOMENTARY = "FunctionMomentary";
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_GROUP10_MOMENTARY;
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( "XNetFunction10MomentaryRequestMessageFormatter: message type not supported");
032        }
033        return "Mobile Decoder Operations Request: " + buildSetFunctionGroup10MomentaryMonitorString((XNetMessage ) m);
034    }
035
036    private String buildSetFunctionGroup10MomentaryMonitorString(XNetMessage m) {
037        String text = Bundle.getMessage(X_NET_MESSAGE_SET_FUNCTION_GROUP_X_MOMENTARY, 10) + " " +
038                LenzCommandStation.calcLocoAddress(m.getElement(2), m.getElement(3)) + " ";
039        int element4 = m.getElement(4);
040        if ((element4 & 0x01) == 0) {
041            text += "F61 " + Bundle.getMessage(FUNCTION_CONTINUOUS) + "; ";
042        } else {
043            text += "F61 " + Bundle.getMessage(FUNCTION_MOMENTARY) + "; ";
044        }
045        if ((element4 & 0x02) == 0) {
046            text += "F62 " + Bundle.getMessage(FUNCTION_CONTINUOUS) + "; ";
047        } else {
048            text += "F62 " + Bundle.getMessage(FUNCTION_MOMENTARY) + "; ";
049        }
050        if ((element4 & 0x04) == 0) {
051            text += "F63 " + Bundle.getMessage(FUNCTION_CONTINUOUS) + "; ";
052        } else {
053            text += "F63 " + Bundle.getMessage(FUNCTION_MOMENTARY) + "; ";
054        }
055        if ((element4 & 0x08) == 0) {
056            text += "F64 " + Bundle.getMessage(FUNCTION_CONTINUOUS) + "; ";
057        } else {
058            text += "F64 " + Bundle.getMessage(FUNCTION_MOMENTARY) + "; ";
059        }
060        if ((element4 & 0x10) == 0) {
061            text += "F65 " + Bundle.getMessage(FUNCTION_CONTINUOUS) + "; ";
062        } else {
063            text += "F65 " + Bundle.getMessage(FUNCTION_MOMENTARY) + "; ";
064        }
065        if ((element4 & 0x20) == 0) {
066            text += "F66 " + Bundle.getMessage(FUNCTION_CONTINUOUS) + "; ";
067        } else {
068            text += "F66 " + Bundle.getMessage(FUNCTION_MOMENTARY) + "; ";
069        }
070        if ((element4 & 0x40) == 0) {
071            text += "F67 " + Bundle.getMessage(FUNCTION_CONTINUOUS) + "; ";
072        } else {
073            text += "F67 " + Bundle.getMessage(FUNCTION_MOMENTARY) + "; ";
074        }
075        if ((element4 & 0x80) == 0) {
076            text += "F68 " + Bundle.getMessage(FUNCTION_CONTINUOUS) + "; ";
077        } else {
078            text += "F68 " + Bundle.getMessage(FUNCTION_MOMENTARY) + "; ";
079        }
080        return text;
081    }
082
083}