Search in sources :

Example 1 with OAuthCredentials

use of org.openhab.binding.netatmo.internal.authentication.OAuthCredentials in project openhab1-addons by openhab.

the class NetatmoBinding method execute.

/**
     * /**
     * {@inheritDoc}
     */
@SuppressWarnings("incomplete-switch")
@Override
protected void execute() {
    if (pollTimeExpired()) {
        logger.debug("Querying Netatmo API");
        for (String userid : credentialsCache.keySet()) {
            // Check if weather and/or camera items are configured
            boolean bWeather = false;
            boolean bCamera = false;
            for (final NetatmoBindingProvider provider : providers) {
                for (final String itemName : provider.getItemNames()) {
                    final String sItemType = provider.getItemType(itemName);
                    if (NETATMO_WEATHER.equals(sItemType)) {
                        bWeather = true;
                    } else if (NETATMO_CAMERA.equals(sItemType)) {
                        bCamera = true;
                    }
                    if (bWeather && bCamera) {
                        break;
                    }
                }
            }
            if (!bWeather && !bCamera) {
                logger.debug("There are not any items configured for this binding!");
                continue;
            }
            OAuthCredentials oauthCredentials = getOAuthCredentials(userid);
            oauthCredentials.setWeather(bWeather);
            oauthCredentials.setCamera(bCamera);
            if (oauthCredentials.noAccessToken()) {
                // initial run after a restart, so get an access token first
                oauthCredentials.refreshAccessToken();
            }
            // Start the execution of the weather station and/or camera binding
            if (bWeather) {
                weatherBinding.execute(oauthCredentials, this.providers, this.eventPublisher);
            }
            if (bCamera) {
                cameraBinding.execute(oauthCredentials, this.providers, this.eventPublisher);
            }
            // schedule the next poll at the standard refresh interval
            schedulePoll(this.refreshInterval);
        }
    }
}
Also used : NetatmoBindingProvider(org.openhab.binding.netatmo.NetatmoBindingProvider) OAuthCredentials(org.openhab.binding.netatmo.internal.authentication.OAuthCredentials)

Example 2 with OAuthCredentials

use of org.openhab.binding.netatmo.internal.authentication.OAuthCredentials in project openhab1-addons by openhab.

the class NetatmoBinding method updated.

/**
     * {@inheritDoc}
     */
@Override
public void updated(final Dictionary<String, ?> config) throws ConfigurationException {
    if (config != null) {
        final String granularityString = (String) config.get(CONFIG_GRANULARITY);
        if (isNotBlank(granularityString)) {
            this.granularity = Long.parseLong(granularityString);
        }
        final String refreshIntervalString = (String) config.get(CONFIG_REFRESH);
        if (isNotBlank(refreshIntervalString)) {
            this.refreshInterval = Long.parseLong(refreshIntervalString);
        }
        Enumeration<String> configKeys = config.keys();
        while (configKeys.hasMoreElements()) {
            String configKey = configKeys.nextElement();
            // don't want to process here ...
            if (CONFIG_GRANULARITY.equals(configKey) || CONFIG_REFRESH.equals(configKey) || "service.pid".equals(configKey)) {
                continue;
            }
            String userid;
            String configKeyTail;
            if (configKey.contains(".")) {
                String[] keyElements = configKey.split("\\.");
                userid = keyElements[0];
                configKeyTail = keyElements[1];
            } else {
                userid = DEFAULT_USER_ID;
                configKeyTail = configKey;
            }
            OAuthCredentials credentials = credentialsCache.get(userid);
            if (credentials == null) {
                credentials = new OAuthCredentials();
                credentialsCache.put(userid, credentials);
            }
            String value = (String) config.get(configKeyTail);
            if (CONFIG_CLIENT_ID.equals(configKeyTail)) {
                credentials.setClientId(value);
            } else if (CONFIG_CLIENT_SECRET.equals(configKeyTail)) {
                credentials.setClientSecret(value);
            } else if (CONFIG_REFRESH_TOKEN.equals(configKeyTail)) {
                credentials.setRefreshToken(value);
            } else if (CONFIG_PRESSURE_UNIT.equals(configKeyTail)) {
                try {
                    weatherBinding.setPressureUnit(NetatmoPressureUnit.fromString(value));
                } catch (IllegalArgumentException e) {
                    throw new ConfigurationException(configKey, "the value '" + value + "' is not valid for the configKey '" + configKey + "'");
                }
            } else if (CONFIG_UNIT_SYSTEM.equals(configKeyTail)) {
                try {
                    weatherBinding.setUnitSystem(NetatmoUnitSystem.fromString(value));
                } catch (IllegalArgumentException e) {
                    throw new ConfigurationException(configKey, "the value '" + value + "' is not valid for the configKey '" + configKey + "'");
                }
            } else {
                throw new ConfigurationException(configKey, "the given configKey '" + configKey + "' is unknown");
            }
        }
        setProperlyConfigured(true);
    }
}
Also used : ConfigurationException(org.osgi.service.cm.ConfigurationException) OAuthCredentials(org.openhab.binding.netatmo.internal.authentication.OAuthCredentials)

Example 3 with OAuthCredentials

use of org.openhab.binding.netatmo.internal.authentication.OAuthCredentials in project openhab1-addons by openhab.

the class NetatmoWeatherBinding method addMeasurement.

private void addMeasurement(final Map<String, MeasurementRequest> requests, final NetatmoBindingProvider provider, final String itemName, final NetatmoMeasureType measureType, final NetatmoScale scale) {
    final String userid = provider.getUserid(itemName);
    final OAuthCredentials oauthCredentials = NetatmoBinding.getOAuthCredentials(userid);
    if (oauthCredentials != null) {
        final String deviceId = provider.getDeviceId(itemName);
        final String moduleId = provider.getModuleId(itemName);
        final String requestKey = createKey(deviceId, moduleId, scale);
        if (!requests.containsKey(requestKey)) {
            requests.put(requestKey, new MeasurementRequest(oauthCredentials.getAccessToken(), deviceId, moduleId, scale));
        }
        requests.get(requestKey).addMeasure(measureType);
    }
}
Also used : OAuthCredentials(org.openhab.binding.netatmo.internal.authentication.OAuthCredentials)

Aggregations

OAuthCredentials (org.openhab.binding.netatmo.internal.authentication.OAuthCredentials)3 NetatmoBindingProvider (org.openhab.binding.netatmo.NetatmoBindingProvider)1 ConfigurationException (org.osgi.service.cm.ConfigurationException)1