001package jmri.jmrix.can.cbus.node;
002
003// import javax.annotation.Nonnull;
004import javax.annotation.*;
005
006import jmri.jmrix.can.CanSystemConnectionMemo;
007
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010
011/**
012 * Class to represent a node.
013 *
014 * @author Steve Young Copyright (C) 2019
015 */
016public class CbusBasicNodeWithManagers extends CbusBasicNode {
017    
018    private final CbusNodeTimerManager _timers;
019    private final CbusNodeParameterManager _nodeParameters;
020    private final CbusNodeNVManager _nvManager;
021    private final CbusNodeEventManager _evManager;
022    private final CbusNodeStats _nodeStats;
023    private final CbusNodeBackupManager thisNodeBackupFile;
024    private CbusNodeCanListener _canListener;
025    private CbusNodeTableDataModel tableModel;
026    
027    protected int _fwMaj;
028    protected int _fwMin;
029    protected int _fwBuild;
030    private int _manu;
031    private int _type;
032    
033    private String _nodeUserName;
034    
035    /**
036     * Create a new CbusBasicNodeWithManagers
037     *
038     * @param connmemo The CAN Connection to use
039     * @param nodenumber The Node Number
040     */
041    public CbusBasicNodeWithManagers ( @CheckForNull CanSystemConnectionMemo connmemo, int nodenumber ){
042        super(connmemo,nodenumber);
043        
044        
045        _nodeUserName = "";
046        
047        if (_memo != null) {
048            log.debug("New CanListener {}",this.getCanListener());
049        }
050        _timers = new CbusNodeTimerManager(this);
051        _nodeParameters = new CbusNodeParameterManager(this);
052        _nvManager = new CbusNodeNVManager(this);
053        _evManager = new CbusNodeEventManager(_memo,this);
054        thisNodeBackupFile = new CbusNodeBackupManager(this);
055        _nodeStats = new CbusNodeStats(this);
056        
057        _manu = -1;
058        _type = -1;
059        setFW( -1, -1, -1);
060        
061    }
062    
063    @Nonnull
064    public CbusNodeCanListener getNewCanListener(){
065        return new CbusNodeCanListener(_memo,this);
066    }
067    
068    @Nonnull
069    public final CbusNodeCanListener getCanListener(){
070        if (_canListener==null){
071            _canListener = getNewCanListener();
072        }
073        return _canListener;
074    }
075    
076    @Nonnull
077    public final CbusNodeTimerManager getNodeTimerManager(){
078        return _timers;
079    }
080    
081    @Nonnull
082    public final CbusNodeParameterManager getNodeParamManager() {
083        return _nodeParameters;
084    }
085    
086    @Nonnull
087    public final CbusNodeNVManager getNodeNvManager() {
088        return _nvManager;
089    }
090    
091    @Nonnull
092    public final CbusNodeEventManager getNodeEventManager() {
093        return _evManager;
094    }
095    
096    /**
097     * Get the CbusNodeXml for Node Backup Details and operations
098     * @return the CbusNodeXml instance for the node
099     */
100    @Nonnull
101    public final CbusNodeBackupManager getNodeBackupManager() {
102        return thisNodeBackupFile;
103    }
104    
105    /**
106     * Get Node Statistics
107     *
108     * @return the CbusNodeXml instance for the node
109     */
110    @Nonnull
111    public final CbusNodeStats getNodeStats() {
112        return _nodeStats;
113    }
114    
115
116    /**
117     * Set the main Node Table Model
118     * @param model the Node Table Model
119     */
120    protected void setTableModel( CbusNodeTableDataModel model){
121        tableModel = model;
122    }
123    
124    protected CbusNodeTableDataModel getTableModel() {
125        return tableModel;
126    }
127    
128
129    
130    
131    /**
132     * Temporarily store Node Firmware version obtained from a CBUS_STAT Response
133     * <p>
134     * Parameter array is not created until total number of parameters is known.
135     * This saves asking the Node for them.
136     *
137     * @param fwMaj Major Firmware Type
138     * @param fwMin Minor Firmware Type
139     * @param fwBuild Firmware Build Number
140     */
141    public final void setFW( int fwMaj, int fwMin, int fwBuild ){
142        _fwMaj = fwMaj;
143        _fwMin = fwMin;
144        _fwBuild = fwBuild;
145    }
146    
147    /**
148     * Temporarily store Node Manufacturer and Module Type obtained from a PNN Response
149     * <p>
150     * Parameter array is not created until total number of parameters is known.
151     * This saves asking the Node for them.
152     *
153     * @param manu Manufacturer
154     * @param modtype Module Type
155     */
156    protected void setManuModule(int manu, int modtype){
157        _manu = manu;
158        _type = modtype;
159    }
160    
161    protected int getPnnManufacturer() {
162        return _manu;
163    }
164    
165    protected int getPnnModule() {
166        return _type;
167    }
168
169    /**
170     * Returns node Username 
171     * @return eg. "John Smith"
172     *
173     */
174    public String getUserName() {
175        return _nodeUserName;
176    }
177    
178    /**
179     * Set Node UserName
180     * Updates Node XML File
181     * @param newName UserName of the node
182     */
183    public void setUserName( String newName ) {
184        _nodeUserName = newName;
185        notifyPropertyChangeListener("NAMECHANGE", null, newName);
186        if (getNodeBackupManager().getBackupStarted()) {
187            if (!getNodeBackupManager().doStore(false,getNodeStats().hasLoadErrors()) ) {
188                log.error("Unable to save Node Name to Node {} Backup File",this);
189            }
190        }
191    }
192    
193    /**
194     * Stops any timers and disconnects from network
195     *
196     */
197    @OverridingMethodsMustInvokeSuper
198    public void dispose(){
199        getCanListener().dispose();
200        getNodeTimerManager().cancelTimers(); // cancel timers
201    }
202    
203    private static final Logger log = LoggerFactory.getLogger(CbusBasicNodeWithManagers.class);
204    
205}