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