001package jmri.jmrix.can.cbus.simulator; 002 003import javax.annotation.Nonnull; 004 005import java.util.ArrayList; 006import jmri.jmrix.can.CanSystemConnectionMemo; 007 008import org.slf4j.Logger; 009import org.slf4j.LoggerFactory; 010 011/** 012 * Simulating a MERG CBUS Command Station + other network objects. 013 * <p> 014 * By default starts with 1 command station, a node in SLiM mode, 015 * and an event request responder. 016 * <p> 017 * All simulation responses can be sent as {@link jmri.jmrix.can.CanMessage} or {@link jmri.jmrix.can.CanReply} 018 * 019 * @author Steve Young Copyright (C) 2018 020 * @see CbusDummyCS 021 * @see CbusEventResponder 022 * @see CbusDummyNode 023 * @since 4.15.2 024 */ 025public class CbusSimulator implements jmri.Disposable { 026 027 private final CanSystemConnectionMemo memo; 028 public ArrayList<CbusDummyCS> _csArr; 029 public ArrayList<CbusDummyNode> _ndArr; 030 public ArrayList<CbusEventResponder> _evResponseArr; 031 032 public CbusSimulator(@Nonnull CanSystemConnectionMemo sysmemo){ 033 memo = sysmemo; 034 _csArr = new ArrayList<>(); 035 _ndArr = new ArrayList<>(); 036 _evResponseArr = new ArrayList<>(); 037 init(); 038 } 039 040 public final void init(){ 041 log.info("Starting CBUS Network Simulation Tools"); 042 043 _csArr.add(new CbusDummyCS(memo)); // type, id, memo 044 045 046 // _ndArr.add(new CbusDummyNode(0,165,0,0,memo)); // nn, manufacturer, type, canid, memo 047 048 049 _evResponseArr.add(new CbusEventResponder(memo) ); 050 } 051 052 public int getNumCS(){ 053 return _csArr.size(); 054 } 055 056 public int getNumNd(){ 057 return _ndArr.size(); 058 } 059 060 public int getNumEv(){ 061 return _evResponseArr.size(); 062 } 063 064 public CbusDummyCS getCS(int id){ 065 return _csArr.get(id); 066 } 067 068 public CbusDummyNode getNd(int id){ 069 return _ndArr.get(id); 070 } 071 072 public void addNode ( CbusDummyNode nd) { 073 _ndArr.add(nd); 074 } 075 076 public void removeNode(CbusDummyNode nd) { 077 _ndArr.remove(nd); 078 } 079 080 public CbusEventResponder getEv( int id ){ 081 return _evResponseArr.get(id); 082 } 083 084 public CbusDummyCS getNewCS(){ 085 CbusDummyCS newcs = new CbusDummyCS(memo); 086 _csArr.add(newcs); 087 return newcs; 088 } 089 090 public CbusEventResponder getNewEv(){ 091 CbusEventResponder newcs = new CbusEventResponder(memo); 092 _evResponseArr.add(newcs); 093 return newcs; 094 } 095 096 /** 097 * Disposes of all simulated objects. 098 * CanListeners can be removed and command stations can stop session timers. 099 * Does not remove instance from InstanceManager or CAN memo. 100 */ 101 @Override 102 public void dispose() { 103 log.info("Stopping {} Simulations",_csArr.size()+_ndArr.size()+_evResponseArr.size()); 104 for ( CbusDummyCS cs : _csArr ) { 105 cs.dispose(); 106 } 107 _csArr.clear(); 108 109 for ( CbusDummyNode cs : _ndArr ) { 110 cs.dispose(); 111 } 112 _ndArr.clear(); 113 114 for ( CbusEventResponder cs : _evResponseArr ) { 115 cs.dispose(); 116 } 117 _evResponseArr.clear(); 118 119 } 120 121 private static final Logger log = LoggerFactory.getLogger(CbusSimulator.class); 122 123}