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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations