use of org.openhab.binding.powerdoglocalapi.PowerDogLocalApiBindingProvider 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 org.openhab.binding.powerdoglocalapi.PowerDogLocalApiBindingProvider 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