001package jmri.jmrix.mqtt;
002
003import javax.annotation.Nonnull;
004import jmri.Light;
005
006/**
007 * Implement LightManager for MQTT systems
008 * <p>
009 * System names are "prefixnnn", where prefix is the system prefix and nnn is
010 * the light number without padding.
011 *
012 * @author Paul Bender Copyright (C) 2010
013 */
014public class MqttLightManager extends jmri.managers.AbstractLightManager {
015
016    public MqttLightManager(@Nonnull MqttSystemConnectionMemo memo) {
017        super(memo);
018    }
019
020    public void setSendTopicPrefix(@Nonnull String sendTopicPrefix) {
021        this.sendTopicPrefix = sendTopicPrefix;
022    }
023
024    public void setRcvTopicPrefix(@Nonnull String rcvTopicPrefix) {
025        this.rcvTopicPrefix  = rcvTopicPrefix;
026    }
027    
028    @Nonnull
029    public String sendTopicPrefix = "track/light/";
030    @Nonnull
031    public String rcvTopicPrefix  = "track/light/";
032
033    /**
034     * {@inheritDoc}
035     */
036    @Override
037    @Nonnull
038    public MqttSystemConnectionMemo getMemo() {
039        return (MqttSystemConnectionMemo) memo;
040    }
041
042    @Override
043    @Nonnull
044    protected Light createNewLight(@Nonnull String systemName, String userName) throws IllegalArgumentException {
045        String suffix = systemName.substring(getSystemNamePrefix().length());
046
047        String sendTopic = java.text.MessageFormat.format(
048            sendTopicPrefix.contains("{0}") ? sendTopicPrefix : (sendTopicPrefix + "{0}"),
049            suffix);
050        String rcvTopic = java.text.MessageFormat.format(
051            rcvTopicPrefix.contains("{0}") ? rcvTopicPrefix : (rcvTopicPrefix + "{0}"),
052            suffix);
053
054        Light t = new MqttLight(getMemo().getMqttAdapter(), systemName, userName, sendTopic, rcvTopic);
055        t.setUserName(userName);
056        return t;
057    }
058
059    @Override
060    public boolean validSystemNameConfig(String systemName) {
061        return true;
062    }
063    
064    /** {@inheritDoc} */
065    @Override
066    public boolean allowMultipleAdditions(String systemName) {
067        return true;
068    }
069
070    /**
071     * {@inheritDoc}
072     */
073    @Override
074    public String getEntryToolTip() {
075        return "A string which will be inserted into \"" + sendTopicPrefix + "\" for transmission";
076    }
077}