001package jmri.jmrix.can.cbus.simulator; 002 003import java.awt.event.ActionListener; 004import javax.swing.Timer; 005import jmri.jmrix.can.CanReply; 006import jmri.jmrix.can.cbus.CbusConstants; 007 008import org.slf4j.Logger; 009import org.slf4j.LoggerFactory; 010 011/** 012 * Simulating a MERG CBUS Command Station Loco Session. 013 * 014 * @author Steve Young Copyright (C) 2018 2019 015 * @see CbusDummyCS 016 * @see CbusSimulator 017 * @since 4.15.2 018 */ 019public class CbusDummyCSSession { 020 021 private CbusDummyCS _cs; 022 private int _sessionID; 023 private int _Addr; 024 private Boolean _isLong; 025 private int _speedDirection; 026 private int _fa; 027 private int _fb; 028 private int _fc; 029 private Timer _RefreshTimer; 030 private Boolean _dispatched; 031 032 public CbusDummyCSSession (CbusDummyCS cs, int sessionID, int rcvdIntAddr,Boolean rcvdIsLong ) { 033 _cs = cs; 034 _sessionID = sessionID; 035 _Addr = rcvdIntAddr; 036 _isLong = rcvdIsLong; 037 init(); 038 } 039 040 private void init(){ 041 _speedDirection = CbusDummyCS.DEFAULT_SESSION_START_SPDDIR; 042 _fa = 0; 043 _fb = 0; 044 _fc = 0; 045 _dispatched=false; 046 _RefreshTimer = new Timer(CbusDummyCS.DEFAULT_CS_TIMEOUT, new ActionListener() { 047 @Override 048 public void actionPerformed(java.awt.event.ActionEvent e) { 049 csSessionTimeout(); 050 } 051 }); 052 _RefreshTimer.setRepeats(false); 053 _RefreshTimer.start(); 054 } 055 056 protected void dispose(){ 057 _RefreshTimer.stop(); 058 _RefreshTimer = null; 059 } 060 061 private void csSessionTimeout() { 062 log.info("Session {} Timeout",_sessionID); 063 if (_cs.getDummyType() == 1 ) { // CANCMD v3 064 _cs.destroySession(this); 065 } 066 } 067 068 protected void keepAlive() { 069 _RefreshTimer.restart(); 070 } 071 072 protected void sendPloc() { 073 int locoaddr = _Addr; 074 if (_isLong) { 075 locoaddr = locoaddr | 0xC000; 076 } 077 CanReply r = new CanReply(7); 078 r.setElement(0, CbusConstants.CBUS_PLOC); 079 r.setElement(1, _sessionID); 080 r.setElement(2, (locoaddr / 256)); // addr hi 081 r.setElement(3, locoaddr & 0xff); // addr low 082 r.setElement(4, _speedDirection); 083 r.setElement(5, _fa); 084 r.setElement(6, _fb); 085 r.setElement(7, _fc); 086 _cs.send.sendWithDelay(r,_cs.getSendIn(),_cs.getSendOut(),_cs.getDelay()); 087 } 088 089 protected int getSessionNum() { 090 return _sessionID; 091 } 092 093 protected int getrcvdIntAddr() { 094 return _Addr; 095 } 096 097 protected Boolean getisLong() { 098 return _isLong; 099 } 100 101 protected void setSpd( int speeddir) { 102 _speedDirection = speeddir; 103 _RefreshTimer.restart(); 104 } 105 106 protected Boolean getIsDispatched() { 107 return _dispatched; 108 } 109 110 private static final Logger log = LoggerFactory.getLogger(CbusDummyCSSession.class); 111 112}