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