alexaactions.SmartThingsAgent.java Source code

Java tutorial

Introduction

Here is the source code for alexaactions.SmartThingsAgent.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package alexaactions;

import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.*;
import org.json.simple.parser.JSONParser;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;

// client id and access token
/**
 *
 * @author Paul Cifarelli
 */
public class SmartThingsAgent {

    private final String client;
    private final String access_token;
    private final String endpoints_url;
    private final RESTCaller smartthings;
    private final JSONArray endpoints;

    SmartThingsAgent() {
        client = "8ecfcc6e-b07b-487d-815f-9494b1789ac5";
        access_token = "10ddc55b-d266-47f5-9dc7-6f9b71e73094";
        endpoints_url = "https://graph.api.smartthings.com/api/smartapps/endpoints/" + client + "?access_token="
                + access_token;
        smartthings = new RESTCaller();
        String endp = getEndpoints();

        Object obj = null;
        JSONParser parser = new JSONParser();

        try {
            obj = parser.parse(endp);
        } catch (ParseException ex) {
            Logger.getLogger(SmartThingsAgent.class.getName()).log(Level.SEVERE, null, ex);
        }

        endpoints = (JSONArray) obj;
        System.out.println(endpoints);
    }

    private String getEndpoints() {
        return smartthings.get(endpoints_url, null);
    }

    String post(String path, String input) {
        int i;

        if (endpoints == null) {
            return null;
        }

        for (i = 0; i < endpoints.size(); i++) {
            JSONObject e = (JSONObject) endpoints.get(i);
            String url = (String) e.get("uri") + "/" + path;
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Authorization", "Bearer " + access_token);

            return smartthings.post(url, input, headers);
        }
        return null;
    }

    String get(String path) {
        int i;

        if (endpoints == null) {
            return null;
        }

        for (i = 0; i < endpoints.size(); i++) {
            JSONObject e = (JSONObject) endpoints.get(i);
            String url = (String) e.get("uri") + "/" + path;
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Authorization", "Bearer " + access_token);

            return smartthings.get(url, headers);
        }
        return null;
    }

    String getById(String path, String id) {
        String devices_json = get(path);

        System.out.println(devices_json);

        JSONParser parser = new JSONParser();
        Object obj = null;
        JSONObject device;
        JSONArray arr;

        // the whole purpose of this code is to determine if the id is valid
        try {
            obj = parser.parse(devices_json);
        } catch (ParseException ex) {
            Logger.getLogger(SmartThingsTemperatureDevices.class.getName()).log(Level.SEVERE, null, ex);
        }

        arr = (JSONArray) obj;
        if (arr != null) {
            int i, j;

            for (i = 0; i < arr.size(); i++) {
                device = (JSONObject) arr.get(i);
                String d_id = (String) device.get("id");
                if (d_id == null ? id == null : d_id.equals(id)) {
                    for (j = 0; j < endpoints.size(); j++) {
                        JSONObject e = (JSONObject) endpoints.get(j);
                        String url = (String) e.get("uri") + "/" + path + "/" + d_id;
                        HashMap<String, String> headers = new HashMap<String, String>();
                        headers.put("Authorization", "Bearer " + access_token);

                        return smartthings.get(url, headers);
                    }
                }
            }
        }
        return null;
    }

    String getByLabel(String path, String label) {
        String devices_json = get(path);

        System.out.println(devices_json);

        JSONParser parser = new JSONParser();
        Object obj = null;
        JSONObject device;
        JSONArray arr;

        // the whole purpose of this code is to determine if the id is valid
        try {
            obj = parser.parse(devices_json);
        } catch (ParseException ex) {
            Logger.getLogger(SmartThingsTemperatureDevices.class.getName()).log(Level.SEVERE, null, ex);
        }

        arr = (JSONArray) obj;
        if (arr != null) {
            int i, j;

            for (i = 0; i < arr.size(); i++) {
                device = (JSONObject) arr.get(i);
                String d_label = (String) device.get("label");
                String d_id = (String) device.get("id");
                if (d_label == null ? label == null : d_label.equals(label)) {
                    for (j = 0; j < endpoints.size(); j++) {
                        JSONObject e = (JSONObject) endpoints.get(j);
                        String url = (String) e.get("uri") + "/" + path + "/" + d_id;
                        HashMap<String, String> headers = new HashMap<String, String>();
                        headers.put("Authorization", "Bearer " + access_token);

                        return smartthings.get(url, headers);
                    }
                }
            }
        }
        return null;
    }
}