001package jmri.jmrix.can.cbus; 002 003import java.util.HashSet; 004 005import javax.annotation.CheckForNull; 006import javax.annotation.Nonnull; 007 008import jmri.jmrix.can.CanSystemConnectionMemo; 009import jmri.jmrix.can.cbus.eventtable.CbusEventBeanData; 010import jmri.jmrix.can.cbus.eventtable.CbusEventTableDataModel; 011import jmri.jmrix.can.cbus.node.CbusNodeTableDataModel; 012 013/** 014 * Class to lookup CBUS event names via the event table 015 * <p> 016 * Node names from the Node Manager 017 * @author Steve Young Copyright (C) 2019 018 */ 019public class CbusNameService { 020 021 private final CanSystemConnectionMemo _memo; 022 023 /** 024 * Create a new instance for the default connection 025 */ 026 public CbusNameService(){ 027 super(); 028 _memo = null; 029 } 030 031 /** 032 * Create a new instance for a given connection 033 * @param memo System Connection 034 */ 035 public CbusNameService(CanSystemConnectionMemo memo){ 036 super(); 037 _memo=memo; 038 } 039 040 /** 041 * Return a formatted String attempting to use the event toString method 042 * <p> 043 * eg no event table (123,456) will return NN:123 EN:456 044 * with event table instance would return NN:123 Node Name EN:456 Event Name 045 * No node present returns just event (0,56) EN:56 046 * All with trailing space 047 * 048 * @param nn Node Number 049 * @param en Event Number 050 * @return Event and node number with event and node name if available 051 */ 052 @Nonnull 053 public String getEventNodeString( int nn, int en ){ 054 CbusEventTableDataModel evMod = getEventModel(); 055 if (evMod!=null) { 056 String addevbuf = evMod.getEventString(nn,en); 057 if ( !addevbuf.isEmpty() ) { 058 return addevbuf; 059 } 060 } 061 return new CbusEvent(nn,en).toString(); 062 } 063 064 /** 065 * Return a formatted String attempting to locate the event name. 066 * <p> 067 * get the event name, empty string if event not on event table, or if event name is empty 068 * 069 * @param nn Node Number 070 * @param en Event Number 071 * @return Event name if available , else empty string 072 */ 073 @Nonnull 074 public String getEventName( int nn, int en ){ 075 CbusEventTableDataModel evMod = getEventModel(); 076 if (evMod!=null) { 077 return evMod.getEventName(nn,en); 078 } else { 079 return (""); 080 } 081 } 082 083 /** 084 * Return a formatted String after attempting to locate the node name. 085 * <p> 1st attempt - Node Username in node table ( eg. Control Panel West ) 086 * <p> 2nd attempt - Node Type Name ( eg. CANPAN ) 087 * <p> fallback empty string 088 * 089 * @param nn Node Number 090 * @return Node name if available , else empty string 091 */ 092 @Nonnull 093 public String getNodeName( int nn ){ 094 CbusNodeTableDataModel model = getNodeModel(); 095 if (model!=null) { 096 return model.getNodeName(nn); 097 } 098 return ""; 099 } 100 101 /** 102 * Get the Sensor Turnout and Light user names associated with event on 103 * @param nn Node Number 104 * @param en Event Number 105 * @param state Event State, either on or off 106 * @return Sensor Turnout and Light Beans associated with the CBUS Event. 107 * @see jmri.NamedBean 108 */ 109 @Nonnull 110 public CbusEventBeanData getJmriBeans(int nn, int en, @Nonnull CbusEvent.EvState state){ 111 CbusEventTableDataModel evMod = getEventModel(); 112 if (evMod!=null) { 113 return evMod.getEventBeans(nn,en,state); 114 } else { 115 return new CbusEventBeanData( new HashSet<>(), new HashSet<>()); 116 } 117 } 118 119 @CheckForNull 120 private CbusNodeTableDataModel getNodeModel(){ 121 if ( _memo != null ) { 122 return _memo.get(CbusNodeTableDataModel.class); 123 } 124 return jmri.InstanceManager.getNullableDefault(CbusNodeTableDataModel.class); 125 } 126 127 @CheckForNull 128 private CbusEventTableDataModel getEventModel(){ 129 if ( _memo != null ) { 130 return _memo.get(CbusEventTableDataModel.class); 131 } 132 return jmri.InstanceManager.getNullableDefault(CbusEventTableDataModel.class); 133 } 134 135 // private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CbusNameService.class); 136}