001package jmri.jmrix.mqtt;
002
003import javax.annotation.Nonnull;
004import jmri.JmriException;
005import jmri.Turnout;
006
007/**
008 * Implement turnout manager for MQTT systems
009 * <p>
010 * System names are "MTnnn", where M is the user configurable system prefix, nnn
011 * is the turnout number without padding.
012 *
013 * @author Lionel Jeanson Copyright (c) 2017
014 */
015public class MqttTurnoutManager extends jmri.managers.AbstractTurnoutManager {
016
017    public MqttTurnoutManager(@Nonnull MqttSystemConnectionMemo memo) {
018        super(memo);
019    }
020
021    /**
022     * {@inheritDoc}
023     */
024    @Override
025    @Nonnull
026    public MqttSystemConnectionMemo getMemo() {
027        return (MqttSystemConnectionMemo) memo;
028    }
029
030    public void setSendTopicPrefix(@Nonnull String sendTopicPrefix) {
031        this.sendTopicPrefix = sendTopicPrefix;
032    }
033    public void setRcvTopicPrefix(@Nonnull String rcvTopicPrefix) {
034        this.rcvTopicPrefix = rcvTopicPrefix;
035    }
036
037    @Nonnull
038    public String sendTopicPrefix = "track/turnout/"; // for constructing topic; public for script access
039    @Nonnull
040    public String rcvTopicPrefix = "track/turnout/"; // for constructing topic; public for script access
041
042    /**
043     * {@inheritDoc}
044     * <p>
045     * Accepts any string as the "topicSuffix"
046     */
047    @Override
048    public String createSystemName(@Nonnull String topicSuffix, @Nonnull String prefix) throws JmriException {
049        return prefix + typeLetter() + topicSuffix;
050    }
051
052    /**
053     * {@inheritDoc}
054     */
055    @Nonnull
056    @Override
057    protected Turnout createNewTurnout(@Nonnull String systemName, String userName) throws IllegalArgumentException {
058        MqttTurnout t;
059        String suffix = systemName.substring(getSystemNamePrefix().length());
060
061        String sendTopic = java.text.MessageFormat.format(
062                            sendTopicPrefix.contains("{0}") ? sendTopicPrefix : sendTopicPrefix+"{0}",
063                            suffix);
064        String rcvTopic = java.text.MessageFormat.format(
065                            rcvTopicPrefix.contains("{0}") ? rcvTopicPrefix : rcvTopicPrefix+"{0}",
066                            suffix);
067
068        t = new MqttTurnout(getMemo().getMqttAdapter(), systemName, sendTopic, rcvTopic);
069        t.setUserName(userName);
070
071        if (parser != null) {
072            t.setParser(parser);
073        }
074
075        return t;
076    }
077
078    /**
079     * {@inheritDoc}
080     */
081    @Override
082    public String getEntryToolTip() {
083        return "A string which will be inserted into \"" + sendTopicPrefix + "\" for transmission";
084    }
085
086    /** {@inheritDoc} */
087    @Override
088    public boolean allowMultipleAdditions(String systemName) {
089        return true;
090    }
091
092    public void setParser(MqttContentParser<Turnout> parser) {
093        this.parser = parser;
094    }
095    MqttContentParser<Turnout> parser = null;
096
097    // private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(MqttTurnoutManager.class);
098}