001package jmri.jmrit.logixng.actions;
002
003import java.beans.PropertyChangeEvent;
004import java.beans.PropertyChangeListener;
005import java.util.*;
006import java.util.concurrent.atomic.AtomicReference;
007
008import jmri.*;
009import jmri.jmrit.logixng.*;
010import jmri.jmrit.logixng.util.LogixNG_SelectEnum;
011import jmri.jmrit.logixng.util.parser.ParserException;
012import jmri.util.ThreadingUtil;
013
014/**
015 * This action turns power on or off.
016 *
017 * @author Daniel Bergqvist Copyright 2021
018 */
019public class ActionPower extends AbstractDigitalAction
020        implements PropertyChangeListener {
021
022    private final LogixNG_SelectEnum<PowerState> _selectEnum =
023            new LogixNG_SelectEnum<>(this, PowerState.values(), PowerState.On, this);
024
025
026    public ActionPower(String sys, String user)
027            throws BadUserNameException, BadSystemNameException {
028        super(sys, user);
029    }
030
031    @Override
032    public Base getDeepCopy(Map<String, String> systemNames, Map<String, String> userNames) throws ParserException, ParserException {
033        DigitalActionManager manager = InstanceManager.getDefault(DigitalActionManager.class);
034        String sysName = systemNames.get(getSystemName());
035        String userName = userNames.get(getSystemName());
036        if (sysName == null) sysName = manager.getAutoSystemName();
037        ActionPower copy = new ActionPower(sysName, userName);
038        copy.setComment(getComment());
039        _selectEnum.copy(copy._selectEnum);
040        return manager.registerAction(copy);
041    }
042
043    public LogixNG_SelectEnum<PowerState> getSelectEnum() {
044        return _selectEnum;
045    }
046
047    /** {@inheritDoc} */
048    @Override
049    public Category getCategory() {
050        return Category.ITEM;
051    }
052
053    /** {@inheritDoc} */
054    @Override
055    public void execute() throws JmriException {
056        AtomicReference<JmriException> exception = new AtomicReference<>();
057        PowerState powerState = _selectEnum.evaluateEnum(getConditionalNG());
058
059        ThreadingUtil.runOnLayoutWithJmriException(() -> {
060            try {
061                InstanceManager.getDefault(PowerManager.class).setPower(powerState.getID());
062            } catch (JmriException e) {
063                exception.set(e);
064            }
065        });
066        if (exception.get() != null) throw exception.get();
067    }
068
069    @Override
070    public FemaleSocket getChild(int index) throws IllegalArgumentException, UnsupportedOperationException {
071        throw new UnsupportedOperationException("Not supported.");
072    }
073
074    @Override
075    public int getChildCount() {
076        return 0;
077    }
078
079    @Override
080    public String getShortDescription(Locale locale) {
081        return Bundle.getMessage(locale, "Power_Short");
082    }
083
084    @Override
085    public String getLongDescription(Locale locale) {
086        return Bundle.getMessage(locale, "Power_Long", _selectEnum.getDescription(locale));
087    }
088
089    /** {@inheritDoc} */
090    @Override
091    public void setup() {
092        // Do nothing
093    }
094
095    /** {@inheritDoc} */
096    @Override
097    public void registerListenersForThisClass() {
098        _selectEnum.registerListeners();
099    }
100
101    /** {@inheritDoc} */
102    @Override
103    public void unregisterListenersForThisClass() {
104        _selectEnum.unregisterListeners();
105    }
106
107    /** {@inheritDoc} */
108    @Override
109    public void disposeMe() {
110    }
111
112
113    public enum PowerState {
114        Off(PowerManager.OFF, Bundle.getMessage("PowerStateOff")),
115        On(PowerManager.ON, Bundle.getMessage("PowerStateOn"));
116
117        private final int _id;
118        private final String _text;
119
120        private PowerState(int id, String text) {
121            this._id = id;
122            this._text = text;
123        }
124
125        static public PowerState get(int id) {
126            switch (id) {
127                case PowerManager.OFF:
128                    return Off;
129
130                case PowerManager.ON:
131                    return On;
132
133                default:
134                    throw new IllegalArgumentException("invalid power state");
135            }
136        }
137
138        public int getID() {
139            return _id;
140        }
141
142        @Override
143        public String toString() {
144            return _text;
145        }
146
147    }
148
149    /** {@inheritDoc} */
150    @Override
151    public void propertyChange(PropertyChangeEvent evt) {
152        getConditionalNG().execute();
153    }
154
155//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionPower.class);
156
157}