use of redstone.xmlrpc.XmlRpcStruct in project openhab1-addons by openhab.
the class PowerDogLocalApiBinding method loadPowerDogResponse.
private XmlRpcStruct loadPowerDogResponse(boolean needsUpdate, PowerDogLocalApiServerConfig server) {
// Get all current linear values from the powerdog in case of an update
XmlRpcStruct response = null;
if (needsUpdate == true) {
try {
logger.debug("PowerDogLocalApi querying PowerDog");
// perform XML RPC call and store response
PowerDog powerdog = (PowerDog) XmlRpcProxy.createProxy(server.url(), "", new Class[] { PowerDog.class }, false);
response = powerdog.getAllCurrentLinearValues(server.password);
server.cache = response;
server.lastUpdate = System.currentTimeMillis();
logger.debug("PowerDog.getAllCurrentLinearValues() result: {}", response.toString());
} catch (Exception e) {
logger.warn("PowerDogLocalApi querying PowerDog failed");
logger.warn(e.getMessage());
}
} else {
logger.debug("Using PowerDogLocalApi cache");
response = server.cache;
}
return response;
}
use of redstone.xmlrpc.XmlRpcStruct in project openhab1-addons by openhab.
the class PowerDogLocalApiBinding method getVariable.
/**
* Parse PowerDog xmlrpc response to getAllCurrentLinearValues
*
* @param response
* PowerDog Response
* @param valueId
* Value ID of PowerDog Item
* @param name
* Parameter name to be updated
* @return
*/
private String getVariable(XmlRpcStruct response, String valueId, String name) {
try {
XmlRpcStruct reply = response.getStruct("Reply");
XmlRpcStruct item = reply.getStruct(valueId);
String value = item.getString(name);
return value;
} catch (Exception e) {
return null;
}
}
use of redstone.xmlrpc.XmlRpcStruct in project openhab1-addons by openhab.
the class PowerDogLocalApiBinding method internalReceiveUpdate.
/**
* @{inheritDoc
*/
@Override
protected void internalReceiveUpdate(String itemName, State newState) {
logger.debug("internalReceiveUpdate({},{}) is called!", itemName, newState);
// cycle on all available powerdogs
for (PowerDogLocalApiBindingProvider provider : providers) {
if (!provider.providesBindingFor(itemName)) {
continue;
}
// only in case of an outbinding, this need to be handled
if (provider.getOutBindingItemNames().contains(itemName)) {
// check if item may send update already now
// time indicated in config is the minimum time between two
// updates
Long lastUpdateTimeStamp = lastUpdateMap.get(itemName);
if (lastUpdateTimeStamp == null) {
lastUpdateTimeStamp = 0L;
}
long age = System.currentTimeMillis() - lastUpdateTimeStamp;
boolean itemMayUpdate = (age >= provider.getRefreshInterval(itemName));
if (itemMayUpdate) {
// Convert new State to PowerDog set Current_Value string
String value = "0";
if (newState instanceof OnOffType) {
if (newState == OnOffType.ON) {
value = "1";
}
} else // the best solution, but it is sufficient
if (newState instanceof OpenClosedType) {
if (newState == OpenClosedType.OPEN) {
value = "1";
}
} else // see comment above
if (newState instanceof PercentType) {
value = newState.toString();
} else if (newState instanceof DecimalType) {
value = newState.toString();
}
// Get the unit serverId from the binding, and relate that
// to the config
String unit = provider.getServerId(itemName);
PowerDogLocalApiServerConfig server = serverList.get(unit);
try {
logger.debug("PowerDogLocalApi sending to PowerDog");
// Perform XML RPC call
PowerDog powerdog = (PowerDog) XmlRpcProxy.createProxy(server.url(), "", new Class[] { PowerDog.class }, false);
XmlRpcStruct response = powerdog.setLinearSensorDevice(server.password, provider.getValueId(itemName), value);
lastUpdateMap.put(itemName, System.currentTimeMillis());
logger.debug("PowerDog.setLinearSensorDevice() result: {}", response.toString());
} catch (Exception e) {
logger.warn("PowerDogLocalApi sending to PowerDog failed");
logger.warn(e.getMessage());
}
}
}
}
}
use of redstone.xmlrpc.XmlRpcStruct in project openhab1-addons by openhab.
the class PowerDogLocalApiBinding method execute.
/**
* @{inheritDoc
*/
@Override
protected void execute() {
logger.debug("execute() method is called!");
// cycle over all available powerdogs
for (PowerDogLocalApiBindingProvider provider : providers) {
/*
* TODO: check if reading of out-bindings is useful - currently it
* seems to be applicable only for openhab init (but it works
* without, so I give it a try) and in case several clients are
* writing to the same PowerAPI, which is risky due to concurrency
*/
for (String itemName : provider.getInBindingItemNames()) {
// get item specific refresh interval
int refreshInterval = provider.getRefreshInterval(itemName);
// check if item needs update
Long lastUpdateTimeStamp = lastUpdateMap.get(itemName);
if (lastUpdateTimeStamp == null) {
lastUpdateTimeStamp = 0L;
}
long age = System.currentTimeMillis() - lastUpdateTimeStamp;
boolean itemNeedsUpdate = (age >= refreshInterval);
if (itemNeedsUpdate) {
logger.debug("Item '{}' is about to be refreshed now", itemName);
// Get the unit serverId from the binding, and relate that
// to the config
String unit = provider.getServerId(itemName);
PowerDogLocalApiServerConfig server = serverList.get(unit);
// get the server specific update time and check if it needs
// an update
boolean serverNeedsUpdate = false;
if (server == null) {
serverNeedsUpdate = false;
logger.error("Unknown PowerDog server referenced: {}", unit);
continue;
} else {
age = System.currentTimeMillis() - server.lastUpdate;
serverNeedsUpdate = (age >= server.refresh);
}
XmlRpcStruct response = loadPowerDogResponse(serverNeedsUpdate, server);
// update item state
if (response != null) {
String value = getVariable(response, provider.getValueId(itemName), provider.getName(itemName));
if (value != null) {
Class<? extends Item> itemType = provider.getItemType(itemName);
State state = createState(itemType, value);
eventPublisher.postUpdate(itemName, state);
lastUpdateMap.put(itemName, System.currentTimeMillis());
}
}
}
}
}
logger.debug("execute() method is finished!");
}
Aggregations