001package jmri.jmrix.can.cbus.swing.cbusslotmonitor;
002
003import jmri.DccLocoAddress;
004import jmri.jmrix.can.cbus.CbusConstants;
005import jmri.jmrix.can.cbus.CbusOpCodes;
006
007/**
008 * Class to represent a session in the MERG CBUS Command Station Session Slot Monitor
009 *
010 * @see CbusSlotMonitorDataModel
011 * @author Steve Young Copyright (C) 2019
012 */
013public class CbusSlotMonitorSession  {
014
015    private final DccLocoAddress _locoAddr;
016    private int _sessionId;
017    private int _speed;
018    private String _speedSteps;
019    private boolean[] function;
020    private int _flags;
021    private int _consistId;
022
023    /**
024     * The table provides and maintains 1 row per loco address
025     *
026     * @param locoAddr Loco Address to be monitored
027     */    
028    protected CbusSlotMonitorSession( DccLocoAddress locoAddr ){
029        _locoAddr = locoAddr;
030        _sessionId = -1; // unset
031        _speed = 0;
032        _speedSteps="";
033        function = new boolean[29];
034        _flags = -1; // unset
035        _consistId = 0;
036    }
037
038    protected DccLocoAddress getLocoAddr(){
039        return _locoAddr;
040    }
041
042    protected void setSessionId( int session ) {
043        _sessionId = session;
044    }
045
046    protected int getSessionId() {
047        return _sessionId;
048    }
049
050    protected void setDccSpeed( int speed) {
051        _speed = speed;
052    }
053
054    protected String getCommandedSpeed() {
055        return CbusOpCodes.getSpeedFromByte(_speed);
056    }
057
058    protected String getDirection() {
059        return CbusOpCodes.getDirectionFromByte(_speed );
060    }
061
062    protected void setSpeedSteps ( String steps ) {
063        _speedSteps = steps;
064    }
065
066    protected String getSpeedSteps() {
067        if ( _speedSteps.isEmpty() ) {
068            return ("128");
069        }
070        else {
071            return _speedSteps;
072        }
073    }
074
075    protected void setFunction( int fn, boolean tof ) {
076        if (fn >= function.length) {
077            boolean[] newArray = new boolean[fn+1];
078            System.arraycopy(function, 0, newArray, 0, function.length);
079            function = newArray;
080        }
081        function[fn] = tof;
082    }
083
084    protected String getFunctionString() {
085        StringBuilder buf = new StringBuilder();
086        for (int i=0; i<function.length; i++) {
087            if ( function[i] ) {
088                buf.append(i);
089                buf.append(" ");
090            }
091        }
092        return buf.toString().trim();
093    }
094
095    protected void setFlags( int flags ){
096        _flags = flags;
097        int mask = 0b11; // last 2 bits
098        switch (flags & mask){
099            case CbusConstants.CBUS_SS_14:
100                _speedSteps="14";
101                break;
102            case CbusConstants.CBUS_SS_28_INTERLEAVE:
103                _speedSteps="28I";
104                break;
105            case CbusConstants.CBUS_SS_28:
106                _speedSteps="28";
107                break;
108            default:
109                _speedSteps="128";
110        }
111    }
112
113    protected String getFlagString() {
114        
115        if ( _flags < 0 ){
116            return ("");
117        }
118        
119        StringBuilder flagstring = new StringBuilder();
120        
121        boolean esa = ((_flags >> 4 ) & 1) != 0; // bit4
122        boolean esb = ((_flags >> 5 ) & 1) != 0; // bit5
123        flagstring.append(Bundle.getMessage("EngineState"));
124        if ((!esa) && (!esb)){
125            flagstring.append(Bundle.getMessage("Active"));
126        }
127        else if ((!esa) && (esb)){
128            flagstring.append(Bundle.getMessage("Consisted"));
129        }        
130        else if ((esa) && (!esb)){
131            flagstring.append(Bundle.getMessage("Consistmaster"));
132        }        
133        else if ((esa) && (esb)){
134            flagstring.append(Bundle.getMessage("Inactive"));
135        }            
136        flagstring.append(" ");            
137        flagstring.append(Bundle.getMessage("Lights"));
138        flagstring.append(((_flags >> 2 ) & 1)); // bit2
139        flagstring.append(" ");
140        flagstring.append(Bundle.getMessage("RelDirection"));
141        flagstring.append(((_flags >> 3 ) & 1)); // bit3
142        flagstring.append(" ");
143        
144        return flagstring.toString();
145    }
146
147    protected void setConsistId( int consistid ) {
148        _consistId = consistid;
149    }
150
151    protected int getConsistId() {
152        return _consistId;
153    }
154
155}