001package jmri.jmrix.powerline.insteon2412s;
002
003import jmri.jmrix.powerline.SerialTrafficController;
004import jmri.jmrix.powerline.X10Sequence;
005import jmri.util.StringUtil;
006import org.slf4j.Logger;
007import org.slf4j.LoggerFactory;
008
009/**
010 * Contains the data payload of a serial reply packet. Note that it's _only_ the
011 * payload.
012 *
013 * @author Bob Jacobsen Copyright (C) 2002, 2006, 2007, 2008, 2009 Converted to
014 * multiple connection
015 * @author kcameron Copyright (C) 2011
016 */
017public class SpecificReply extends jmri.jmrix.powerline.SerialReply {
018
019    // create a new one
020    public SpecificReply(SerialTrafficController tc) {
021        super(tc);
022        setBinary(true);
023    }
024
025    public SpecificReply(String s, SerialTrafficController tc) {
026        super(tc, s);
027        setBinary(true);
028    }
029
030    @Override
031    public String toMonitorString() {
032        // check for valid length
033        int len = getNumDataElements();
034        StringBuilder text = new StringBuilder();
035        if ((getElement(0) & 0xFF) != Constants.HEAD_STX) {
036            text.append("INVALID HEADER: " + String.format("0x%1X", getElement(0) & 0xFF));
037            text.append(" len: " + len);
038        } else {
039            switch (getElement(1) & 0xFF) {
040                case Constants.FUNCTION_REQ_STD:
041                    text.append("Send Cmd ");
042                    if (len == 8 || len == 22) {
043                        if ((getElement(5) & Constants.FLAG_BIT_STDEXT) == Constants.FLAG_STD) {
044                            text.append(" Std");
045                        } else if (len == 22) {
046                            text.append(" Ext");
047                        }
048                        switch (getElement(5) & Constants.FLAG_MASK_MSGTYPE) {
049                            case Constants.FLAG_TYPE_P2P:
050                                text.append(" Direct");
051                                break;
052                            case Constants.FLAG_TYPE_ACK:
053                                text.append(" ACK");
054                                break;
055                            case Constants.FLAG_TYPE_NAK:
056                                text.append(" NAK");
057                                break;
058                            case Constants.FLAG_TYPE_GBCAST:
059                                text.append(" Group Broadcast");
060                                break;
061                            case Constants.FLAG_TYPE_GBCLEANUP:
062                                text.append(" Group Broadcast Cleanup");
063                                break;
064                            case Constants.FLAG_TYPE_GBCLEANACK:
065                                text.append(" Group Broadcast Cleanup ACK");
066                                break;
067                            case Constants.FLAG_TYPE_GBCLEANNAK:
068                                text.append(" Group Broadcast Cleanup NAK");
069                                break;
070                            default:
071                                log.warn("Unhandled msg type: {}", getElement(5) & Constants.FLAG_MASK_MSGTYPE);
072                                break;
073                        }
074                        text.append(" message,");
075                        text.append(String.format(" %d hops left", (getElement(5) & Constants.FLAG_MASK_HOPSLEFT >> Constants.FLAG_SHIFT_HOPSLEFT)));
076                        text.append(String.format(" , %d max hops", (getElement(5) & Constants.FLAG_MASK_MAXHOPS)));
077                        text.append(" addr " + String.format("%1$X.%2$X.%3$X", (getElement(2) & 0xFF), (getElement(3) & 0xFF), (getElement(4) & 0xFF)));
078                        switch (getElement(6) & 0xFF) {
079                            case Constants.CMD_LIGHT_ON_FAST:
080                                text.append(" ON FAST ");
081                                text.append((getElement(7) & 0xFF) / 256.0);
082                                break;
083                            case Constants.CMD_LIGHT_ON_RAMP:
084                                text.append(" ON RAMP ");
085                                text.append((getElement(7) & 0xFF) / 256.0);
086                                break;
087                            case Constants.CMD_LIGHT_OFF_FAST:
088                                text.append(" OFF FAST ");
089                                text.append((getElement(7) & 0xFF) / 256.0);
090                                break;
091                            case Constants.CMD_LIGHT_OFF_RAMP:
092                                text.append(" OFF RAMP ");
093                                text.append((getElement(7) & 0xFF) / 256.0);
094                                break;
095                            case Constants.CMD_LIGHT_CHG:
096                                text.append(" CHG ");
097                                text.append((getElement(7) & 0xFF) / 256.0);
098                                break;
099                            default:
100                                text.append(" Unknown cmd: " + StringUtil.twoHexFromInt(getElement(6) & 0xFF));
101                                break;
102                        }
103                        if ((getElement(8) & 0xFF) == Constants.REPLY_NAK) {
104                            text.append(" NAK - command not processed");
105                        }
106                    } else {
107                        text.append(" !! Length wrong: " + len);
108                    }
109                    break;
110                case Constants.POLL_REQ_BUTTON:
111                    text.append("Poll Button ");
112                    int button = ((getElement(2) & Constants.BUTTON_BITS_ID) >> 4) + 1;
113                    text.append(button);
114                    int op = getElement(2) & Constants.BUTTON_BITS_OP;
115                    if (op == Constants.BUTTON_HELD) {
116                        text.append(" HELD");
117                    } else if (op == Constants.BUTTON_REL) {
118                        text.append(" RELEASED");
119                    } else if (op == Constants.BUTTON_TAP) {
120                        text.append(" TAP");
121                    }
122                    break;
123                case Constants.POLL_REQ_BUTTON_RESET:
124                    text.append("Reset by Button at Power Cycle");
125                    break;
126                case Constants.FUNCTION_REQ_X10:
127                    text.append("Send Cmd X10 ");
128                    if ((getElement(3) & Constants.FLAG_BIT_X10_CMDUNIT) == Constants.FLAG_X10_RECV_CMD) {
129                        text.append(X10Sequence.formatCommandByte(getElement(2) & 0xFF));
130                    } else {
131                        text.append(X10Sequence.formatAddressByte(getElement(2) & 0xFF));
132                    }
133                    if ((getElement(4) & 0xFF) == Constants.REPLY_NAK) {
134                        text.append(" NAK - command not processed");
135                    }
136                    break;
137                case Constants.POLL_REQ_X10:
138                    text.append("Poll Cmd X10 ");
139                    if ((getElement(3) & Constants.FLAG_BIT_X10_CMDUNIT) == Constants.FLAG_X10_RECV_CMD) {
140                        text.append(X10Sequence.formatCommandByte(getElement(2) & 0xFF));
141                    } else {
142                        text.append(X10Sequence.formatAddressByte(getElement(2) & 0xFF));
143                    }
144                    break;
145                default: {
146                    text.append(" Unknown command: " + StringUtil.twoHexFromInt(getElement(1) & 0xFF));
147                    text.append(" len: " + len);
148                }
149            }
150        }
151        return text + "\n";
152    }
153    private final static Logger log = LoggerFactory.getLogger(SpecificReply.class); // NOI18N
154
155}
156
157