001package jmri.jmrix.can.cbus.swing.eventrequestmonitor;
002
003import java.awt.event.ActionEvent;
004import java.awt.event.ActionListener;
005import java.util.Date;
006import javax.swing.Timer;
007import jmri.jmrix.can.cbus.CbusEvent;
008
009// import org.slf4j.Logger;
010// import org.slf4j.LoggerFactory;
011
012/**
013 * Class to represent a request event in the MERG CBUS event request monitor table
014 *
015 * @author Steve Young Copyright (C) 2019
016 */
017public class CbusEventRequestMonitorEvent extends CbusEvent {
018    
019    // last feedback state
020    public enum FbState{
021        LfbFinding, LfbGood, LfbBad, LfbUnknown;
022    }
023    
024    private ActionListener eventFeedbackListener;
025    private final CbusEventRequestDataModel _model;
026    private Date _timestamp;
027    private int _feedbackTimeout;
028    private int _feedbackTotReqd;
029    private int _extraEvent;
030    private int _extraNode;
031    private int _feedbackOutstanding;
032    private FbState _lfb;
033    private Timer _timer;
034    
035    public CbusEventRequestMonitorEvent( int nn, int en, 
036        EvState state, Date timestamp, int feedbackTimeout, int feedbackTotReqd,
037        CbusEventRequestDataModel model ){
038        
039        super(nn,en);
040        _state = state;
041        _model = model;
042        _timestamp = timestamp;
043        _feedbackTimeout = feedbackTimeout;
044        _feedbackTotReqd = feedbackTotReqd;
045        _extraEvent = 0;
046        _extraNode = 0;
047        _feedbackOutstanding = 0;
048        _lfb = FbState.LfbUnknown;
049        _timer = null;
050    }
051    
052    public Boolean matchesFeedback(int nn, int en) {
053        return (nn == _extraNode) && (en == _extraEvent);
054    }
055    
056    protected Date getDate(){
057        return _timestamp;
058    }
059    
060    protected void setDate(Date newval) {
061        _timestamp = newval;
062    }
063
064    protected void setFeedbackTimeout(int newval) {
065        _feedbackTimeout = newval;
066    }
067    
068    protected int getFeedbackTimeout() {
069        return _feedbackTimeout;
070    }
071    
072    protected int getFeedbackTotReqd() {
073        return _feedbackTotReqd;
074    }
075    
076    protected void setFeedbackTotReqd( int newval ) {
077        _feedbackTotReqd = newval;
078    }
079    
080    protected int getExtraEvent(){
081        return _extraEvent;
082    }
083
084    protected int getExtraNode(){
085        return _extraNode;
086    }
087    
088    protected void setExtraEvent( int newval ) {
089        _extraEvent = newval;
090    }
091
092    protected void setExtraNode( int newval ) {
093        _extraNode = newval;
094    }
095    
096    protected void setFeedbackOutstanding( int newval ) {
097        _feedbackOutstanding = newval;
098    }
099
100    protected int getFeedbackOutstanding() {
101        return _feedbackOutstanding;
102    }
103    
104    protected FbState getLastFb() {
105        return _lfb;
106    }
107
108    protected void setLastFb( FbState newval ) {
109        _lfb = newval;
110    }
111    
112    protected void setRequestReceived(){
113        _feedbackOutstanding = _feedbackTotReqd;
114        
115       // getFeedbackOutstanding
116        _model.setValueAt(FbState.LfbFinding, _model.eventRow(getNn(),getEn()), 
117            CbusEventRequestDataModel.LASTFEEDBACK_COLUMN);
118        
119        _model.setValueAt(getFeedbackOutstanding(), _model.eventRow(getNn(),getEn()), 
120            CbusEventRequestDataModel.FEEDBACKOUTSTANDING_COLUMN);
121        if ( getFeedbackTotReqd() > 0 ) {
122            startTheTimer();
123        }
124    }
125    
126    private void startTheTimer(){
127        
128        final String _evName = this.toString();
129        eventFeedbackListener = (ActionEvent e) -> {
130            _model.setValueAt(0, _model.eventRow( getNn(),getEn() ), 
131                    CbusEventRequestDataModel.FEEDBACKOUTSTANDING_COLUMN);
132            _model.setValueAt(CbusEventRequestMonitorEvent.FbState.LfbBad, _model.eventRow(getNn(),getEn()),
133                    CbusEventRequestDataModel.LASTFEEDBACK_COLUMN);
134            _model.addToLog(3, Bundle.getMessage("FeedBackNotOK", _evName ) );
135            _timer.stop();
136            _timer = null;
137        };
138        _timer = new Timer( getFeedbackTimeout(), eventFeedbackListener);
139        _timer.setRepeats( false );
140        _timer.start();        
141    }
142
143    protected void stopTheTimer() {
144        if ( _timer !=null ) {
145            _timer.stop();
146            _timer=null;
147        }
148        eventFeedbackListener=null;
149    }
150    
151    protected void setResponseReceived(){
152        if ( _feedbackOutstanding < 0 ) {
153            return;
154        }
155        _feedbackOutstanding--;
156        
157        if ( _feedbackOutstanding > 0 ) {
158            _model.setValueAt(_feedbackOutstanding, _model.eventRow(getNn(),getEn()), 
159                CbusEventRequestDataModel.FEEDBACKOUTSTANDING_COLUMN);
160        }
161        if ( _feedbackOutstanding == 0 ) {
162            _model.setValueAt(_feedbackOutstanding, _model.eventRow(getNn(),getEn()), 
163                CbusEventRequestDataModel.FEEDBACKOUTSTANDING_COLUMN);
164            if ( _timer != null ) {
165                stopTheTimer();
166                _model.setValueAt(FbState.LfbGood, _model.eventRow(getNn(),getEn()),
167                    CbusEventRequestDataModel.LASTFEEDBACK_COLUMN);
168                _model.addToLog(2,Bundle.getMessage("FeedBackOK",this.toString() ) );
169            }
170        }
171    }
172    
173    /** 
174     * {@inheritDoc} 
175     */
176    @Override
177    public boolean equals(Object o) {
178        return super.equals(o);
179    }
180    
181    /** {@inheritDoc} */
182    @Override
183    public int hashCode() {
184        return super.hashCode();
185    }
186
187    // private final static Logger log = LoggerFactory.getLogger(CbusEventRequestMonitorEvent.class);
188
189}