sexta-feira, 31 de janeiro de 2014

Como acessar o Java Embedded Panel com Jersey Client facinho facinho! - Campus Party 2014

É assim bem simples, crie um projeto no NetBeans e depois adicione a biblioteca Jersey clicando com botão direito do mouse nas propriedades do projetos -> bibliotecas.

Ai copia e cola este código:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package embeddedpanel;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

/**
 *
 * @author root
 */
public class EmbeddedPanel {

    public static void main(String[] args) {
        System.out.println("External Temperature: " + getTemperature());
        System.out.println("Humidity            : " + getHumidity());
        System.out.println("Distance            : " + getDistance());
        System.out.println("Alcohol             : " + getAlcohol());
        setRGB(255, 0, 0);
        setRGB(0, 255, 0);
        setRGB(0, 0, 255);
    }

    public static void setRGB(int red, int green, int blue) {
        Client client = Client.create();
        WebResource webResource = client
                .resource("http://192.168.1.12:8080/things/red/" + red);
        ClientResponse response = webResource.accept("text/plain")
                .get(ClientResponse.class);
        if (response.getStatus() != 200) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + response.getStatus());
        }
        webResource = client
                .resource("http://192.168.1.12:8080/things/green/" + green);
        response = webResource.accept("text/plain")
                .get(ClientResponse.class);
        if (response.getStatus() != 200) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + response.getStatus());
        }
        webResource = client
                .resource("http://192.168.1.12:8080/things/blue/" + blue);
        response = webResource.accept("text/plain")
                .get(ClientResponse.class);
        if (response.getStatus() != 200) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + response.getStatus());
        }

    }

    public static float getTemperature() {
        Client client = Client.create();
        WebResource webResource = client
                .resource("http://192.168.1.11:8080/things/temp_out");
        ClientResponse response = webResource.accept("text/plain")
                .get(ClientResponse.class);
        if (response.getStatus() != 200) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + response.getStatus());
        }
        String output = response.getEntity(String.class);
        return Float.parseFloat(output);

    }

    public static float getHumidity() {
        Client client = Client.create();
        WebResource webResource = client
                .resource("http://192.168.1.11:8080/things/humidity");
        ClientResponse response = webResource.accept("text/plain")
                .get(ClientResponse.class);
        if (response.getStatus() != 200) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + response.getStatus());
        }
        String output = response.getEntity(String.class);
        return Float.parseFloat(output);

    }

    public static int getDistance() {
        Client client = Client.create();
        WebResource webResource = client
                .resource("http://192.168.1.11:8080/things/distance");
        ClientResponse response = webResource.accept("text/plain")
                .get(ClientResponse.class);
        if (response.getStatus() != 200) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + response.getStatus());
        }
        String output = response.getEntity(String.class);
        return Integer.parseInt(output);

    }

    public static int getAlcohol() {
        Client client = Client.create();
        WebResource webResource = client
                .resource("http://192.168.1.12:8080/things/alcohol");
        ClientResponse response = webResource.accept("text/plain")
                .get(ClientResponse.class);
        if (response.getStatus() != 200) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + response.getStatus());
        }
        String output = response.getEntity(String.class);
        return Integer.parseInt(output);
    }
}

E boa!

Ah, se estiver aqui na Campus não esqueca de entrar na rede WIFI do Embedded Panel:

WIFI: iot
PWD: iotiotiot

É assim bem simples começar a usar Internet of Things sem ter que soldar nem programar placas!

Boa Diversão,
Vinicius Senger

2 comentários:

  1. تعد شركة Mosquito Control Company واحدة من أفضل الشركات التي تقوم برش جميع البعوض بأحجام مختلفة وترش دائمًا بالعديد من الأدوات المخصصة للرش ، كما توفر مجموعة واسعة من الرش التي تساعد في قتل البعوض لأنها واحدة من أكثر الحشرات الضارة حمل الحشرات.
    شركة النجوم لمكافحة الحشرات
    شركة مكافحة حشرات بالطائف
    شركة مكافحة النمل الابيض بالطائف
    شركة كشف تسربات المياه بالطائف

    ResponderExcluir