001package jmri.jmrix.roco.z21.messageformatters;
002
003import jmri.jmrix.Message;
004import jmri.jmrix.roco.z21.Z21MessageFormatter;
005import jmri.jmrix.roco.z21.Z21Reply;
006
007/**
008 * Class for formatting the Z21 RMBus Feedback Reply
009 *
010 * @author Paul Bender Copyright (C) 2025
011 */
012public class Z21RMBusFeedbackReplyFormatter implements Z21MessageFormatter {
013
014    @Override
015    public boolean handlesMessage(Message m) {
016        return m instanceof Z21Reply && ((Z21Reply) m).getOpCode() == 0x0080;
017    }
018
019    @Override
020    public String formatMessage(Message m) {
021        if(!handlesMessage(m)){
022            throw new IllegalArgumentException("Message is not supported");
023        }
024               int groupIndex = m.getElement(4) & 0xff;
025               int offset = (groupIndex * 10) + 1;
026               String[] moduleStatus = new String[10];
027        for(int i=0;i<10;i++){
028               moduleStatus[i]= Bundle.getMessage("RMModuleFeedbackStatus",offset+i,
029                   getState(1,(m.getElement(i+5)&0x01)==0x01),
030                   getState(2,(m.getElement(i+5)&0x02)==0x02),
031                   getState(3,(m.getElement(i+5)&0x04)==0x04),
032                   getState(4,(m.getElement(i+5)&0x08)==0x08),
033                   getState(5,(m.getElement(i+5)&0x10)==0x10),
034                   getState(6,(m.getElement(i+5)&0x20)==0x20),
035                   getState(7,(m.getElement(i+5)&0x40)==0x40),
036                   getState(8,(m.getElement(i+5)&0x80)==0x80));
037        }
038        return Bundle.getMessage("RMBusFeedbackStatus",groupIndex,
039               moduleStatus[0],moduleStatus[1],moduleStatus[2],
040               moduleStatus[3],moduleStatus[4],moduleStatus[5],
041               moduleStatus[6],moduleStatus[7],moduleStatus[8],
042               moduleStatus[9]);
043    }
044
045    String getState(int contact,boolean on){
046
047        return Bundle.getMessage("RMModuleContactStatus",contact,
048                on ? Bundle.getMessage("PowerStateOn") : Bundle.getMessage("PowerStateOff"));
049    }
050
051}