001package jmri.jmrit.vsdecoder;
002
003import java.beans.PropertyChangeEvent;
004import org.jdom2.Element;
005import org.slf4j.Logger;
006import org.slf4j.LoggerFactory;
007
008/**
009 * Notch 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 NotchTrigger extends Trigger {
027
028    int current_notch, prev_notch;
029
030    public NotchTrigger(String name) {
031        this(name, 0, 0);
032    }
033
034    public NotchTrigger(String name, int prev, int cur) {
035        super(name);
036        this.setTriggerType(Trigger.TriggerType.NOTCH);
037        prev_notch = prev;
038        current_notch = cur;
039    }
040
041    public void setNotch(int next) {
042        current_notch = next;
043    }
044
045    public int getNotch() {
046        return current_notch;
047    }
048
049    @Override
050    public void propertyChange(PropertyChangeEvent event) {
051
052        // Validate
053        // If no target, or not a name match, or no trigger, or no action
054        // then just return quickly.
055        // Careful: Takes advantage of "lazy OR" behavior
056        if (target == null) {
057            //log.debug("Quit.  No target.");
058            return;
059        }
060        if (!event.getPropertyName().equals(this.getEventName())) {
061            //log.debug("Quit. Event name mismatch event: {}, this: {}", event.getPropertyName(), this.getEventName());
062            return;
063        }
064        if (this.getTriggerType() == TriggerType.NONE) {
065            //log.debug("Quit.  TriggerType = NONE");
066            return;
067        }
068        if (this.getTargetAction() == TargetAction.NOTHING) {
069            //log.debug("Quit.  TargetAction = NOTHING");
070            return;
071        }
072
073        // Compare
074        prev_notch = current_notch;
075        current_notch = EngineSound.calcEngineNotch((Float) event.getNewValue());
076
077        log.debug("Notch Trigger prev_notch: {}, current_notch: {}", prev_notch, current_notch);
078        this.callback.takeAction(current_notch);
079        /*
080         if ((prev == prev_notch) && (next == next_notch)) {
081         this.callback.takeAction();
082         }
083         */
084    }
085
086    @Override
087    public Element getXml() {
088        Element me = new Element("Trigger");
089        me.setAttribute("name", this.getName());
090        me.setAttribute("type", "NOTCH");
091        log.warn("CompareTrigger.getXml() not implemented");
092        return me;
093    }
094
095    @Override
096    public void setXml(Element e) {
097        //Get common stuff
098        super.setXml(e);
099    }
100
101    private static final Logger log = LoggerFactory.getLogger(NotchTrigger.class);
102
103}