001package jmri.jmrix.rfid;
002
003import java.util.ArrayList;
004import jmri.IdTag;
005import jmri.Sensor;
006import jmri.implementation.AbstractSensor;
007
008/**
009 * Extend jmri.AbstractSensor for RFID systems
010 * <p>
011 * System names are "FSpppp", where ppp is a representation of the RFID reader.
012 *
013 * @author Bob Jacobsen Copyright (C) 2007
014 * @author Matthew Harris Copyright (C) 2011
015 * @since 2.11.4
016 */
017public class RfidSensor extends AbstractSensor
018        implements RfidTagListener {
019
020    public RfidSensor(String systemName) {
021        super(systemName);
022    }
023
024    public RfidSensor(String systemName, String userName) {
025        super(systemName, userName);
026    }
027
028    @Override
029    public void notify(IdTag r) {
030        setOwnState(r != null ? Sensor.ACTIVE : Sensor.INACTIVE);
031    }
032
033    // if somebody outside sets state to INACTIVE, clear list
034    @Override
035    public void setOwnState(int state) {
036        if (state == Sensor.INACTIVE) {
037            if (contents.size() > 0) {
038                contents = new ArrayList<>();
039            }
040        }
041        super.setOwnState(state);
042    }
043
044    java.util.List<Integer> getContents() {
045        return contents;
046    }
047
048    void notifyInRegion(Integer id) {
049        // make sure region contains this Reading.getId();
050        if (!contents.contains(id)) {
051            contents.add(id);
052            notifyArriving(id);
053        }
054    }
055
056    void notifyOutOfRegion(Integer id) {
057        // make sure region does not contain this Reading.getId();
058        if (contents.contains(id)) {
059            contents.remove(id);
060            notifyLeaving(id);
061        }
062    }
063
064//    transient Region region;
065    ArrayList<Integer> contents = new ArrayList<>();
066
067    /**
068     * Notify parameter listeners that a device has left the region covered by
069     * this sensor
070     * @param id number of region being left
071     */
072    void notifyLeaving(Integer id) {
073        firePropertyChange("Leaving", null, id);
074    }
075
076    /**
077     * Notify parameter listeners that a device has entered the region covered
078     * by this sensor
079     * @param id number of arrived-in region
080     */
081    void notifyArriving(Integer id) {
082        firePropertyChange("Arriving", null, id);
083    }
084
085    @Override
086    public void requestUpdateFromLayout() {
087    }
088
089}