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: ").append( String.format("0x%1X", getElement(0) & 0xFF));
035            text.append(" len: ").append( 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 ").append( String.format("%1$X.%2$X.%3$X",
047                            (getElement(2) & 0xFF), (getElement(3) & 0xFF), (getElement(4) & 0xFF)));
048                        switch (getElement(6) & 0xFF) {
049                            case Constants.CMD_LIGHT_ON_FAST:
050                                text.append(" ON FAST ");
051                                text.append((getElement(7) & 0xFF) / 256.0);
052                                break;
053                            case Constants.CMD_LIGHT_ON_RAMP:
054                                text.append(" ON RAMP ");
055                                text.append((getElement(7) & 0xFF) / 256.0);
056                                break;
057                            case Constants.CMD_LIGHT_OFF_FAST:
058                                text.append(" OFF FAST ");
059                                text.append((getElement(7) & 0xFF) / 256.0);
060                                break;
061                            case Constants.CMD_LIGHT_OFF_RAMP:
062                                text.append(" OFF RAMP ");
063                                text.append((getElement(7) & 0xFF) / 256.0);
064                                break;
065                            case Constants.CMD_LIGHT_CHG:
066                                text.append(" CHG ");
067                                text.append((getElement(7) & 0xFF) / 256.0);
068                                break;
069                            default:
070                                text.append(" Unknown cmd: ").append( StringUtil.twoHexFromInt(getElement(6) & 0xFF));
071                                break;
072                        }
073                        if ((getElement(8) & 0xFF) == Constants.REPLY_NAK) {
074                            text.append(" NAK - command not processed");
075                        }
076                    } else {
077                        text.append(" !! Length wrong: ").append(len);
078                    }
079                    break;
080                case Constants.POLL_REQ_BUTTON:
081                    text.append("Poll Button ");
082                    int button = ((getElement(2) & Constants.BUTTON_BITS_ID) >> 4) + 1;
083                    text.append(button);
084                    int op = getElement(2) & Constants.BUTTON_BITS_OP;
085                    if (op == Constants.BUTTON_HELD) {
086                        text.append(" HELD");
087                    } else if (op == Constants.BUTTON_REL) {
088                        text.append(" RELEASED");
089                    } else if (op == Constants.BUTTON_TAP) {
090                        text.append(" TAP");
091                    }
092                    break;
093                case Constants.POLL_REQ_BUTTON_RESET:
094                    text.append("Reset by Button at Power Cycle");
095                    break;
096                case Constants.FUNCTION_REQ_X10:
097                    text.append("Send Cmd X10 ");
098                    if ((getElement(3) & Constants.FLAG_BIT_X10_CMDUNIT) == Constants.FLAG_X10_RECV_CMD) {
099                        text.append(X10Sequence.formatCommandByte(getElement(2) & 0xFF));
100                    } else {
101                        text.append(X10Sequence.formatAddressByte(getElement(2) & 0xFF));
102                    }
103                    if ((getElement(4) & 0xFF) == Constants.REPLY_NAK) {
104                        text.append(" NAK - command not processed");
105                    }
106                    break;
107                case Constants.POLL_REQ_X10:
108                    text.append("Poll Cmd X10 ");
109                    if ((getElement(3) & Constants.FLAG_BIT_X10_CMDUNIT) == Constants.FLAG_X10_RECV_CMD) {
110                        text.append(X10Sequence.formatCommandByte(getElement(2) & 0xFF));
111                    } else {
112                        text.append(X10Sequence.formatAddressByte(getElement(2) & 0xFF));
113                    }
114                    break;
115                default: {
116                    text.append(" Unknown command: ").append(StringUtil.twoHexFromInt(getElement(1) & 0xFF));
117                    text.append(" len: ").append(len);
118                }
119            }
120        }
121        return text + "\n";
122    }
123
124}
125
126