001package jmri.jmrit.logixng.expressions;
002
003import java.beans.*;
004import java.util.*;
005
006import jmri.*;
007import jmri.jmrit.logixng.*;
008import jmri.jmrit.logixng.util.parser.*;
009
010/**
011 * This expression sets the state of a power.
012 *
013 * @author Daniel Bergqvist Copyright 2021
014 */
015public class ExpressionPower extends AbstractDigitalExpression
016        implements PropertyChangeListener {
017
018    private Is_IsNot_Enum _is_IsNot = Is_IsNot_Enum.Is;
019    private PowerState _powerState = PowerState.On;
020    private boolean _ignoreUnknownState = true;
021
022    public ExpressionPower(String sys, String user)
023            throws BadUserNameException, BadSystemNameException {
024        super(sys, user);
025    }
026
027    @Override
028    public Base getDeepCopy(Map<String, String> systemNames, Map<String, String> userNames) throws ParserException {
029        DigitalExpressionManager manager = InstanceManager.getDefault(DigitalExpressionManager.class);
030        String sysName = systemNames.get(getSystemName());
031        String userName = userNames.get(getSystemName());
032        if (sysName == null) sysName = manager.getAutoSystemName();
033        ExpressionPower copy = new ExpressionPower(sysName, userName);
034        copy.setComment(getComment());
035        copy.set_Is_IsNot(_is_IsNot);
036        copy.setBeanState(_powerState);
037        copy.setIgnoreUnknownState(_ignoreUnknownState);
038        return manager.registerExpression(copy);
039    }
040
041    public void set_Is_IsNot(Is_IsNot_Enum is_IsNot) {
042        _is_IsNot = is_IsNot;
043    }
044
045    public Is_IsNot_Enum get_Is_IsNot() {
046        return _is_IsNot;
047    }
048
049    public void setBeanState(PowerState state) {
050        _powerState = state;
051    }
052
053    public PowerState getBeanState() {
054        return _powerState;
055    }
056
057    public void setIgnoreUnknownState(boolean ignoreUnknownState) {
058        _ignoreUnknownState = ignoreUnknownState;
059    }
060
061    public boolean isIgnoreUnknownState() {
062        return _ignoreUnknownState;
063    }
064
065    /** {@inheritDoc} */
066    @Override
067    public Category getCategory() {
068        return Category.ITEM;
069    }
070
071    /** {@inheritDoc} */
072    @Override
073    public boolean evaluate() throws JmriException {
074
075        PowerState checkPowerState = _powerState;
076
077        PowerState currentPowerState =
078                PowerState.get(InstanceManager.getDefault(PowerManager.class)
079                        .getPower());
080
081        boolean result;
082        if (_powerState == PowerState.OnOrOff) {
083            result = currentPowerState == PowerState.On || currentPowerState == PowerState.Off;
084        } else {
085            result = currentPowerState == checkPowerState;
086        }
087        if (_is_IsNot == Is_IsNot_Enum.Is) {
088            return result;
089        } else {
090            return !result;
091        }
092    }
093
094    @Override
095    public FemaleSocket getChild(int index) throws IllegalArgumentException, UnsupportedOperationException {
096        throw new UnsupportedOperationException("Not supported.");
097    }
098
099    @Override
100    public int getChildCount() {
101        return 0;
102    }
103
104    @Override
105    public String getShortDescription(Locale locale) {
106        return Bundle.getMessage(locale, "Power_Short");
107    }
108
109    @Override
110    public String getLongDescription(Locale locale) {
111        if (_ignoreUnknownState) {
112            return Bundle.getMessage(locale, "Power_Long3",
113                    _is_IsNot.toString(),
114                    _powerState._text,
115                    Bundle.getMessage(locale, "Power_IgnoreUnknownState"));
116        } else {
117            return Bundle.getMessage(locale, "Power_Long2", _is_IsNot.toString(), _powerState._text);
118        }
119    }
120
121    /** {@inheritDoc} */
122    @Override
123    public void setup() {
124        // Do nothing
125    }
126
127    /** {@inheritDoc} */
128    @Override
129    public void registerListenersForThisClass() {
130        if (!_listenersAreRegistered) {
131            InstanceManager.getDefault(PowerManager.class)
132                    .addPropertyChangeListener(PowerManager.POWER, this);
133            _listenersAreRegistered = true;
134        }
135    }
136
137    /** {@inheritDoc} */
138    @Override
139    public void unregisterListenersForThisClass() {
140        if (_listenersAreRegistered) {
141            InstanceManager.getDefault(PowerManager.class)
142                    .removePropertyChangeListener(PowerManager.POWER, this);
143            _listenersAreRegistered = false;
144        }
145    }
146
147    /** {@inheritDoc} */
148    @Override
149    public void propertyChange(PropertyChangeEvent evt) {
150        if (_ignoreUnknownState
151                && PowerManager.POWER.equals(evt.getPropertyName())
152                && evt.getNewValue().equals(PowerManager.UNKNOWN)) {
153            log.debug("Ignoring unknown state");
154            return;
155        }
156        getConditionalNG().execute();
157    }
158
159    /** {@inheritDoc} */
160    @Override
161    public void disposeMe() {
162    }
163
164
165    public enum PowerState {
166        On(PowerManager.ON, Bundle.getMessage("PowerStateOn")),
167        Off(PowerManager.OFF, Bundle.getMessage("PowerStateOff")),
168        Idle(PowerManager.IDLE, Bundle.getMessage("PowerStateIdle")),
169        Unknown(PowerManager.UNKNOWN, Bundle.getMessage("PowerStateUnknown")),
170        OnOrOff(-1, Bundle.getMessage("PowerStateOnOrOff"));
171
172        private final int _id;
173        private final String _text;
174
175        private PowerState(int id, String text) {
176            this._id = id;
177            this._text = text;
178        }
179
180        static public PowerState get(int id) {
181            switch (id) {
182                case PowerManager.OFF:
183                    return Off;
184
185                case PowerManager.ON:
186                    return On;
187
188                case PowerManager.IDLE:
189                    return Idle;
190
191                case PowerManager.UNKNOWN:
192                    return Unknown;
193
194                default:
195                    throw new IllegalArgumentException("Unknown state: "+Integer.toString(id));
196            }
197        }
198
199        public int getID() {
200            return _id;
201        }
202
203        @Override
204        public String toString() {
205            return _text;
206        }
207
208    }
209
210    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExpressionPower.class);
211
212}