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