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 * <p>
022 *
023 *
024 *
025 * @author Dave Duchamp Copyright (C) 2004
026 * @author Bob Jacobsen Copyright (C) 2006, 2007, 2008, 2010 Converted to
027 * multiple connection
028 * @author kcameron Copyright (C) 2011
029 */
030public class SpecificLight extends jmri.jmrix.powerline.SerialX10Light {
031
032    /**
033     * Create a Light object, with only system name.
034     * <p>
035     * 'systemName' was previously validated in SerialLightManager
036     * @param systemName systemName for light
037     * @param tc         traffic controller for connection
038     */
039    public SpecificLight(String systemName, SerialTrafficController tc) {
040        super(systemName, tc);
041        this.tc = tc;
042        maxDimStep = tc.getNumberOfIntensitySteps();
043    }
044
045    /**
046     * Create a Light object, with both system and user names.
047     * <p>
048     * 'systemName' was previously validated in SerialLightManager
049     * @param systemName systemName for light
050     * @param tc         tc for connection
051     * @param userName   userName for light
052     */
053    public SpecificLight(String systemName, SerialTrafficController tc, String userName) {
054        super(systemName, tc, userName);
055        this.tc = tc;
056        maxDimStep = tc.getNumberOfIntensitySteps();
057    }
058
059    SerialTrafficController tc = null;
060
061    /**
062     * Optionally, force control to a known "dim count".
063     * <p>
064     * Invoked the first time intensity is set.
065     */
066    @Override
067    protected void initIntensity(double intensity) {
068        maxDimStep = tc.getNumberOfIntensitySteps();
069
070        // Set initial state
071        // see if going to stabilize at on or off
072        if (intensity <= 0.5) {
073            // create output sequence
074            X10Sequence out = new X10Sequence();
075            // going to low, first set off
076            out.addAddress(housecode, devicecode);
077            out.addFunction(housecode, X10Sequence.FUNCTION_OFF, 0);
078            // then set to full dim
079            out.addFunction(housecode, X10Sequence.FUNCTION_DIM, maxDimStep);
080            // send
081            tc.sendX10Sequence(out, null);
082
083            lastOutputStep = 0;
084
085            if (log.isDebugEnabled()) {
086                log.debug("initIntensity: sent dim reset");
087            }
088        } else {
089            // create output sequence
090            X10Sequence out = new X10Sequence();
091            // going to high, first set on
092            out.addAddress(housecode, devicecode);
093            out.addFunction(housecode, X10Sequence.FUNCTION_ON, 0);
094            // then set to full dim
095            out.addFunction(housecode, X10Sequence.FUNCTION_BRIGHT, maxDimStep);
096            // send
097            tc.sendX10Sequence(out, null);
098
099            lastOutputStep = maxDimStep;
100
101            if (log.isDebugEnabled()) {
102                log.debug("initIntensity: sent bright reset");
103            }
104        }
105    }
106
107    private final static Logger log = LoggerFactory.getLogger(SpecificLight.class);
108}