001package jmri.jmrix.mqtt;
002
003import javax.annotation.*;
004
005import jmri.*;
006import org.slf4j.Logger;
007import org.slf4j.LoggerFactory;
008
009/**
010 * Implementation of the MqttSensorManager interface.
011 *
012 * @author  Bob Jacobsen Copyright (C) 2001, 2003, 2006, 2019
013 */
014public class MqttSensorManager extends jmri.managers.AbstractSensorManager {
015
016    public MqttSensorManager(@Nonnull SystemConnectionMemo memo ) {
017        super(memo);
018    }
019
020    /**
021     * {@inheritDoc}
022     */
023    @Override
024    @Nonnull
025    public MqttSystemConnectionMemo getMemo() {
026        return (MqttSystemConnectionMemo) memo;
027    }
028
029    public void setSendTopicPrefix(@Nonnull String sendTopicPrefix) {
030        this.sendTopicPrefix = sendTopicPrefix;
031    }
032    public void setRcvTopicPrefix(@Nonnull String rcvTopicPrefix) {
033        this.rcvTopicPrefix = rcvTopicPrefix;
034    }
035
036    @Nonnull
037    public String sendTopicPrefix = "track/sensor/"; // for constructing topic; public for script access
038    @Nonnull
039    public String rcvTopicPrefix = "track/sensor/"; // for constructing topic; public for script access
040
041    /** {@inheritDoc} */
042    @Override
043    public boolean allowMultipleAdditions(String systemName) {
044        return true;
045    }
046
047
048    /**
049     * {@inheritDoc}
050     *
051     * Accepts any string.
052     */
053    @Override
054    public String createSystemName(@Nonnull String topicSuffix, @Nonnull String prefix) throws JmriException {
055        return prefix + typeLetter() + topicSuffix;
056    }
057
058    /**
059     * Create an new sensor object.
060     * {@inheritDoc}
061     * @return never null
062     */
063    @Nonnull
064    @Override
065    protected Sensor createNewSensor(String systemName, String userName) throws IllegalArgumentException {
066        MqttSensor s;
067        String suffix = systemName.substring(getSystemPrefix().length() + 1);
068
069
070        String sendTopic = java.text.MessageFormat.format(
071                            sendTopicPrefix.contains("{0}") ? sendTopicPrefix : sendTopicPrefix+"{0}",
072                            suffix);
073        String rcvTopic = java.text.MessageFormat.format(
074                            rcvTopicPrefix.contains("{0}") ? rcvTopicPrefix : rcvTopicPrefix+"{0}",
075                            suffix);
076
077        s = new MqttSensor(getMemo().getMqttAdapter(), systemName, sendTopic, rcvTopic);
078        s.setUserName(userName);
079
080        if (parser != null) s.setParser(parser);
081
082        return s;
083    }
084
085    static int defaultState = Sensor.UNKNOWN;
086
087    public static synchronized void setDefaultStateForNewSensors(int defaultSetting) {
088        log.debug("Default new-Sensor state set to {}", defaultSetting);
089        defaultState = defaultSetting;
090    }
091
092    public static synchronized int getDefaultStateForNewSensors() {
093        return defaultState;
094    }
095
096    protected String prefix = "M";
097
098    /** {@inheritDoc} */
099    @Override
100    public NameValidity validSystemNameFormat(String systemName) {
101        return NameValidity.VALID;
102    }
103
104    /** {@inheritDoc} */
105    @Override
106    public String getEntryToolTip() {
107        return Bundle.getMessage("AddInputEntryToolTip");
108    }
109
110    public void setParser(MqttContentParser<Sensor> parser) {
111        this.parser = parser;
112    }
113    MqttContentParser<Sensor> parser = null;
114
115    private final static Logger log = LoggerFactory.getLogger(MqttSensorManager.class);
116
117}