use of org.openhab.binding.openpaths.OpenPathsBindingProvider in project openhab1-addons by openhab.
the class OpenPathsBinding method execute.
/**
* @{inheritDoc
*/
@Override
public void execute() {
if (!bindingsExist()) {
logger.debug("There is no existing OpenPaths binding configuration => refresh cycle aborted!");
return;
}
for (OpenPathsBindingProvider provider : providers) {
for (String itemName : provider.getItemNames()) {
logger.trace("try binding provider item: " + itemName);
OpenPathsBindingConfig bindingConfig = provider.getItemConfig(itemName);
String bindingConfigName = bindingConfig.getName();
String[] bindingParts = bindingConfigName.split("\\:");
if (bindingParts.length < 1) {
logger.error("Empty OpenPaths binding config");
continue;
}
String name = bindingParts[0];
if (!openPathsUsers.containsKey(name)) {
logger.warn("There is no OpenPaths user configured for '" + name + "'. Please add this user to the binding configuration, including both the ACCESS_KEY and SECRET_KEY from the OpenPaths profile.");
continue;
}
Location location = null;
OpenPathsUser openPathsUser = openPathsUsers.get(name);
if (openPathsUser.lastUpdateTS + this.refreshInterval < System.currentTimeMillis()) {
String accessKey = openPathsUser.getAccessKey();
String secretKey = openPathsUser.getSecretKey();
if (StringUtils.isEmpty(accessKey)) {
logger.warn("There is no ACCESS_KEY configured for '" + name + "'. Please add this user to the binding configuration, including both the ACCESS_KEY and SECRET_KEY from the OpenPaths profile.");
continue;
}
if (StringUtils.isEmpty(secretKey)) {
logger.warn("There is no SECRET_KEY configured for '" + name + "'. Please add this user to the binding configuration, including both the ACCESS_KEY and SECRET_KEY from the OpenPaths profile.");
continue;
}
logger.debug("Requesting location for '{}'...", name);
location = getUserLocation(accessKey, secretKey);
if (location != null) {
openPathsUsers.get(name).setLastLocation(location);
logger.debug("New location received for '{}': {}", name, location.toString());
} else {
logger.warn("Unable to determine location for '{}'. Skipping.", name);
continue;
}
} else {
location = openPathsUsers.get(name).getLastLocation();
logger.trace("Using cached location for '{}'", openPathsUser.toString());
}
String bindingLocationName = bindingParts.length > 1 ? bindingParts[1] : "";
if (bindingLocationName.startsWith("current")) {
if (bindingLocationName.equals("currentLocation")) {
eventPublisher.postUpdate(itemName, new StringType("" + location.getLatitude() + ", " + location.getLongitude()));
} else if (bindingLocationName.equals("currentLatitude")) {
eventPublisher.postUpdate(itemName, new DecimalType(new BigDecimal(location.getLatitude())));
} else if (bindingLocationName.equals("currentLongitude")) {
eventPublisher.postUpdate(itemName, new DecimalType(new BigDecimal(location.getLongitude())));
} else {
logger.warn("unsupported Binding: " + bindingLocationName);
}
continue;
}
if (!locations.containsKey(bindingLocationName)) {
logger.warn("location name " + bindingLocationName + " not configured, falling back to 'home'");
bindingLocationName = "home";
}
logger.debug("OpenPathsUser: " + name + "@" + bindingLocationName);
LocationBindingType bindingType = LocationBindingType.on;
if (bindingParts.length == 3) {
if (bindingParts[2].equals("distance")) {
bindingType = LocationBindingType.distance;
}
}
Location bindingLocation = locations.get(bindingLocationName);
logger.trace("Calculating distance between home ({}) and user location ({}) for '{}'...", new Object[] { bindingLocation.toString(), location.toString(), name });
double distance = calculateDistance(bindingLocation, location);
bindingLocation.setDistance(distance);
logger.trace("Distance calculated as {} for '{}'@'{}'", distance, name, bindingLocationName);
if (bindingType.equals(LocationBindingType.on)) {
float fence = bindingLocation.getGeofence() == 0.0 ? geoFence : bindingLocation.getGeofence();
if (distance <= fence) {
logger.trace("Detected that '{}'@'{}' is inside the geofence ({}m)", name, bindingLocationName, fence);
eventPublisher.postUpdate(itemName, OnOffType.ON);
} else {
logger.trace("Detected that '{}'@'{}' is outside the geofence ({}m)", name, bindingLocationName, fence);
eventPublisher.postUpdate(itemName, OnOffType.OFF);
}
} else if (bindingType.equals(LocationBindingType.distance)) {
eventPublisher.postUpdate(itemName, new DecimalType(new BigDecimal(distance / 1000)));
}
}
}
}
Aggregations