001package jmri.jmrix.powerline.insteon2412s;
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 receivers on Insteon 2412S
010 * interfaces.
011 * <p>
012 * Uses X10 dimming commands to set intensity unless the value is 0.0 or 1.0, in
013 * which case it uses on/off commands only.
014 * <p>
015 * Since the dim/bright step of the hardware is unknown then the Light object is
016 * first created, the first time the intensity (not state) is set to other than
017 * 0.0 or 1.0, the output is run to it's maximum dim or bright step so that we
018 * know the count is right.
019 * <p>
020 * Keeps track of the controller's "dim count", and if not certain forces it to
021 * zero to be sure.
022 *
023 *
024 * @author Dave Duchamp Copyright (C) 2004
025 * @author Bob Jacobsen Copyright (C) 2006, 2007, 2008, 2009, 2010
026 * @author Ken Cameron Copyright (C) 2009, 2010 Converted to multiple connection
027 * @author kcameron Copyright (C) 2011
028 */
029public class SpecificX10Light 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     *
036     * @param systemName text for systemName of light
037     * @param tc         tc for connection
038     */
039    public SpecificX10Light(String systemName, SerialTrafficController tc) {
040        super(systemName, tc);
041        this.tc = tc;
042        // fixed number of steps for X10 Insteon
043        maxDimStep = 22;
044    }
045
046    /**
047     * Create a Light object, with both system and user names.
048     * <p>
049     * 'systemName' was previously validated in SerialLightManager
050     *
051     * @param systemName text for systemName of light
052     * @param tc         tc for connection
053     * @param userName   text for userName of light
054     */
055    public SpecificX10Light(String systemName, SerialTrafficController tc, String userName) {
056        super(systemName, tc, userName);
057        this.tc = tc;
058        maxDimStep = 22;
059    }
060
061    SerialTrafficController tc = null;
062
063    // System-dependent instance variables
064    /**
065     * Send a Dim/Bright commands to the X10 hardware to reach a specific
066     * intensity. Acts immediately, and changes no general state.
067     * <p>
068     * This sends "Extended Cmd Dim" commands.
069     */
070    @Override
071    protected void sendIntensity(double intensity) {
072        if (log.isDebugEnabled()) {
073            log.debug("sendIntensity({}) lastOutputStep: {} maxDimStep: {}", intensity, lastOutputStep, maxDimStep);
074        }
075
076        // if we don't know the dim count, force it to a value.
077//        initIntensity(intensity);
078        // find the new correct dim count
079        int newStep = (int) Math.round(intensity * maxDimStep);  // maxDimStep is full on, 0 is full off, etc
080
081        // check for errors
082        if ((newStep < 0) || (newStep > maxDimStep)) {
083            log.error("newStep wrong: {} intensity: {}", newStep, intensity);
084        }
085
086        if (newStep == lastOutputStep) {
087            // nothing to do!
088            if (log.isDebugEnabled()) {
089                log.debug("intensity {} within current step, return", intensity);
090            }
091            return;
092
093        }
094
095        // create output sequence of address, then function
096        X10Sequence out = new X10Sequence();
097        out.addExtData(housecode, devicecode, X10Sequence.EXTCMD_DIM, newStep);
098        // send
099        tc.sendX10Sequence(out, null);
100        lastOutputStep = newStep;
101
102        if (log.isDebugEnabled()) {
103            log.debug("sendIntensity({}) house {} device {} newStep: {}", intensity, X10Sequence.houseValueToText(housecode), devicecode, newStep);
104        }
105    }
106
107    private final static Logger log = LoggerFactory.getLogger(SpecificX10Light.class);
108}
109
110