Search in sources :

Example 1 with JSONCachedMeteringValueImpl

use of org.openhab.binding.digitalstrom.internal.client.entity.impl.JSONCachedMeteringValueImpl 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)

Aggregations

LinkedList (java.util.LinkedList)1 JSONArray (org.json.simple.JSONArray)1 JSONObject (org.json.simple.JSONObject)1 CachedMeteringValue (org.openhab.binding.digitalstrom.internal.client.entity.CachedMeteringValue)1 JSONCachedMeteringValueImpl (org.openhab.binding.digitalstrom.internal.client.entity.impl.JSONCachedMeteringValueImpl)1