Search in sources :

Example 46 with JsonObject

use of org.json.simple.JsonObject in project openhab1-addons by openhab.

the class DigitalSTROMJSONImpl method loginApplication.

@Override
public String loginApplication(String loginToken) {
    if (loginToken != null && !loginToken.trim().equals("")) {
        String response = null;
        response = transport.execute(JSONRequestConstants.JSON_SYSTEM_LOGIN_APPLICATION + loginToken);
        JSONObject responseObj = handler.toJSONObject(response);
        if (handler.checkResponse(responseObj)) {
            JSONObject obj = handler.getResultJSONObject(responseObj);
            String tokenStr = null;
            if (obj != null && obj.get(JSONApiResponseKeysEnum.SYSTEM_LOGIN.getKey()) != null) {
                tokenStr = obj.get(JSONApiResponseKeysEnum.SYSTEM_LOGIN.getKey()).toString();
            }
            if (tokenStr != null) {
                return tokenStr;
            }
        }
    }
    return null;
}
Also used : JSONObject(org.json.simple.JSONObject)

Example 47 with JsonObject

use of org.json.simple.JsonObject in project openhab1-addons by openhab.

the class DigitalSTROMJSONImpl method getLatest.

@Override
public List<CachedMeteringValue> getLatest(String token, MeteringTypeEnum type, String from, MeteringUnitsEnum unit) {
    if (type != null && from != null) {
        String response = null;
        if (unit != null && type != MeteringTypeEnum.consumption) {
            response = transport.execute(JSONRequestConstants.JSON_METERING_GET_LATEST + JSONRequestConstants.PARAMETER_TOKEN + token + JSONRequestConstants.INFIX_PARAMETER_TYPE + type.name() + JSONRequestConstants.INFIX_PARAMETER_FROM + from + JSONRequestConstants.INFIX_PARAMETER_UNIT + unit.name());
        } else {
            response = transport.execute(JSONRequestConstants.JSON_METERING_GET_LATEST + JSONRequestConstants.PARAMETER_TOKEN + token + JSONRequestConstants.INFIX_PARAMETER_TYPE + type.name() + JSONRequestConstants.INFIX_PARAMETER_FROM + from);
        }
        JSONObject responseObj = handler.toJSONObject(response);
        if (handler.checkResponse(responseObj)) {
            JSONObject latestObj = handler.getResultJSONObject(responseObj);
            if (latestObj != null && latestObj.get(JSONApiResponseKeysEnum.METERING_GET_LATEST.getKey()) instanceof JSONArray) {
                JSONArray array = (JSONArray) latestObj.get(JSONApiResponseKeysEnum.METERING_GET_LATEST.getKey());
                List<CachedMeteringValue> list = new LinkedList<CachedMeteringValue>();
                for (int i = 0; i < array.size(); i++) {
                    if (array.get(i) instanceof JSONObject) {
                        list.add(new JSONCachedMeteringValueImpl((JSONObject) array.get(i)));
                    }
                }
                return list;
            }
        }
    }
    return null;
}
Also used : JSONObject(org.json.simple.JSONObject) JSONCachedMeteringValueImpl(org.openhab.binding.digitalstrom.internal.client.entity.impl.JSONCachedMeteringValueImpl) JSONArray(org.json.simple.JSONArray) CachedMeteringValue(org.openhab.binding.digitalstrom.internal.client.entity.CachedMeteringValue) LinkedList(java.util.LinkedList)

Example 48 with JsonObject

use of org.json.simple.JsonObject in project openhab1-addons by openhab.

the class DigitalSTROMJSONImpl method getTime.

@Override
public int getTime(String token) {
    String response = null;
    response = transport.execute(JSONRequestConstants.JSON_SYSTEM_TIME + JSONRequestConstants.PARAMETER_TOKEN + token);
    JSONObject responseObj = handler.toJSONObject(response);
    if (handler.checkResponse(responseObj)) {
        JSONObject obj = handler.getResultJSONObject(responseObj);
        if (obj != null && obj.get(JSONApiResponseKeysEnum.SYSTEM_GET_TIME.getKey()) != null) {
            int time = -1;
            try {
                time = Integer.parseInt(obj.get(JSONApiResponseKeysEnum.SYSTEM_GET_TIME.getKey()).toString());
            } catch (java.lang.NumberFormatException e) {
                logger.error("NumberFormatException by getTime: " + obj.get(JSONApiResponseKeysEnum.SYSTEM_GET_TIME.getKey()).toString());
            }
            return time;
        }
    }
    return -1;
}
Also used : JSONObject(org.json.simple.JSONObject)

Example 49 with JsonObject

use of org.json.simple.JsonObject in project openhab1-addons by openhab.

the class DigitalSTROMJSONImpl method getDeviceSensorValue.

