001package jmri.jmrit.logixng.expressions; 002 003import java.beans.PropertyChangeEvent; 004import java.beans.PropertyChangeListener; 005import java.util.*; 006 007import jmri.*; 008import jmri.jmrit.logixng.*; 009import jmri.jmrit.logixng.util.LogixNG_SelectNamedBean; 010 011/** 012 * Reads a AnalogIO. 013 * 014 * @author Daniel Bergqvist Copyright 2021 015 */ 016public class AnalogExpressionAnalogIO extends AbstractAnalogExpression 017 implements PropertyChangeListener { 018 019 private final LogixNG_SelectNamedBean<AnalogIO> _selectNamedBean = 020 new LogixNG_SelectNamedBean<>( 021 this, AnalogIO.class, InstanceManager.getDefault(AnalogIOManager.class), this); 022 023 public AnalogExpressionAnalogIO(String sys, String user) 024 throws BadUserNameException, BadSystemNameException { 025 026 super(sys, user); 027 } 028 029 @Override 030 public Base getDeepCopy(Map<String, String> systemNames, Map<String, String> userNames) throws JmriException { 031 AnalogExpressionManager manager = InstanceManager.getDefault(AnalogExpressionManager.class); 032 String sysName = systemNames.get(getSystemName()); 033 String userName = userNames.get(getSystemName()); 034 if (sysName == null) sysName = manager.getAutoSystemName(); 035 AnalogExpressionAnalogIO copy = new AnalogExpressionAnalogIO(sysName, userName); 036 copy.setComment(getComment()); 037 _selectNamedBean.copy(copy._selectNamedBean); 038 return manager.registerExpression(copy).deepCopyChildren(this, systemNames, userNames); 039 } 040 041 public LogixNG_SelectNamedBean<AnalogIO> getSelectNamedBean() { 042 return _selectNamedBean; 043 } 044 045 /** {@inheritDoc} */ 046 @Override 047 public Category getCategory() { 048 return Category.ITEM; 049 } 050 051 /** {@inheritDoc} */ 052 @Override 053 public double evaluate() throws JmriException { 054 AnalogIO analogIO = _selectNamedBean.evaluateNamedBean(getConditionalNG()); 055 if (analogIO != null) { 056 return jmri.util.TypeConversionUtil.convertToDouble(analogIO.getKnownAnalogValue(), false); 057 } else { 058 return 0.0; 059 } 060 } 061 062 /** {@inheritDoc} */ 063 @Override 064 public FemaleSocket getChild(int index) 065 throws IllegalArgumentException, UnsupportedOperationException { 066 throw new UnsupportedOperationException("Not supported."); 067 } 068 069 /** {@inheritDoc} */ 070 @Override 071 public int getChildCount() { 072 return 0; 073 } 074 075 /** {@inheritDoc} */ 076 @Override 077 public String getShortDescription(Locale locale) { 078 return Bundle.getMessage(locale, "AnalogExpressionAnalogIO_Short"); 079 } 080 081 /** {@inheritDoc} */ 082 @Override 083 public String getLongDescription(Locale locale) { 084 return Bundle.getMessage(locale, "AnalogExpressionAnalogIO_Long", _selectNamedBean.getDescription(locale)); 085 } 086 087 /** {@inheritDoc} */ 088 @Override 089 public void setup() { 090 // Do nothing 091 } 092 093 /** {@inheritDoc} */ 094 @Override 095 public void registerListenersForThisClass() { 096 if (!_listenersAreRegistered) { 097 _selectNamedBean.addPropertyChangeListener("value", this); 098 _selectNamedBean.registerListeners(); 099 _listenersAreRegistered = true; 100 } 101 } 102 103 /** {@inheritDoc} */ 104 @Override 105 public void unregisterListenersForThisClass() { 106 if (_listenersAreRegistered) { 107 _selectNamedBean.removePropertyChangeListener("value", this); 108 _selectNamedBean.unregisterListeners(); 109 _listenersAreRegistered = false; 110 } 111 } 112 113 /** {@inheritDoc} */ 114 @Override 115 public void propertyChange(PropertyChangeEvent evt) { 116 if (getTriggerOnChange()) { 117 getConditionalNG().execute(); 118 } 119 } 120 121 /** {@inheritDoc} */ 122 @Override 123 public void disposeMe() { 124 } 125 126 /** {@inheritDoc} */ 127 @Override 128 public void getUsageDetail(int level, NamedBean bean, List<NamedBeanUsageReport> report, NamedBean cdl) { 129 log.debug("getUsageReport :: AnalogExpressionAnalogIO: bean = {}, report = {}", cdl, report); 130 _selectNamedBean.getUsageDetail(level, bean, report, cdl, this, LogixNG_SelectNamedBean.Type.Expression); 131 } 132 133 private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AnalogExpressionAnalogIO.class); 134 135}