001package jmri.jmrit.vsdecoder;
002
003import java.beans.PropertyChangeEvent;
004import org.jdom2.Element;
005import org.slf4j.Logger;
006import org.slf4j.LoggerFactory;
007
008/**
009 * Throttle trigger.
010 *
011 * <hr>
012 * This file is part of JMRI.
013 * <p>
014 * JMRI is free software; you can redistribute it and/or modify it under
015 * the terms of version 2 of the GNU General Public License as published
016 * by the Free Software Foundation. See the "COPYING" file for a copy
017 * of this license.
018 * <p>
019 * JMRI is distributed in the hope that it will be useful, but WITHOUT
020 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
021 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
022 * for more details.
023 *
024 * @author Mark Underwood Copyright (C) 2011
025 */
026class ThrottleTrigger extends Trigger {
027
028    int current_notch, prev_notch;
029
030    public ThrottleTrigger(String name) {
031        super(name);
032        this.setTriggerType(Trigger.TriggerType.THROTTLE);
033    }
034
035    @Override
036    public void propertyChange(PropertyChangeEvent event) {
037
038        // Validate
039        // If no target, or not a name match, or no trigger, or no action
040        // then just return quickly.
041        // Careful: Takes advantage of "lazy OR" behavior
042        if (target == null) {
043            log.debug("Quit.  No target.");
044            return;
045        }
046        if (event.getPropertyName().equals(this.getEventName()) != true) {
047            //log.debug("Quit. Event name mismatch event = " + event.getPropertyName() + " this = " + this.getEventName());
048            return;
049        }
050        if (this.getTriggerType() == TriggerType.NONE) {
051            //log.debug("Quit.  TriggerType = NONE");
052            return;
053        }
054        if (this.getTargetAction() == TargetAction.NOTHING || this.getTargetAction() == TargetAction.STOP_AT_ZERO) {
055            //log.debug("Quit.  TargetAction = NOTHING");
056            return;
057        }
058
059        log.debug("Throttle Trigger old value: {}, new value: {}", event.getOldValue(), event.getNewValue());
060        this.callback.takeAction((Float) event.getNewValue());
061    }
062
063    @Override
064    public Element getXml() {
065        Element me = new Element("Trigger");
066        me.setAttribute("name", this.getName());
067        me.setAttribute("type", "THROTTLE");
068        log.warn("CompareTrigger.getXml() not implemented");
069        return me;
070    }
071
072    @Override
073    public void setXml(Element e) {
074        //Get common stuff
075        super.setXml(e);
076    }
077
078    private static final Logger log = LoggerFactory.getLogger(ThrottleTrigger.class);
079
080}