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 * 
019 */
020import java.beans.PropertyChangeEvent;
021import org.jdom2.Element;
022import org.slf4j.Logger;
023import org.slf4j.LoggerFactory;
024
025class FloatTrigger extends Trigger {
026
027    Float match_value;
028    CompareType compare_type;
029
030    public FloatTrigger(String name, Float next, Trigger.CompareType ct) {
031        super(name);
032        this.setTriggerType(Trigger.TriggerType.FLOAT);
033        match_value = next;
034        compare_type = ct;
035    }
036
037    public void setMatchValue(Float next) {
038        match_value = next;
039    }
040
041    public Float getMatchValue() {
042        return (match_value);
043    }
044
045    public void setCompareType(CompareType ct) {
046        compare_type = ct;
047    }
048
049    public CompareType getCompareType() {
050        return (compare_type);
051    }
052
053    @Override
054    public void propertyChange(PropertyChangeEvent event) {
055        Float next;
056        boolean compare = false;
057        int compare_val;
058
059        // Validate
060        // If no target, or not a name match, or no trigger, or no action
061        // then just return quickly.
062        // Careful: Takes advantage of "lazy OR" behavior
063        if (target == null) {
064            log.debug("Quit.  No target.");
065            return;
066        }
067        if (event.getPropertyName().equals(this.getEventName()) != true) {
068            log.debug("Quit. Event name mismatch event = {} this = {}", event.getPropertyName(), this.getEventName());
069            return;
070        }
071        if (this.getTriggerType() == TriggerType.NONE) {
072            log.debug("Quit.  TriggerType = NONE");
073            return;
074        }
075        if (this.getTargetAction() == TargetAction.NOTHING) {
076            log.debug("Quit.  TargetAction = NOTHING");
077            return;
078        }
079
080        // Compare
081        next = (Float) event.getNewValue(); // HACK!  Needs to be flexible.
082        compare_val = next.compareTo(match_value);
083        switch (compare_type) {
084            case GT:
085                compare = (compare_val > 0);
086                break;
087            case LT:
088                compare = (compare_val < 0);
089                break;
090            case GTE:
091                compare = (compare_val >= 0);
092                break;
093            case LTE:
094                compare = (compare_val <= 0);
095                break;
096            case EQ:
097            default:
098                compare = (compare_val == 0);
099                break;
100        }
101
102        log.debug("compareTrigger match_value = {} next = {} compare_val = {} compare = {}", match_value, next, compare_val, compare);
103
104        if (compare) {
105            log.debug("compareTrigger taking action");
106            this.callback.takeAction();
107        }
108    }
109
110    @Override
111    public Element getXml() {
112        Element me = new Element("trigger");
113        me.setAttribute("name", this.getName());
114        me.setAttribute("type", "FLOAT");
115        log.warn("CompareTrigger.getXml() not implemented");
116        return (me);
117    }
118
119    @Override
120    public void setXml(Element e) {
121        log.debug("FloatTrigger.setXml()");
122
123        //Get common stuff
124        super.setXml(e);
125
126        if (e.getAttributeValue("type").equals("FLOAT")) {
127            match_value = Float.parseFloat(e.getChild("match").getValue() + "f");
128
129            compare_type = Trigger.CompareType.valueOf(e.getChild("compare-type").getValue().toUpperCase());
130        }
131    }
132
133    private final static Logger log = LoggerFactory.getLogger(FloatTrigger.class);
134
135}