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