All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bidib.wizard.model.loco.LocoModel Maven / Gradle / Ivy

There is a newer version: 2.0.26
Show newest version
package org.bidib.wizard.model.loco;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.BitSet;
import java.util.Collection;
import java.util.LinkedList;
import java.util.concurrent.atomic.AtomicInteger;

import org.bidib.jbidibc.messages.enums.DriveAcknowledge;
import org.bidib.wizard.model.loco.listener.LocoModelListener;
import org.bidib.wizard.model.locolist.LocoListModel;
import org.bidib.wizard.model.status.BinStateValue;
import org.bidib.wizard.model.status.DirectionStatus;
import org.bidib.wizard.model.status.RfBasisMode;
import org.bidib.wizard.model.status.SpeedSteps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jgoodies.common.bean.Bean;

public class LocoModel extends Bean {
    private static final long serialVersionUID = 1L;

    private static final Logger LOGGER = LoggerFactory.getLogger(LocoModel.class);

    public static final String PROPERTY_ADDRESS = "address";

    public static final String PROPERTY_LOCO_NAME = "locoName";

    public static final String PROPERTY_LOCO_LIST_MODEL = "locoListModel";

    public static final String PROPERTY_SPEED = "speed";

    public static final String PROPERTY_SPEED_STEPS = "speedSteps";

    public static final String PROPERTY_DIRECTION = "direction";

    public static final String PROPERTY_REPORTED_SPEED = "reportedSpeed";

    public static final String PROPERTY_DRIVE_ACKNOWLEDGE = "driveAcknowledge";

    public static final String PROPERTY_DRIVE_ACKNOWLEDGE_COUNTER = "driveAcknowledgeCounter";

    public static final String PROPERTY_DYNSTATE_ENERGY = "dynStateEnergy";

    public static final String PROPERTY_ACTIVE_BASE = "activeBase";

    public static final String PROPERTY_REPORTED_CELLNUMBER = "reportedCellNumber";

    public static final String PROPERTYNAME_DEV_BINSTATE_NUMBER = "devBinStateNumber";

    public static final String PROPERTYNAME_DEV_BINSTATE_VALUE = "devBinStateValue";

    public static final String PROPERTYNAME_COUNTER_CS_DRIVE = "counterCsDrive";

    public static final String PROPERTYNAME_COUNTER_CS_DRIVE_ACK = "counterCsDriveAck";

    public static final String PROPERTYNAME_COUNTER_CS_BIN_STATE = "counterCsBinState";

    public static final int FUNCTIONINDEX_LIGHTS = 0;

    private LocoListModel llm;

    private int dynStateEnergy;

    private RfBasisMode activeBase = RfBasisMode.SINGLE;

    private RfBasisMode prevActiveBase;

    private Integer reportedCellNumber;

    private Integer devBinStateNumber;

    private BinStateValue devBinStateValue = BinStateValue.ON;

    private AtomicInteger counterCsDrive = new AtomicInteger();

    private AtomicInteger counterCsDriveAck = new AtomicInteger();

    private AtomicInteger counterCsBinState = new AtomicInteger();

    private final Collection listeners = new LinkedList();

    private final PropertyChangeListener listener;

