001package jmri.implementation; 002 003import java.beans.PropertyChangeEvent; 004import jmri.ConditionalAction; 005import jmri.InstanceManager; 006import jmri.JmriException; 007import jmri.Sensor; 008import org.slf4j.Logger; 009import org.slf4j.LoggerFactory; 010 011/** 012 * Conditional.java 013 * 014 * A Conditional type to provide runtime support for Densor Groups. 015 * <p> 016 * This file is part of JMRI. 017 * <p> 018 * JMRI is free software; you can redistribute it and/or modify it under the 019 * terms of version 2 of the GNU General Public License as published by the Free 020 * Software Foundation. See the "COPYING" file for a copy of this license. 021 * <p> 022 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY 023 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 024 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 025 * 026 * @author Pete Cressman Copyright (C) 2009 027 */ 028public class SensorGroupConditional extends DefaultConditional { 029 030 public SensorGroupConditional(String systemName, String userName) { 031 super(systemName, userName); 032 } 033 034 @Override 035 public int calculate(boolean enabled, PropertyChangeEvent evt) { 036 int currentState = super.calculate(false, evt); 037 if (!enabled || evt == null) { 038 return currentState; 039 } 040 Sensor evtSensor = (Sensor) evt.getSource(); 041 if (evtSensor == null) { 042 return currentState; 043 } 044 String listener = evtSensor.getSystemName(); 045 log.debug("SGConditional \"{}\" ({}) has event from \"{}\"", getUserName(), getSystemName(), listener); 046 if (Sensor.INACTIVE == ((Integer) evt.getNewValue()).intValue()) { 047 return currentState; 048 } 049 for (int i = 0; i < _actionList.size(); i++) { 050 ConditionalAction action = _actionList.get(i); 051 Sensor sn = InstanceManager.sensorManagerInstance().getSensor(action.getDeviceName()); 052 if (sn == null) { 053 log.error("invalid sensor name in action - {}", action.getDeviceName()); 054 return currentState; 055 } 056 if (sn != evtSensor) { // don't change who triggered the action 057 // find the old one and reset it 058 if (sn.getState() != action.getActionData()) { 059 try { 060 sn.setKnownState(action.getActionData()); 061 } catch (JmriException e) { 062 log.warn("Exception setting sensor {} in action", action.getDeviceName()); 063 } 064 } 065 } 066 } 067 log.debug("SGConditional \"{}\" ({}), state= {}has set the group actions for {}", getUserName(), getSystemName(), currentState, listener); 068 return currentState; 069 } 070 071 private final static Logger log = LoggerFactory.getLogger(SensorGroupConditional.class); 072}