001package jmri.jmrix.loconet.logixng; 002 003import jmri.jmrit.logixng.actions.*; 004 005import java.util.*; 006 007import jmri.*; 008import jmri.JmriException; 009import jmri.jmrit.logixng.*; 010import jmri.jmrix.loconet.*; 011 012/** 013 * Sets the speed to zero if the loco hasn't been used in a while. 014 * 015 * @author Daniel Bergqvist Copyright 2020 016 */ 017public class SetSpeedZero extends AbstractDigitalAction { 018 019 private static final int NUM_LOCO_SLOTS_TO_CHECK = 119; 020 021 private LocoNetSystemConnectionMemo _memo; 022 023 public SetSpeedZero(String sys, String user, LocoNetSystemConnectionMemo memo) { 024 super(sys, user); 025 _memo = memo; 026 } 027 028 @Override 029 public Base getDeepCopy(Map<String, String> systemNames, Map<String, String> userNames) throws JmriException { 030 DigitalActionManager manager = InstanceManager.getDefault(DigitalActionManager.class); 031 String sysName = systemNames.get(getSystemName()); 032 String userName = userNames.get(getSystemName()); 033 if (sysName == null) sysName = manager.getAutoSystemName(); 034 SetSpeedZero copy = new SetSpeedZero(sysName, userName, _memo); 035 copy.setComment(getComment()); 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 private void setSlotSpeedZero(LocoNetSlot slot) { 055 boolean inUse = 056 slot.slotStatus() == LnConstants.LOCO_IN_USE 057 || slot.slotStatus() == LnConstants.LOCO_COMMON; 058 059 boolean upLinkConsist = 060 slot.consistStatus() == LnConstants.CONSIST_MID 061 || slot.consistStatus() == LnConstants.CONSIST_SUB; 062 063 if (inUse && !upLinkConsist && slot.speed() != 0) { 064 LocoNetMessage msg = new LocoNetMessage(4); 065 msg.setOpCode(LnConstants.OPC_LOCO_SPD); 066 msg.setElement(1, slot.getSlot()); 067 msg.setElement(2, 0); 068 _memo.getLnTrafficController().sendLocoNetMessage(msg); 069 } 070 } 071 072 /** {@inheritDoc} */ 073 @Override 074 public void execute() { 075 for (int i=1; i <= NUM_LOCO_SLOTS_TO_CHECK; i++) { 076 setSlotSpeedZero(_memo.getSlotManager().slot(i)); 077 } 078 } 079 080 @Override 081 public FemaleSocket getChild(int index) throws IllegalArgumentException, UnsupportedOperationException { 082 throw new UnsupportedOperationException("Not supported."); 083 } 084 085 @Override 086 public int getChildCount() { 087 return 0; 088 } 089 090 @Override 091 public String getShortDescription(Locale locale) { 092 return Bundle.getMessage(locale, "SetSpeedZero_Short"); 093 } 094 095 @Override 096 public String getLongDescription(Locale locale) { 097 return Bundle.getMessage(locale, "SetSpeedZero_Long", 098 _memo != null ? _memo.getUserName() : Bundle.getMessage("MemoNotSet")); 099 } 100 101 /** {@inheritDoc} */ 102 @Override 103 public void setup() { 104 // Do nothing 105 } 106 107 /** {@inheritDoc} */ 108 @Override 109 public void registerListenersForThisClass() { 110 if (!_listenersAreRegistered) { 111 _listenersAreRegistered = true; 112 } 113 } 114 115 /** {@inheritDoc} */ 116 @Override 117 public void unregisterListenersForThisClass() { 118 _listenersAreRegistered = false; 119 } 120 121 /** {@inheritDoc} */ 122 @Override 123 public void disposeMe() { 124 } 125 126 127 private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(SetSpeedZero.class); 128 129}