    public LocoModel(final LocoListModel llm) {
        this.llm = llm;

        LOGGER.info("Create new LocoModel, llm: {}", llm);

        this.listener = new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                LOGGER.info("Received pce: {}", evt);

                firePropertyChange(evt.getPropertyName(), evt.getOldValue(), evt.getNewValue());
            }
        };

        addPropertyChangeListener();
    }

    public void cleanup() {
        LOGGER.info("Cleanup the locoModel, remove propertyChangeListener, llm: {}", this.llm);
        removePropertyChangeListener();
    }

    public void setLocoListModel(final LocoListModel llm) {
        LOGGER.info("Set new LocoListModel, llm: {}", llm);

        LocoListModel oldValue = this.llm;

        removePropertyChangeListener();
        this.llm = llm;
        addPropertyChangeListener();

        firePropertyChange(PROPERTY_LOCO_LIST_MODEL, oldValue, this.llm);

        // iterate over all properties
        firePropertyChange(PROPERTY_ADDRESS, oldValue != null ? oldValue.getAddress() : null,
            this.llm != null ? this.llm.getAddress() : null);
        firePropertyChange(PROPERTY_SPEED_STEPS, oldValue != null ? oldValue.getSpeedSteps() : null,
            this.llm != null ? this.llm.getSpeedSteps() : null);
        firePropertyChange(PROPERTY_SPEED, oldValue != null ? oldValue.getSpeed() : null,
            this.llm != null ? this.llm.getSpeed() : null);

    }

    public LocoListModel getLocoListModel() {
        return this.llm;
    }

    private void removePropertyChangeListener() {
        if (this.llm != null) {
            this.llm.removePropertyChangeListener(this.listener);
        }
    }

    private void addPropertyChangeListener() {
        if (this.llm != null) {
            this.llm.addPropertyChangeListener(this.listener);
        }
    }

    public void addLocoModelListener(LocoModelListener l) {
        listeners.add(l);
    }

    public void removeLocoModelListener(LocoModelListener l) {
        listeners.remove(l);
    }

    /**
     * @return the address of the loco
     */
    public Integer getAddress() {
        if (llm != null) {
            return llm.getAddress();
        }
        return null;
    }

    /**
     * @return the locoName
     */
    public String getLocoName() {
        if (llm != null) {
            return llm.getLocoName();
        }
        return null;
    }

    /**
     * @param locoName
     *            the locoName to set
     */
    public void setLocoName(String locoName) {

        if (llm != null) {
            String oldValue = llm.getLocoName();
            llm.setLocoName(locoName);

            firePropertyChange(PROPERTY_LOCO_NAME, oldValue, locoName);
        }
    }

    public Integer getReportedSpeed() {
        if (llm != null) {
            return llm.getReportedSpeed();
        }
        return null;
    }

    public void setReportedSpeed(Integer reportedSpeed) {
        LOGGER.debug("Update the reported speed: {}", reportedSpeed);
        if (llm != null) {
            Integer oldValue = llm.getReportedSpeed();
            llm.setReportedSpeed(reportedSpeed);

            firePropertyChange(PROPERTY_REPORTED_SPEED, oldValue, reportedSpeed);
        }
    }

    /**
     * @return the driveAcknowledge
     */
    public DriveAcknowledge getDriveAcknowledge() {
        if (llm != null) {
            return llm.getDriveAcknowledge();
        }
        return null;
    }

    /**
     * @param driveAcknowledge
     *            the driveAcknowledge to set
     */
    public void setDriveAcknowledge(DriveAcknowledge driveAcknowledge) {
        if (llm != null) {
            DriveAcknowledge oldValue = llm.getDriveAcknowledge();
            llm.setDriveAcknowledge(driveAcknowledge);

            firePropertyChange(PROPERTY_DRIVE_ACKNOWLEDGE, oldValue, driveAcknowledge);
        }
    }

    /**
     * @return the driveAcknowledgeCounter
     */
    public Integer getDriveAcknowledgeCounter() {
        if (llm != null) {
            return llm.getDriveAcknowledgeCounter();
        }
        return null;
    }

    /**
     * @param driveAcknowledgeCounter
     *            the driveAcknowledgeCounter to set
     */
    public void setDriveAcknowledgeCounter(Integer driveAcknowledgeCounter) {
        if (llm != null) {
            Integer oldValue = llm.getDriveAcknowledgeCounter();
            llm.setDriveAcknowledgeCounter(driveAcknowledgeCounter);

            firePropertyChange(PROPERTY_DRIVE_ACKNOWLEDGE_COUNTER, oldValue, driveAcknowledgeCounter);
        }
    }

    public DirectionStatus getDirection() {
        if (llm != null) {
            return llm.getDirection();
        }
        return null;
    }

    public void setDirection(DirectionStatus direction) {
        if (llm != null) {
            DirectionStatus oldValue = llm.getDirection();
            llm.setDirection(direction);

            firePropertyChange(PROPERTY_DIRECTION, oldValue, direction);
        }
    }

    public SpeedSteps getSpeedSteps() {
        if (llm != null) {
            return llm.getSpeedSteps();
        }
        return null;
    }

    public void setSpeedSteps(SpeedSteps speedSteps) {
        if (llm != null) {
            SpeedSteps oldValue = llm.getSpeedSteps();
            llm.setSpeedSteps(speedSteps);

            firePropertyChange(PROPERTY_SPEED_STEPS, oldValue, speedSteps);
        }
    }

    public Integer getSpeed() {
        if (llm != null) {
            return llm.getSpeed();
        }
        return null;
    }

    public void setSpeed(Integer speed) {
        if (llm != null) {
            Integer oldValue = llm.getSpeed();

            LOGGER.debug("Set the new speed: {}, oldValue: {}", speed, oldValue);

            llm.setSpeed(speed != null ? speed.intValue() : 0);
            // force signal 0 or 1 to listeners every time
            if (speed != null && (speed.intValue() == 0 || speed.intValue() == 1)) {
                oldValue = null;
            }

            firePropertyChange(PROPERTY_SPEED, oldValue, speed);
        }
    }

    public boolean getFunction(int index) {
        if (llm != null) {
            return llm.getFunction(index);
        }
        return false;
    }

    public BitSet getFunctions() {
        if (llm != null) {
            return llm.getFunctions();
        }
        return null;
    }

    public void setFunction(int index, boolean value) {
        if (llm != null) {
            llm.setFunction(index, value);
        }
        fireFunctionChanged(index, value);
    }

    public void setFunctions(BitSet functions) {
        if (llm != null) {
            llm.setFunctions(functions);
        }
    }

    public int getDynStateEnergy() {
        return dynStateEnergy;
    }

    public void setDynStateEnergy(int dynStateEnergy) {
        int oldValue = this.dynStateEnergy;

        this.dynStateEnergy = dynStateEnergy;
        firePropertyChange(PROPERTY_DYNSTATE_ENERGY, oldValue, dynStateEnergy);
    }

    private void fireFunctionChanged(int index, boolean value) {
        for (LocoModelListener l : listeners) {
            l.functionChanged(index, value);
        }
    }

    private void fireBinaryStateChanged(int state, boolean value) {
        for (LocoModelListener l : listeners) {
            l.binaryStateChanged(state, value);
        }
    }

    public void setBinaryState(int state, boolean value) {
        fireBinaryStateChanged(state, value);
    }

    /**
     * @return the activeBase
     */
    public RfBasisMode getActiveBase() {
        return activeBase;
    }

    /**
     * @param activeBase
     *            the activeBase to set
     */
    public void setActiveBase(RfBasisMode activeBase) {
        LOGGER.info("Set the new active rf base: {}", activeBase);
        RfBasisMode oldValue = this.activeBase;

        // keep the previous active base
        if (this.activeBase != activeBase) {
            LOGGER.info("The new active rf base has changed. Update the prev active base: {}", this.activeBase);
            this.prevActiveBase = this.activeBase;
        }

        this.activeBase = activeBase;

        firePropertyChange(PROPERTY_ACTIVE_BASE, oldValue, activeBase);
    }

    /**
     * @return the previous activeBase
     */
    public RfBasisMode getPrevActiveBase() {
        return prevActiveBase;
    }

    /**
     * Reset the prev active base.
     */
    public void resetPrevActiveBase() {
        prevActiveBase = null;
    }

    /**
     * @return the reportedCellNumber
     */
    public Integer getReportedCellNumber() {
        return reportedCellNumber;
    }

    /**
     * @param reportedCellNumber
     *            the reportedCellNumber to set
     */
    public void setReportedCellNumber(Integer reportedCellNumber) {
        Integer oldValue = this.reportedCellNumber;
        this.reportedCellNumber = reportedCellNumber;
        firePropertyChange(PROPERTY_REPORTED_CELLNUMBER, oldValue, this.reportedCellNumber);
    }

    /**
     * @return the devBinStateNumber
     */
    public Integer getDevBinStateNumber() {
        return devBinStateNumber;
    }

    /**
     * @param devBinStateNumber
     *            the devBinStateNumber to set
     */
    public void setDevBinStateNumber(Integer devBinStateNumber) {
        Integer oldValue = this.devBinStateNumber;
        this.devBinStateNumber = devBinStateNumber;

        firePropertyChange(PROPERTYNAME_DEV_BINSTATE_NUMBER, oldValue, devBinStateNumber);
    }

    /**
     * @return the devBinStateValue
     */
    public BinStateValue getDevBinStateValue() {
        return devBinStateValue;
    }

    /**
     * @param devBinStateValue
     *            the devBinStateValue to set
     */
    public void setDevBinStateValue(BinStateValue devBinStateValue) {
        BinStateValue oldValue = this.devBinStateValue;
        this.devBinStateValue = devBinStateValue;

        firePropertyChange(PROPERTYNAME_DEV_BINSTATE_VALUE, oldValue, devBinStateValue);
    }

    /**
     * @return the counterCsDrive
     */
    public int getCounterCsDrive() {
        return counterCsDrive.get();
    }

    /**
     * Increment the CS drive counter by 1.
     */
    public int incCounterCsDrive() {
        int oldValue = this.counterCsDrive.get();
        int newValue = this.counterCsDrive.addAndGet(1);

        firePropertyChange(PROPERTYNAME_COUNTER_CS_DRIVE, oldValue, newValue);

        return newValue;
    }

    /**
     * Reset the CS drive counter.
     */
    public void resetCounterCsDrive() {
        int oldValue = this.counterCsDrive.get();
        this.counterCsDrive.set(0);

        firePropertyChange(PROPERTYNAME_COUNTER_CS_DRIVE, oldValue, 0);
    }

    /**
     * @return the counterCsDriveAck
     */
    public int getCounterCsDriveAck() {
        return counterCsDriveAck.get();
    }

    /**
     * Increment the CS drive ack counter by 1.
     */
    public int incCounterCsDriveAck() {
        final int oldValue = this.counterCsDriveAck.get();
        final int newValue = this.counterCsDriveAck.addAndGet(1);

        firePropertyChange(PROPERTYNAME_COUNTER_CS_DRIVE_ACK, oldValue, newValue);

        return newValue;
    }

    /**
     * Reset the CS drive ack counter.
     */
    public void resetCounterCsAckDrive() {
        int oldValue = this.counterCsDriveAck.get();
        this.counterCsDriveAck.set(0);

        firePropertyChange(PROPERTYNAME_COUNTER_CS_DRIVE_ACK, oldValue, 0);
    }

    /**
     * @return the counterCsBinState
     */
    public int getCounterCsBinState() {
        return counterCsBinState.get();
    }

    /**
     * Increment the CS binState counter by 1.
     */
    public int incCounterCsBinState() {
        int oldValue = this.counterCsBinState.get();
        int newValue = this.counterCsBinState.addAndGet(1);

        firePropertyChange(PROPERTYNAME_COUNTER_CS_BIN_STATE, oldValue, newValue);

        return newValue;
    }

    /**
     * Reset the CS binState counter.
     */
    public void resetCounterCsBinState() {
        int oldValue = this.counterCsBinState.get();
        this.counterCsBinState.set(0);

        firePropertyChange(PROPERTYNAME_COUNTER_CS_BIN_STATE, oldValue, 0);
    }

    @Override
    public String toString() {
        return "LocoModel, llm: " + llm + ", lights on: " + getFunction(FUNCTIONINDEX_LIGHTS);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy