001package jmri.jmrix.loconet.logixng;
002
003import jmri.jmrit.logixng.actions.*;
004
005import java.util.Locale;
006import java.util.Map;
007
008import jmri.InstanceManager;
009import jmri.JmriException;
010import jmri.jmrit.logixng.*;
011import jmri.jmrix.loconet.*;
012
013/**
014 * Sets all engine slots to status common
015 *
016 * @author Daniel Bergqvist Copyright 2020
017 */
018public class ActionClearSlots extends AbstractDigitalAction {
019
020    private static final int NUM_LOCO_SLOTS_TO_CLEAR = 119;
021
022    private LocoNetSystemConnectionMemo _memo;
023
024    public ActionClearSlots(String sys, String user, LocoNetSystemConnectionMemo memo) {
025        super(sys, user);
026        _memo = memo;
027    }
028
029    @Override
030    public Base getDeepCopy(Map<String, String> systemNames, Map<String, String> userNames) throws JmriException {
031        DigitalActionManager manager = InstanceManager.getDefault(DigitalActionManager.class);
032        String sysName = systemNames.get(getSystemName());
033        String userName = userNames.get(getSystemName());
034        if (sysName == null) sysName = manager.getAutoSystemName();
035        ActionClearSlots copy = new ActionClearSlots(sysName, userName, _memo);
036        copy.setComment(getComment());
037        return manager.registerAction(copy).deepCopyChildren(this, systemNames, userNames);
038    }
039
040    /** {@inheritDoc} */
041    @Override
042    public Category getCategory() {
043        return CategoryLocoNet.LOCONET;
044    }
045
046    public void setMemo(LocoNetSystemConnectionMemo memo) {
047        assertListenersAreNotRegistered(log, "setMemo");
048        _memo = memo;
049    }
050
051    public LocoNetSystemConnectionMemo getMemo() {
052        return _memo;
053    }
054
055    /** {@inheritDoc} */
056    @Override
057    public void execute() {
058        for (int i=1; i <= NUM_LOCO_SLOTS_TO_CLEAR; i++) {
059            LocoNetSlot slot = _memo.getSlotManager().slot(i);
060            if ((slot.slotStatus() & LnConstants.LOCOSTAT_MASK) != LnConstants.LOCO_FREE) {
061//                _memo.getLnTrafficController().sendLocoNetMessage(slot.writeStatus(LnConstants.LOCO_FREE));
062                _memo.getLnTrafficController().sendLocoNetMessage(slot.releaseSlot());
063            }
064        }
065    }
066
067    @Override
068    public FemaleSocket getChild(int index) throws IllegalArgumentException, UnsupportedOperationException {
069        throw new UnsupportedOperationException("Not supported.");
070    }
071
072    @Override
073    public int getChildCount() {
074        return 0;
075    }
076
077    @Override
078    public String getShortDescription(Locale locale) {
079        return Bundle.getMessage(locale, "ActionClearSlots_Short");
080    }
081
082    @Override
083    public String getLongDescription(Locale locale) {
084        return Bundle.getMessage(locale, "ActionClearSlots_Long",
085                _memo != null ? _memo.getUserName() : Bundle.getMessage("MemoNotSet"));
086    }
087
088    /** {@inheritDoc} */
089    @Override
090    public void setup() {
091        // Do nothing
092    }
093
094    /** {@inheritDoc} */
095    @Override
096    public void registerListenersForThisClass() {
097        if (!_listenersAreRegistered) {
098            _listenersAreRegistered = true;
099        }
100    }
101
102    /** {@inheritDoc} */
103    @Override
104    public void unregisterListenersForThisClass() {
105        _listenersAreRegistered = false;
106    }
107
108    /** {@inheritDoc} */
109    @Override
110    public void disposeMe() {
111    }
112
113
114    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionClearSlots.class);
115
116}