@Override
public short getDeviceSensorValue(String token, DSID dsid, String name, SensorIndexEnum sensorIndex) {
    if (((dsid != null && dsid.getValue() != null) || name != null) && sensorIndex != null) {
        String response = null;
        if (dsid != null && dsid.getValue() != null) {
            if (name != null) {
                response = transport.execute(JSONRequestConstants.JSON_DEVICE_GET_SENSOR_VALUE + JSONRequestConstants.PARAMETER_TOKEN + token + JSONRequestConstants.INFIX_PARAMETER_DSID + dsid.getValue() + JSONRequestConstants.INFIX_PARAMETER_NAME + name + JSONRequestConstants.INFIX_PARAMETER_SENSOR_INDEX + sensorIndex.getIndex());
            } else {
                response = transport.execute(JSONRequestConstants.JSON_DEVICE_GET_SENSOR_VALUE + JSONRequestConstants.PARAMETER_TOKEN + token + JSONRequestConstants.INFIX_PARAMETER_DSID + dsid.getValue() + JSONRequestConstants.INFIX_PARAMETER_SENSOR_INDEX + sensorIndex.getIndex());
            }
        } else if (name != null) {
            response = transport.execute(JSONRequestConstants.JSON_DEVICE_GET_SENSOR_VALUE + JSONRequestConstants.PARAMETER_TOKEN + token + JSONRequestConstants.INFIX_PARAMETER_NAME + name + JSONRequestConstants.INFIX_PARAMETER_SENSOR_INDEX + sensorIndex.getIndex());
        }
        JSONObject responseObj = handler.toJSONObject(response);
        if (handler.checkResponse(responseObj)) {
            JSONObject valueObject = handler.getResultJSONObject(responseObj);
            if (valueObject != null && valueObject.get(JSONApiResponseKeysEnum.DEVICE_GET_SENSOR_VALUE_SENSOR_VALUE.getKey()) != null) {
                short value = -1;
                try {
                    value = Short.parseShort(valueObject.get(JSONApiResponseKeysEnum.DEVICE_GET_SENSOR_VALUE_SENSOR_VALUE.getKey()).toString());
                } catch (java.lang.NumberFormatException e) {
                    logger.error("NumberFormatException by getDeviceSensorValue: " + valueObject.get(JSONApiResponseKeysEnum.DEVICE_GET_SENSOR_VALUE_SENSOR_VALUE.getKey()).toString());
                }
                return value;
            }
        }
    }
    return -1;
}
Also used : JSONObject(org.json.simple.JSONObject)

Example 50 with JsonObject

use of org.json.simple.JsonObject in project openhab1-addons by openhab.

the class JointSpaceBinding method getTVVolume.

/**
     * Function to query the TV Volume
     *
     * @param host
     * @return struct containing all given information about current volume
     *         settings (volume, mute, min, max) @see volumeConfig
     */
private volumeConfig getTVVolume(String host) {
    volumeConfig conf = new volumeConfig();
    String url = "http://" + host + "/1/audio/volume";
    String volume_json = HttpUtil.executeUrl("GET", url, IOUtils.toInputStream(""), CONTENT_TYPE_JSON, 1000);
    if (volume_json != null) {
        try {
            Object obj = JSONValue.parse(volume_json);
            JSONObject array = (JSONObject) obj;
            conf.mute = Boolean.parseBoolean(array.get("muted").toString());
            conf.volume = Integer.parseInt(array.get("current").toString());
            conf.min = Integer.parseInt(array.get("min").toString());
            conf.max = Integer.parseInt(array.get("max").toString());
        } catch (NumberFormatException ex) {
            logger.warn("Exception while interpreting volume json return");
        } catch (Throwable t) {
            logger.warn("Could not parse JSON String for volume value. Error: {}", t.toString());
        }
    }
    return conf;
}
Also used : JSONObject(org.json.simple.JSONObject) JSONObject(org.json.simple.JSONObject)

Aggregations

JSONObject (org.json.simple.JSONObject)961 Test (org.junit.Test)312 JSONArray (org.json.simple.JSONArray)232 JSONParser (org.json.simple.parser.JSONParser)164 Map (java.util.Map)97 HashMap (java.util.HashMap)91 IOException (java.io.IOException)84 ArrayList (java.util.ArrayList)75 ParseException (org.json.simple.parser.ParseException)60 HttpClient (org.apache.commons.httpclient.HttpClient)39 File (java.io.File)38 List (java.util.List)38 BlockchainTest (org.xel.BlockchainTest)34 GetMethod (org.apache.commons.httpclient.methods.GetMethod)30 HttpURLConnection (java.net.HttpURLConnection)27 Tuple (org.apache.storm.tuple.Tuple)23 Transaction (org.xel.Transaction)23 APICall (org.xel.http.APICall)23 InputStreamReader (java.io.InputStreamReader)22 WriterConfiguration (org.apache.metron.common.configuration.writer.WriterConfiguration)22