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