001package jmri.implementation;
002
003import javax.annotation.Nonnull;
004
005import jmri.JmriException;
006import jmri.CurrentMeter;
007import jmri.Meter;
008import jmri.VoltageMeter;
009
010/**
011 * Abstract base class for current meter objects.
012 *
013 * @author Mark Underwood    (C) 2015
014 * @author Daniel Bergqvist  (C) 2020
015 */
016public class DefaultMeter extends AbstractAnalogIO implements Meter {
017    
018    @Nonnull private final MeterUpdateTask _updateTask;
019    @Nonnull private final Unit _unit;
020    private final double _min;
021    private final double _max;
022    private final double _resolution;
023    
024    public DefaultMeter(
025            @Nonnull String sys,
026            @Nonnull Unit unit,
027            double min, double max, double resolution,
028            @Nonnull MeterUpdateTask updateTask) {
029        super(sys, true);
030        this._unit = unit;
031        this._updateTask = updateTask;
032        this._min = min;
033        this._max = max;
034        this._resolution = resolution;
035        _updateTask.addMeter(DefaultMeter.this);
036    }
037    
038    /** {@inheritDoc} */
039    @Override
040    public void enable() {
041        log.debug("Enabling meter.");
042        _updateTask.enable(this);
043    }
044
045    /** {@inheritDoc} */
046    @Override
047    public void disable() {
048        log.debug("Disabling meter.");
049        _updateTask.disable(this);
050    }
051
052    /**
053     * {@inheritDoc}
054     * <p>
055     * Override this if the meter can send value to the layout.
056     */
057    @Override
058    protected void sendValueToLayout(double value) throws JmriException {
059        // Do nothing
060    }
061    
062    /** {@inheritDoc} */
063    @Override
064    protected boolean cutOutOfBoundsValues() {
065        return true;
066    }
067    
068    /** {@inheritDoc} */
069    @Override
070    public void setState(int s) throws JmriException {
071        throw new UnsupportedOperationException("Not supported.");
072    }
073
074    /** {@inheritDoc} */
075    @Override
076    public int getState() {
077        throw new UnsupportedOperationException("Not supported.");
078    }
079
080    /** {@inheritDoc} */
081    @Override
082    public String getBeanType() {
083        return Bundle.getMessage("BeanNameMeter");
084    }
085
086    /** {@inheritDoc} */
087    @Override
088    public Unit getUnit() {
089        return _unit;
090    }
091
092    /** {@inheritDoc} */
093    @Override
094    public double getMin() {
095        return _min;
096    }
097
098    /** {@inheritDoc} */
099    @Override
100    public double getMax() {
101        return _max;
102    }
103
104    /** {@inheritDoc} */
105    @Override
106    public double getResolution() {
107        return _resolution;
108    }
109
110    /** {@inheritDoc} */
111    @Override
112    public AbsoluteOrRelative getAbsoluteOrRelative() {
113        return _unit == Unit.Percent ? AbsoluteOrRelative.RELATIVE : AbsoluteOrRelative.ABSOLUTE;
114    }
115    
116    /** {@inheritDoc} */
117    @Override
118    public void dispose() {
119        _updateTask.removeMeter(this);
120        super.dispose();
121    }
122    
123    /**
124     * Request an update from the layout.
125     */
126    @Override
127    public void requestUpdateFromLayout() {
128        _updateTask.requestUpdateFromLayout();
129    }
130    
131    
132    /**
133     * Default implementation of a voltage meter.
134     */
135    public static class DefaultVoltageMeter extends DefaultMeter implements VoltageMeter
136    {
137        public DefaultVoltageMeter(@Nonnull String sys, Unit unit, double min, double max, double resolution, MeterUpdateTask updateTask) {
138            super(sys, unit, min, max, resolution, updateTask);
139        }
140    }
141    
142    
143    /**
144     * Default implementation of a current meter.
145     */
146    public static class DefaultCurrentMeter extends DefaultMeter implements CurrentMeter
147    {
148        public DefaultCurrentMeter(@Nonnull String sys, Unit unit, double min, double max, double resolution, MeterUpdateTask updateTask) {
149            super(sys, unit, min, max, resolution, updateTask);
150        }
151    }
152    
153    
154    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DefaultMeter.class);
155}