Search in sources :

Example 1 with GetDevicesResult

use of org.openhab.binding.mystromecopower.internal.api.model.GetDevicesResult in project openhab1-addons by openhab.

the class MystromClient method getDevices.

private List<MystromDevice> getDevices(boolean minimalMode) {
    Reader reader = null;
    logger.debug("get all devices state");
    try {
        String url = API_URL + "devices" + "?authToken=" + this.authToken;
        if (minimalMode) {
            url = url + "&minimal=true";
        }
        HttpURLConnection httpURLConnection;
        httpURLConnection = (HttpURLConnection) new URL(url).openConnection();
        httpURLConnection.connect();
        int responseCode = httpURLConnection.getResponseCode();
        if (responseCode != HttpURLConnection.HTTP_OK) {
            logger.error("Get devices http code: '{}'", responseCode);
            return new ArrayList<MystromDevice>();
        }
        InputStream inputStream = httpURLConnection.getInputStream();
        reader = new InputStreamReader(inputStream, "UTF-8");
        JsonObject jsonObject = (JsonObject) jsonParser.parse(reader);
        String status = jsonObject.get("status").getAsString();
        if (!status.equals("ok")) {
            logger.error("Error while getting devices: '{}'", status);
            return new ArrayList<MystromDevice>();
        }
        GetDevicesResult result = gson.fromJson(jsonObject, GetDevicesResult.class);
        logger.debug("Devices discovery sucessfull, found '{}' devices", result.devices.size());
        return result.devices;
    } catch (Exception ex) {
        logger.error("Error getting devices: '{}'", ex.toString());
        return new ArrayList<MystromDevice>();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ignored) {
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) URL(java.net.URL) IOException(java.io.IOException) MystromDevice(org.openhab.binding.mystromecopower.internal.api.model.MystromDevice) HttpURLConnection(java.net.HttpURLConnection) GetDevicesResult(org.openhab.binding.mystromecopower.internal.api.model.GetDevicesResult)

Aggregations

JsonObject (com.google.gson.JsonObject)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 GetDevicesResult (org.openhab.binding.mystromecopower.internal.api.model.GetDevicesResult)1 MystromDevice (org.openhab.binding.mystromecopower.internal.api.model.MystromDevice)1