001/**
002 * Consist Manager for use with the MqttConsist class for the
003 * consists it builds.
004 *
005 * @author Dean Cording Copyright (C) 2023
006 */
007package jmri.jmrix.mqtt;
008
009import jmri.Consist;
010import jmri.LocoAddress;
011import jmri.DccLocoAddress;
012import jmri.implementation.AbstractConsistManager;
013import javax.annotation.Nonnull;
014
015//import org.slf4j.Logger;
016//import org.slf4j.LoggerFactory;
017
018
019public class MqttConsistManager extends AbstractConsistManager {
020
021    protected MqttSystemConnectionMemo adapterMemo;
022
023    /**
024     * Constructor - call the constructor for the superclass, and initialize the
025     * consist reader thread, which retrieves consist information from the
026     * command station.
027     *
028     * @param memo the associated connection memo
029     */
030    public MqttConsistManager(MqttSystemConnectionMemo memo) {
031        super();
032        adapterMemo = memo;
033    }
034
035    public void setSendTopic(@Nonnull String sendTopicPrefix) {
036        this.sendTopicPrefix = sendTopicPrefix;
037    }
038
039    @Nonnull
040    public String sendTopicPrefix = "cab/{0}/consist";
041
042
043    /**
044     * This implementation does support advanced consists, so return true.
045     *
046     */
047    @Override
048    public boolean isCommandStationConsistPossible() {
049        return true;
050    }
051
052    /**
053     * Does a CS consist require a separate consist address? CS consist
054     * addresses are assigned by the user, so return true.
055     *
056     */
057    @Override
058    public boolean csConsistNeedsSeperateAddress() {
059        return true;
060    }
061
062    /**
063     * Add a new MQTT Consist with the given address to
064     * consistTable/consistList.
065     */
066    @Override
067    public Consist addConsist(LocoAddress address) {
068        if (! (address instanceof DccLocoAddress)) {
069            throw new IllegalArgumentException("address is not a DccLocoAddress object");
070        }
071        if (consistTable.containsKey(address)) { // no duplicates allowed across all connections
072            return consistTable.get(address);
073        }
074        MqttConsist consist;
075        consist = new MqttConsist((DccLocoAddress) address, adapterMemo,
076                sendTopicPrefix);
077        consistTable.put(address, consist);
078        notifyConsistListChanged();
079        return consist;
080    }
081
082    /* Request an update from the layout, loading
083     * Consists from the command station.
084     */
085    @Override
086    public void requestUpdateFromLayout() {
087    }
088
089    @Override
090    protected boolean shouldRequestUpdateFromLayout() {
091        return false;
092    }
093
094    /**
095     * Consist is activated on the controller for the specified LocoAddress
096     * This is used by MqttThrottle to either publish an existing consist or clear
097     * an old one upon opening the new throttle.
098     * @param address Consist address to be activated
099     */
100    public void activateConsist(LocoAddress address) {
101
102        ((MqttConsist)addConsist(address)).activate();
103
104    }
105
106    /**
107     * If a consist exists with the given address, the consist is deactivated on the controller,
108     * otherwise it does nothing.
109     * This is used by a throttle in case it is controlling a consist.
110     * @param address Consist address to be deactivated
111     */
112    public void deactivateConsist(LocoAddress address) {
113
114        if (!consistTable.containsKey(address)) return;
115
116        ((MqttConsist)consistTable.get(address)).deactivate();
117
118    }
119
120
121
122//    private final static Logger log = LoggerFactory.getLogger(MqttConsistManager.class);
123
124}