ZeroOne3010 / yetanotherhueapi

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

Importing this API #8

Closed DanielBlik closed 5 years ago

DanielBlik commented 5 years ago

Hello ,

still learning the ropes of programming I took a break cuz school. I used to make the hue classes in the API project package itself that worked. I can't seem to find how I would import this API in a different java project all the library tutorials on the internet won't work I would appreciate if you could take a look :) thanks in advance package Hue_app;

import java.awt.EventQueue; import io.github.zeerone3010.yahueapi; import javax.swing.JFrame; import org.jsoup.*; import javax.swing.JLabel; import com.jgoodies.forms.factories.DefaultComponentFactory; import java.awt.BorderLayout;

public class Hue_app {

private JFrame frame;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Hue_app window = new Hue_app();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Hue_app() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JLabel label = DefaultComponentFactory.getInstance().createTitle("");
    frame.getContentPane().add(label, BorderLayout.NORTH);
}

}

ZeroOne3010 commented 5 years ago

The easiest way to do that is to use a build tool like Apache Maven and to add the library as a dependency into the pom.xml file. Your IDE probably offers a New Project command where you can choose to use Maven. Then just take the dependency definition from the README.md of this project and add it there. If you are not using an IDE then you can also use Maven with the command line by following, say, this tutorial: https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

ZeroOne3010 commented 5 years ago

@MrBlik123 Did you get this thing sorted out? For what it's worth, I've got another project called hue2influx where I'm using this library as a dependency. You can see the relevant pom.xml here: https://github.com/ZeroOne3010/hue2influx/blob/master/pom.xml

DanielBlik commented 5 years ago

@ZeroOne3010 I managed to figure out how to get the maven project in my package explorer but I just can't seem to figure out how I would inherit / import this library into a different java project. I follow each step of tutorials but I can't seem to figure it out probably because I'm using the wrong search terms idk. But I really appreciate you taking the time to help me :)

zone4182 commented 5 years ago

Hi,

I really like the package so far, its very easy to use. However I'm struggling with one thing. So i can easily set the status of all light in a room like your example shows: (doing this with a restcontroller)

`@GetMapping(value = "/set-room-light-status") public ResponseEntity setRoomLightColors(@RequestParam(value = "bridge") String bridge, @RequestParam(value = "room") String room, @RequestParam(value = "status") Boolean on, @RequestParam(value = "color") String color) { //get bridge info from repo HueBridge hBridge = hueService.getByName(bridge);

    final Hue oHue = new Hue(hBridge.getIpaddress(), hBridge.getApikey());
    final Room oRoom = oHue.getRoomByName(room).get();

    Color c;
    try {           
        pattern = Pattern.compile(HEX_PATTERN);
        matcher = pattern.matcher(color);

        if(matcher.matches()){
            c = Color.decode(color);
        }else{
            c = (Color)Color.class.getField(color).get(null);
        }
        oRoom.setState(State.builder().color(c).on(on));

    } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

        return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
    }       

    return new ResponseEntity(HttpStatus.OK);
}`

But I'm trying to figure out how to do this for an individual light. I thought it would be something like this:

`@GetMapping(value = "/set-lights-color") public ResponseEntity setLightColor(@RequestParam(value = "bridge") String bridge, @RequestParam(value = "lights") String lights, @RequestParam(value = "color") String color) { //get bridge info from repo HueBridge hBridge = hueService.getByName(bridge); final Hue oHue = new Hue(hBridge.getIpaddress(), hBridge.getApikey()); List hLights = Arrays.asList(lights.split(","));

    Color c;
    try {
        pattern = Pattern.compile(HEX_PATTERN);
        matcher = pattern.matcher(color);

        if(matcher.matches()){
            c = Color.decode(color);
        }else{
            c = (Color)Color.class.getField(color).get(null);
        }

        oHue.getRaw().getLights().forEach((k,v)->{
            if(hLights.contains(v.getName())){
                v.getState().setOn(false);
            }
        });
    } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       

    return new ResponseEntity(HttpStatus.OK);
}`

But it doesnt seem to work. Also I wouldnt know how to actually change the color for instance. Is it even possible like this? Or maybe I'm not understanding the purpose of it ;-)

DanielBlik commented 5 years ago

@zone4182 in order to change the color you can only do it per room i believe to set the color of a room use room.setState(State.builder().color(java.awt.Color.RED).on());

ZeroOne3010 commented 5 years ago

@zone4182 Hello there! I'm sorry about the poor documentation, I should improve it. You most definitely can change the colors of individual lights, I'd say just as easily as you can do that for rooms. First, forget about the getRaw() method. You can only read data using it, you cannot change anything. The individual lights are accessed through their Room objects. In your first example you have this line:

oRoom.setState(State.builder().color(c).on(on));

To have it set the color of an individual light named Foo, you'd do it like this:

oRoom.getLightByName("Foo").ifPresent(light -> light.setState(State.builder().color(c).on(on)));
ZeroOne3010 commented 5 years ago

@ZeroOne3010 I managed to figure out how to get the maven project in my package explorer but I just can't seem to figure out how I would inherit / import this library into a different java project. I follow each step of tutorials but I can't seem to figure it out probably because I'm using the wrong search terms idk. But I really appreciate you taking the time to help me :)

@MrBlik123 Can you create a repository for your project and push it to GitHub? I might be able to see why it's not working.

DanielBlik commented 5 years ago

@ZeroOne3010 i managed to figure out how one inherits stuff but i just cant figure out how i find my group id for a dependencie thank you for your patience in helping me btw

ZeroOne3010 commented 5 years ago

For what it's worth, I've now updated the README.md file to define the required import statement and to give an example on how to change the color of an individual light.

ZeroOne3010 commented 5 years ago

@ZeroOne3010 i managed to figure out how one inherits stuff but i just cant figure out how i find my group id for a dependencie thank you for your patience in helping me btw

@MrBlik123 The group id for your own project is basically something that you can just make up. It's usually a domain name that you control, but in reverse. In case you don't own a domain name, you can also use something like io.github.mrblik123, or, simply, mrblik123. Do read this short guide on the naming conventions: https://maven.apache.org/guides/mini/guide-naming-conventions.html

ZeroOne3010 commented 5 years ago

In 45bfd4240c I have now also added Javadocs that should explain where @zone4182 went wrong and what he should try instead.

DanielBlik commented 5 years ago

@ZeroOne3010 Thank you so much for your patience and help. I managed to figure it out