001package jmri.jmrix.powerline.cp290;
002
003import jmri.jmrix.powerline.SerialTrafficController;
004import jmri.jmrix.powerline.X10Sequence;
005import org.slf4j.Logger;
006import org.slf4j.LoggerFactory;
007
008/**
009 * Implementation of the Light Object for X10 for CP290 interfaces.
010 * <p>
011 * Uses X10 dimming commands to set intensity unless the value is 0.0 or 1.0, in
012 * which case it uses on/off commands only.
013 * <p>
014 * Since the dim/bright step of the hardware is unknown then the Light object is
015 * first created, the first time the intensity (not state) is set to other than
016 * 0.0 or 1.0, the output is run to it's maximum dim or bright step so that we
017 * know the count is right.
018 * <p>
019 * Keeps track of the controller's "dim count", and if not certain forces it to
020 * zero to be sure.
021 *
022 *
023 *
024 * @author Dave Duchamp Copyright (C) 2004
025 * @author Bob Jacobsen Copyright (C) 2006, 2007, 2008, 2010 Converted to
026 * multiple connection
027 * @author kcameron Copyright (C) 2011
028 */
029public class SpecificLight extends jmri.jmrix.powerline.SerialX10Light {
030
031    /**
032     * Create a Light object, with only system name.
033     * <p>
034     * 'systemName' was previously validated in SerialLightManager
035     * @param systemName systemName for light
036     * @param tc         traffic controller for connection
037     */
038    public SpecificLight(String systemName, SerialTrafficController tc) {
039        super(systemName, tc);
040        this.tc = tc;
041        maxDimStep = tc.getNumberOfIntensitySteps();
042    }
043
044    /**
045     * Create a Light object, with both system and user names.
046     * <p>
047     * 'systemName' was previously validated in SerialLightManager
048     * @param systemName systemName for light
049     * @param tc         tc for connection
050     * @param userName   userName for light
051     */
052    public SpecificLight(String systemName, SerialTrafficController tc, String userName) {
053        super(systemName, tc, userName);
054        this.tc = tc;
055        maxDimStep = tc.getNumberOfIntensitySteps();
056    }
057
058    SerialTrafficController tc = null;
059
060    /**
061     * Optionally, force control to a known "dim count".
062     * <p>
063     * Invoked the first time intensity is set.
064     */
065    @Override
066    protected void initIntensity(double intensity) {
067        maxDimStep = tc.getNumberOfIntensitySteps();
068
069        // Set initial state
070        // see if going to stabilize at on or off
071        if (intensity <= 0.5) {
072            // create output sequence
073            X10Sequence out = new X10Sequence();
074            // going to low, first set off
075            out.addAddress(housecode, devicecode);
076            out.addFunction(housecode, X10Sequence.FUNCTION_OFF, 0);
077            // then set to full dim
078            out.addFunction(housecode, X10Sequence.FUNCTION_DIM, maxDimStep);
079            // send
080            tc.sendX10Sequence(out, null);
081
082            lastOutputStep = 0;
083
084            if (log.isDebugEnabled()) {
085                log.debug("initIntensity: sent dim reset");
086            }
087        } else {
088            // create output sequence
089            X10Sequence out = new X10Sequence();
090            // going to high, first set on
091            out.addAddress(housecode, devicecode);
092            out.addFunction(housecode, X10Sequence.FUNCTION_ON, 0);
093            // then set to full dim
094            out.addFunction(housecode, X10Sequence.FUNCTION_BRIGHT, maxDimStep);
095            // send
096            tc.sendX10Sequence(out, null);
097
098            lastOutputStep = maxDimStep;
099
100            if (log.isDebugEnabled()) {
101                log.debug("initIntensity: sent bright reset");
102            }
103        }
104    }
105
106    private final static Logger log = LoggerFactory.getLogger(SpecificLight.class);
107}