001package jmri.jmrix.mqtt;
002
003import javax.annotation.*;
004
005import jmri.Reporter;
006import jmri.SystemConnectionMemo;
007
008import jmri.managers.AbstractReporterManager;
009
010/**
011 * Provide a ReporterManager implementation for MQTT communications
012 *
013 * @author Bob Jacobsen Copyright (C) 2023
014 */
015
016public class MqttReporterManager extends AbstractReporterManager {
017
018    public MqttReporterManager(@Nonnull SystemConnectionMemo memo ) {
019        super(memo);
020    }
021
022    /**
023     * {@inheritDoc}
024     */
025    @Override
026    @Nonnull
027    public MqttSystemConnectionMemo getMemo() {
028        return (MqttSystemConnectionMemo) memo;
029    }
030
031    public void setRcvTopicPrefix(@Nonnull String rcvTopicPrefix) {
032        this.rcvTopicPrefix = rcvTopicPrefix;
033    }
034
035    @Nonnull
036    public String rcvTopicPrefix = "track/reporter/"; // for constructing topic; public for script access
037
038    /** {@inheritDoc} */
039    @Override
040    public boolean allowMultipleAdditions(String systemName) {
041        return true;
042    }
043
044
045    /**
046     * {@inheritDoc}
047     *
048     * Accepts any string.
049     */
050    @Override
051    public String createSystemName(@Nonnull String topicSuffix, @Nonnull String prefix) {
052        return prefix + typeLetter() + topicSuffix;
053    }
054
055    /**
056     * Create an new sensor object.
057     * {@inheritDoc}
058     * @return never null
059     */
060    @Nonnull
061    @Override
062    public Reporter createNewReporter(String systemName, String userName) {
063        MqttReporter r;
064        String suffix = systemName.substring(getSystemPrefix().length() + 1);
065
066
067        String rcvTopic = java.text.MessageFormat.format(
068                            rcvTopicPrefix.contains("{0}") ? rcvTopicPrefix : rcvTopicPrefix+"{0}",
069                            suffix);
070
071        r = new MqttReporter(getMemo().getMqttAdapter(), systemName, rcvTopic);
072        r.setUserName(userName);
073
074        return r;
075    }
076
077    /** {@inheritDoc} */
078    @Override
079    public NameValidity validSystemNameFormat(String systemName) {
080        return NameValidity.VALID;
081    }
082
083    /** {@inheritDoc} */
084    @Override
085    public String getEntryToolTip() {
086        return Bundle.getMessage("AddInputEntryToolTip");
087    }
088
089    // private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(MqttReporterManager.class);
090
091}