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