use of org.openhab.core.cache.ExpiringCache in project openhab-addons by openhab.
the class SmartherBridgeHandler method unregisterNotification.
@Override
public synchronized void unregisterNotification(String plantId) throws SmartherGatewayException {
if (!config.isUseNotifications()) {
return;
}
final ExpiringCache<List<Location>> localLocationCache = this.locationCache;
if (localLocationCache != null) {
List<Location> locations = localLocationCache.getValue();
final long remainingModules = getThing().getThings().stream().map(t -> (SmartherModuleHandler) t.getHandler()).filter(h -> h.getPlantId().equals(plantId)).count();
if (locations != null && remainingModules == 0) {
final Optional<Location> maybeLocation = locations.stream().filter(l -> l.getPlantId().equals(plantId)).findFirst();
if (maybeLocation.isPresent()) {
Location location = maybeLocation.get();
final String subscriptionId = location.getSubscriptionId();
if (location.hasSubscription() && (subscriptionId != null)) {
// Call gateway to unregister plant subscription
unsubscribePlant(plantId, subscriptionId);
logger.debug("Bridge[{}] Notification unregistered: [plantId={}, subscriptionId={}]", thing.getUID(), plantId, subscriptionId);
// Remove the subscription from notifications list
List<String> notifications = config.removeNotification(subscriptionId);
// Save the updated notifications list back to bridge config
Configuration configuration = editConfiguration();
configuration.put(PROPERTY_NOTIFICATIONS, notifications);
updateConfiguration(configuration);
// Update the local locationCache with the removed data
locations.stream().forEach(l -> {
if (l.getPlantId().equals(plantId)) {
l.unsetSubscription();
}
});
localLocationCache.putValue(locations);
}
}
}
}
}
use of org.openhab.core.cache.ExpiringCache in project openhab-addons by openhab.
the class SmartherBridgeHandler method registerNotification.
@Override
public synchronized void registerNotification(String plantId) throws SmartherGatewayException {
if (!config.isUseNotifications()) {
return;
}
final ExpiringCache<List<Location>> localLocationCache = this.locationCache;
if (localLocationCache != null) {
List<Location> locations = localLocationCache.getValue();
if (locations != null) {
final Optional<Location> maybeLocation = locations.stream().filter(l -> l.getPlantId().equals(plantId)).findFirst();
if (maybeLocation.isPresent()) {
Location location = maybeLocation.get();
if (!location.hasSubscription()) {
// Validate notification Url (must be non-null and https)
final String notificationUrl = config.getNotificationUrl();
if (isValidNotificationUrl(notificationUrl)) {
// Call gateway to register plant subscription
String subscriptionId = subscribePlant(plantId, config.getNotificationUrl());
logger.debug("Bridge[{}] Notification registered: [plantId={}, subscriptionId={}]", thing.getUID(), plantId, subscriptionId);
// Add the new subscription to notifications list
List<String> notifications = config.addNotification(subscriptionId);
// Save the updated notifications list back to bridge config
Configuration configuration = editConfiguration();
configuration.put(PROPERTY_NOTIFICATIONS, notifications);
updateConfiguration(configuration);
// Update the local locationCache with the added data
locations.stream().forEach(l -> {
if (l.getPlantId().equals(plantId)) {
l.setSubscription(subscriptionId, config.getNotificationUrl());
}
});
localLocationCache.putValue(locations);
} else {
logger.warn("Bridge[{}] Invalid notification Url [{}]: must be non-null, public https address", thing.getUID(), notificationUrl);
}
}
}
}
}
}
use of org.openhab.core.cache.ExpiringCache in project openhab-addons by openhab.
the class SmartherBridgeHandler method initialize.
// ===========================================================================
//
// Bridge thing lifecycle management methods
//
// ===========================================================================
@Override
public void initialize() {
logger.debug("Bridge[{}] Initialize handler", thing.getUID());
this.config = getConfigAs(SmartherBridgeConfiguration.class);
if (StringUtil.isBlank(config.getSubscriptionKey())) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "The 'Subscription Key' property is not set or empty. If you have an older thing please recreate it.");
return;
}
if (StringUtil.isBlank(config.getClientId())) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "The 'Client Id' property is not set or empty. If you have an older thing please recreate it.");
return;
}
if (StringUtil.isBlank(config.getClientSecret())) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "The 'Client Secret' property is not set or empty. If you have an older thing please recreate it.");
return;
}
// Initialize OAuth2 authentication support
final OAuthClientService localOAuthService = oAuthFactory.createOAuthClientService(thing.getUID().getAsString(), SMARTHER_API_TOKEN_URL, SMARTHER_AUTHORIZE_URL, config.getClientId(), config.getClientSecret(), SMARTHER_API_SCOPES, false);
localOAuthService.addAccessTokenRefreshListener(SmartherBridgeHandler.this);
this.oAuthService = localOAuthService;
// Initialize Smarther Api
final SmartherApi localSmartherApi = new SmartherApi(localOAuthService, config.getSubscriptionKey(), scheduler, httpClient);
this.smartherApi = localSmartherApi;
// Initialize locations (plant Ids) local cache
final ExpiringCache<List<Location>> localLocationCache = new ExpiringCache<>(Duration.ofMinutes(config.getStatusRefreshPeriod()), this::locationCacheAction);
this.locationCache = localLocationCache;
// Initialize bridge local status
final BridgeStatus localBridgeStatus = new BridgeStatus();
this.bridgeStatus = localBridgeStatus;
updateStatus(ThingStatus.UNKNOWN);
schedulePoll();
logger.debug("Bridge[{}] Finished initializing!", thing.getUID());
}
use of org.openhab.core.cache.ExpiringCache in project openhab-addons by openhab.
the class SmartherModuleHandler method initialize.
// ===========================================================================
//
// Chronothermostat thing lifecycle management methods
//
// ===========================================================================
@Override
public void initialize() {
logger.debug("Module[{}] Initialize handler", thing.getUID());
final Bridge localBridge = getBridge();
if (localBridge == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED);
return;
}
final SmartherBridgeHandler localBridgeHandler = (SmartherBridgeHandler) localBridge.getHandler();
this.bridgeHandler = localBridgeHandler;
if (localBridgeHandler == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, String.format("Missing configuration from the Smarther Bridge (UID:%s). Fix configuration or report if this problem remains.", localBridge.getBridgeUID()));
return;
}
this.config = getConfigAs(SmartherModuleConfiguration.class);
if (StringUtil.isBlank(config.getPlantId())) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "The 'Plant Id' property is not set or empty. If you have an older thing please recreate it.");
return;
}
if (StringUtil.isBlank(config.getModuleId())) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "The 'Module Id' property is not set or empty. If you have an older thing please recreate it.");
return;
}
if (config.getProgramsRefreshPeriod() <= 0) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "The 'Programs Refresh Period' must be > 0. If you have an older thing please recreate it.");
return;
}
if (config.getStatusRefreshPeriod() <= 0) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "The 'Module Status Refresh Period' must be > 0. If you have an older thing please recreate it.");
return;
}
// Initialize automatic mode programs local cache
final ExpiringCache<List<Program>> localProgramCache = new ExpiringCache<>(Duration.ofHours(config.getProgramsRefreshPeriod()), this::programCacheAction);
this.programCache = localProgramCache;
// Initialize module local settings
final ModuleSettings localModuleSettings = new ModuleSettings(config.getPlantId(), config.getModuleId());
this.moduleSettings = localModuleSettings;
updateStatus(ThingStatus.UNKNOWN);
scheduleJob();
schedulePoll();
logger.debug("Module[{}] Finished initializing!", thing.getUID());
}
Aggregations