use of org.openhab.binding.digitalstrom.internal.client.entity.CachedMeteringValue in project openhab1-addons by openhab.
the class DigitalSTROMBinding method execute.
/**
* @{inheritDoc
*/
@Override
protected void execute() {
if (!serverIsFound()) {
login();
} else {
if (digitalSTROM.getTime(getSessionToken()) == -1) {
logger.warn("test method failed ... new login now");
login();
}
}
for (DigitalSTROMBindingProvider provider : providers) {
for (DigitalSTROMBindingConfig itemConf : provider.getAllCircuitConsumptionItems()) {
String itemName = itemConf.itemName;
int refreshInterval = itemConf.timeinterval;
Long lastUpdateTimeStamp = lastUpdateMap.get(itemName);
if (lastUpdateTimeStamp == null) {
lastUpdateTimeStamp = 0L;
}
long age = System.currentTimeMillis() - lastUpdateTimeStamp;
boolean needsUpdate = age >= refreshInterval;
if (needsUpdate) {
logger.debug("item '{}' is about to be refreshed now", itemName);
int consumptionValue = -1;
if (itemConf.consumption == null || itemConf.consumption.equals(ConsumptionConfig.OUTPUT_CURRENT)) {
itemConf.consumption = ConsumptionConfig.ACTIVE_POWER;
}
switch(itemConf.consumption) {
case ACTIVE_POWER:
List<CachedMeteringValue> consumptionList = digitalSTROM.getLatest(getSessionToken(), MeteringTypeEnum.consumption, ".meters(" + itemConf.dsmid.getValue().toLowerCase() + ")", null);
if (consumptionList != null) {
consumptionValue = 0;
for (CachedMeteringValue value : consumptionList) {
consumptionValue += value.getValue();
}
}
break;
case ELECTRIC_METER:
List<CachedMeteringValue> energyList = digitalSTROM.getLatest(getSessionToken(), MeteringTypeEnum.energy, ".meters(" + itemConf.dsmid.getValue().toLowerCase() + ")", MeteringUnitsEnum.Wh);
if (energyList != null) {
consumptionValue = 0;
for (CachedMeteringValue value : energyList) {
consumptionValue += value.getValue();
}
}
break;
default:
break;
}
org.openhab.core.types.State state = UnDefType.NULL;
if (consumptionValue != -1) {
state = new DecimalType(consumptionValue);
}
if (state != null) {
eventPublisher.postUpdate(itemName, state);
}
lastUpdateMap.put(itemName, System.currentTimeMillis());
}
}
for (DigitalSTROMBindingConfig itemConf : provider.getAllDeviceConsumptionItems()) {
String itemName = itemConf.itemName;
int refreshInterval = itemConf.timeinterval;
Long lastUpdateTimeStamp = lastUpdateMap.get(itemName);
if (lastUpdateTimeStamp == null) {
lastUpdateTimeStamp = 0L;
}
long age = System.currentTimeMillis() - lastUpdateTimeStamp;
boolean needsUpdate = age >= refreshInterval;
if (needsUpdate) {
logger.debug("item '{}' is about to be refreshed now", itemName);
Device device = getDsidToDeviceMap().get(itemConf.dsid.getValue());
if (device != null) {
if (itemConf.sensor == null) {
SensorIndexEnum sensorIndex = null;
try {
sensorIndex = SensorIndexEnum.valueOf(itemConf.consumption.name());
} catch (Exception e) {
sensorIndex = SensorIndexEnum.ACTIVE_POWER;
}
addLowPriorityJob(new DeviceConsumptionSensorJob(device, sensorIndex));
lastUpdateMap.put(itemName, System.currentTimeMillis());
} else {
SensorIndexEnum sensorIndex = null;
try {
sensorIndex = SensorIndexEnum.valueOf(itemConf.sensor.name());
} catch (Exception e) {
}
addHighPriorityJob(new DeviceSensorValueJob(device, sensorIndex));
}
}
}
}
}
}
use of org.openhab.binding.digitalstrom.internal.client.entity.CachedMeteringValue 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;
}
Aggregations