001package jmri.jmrix.can.cbus.swing.modeswitcher;
002
003import java.awt.BorderLayout;
004
005import javax.swing.BorderFactory;
006import javax.swing.BoxLayout;
007import javax.swing.JLabel;
008import javax.swing.JPanel;
009
010import jmri.GlobalProgrammerManager;
011import jmri.InstanceManager;
012import jmri.jmrix.can.*;
013import jmri.jmrix.can.cbus.CbusDccProgrammerManager;
014import jmri.jmrix.can.cbus.CbusPreferences;
015import jmri.jmrix.can.cbus.CbusSend;
016import jmri.jmrix.can.cbus.node.CbusNode;
017import jmri.jmrix.can.cbus.node.CbusNodeTableDataModel;
018import jmri.jmrix.can.ConfigurationManager.ProgModeSwitch;
019import jmri.util.JmriJFrame;
020
021import org.slf4j.Logger;
022import org.slf4j.LoggerFactory;
023
024/**
025 * Base class for CBUS SPROG Mode Switcher .
026 *
027 * @author Andrew Crosland Copyright (C) 2020
028 */
029public class SprogCbusModeSwitcherFrame extends JmriJFrame 
030        implements CanListener {
031    
032    protected static final int PROP_CMD_STATION = 5;
033    
034    protected CbusPreferences preferences;
035    protected CbusDccProgrammerManager pm = null;
036    protected CanSystemConnectionMemo _memo = null;
037    protected ProgModeSwitch _pms;
038    protected TrafficController tc;
039    protected CbusSend send;
040    
041    protected JLabel label = null;
042    protected JPanel panel = null;
043    protected JPanel modePane = null;
044
045    protected int mode;
046
047    int csNode;
048
049    public SprogCbusModeSwitcherFrame(CanSystemConnectionMemo memo, String s) {
050        super(s);
051        _memo = memo;
052        _pms = memo.getProgModeSwitch();
053        
054        preferences = memo.get(jmri.jmrix.can.cbus.CbusPreferences.class);
055
056        pm = (CbusDccProgrammerManager)InstanceManager.getNullableDefault(GlobalProgrammerManager.class);
057        
058        // connect to the CanInterface
059        tc = _memo.getTrafficController();
060        addTc(tc);
061    }
062    
063    
064    protected boolean initSetup()  {
065        label = new JLabel();
066        panel = new JPanel(new BorderLayout());
067        
068        if (pm == null) {
069            // Wrap  in html to get wrapping when added to border layout
070            label.setText("<html>"+Bundle.getMessage("NoCbusProgrammer")+"</html>");
071            panel.add(label, BorderLayout.NORTH);
072            return false;
073        } else {
074            csNode = 65534;
075            CbusNodeTableDataModel cs =  _memo.get(CbusNodeTableDataModel.class);
076            if (cs != null) {
077                CbusNode csnode = cs.getCsByNum(0);
078                if (csnode != null) {
079                    csNode = csnode.getNodeNumber();
080                }
081            } else {
082                log.info("Unable to fetch Master Command Station from Node Manager");
083            }
084            send = new CbusSend(_memo);
085            
086            // Wrap  in html to get wrapping when added to border layout
087            label.setText("<html>"+Bundle.getMessage("HardwareModeLabel")+"</html>");
088
089            // Mode selector
090            modePane = new JPanel();
091            modePane.setLayout(new BoxLayout(modePane, BoxLayout.Y_AXIS));
092            modePane.setBorder(BorderFactory.createTitledBorder(
093            BorderFactory.createEtchedBorder(), Bundle.getMessage("HardwareMode")));
094            
095            return true;
096        }
097    }
098    
099
100    /**
101     * Switch the hardware to the requested operating mode
102     * 
103     * @param mode mode requested
104     */
105    protected void setHardwareMode(int mode) {
106        send.nVSET(csNode, PROP_CMD_STATION, mode);
107    }
108    
109    
110    /**
111     * Process outgoing CAN messages
112     * 
113     * {@inheritDoc} 
114     */
115    @Override
116    public void message(CanMessage m) {
117    }
118    
119    
120    /**
121     * Processes incoming CAN replies
122     * <p>
123     *
124     * {@inheritDoc} 
125     */
126    @Override
127    public void reply(CanReply r) {
128        
129        if ( r.isRtr() ) {
130            return;
131        }
132        
133        if (!r.isExtended() ) {
134            log.debug("Standard Reply {}", r);
135            
136        }
137    }
138    
139    
140    /**
141     * disconnect from the CBUS
142     */
143    @Override
144    public void dispose() {
145        tc.removeCanListener(this);
146        super.dispose();
147    }
148
149    
150    private final static Logger log = LoggerFactory.getLogger(SprogCbusModeSwitcherFrame.class);
151    
152}