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