ZeroOne3010 / yetanotherhueapi

A Java library for controlling Philips Hue lights. Available from the Maven Central.
MIT License
68 stars 21 forks source link

Change Brightness of a room #9

Closed DanielBlik closed 5 years ago

DanielBlik commented 5 years ago

How does one change the brightness of a room i have tried it these ways

int brightnessmax = 254;
int brighnessmin = 1;
JSlider slider = new JSlider(brighnessmin , brightnessmax);
slider.setBackground(Color.DARK_GRAY);
slider.setForeground(Color.DARK_GRAY);
slider.setBounds(157, 130, 111, 26);
slider.addChangeListener(new ChangeListener() {

    public void stateChanged(ChangeEvent e) {
        JSlider source = (JSlider)e.getSource();
        int brightness = (int)source.getValue();
        if(!source.getValueIsAdjusting()) {
            ((BrightnessStep) room).brightness(brightness);
        }
    }
});
int brightnessmax = 254;
int brighnessmin = 1;
JSlider slider = new JSlider(brighnessmin , brightnessmax);
slider.setBackground(Color.DARK_GRAY);
slider.setForeground(Color.DARK_GRAY);
slider.setBounds(157, 130, 111, 26);
slider.addChangeListener(new ChangeListener() {

    public void stateChanged(ChangeEvent e) {
        JSlider source = (JSlider)e.getSource();
        int brightness = (int)source.getValue();
        if(!source.getValueIsAdjusting()) {
            //((BrightnessStep) room).brightness(brightness);   
            room.setState(((BrightnessStep) State.builder()).brightness(brightness));
        }
    }
});

but they both don't work first one gives a ClassCastException which is logical but the 2nd gives an error on the code and gives The method setState(State) in the type Room is not applicable for the arguments (StateBuilderSteps.BuildStep) How do I properly change the brightness then?

thanks for the great api btw it has been working really well

ZeroOne3010 commented 5 years ago

Hey, thanks for creating an issue, that's actually a really good question! You were so close with the second one -- this one should do the trick:

room.setState(((BrightnessStep) State.builder()).brightness(brightness).keepCurrentState())

Replace keepCurrentState() with on() if you are not 100% sure that the lights are already on.

I admit this is far from an optimal solution. The justification is that the lights are actually controlled with hue, saturation and brightness, so the State builder requires you to enter those other two values first -- unless you do something clever like cast it right away into a BrightnessStep instance. ;) However, obviously just changing the brightness only should be a supported use case -- and while at it, I suppose changing the hue and saturation alone should be supported too.

The State builder is designed to protect against creating self-contradictory states, such as a state where a user would set a color with both Color.DARK_GRAY and a string like #A12345. In this case it seems to go too far in requiring a hue and a saturation before allowing to change the brightness.

DanielBlik commented 5 years ago

Thank you it worked :)