001package jmri.jmrix.powerline.simulator;
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     * @param systemName text for systemName of light
036     * @param tc         tc for connection
037     */
038    public SpecificX10Light(String systemName, SerialTrafficController tc) {
039        super(systemName, tc);
040        this.tc = tc;
041        // fixed number of steps for X10 Insteon
042        maxDimStep = 22;
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 text for systemName of light
050     * @param tc         tc for connection
051     * @param userName   text for userName of light
052     */
053    public SpecificX10Light(String systemName, SerialTrafficController tc, String userName) {
054        super(systemName, tc, userName);
055        this.tc = tc;
056        maxDimStep = 22;
057    }
058
059    SerialTrafficController tc = null;
060
061    // System-dependent instance variables
062    /**
063     * Send a Dim/Bright commands to the X10 hardware to reach a specific
064     * intensity. Acts immediately, and changes no general state.
065     * <p>
066     * This sends "Extended Cmd Dim" commands.
067     */
068    @Override
069    protected void sendIntensity(double intensity) {
070        if (log.isDebugEnabled()) {
071            log.debug("sendIntensity({}) lastOutputStep: {} maxDimStep: {}", intensity, lastOutputStep, maxDimStep);
072        }
073
074        // if we don't know the dim count, force it to a value.
075//        initIntensity(intensity);
076        // find the new correct dim count
077        int newStep = (int) Math.round(intensity * maxDimStep);  // maxDimStep is full on, 0 is full off, etc
078
079        // check for errors
080        if ((newStep < 0) || (newStep > maxDimStep)) {
081            log.error("newStep wrong: {} intensity: {}", newStep, intensity);
082        }
083
084        if (newStep == lastOutputStep) {
085            // nothing to do!
086            if (log.isDebugEnabled()) {
087                log.debug("intensity {} within current step, return", intensity);
088            }
089            return;
090
091        }
092
093        // create output sequence of address, then function
094        X10Sequence out = new X10Sequence();
095        out.addExtData(housecode, devicecode, X10Sequence.EXTCMD_DIM, newStep);
096        // send
097        tc.sendX10Sequence(out, null);
098        lastOutputStep = newStep;
099
100        if (log.isDebugEnabled()) {
101            log.debug("sendIntensity({}) house {} device {} newStep: {}", intensity, X10Sequence.houseValueToText(housecode), devicecode, newStep);
102        }
103    }
104
105    private final static Logger log = LoggerFactory.getLogger(SpecificX10Light.class);
106}