001package jmri.jmrix.can.cbus.eventtable;
002
003import java.util.Date;
004import java.util.HashSet;
005import java.util.Set;
006import jmri.NamedBean;
007import jmri.jmrix.can.cbus.CbusConstants;
008import jmri.jmrix.can.cbus.CbusEvent;
009
010// import org.slf4j.Logger;
011// import org.slf4j.LoggerFactory;
012
013/**
014 * Class to represent an event in the MERG CBUS event table
015 *
016 * @author Steve Young Copyright (C) 2019
017 */
018public class CbusTableEvent extends CbusEvent {
019    
020    private int _canid;
021    private String _comment;
022    private int _sesson;
023    private int _toton;
024    private int _sessoff;
025    private int _totoff;
026    private int _sessin;
027    private int _totin;
028    private int _sessout;
029    private int _totout;
030    private Set<NamedBean> _nbOnActiveA;
031    private Set<NamedBean> _nbOffActiveA;
032    private Set<NamedBean> _nbOnActiveB;
033    private Set<NamedBean> _nbOffActiveB;
034    private Date _timestamp;
035    
036    public CbusTableEvent( jmri.jmrix.can.CanSystemConnectionMemo memo, int nn, int en ){
037        
038        super(memo,nn,en);
039        _canid = -1;
040        _name = "";
041        _comment = "";
042        _sesson = 0;
043        _sessoff = 0;
044        _sessin = 0;
045        _sessout = 0;
046        _timestamp = null;
047        resetBeans();
048        
049    }
050    
051    /**
052     * Updates Event State and session / total on and off's.
053     * {@inheritDoc}
054     */
055    @Override
056    public void setState( EvState newval ) {
057        super.setState(newval);
058        if (newval == CbusTableEvent.EvState.ON) {
059            _sesson++;
060            _toton++;
061            setDate( new Date() );
062        } 
063        else if (newval == CbusTableEvent.EvState.OFF) {
064            _sessoff++;
065            _totoff++;
066            setDate( new Date() );
067        }
068    }
069
070    /**
071     * Get the last-seen date time.
072     * @return The last time the event was heard on the network
073     */    
074    protected Date getDate(){
075        if (_timestamp!=null) {
076            return new Date(_timestamp.getTime());
077        }
078        return null;
079    }
080
081    /**
082     * Set the last-seen date time
083     * @param newval the last-seen date time
084     */       
085    protected void setDate(Date newval) {
086        _timestamp = newval;
087    }
088        
089    /**
090     * Get the Sensor Turnout and Light user names associated with event on or off.
091     * @param state CbusEvent State of ON or OFF
092     * @return Sensor Turnout and Light set.
093     */   
094    protected CbusEventBeanData getBeans(EvState state){
095        return new CbusEventBeanData( _nbOnActiveA, _nbOnActiveB, _nbOffActiveA, _nbOffActiveB, state);
096    }
097    
098    protected final void resetBeans(){
099        _nbOnActiveA = new HashSet<>();
100        _nbOnActiveB = new HashSet<>();
101        _nbOffActiveA = new HashSet<>();
102        _nbOffActiveB = new HashSet<>();
103    }
104    
105    public void appendOnOffBean(NamedBean nb, boolean beanState, EvState evState ){
106        if (evState==EvState.ON) {
107            ( beanState ? _nbOnActiveA : _nbOnActiveB ).add(nb);
108        } else if (evState==EvState.OFF) {
109            ( beanState ? _nbOffActiveA : _nbOffActiveB ).add(nb);
110        }
111    }
112
113    /**
114     * Get the CAN ID to last send the event
115     * @return CAN ID
116     */
117    protected int getEventCanId(){
118        return _canid;
119    }
120
121    /**
122     * Set the event comment
123     * @param newval Comment String
124     */    
125    public void setComment(String newval){
126        _comment = newval;
127    }
128
129    /**
130     * Get the event comment
131     * @return Comment String
132     */
133    protected String getComment(){
134        return _comment;
135    }
136
137    /**
138     * Set the CAN ID to last send the event
139     * @param newval CAN ID
140     */
141    protected void setCanId(int newval){
142        _canid = newval;
143    }
144    
145    /**
146     * Set Event Counts.
147     * @param on Total On
148     * @param off Total Off
149     * @param in Total In
150     * @param out Total Out
151     */
152    protected void setCounts( int on, int off, int in, int out) {
153        _toton = on;
154        _totoff = off;
155        _totin = in;
156        _totout = out;
157    }
158
159    /**
160     * Number of times event on or off for current session.
161     * @param on true for on, false for off
162     * @return Number of times event on for current session
163     */
164    protected int getSessionOnOff(boolean on){
165        return (on ? _sesson : _sessoff );
166    }
167    
168    /**
169     * Number of times event on or off all sessions.
170     * @param on true for on, false for off.
171     * @return Number of times event on or off all sessions.
172     */
173    protected int getTotalOnOff(boolean on){
174        return (on ? _toton : _totoff );
175    }
176    
177    /**
178     * Number of times event heard coming in to JMRI this session.
179     * @param in true for in, false for out.
180     * @return Number of times event heard coming in to JMRI this session
181     */
182    protected int getSessionInOut(boolean in){
183        return (in ? _sessin : _sessout );
184    }
185    
186    /**
187     * Number of times event heard, all sessions.
188     * @param in true for in, false for out.
189     * @return Number of times event heard coming in or out to JMRI all sessions
190     */
191    protected int getTotalInOut(boolean in){
192        return (in ? _totin : _totout );
193    }
194    
195    /**
196     * Increase Direction session and total counts.
197     * @param direction CbusConstant of EVENT_DIR_IN or EVENT_DIR_OUT
198     */
199    protected void bumpDirection(int direction){
200        if ( direction == CbusConstants.EVENT_DIR_IN ){
201            _sessin++;
202            _totin++;
203        }
204        else {
205            _sessout++;
206            _totout++;
207        }
208    }
209    
210    /**
211     * Reset on, off, in and out session counts to 0
212     */
213    protected void resetSessionTotals(){
214        _sesson = 0;
215        _sessoff = 0;
216        _sessin = 0;
217        _sessout = 0;
218    }
219    
220    /** 
221     * {@inheritDoc} 
222     */
223    @Override
224    public boolean equals(Object o) {
225        return super.equals(o);
226    }
227    
228    
229    /** {@inheritDoc} */
230    @Override
231    public int hashCode() {
232        return super.hashCode();
233    }
234    
235    // private final static Logger log = LoggerFactory.getLogger(CbusTableEvent.class);
236
237}