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        ActionUpdateSlots copy = new ActionUpdateSlots(sysName, userName, _memo);
036        return manager.registerAction(copy).deepCopyChildren(this, systemNames, userNames);
037    }
038    
039    /** {@inheritDoc} */
040    @Override
041    public Category getCategory() {
042        return CategoryLocoNet.LOCONET;
043    }
044
045    public void setMemo(LocoNetSystemConnectionMemo memo) {
046        assertListenersAreNotRegistered(log, "setMemo");
047        _memo = memo;
048    }
049    
050    public LocoNetSystemConnectionMemo getMemo() {
051        return _memo;
052    }
053    
054    /** {@inheritDoc} */
055    @Override
056    public void execute() {
057        for (int i=1; i <= NUM_LOCO_SLOTS_TO_CLEAR; i++) {
058            LocoNetSlot slot = _memo.getSlotManager().slot(i);
059            if ((slot.slotStatus() & LnConstants.LOCOSTAT_MASK) != LnConstants.LOCO_FREE) {
060//                _memo.getLnTrafficController().sendLocoNetMessage(slot.writeStatus(LnConstants.LOCO_FREE));
061                _memo.getLnTrafficController().sendLocoNetMessage(slot.releaseSlot());
062            }
063        }
064    }
065
066    @Override
067    public FemaleSocket getChild(int index) throws IllegalArgumentException, UnsupportedOperationException {
068        throw new UnsupportedOperationException("Not supported.");
069    }
070
071    @Override
072    public int getChildCount() {
073        return 0;
074    }
075
076    @Override
077    public String getShortDescription(Locale locale) {
078        return Bundle.getMessage(locale, "ActionClearSlots_Short");
079    }
080
081    @Override
082    public String getLongDescription(Locale locale) {
083        return Bundle.getMessage(locale, "ActionClearSlots_Long",
084                _memo != null ? _memo.getUserName() : Bundle.getMessage("MemoNotSet"));
085    }
086
087    /** {@inheritDoc} */
088    @Override
089    public void setup() {
090        // Do nothing
091    }
092    
093    /** {@inheritDoc} */
094    @Override
095    public void registerListenersForThisClass() {
096        if (!_listenersAreRegistered) {
097            _listenersAreRegistered = true;
098        }
099    }
100    
101    /** {@inheritDoc} */
102    @Override
103    public void unregisterListenersForThisClass() {
104        _listenersAreRegistered = false;
105    }
106    
107    /** {@inheritDoc} */
108    @Override
109    public void disposeMe() {
110    }
111
112
113    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionClearSlots.class);
114
115}