<commit_before>/**
 * Copyright (c) 2010-2015, openHAB.org and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.openhab.binding.netatmo.internal;

import static org.apache.commons.lang.StringUtils.isNotBlank;
import static org.openhab.binding.netatmo.internal.NetatmoGenericBindingProvider.*;

import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import org.openhab.binding.netatmo.NetatmoBindingProvider;
import org.openhab.binding.netatmo.internal.authentication.OAuthCredentials;
import org.openhab.binding.netatmo.internal.camera.NetatmoCameraBinding;
import org.openhab.binding.netatmo.internal.weather.NetatmoPressureUnit;
import org.openhab.binding.netatmo.internal.weather.NetatmoUnitSystem;
import org.openhab.binding.netatmo.internal.weather.NetatmoWeatherBinding;
import org.openhab.core.binding.AbstractActiveBinding;
import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.cm.ManagedService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Binding that gets measurements from the Netatmo API every couple of minutes.
 *
 * @author <NAME>
 * @author Thomas.Eichstaedt-Engelen
 * @author <NAME>
 * @author <NAME>
 * @author Ing. <NAME>
 * @since 1.4.0
 */
public class NetatmoBinding extends AbstractActiveBinding<NetatmoBindingProvider>implements ManagedService {

    private static final String DEFAULT_USER_ID = "DEFAULT_USER";

    private static final Logger logger = LoggerFactory.getLogger(NetatmoBinding.class);

    protected static final String CONFIG_CLIENT_ID = "clientid";
    protected static final String CONFIG_CLIENT_SECRET = "clientsecret";
    protected static final String CONFIG_REFRESH = "refresh";
    protected static final String CONFIG_REFRESH_TOKEN = "<PASSWORD>";
    protected static final String CONFIG_PRESSURE_UNIT = "pressureunit";
    protected static final String CONFIG_UNIT_SYSTEM = "unitsystem";

    /**
     * The refresh interval which is used to poll values from the Netatmo server
     * (optional, defaults to 300000ms)
     */
    private long refreshInterval = 300000;

    private static Map<String, OAuthCredentials> credentialsCache = new HashMap<String, OAuthCredentials>();

    private final NetatmoCameraBinding cameraBinding = new NetatmoCameraBinding();
    private final NetatmoWeatherBinding weatherBinding = new NetatmoWeatherBinding();

    /**
     * {@inheritDoc}
     */
    @Override
    protected String getName() {
        return "Netatmo Refresh Service";
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected long getRefreshInterval() {
        return this.refreshInterval;
    }

    /**
     * /**
     * {@inheritDoc}
     */
    @SuppressWarnings("incomplete-switch")
    @Override
    protected void execute() {
        logger.debug("Querying Netatmo API");
        for (String userid : credentialsCache.keySet()) {

            OAuthCredentials oauthCredentials = getOAuthCredentials(userid);
            if (oauthCredentials.noAccessToken()) {
                // initial run after a restart, so get an access token first
                oauthCredentials.refreshAccessToken();
            }

            // 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;
                    }
                }
            }

            // 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);
            }
        }
    }

    /**
     * Returns the cached {@link OAuthCredentials} for the given {@code userid}.
     * If their is no such cached {@link OAuthCredentials} element, the cache is
     * searched with the {@code DEFAULT_USER}. If there is still no cached
     * element found {@code NULL} is returned.
     *
     * @param userid
     *            the userid to find the {@link OAuthCredentials}
     * @return the cached {@link OAuthCredentials} or {@code NULL}
     */
    public static OAuthCredentials getOAuthCredentials(String userid) {
        if (credentialsCache.containsKey(userid)) {
            return credentialsCache.get(userid);
        } else {
            return credentialsCache.get(DEFAULT_USER_ID);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void updated(final Dictionary<String, ?> config) throws ConfigurationException {
        if (config != null) {

            final String refreshIntervalString = (String) config.get(CONFIG_REFRESH);
            if (isNotBlank(refreshIntervalString)) {
                this.refreshInterval = Long.parseLong(refreshIntervalString);
            }<commit_msg>netatmo<commit_after>/**
 * Copyright (c) 2010-2015, openHAB.org and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.openhab.binding.netatmo.internal;

import static org.apache.commons.lang.StringUtils.isNotBlank;
import static org.openhab.binding.netatmo.internal.NetatmoGenericBindingProvider.*;

import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import org.openhab.binding.netatmo.NetatmoBindingProvider;
import org.openhab.binding.netatmo.internal.authentication.OAuthCredentials;
import org.openhab.binding.netatmo.internal.camera.NetatmoCameraBinding;
import org.openhab.binding.netatmo.internal.weather.NetatmoPressureUnit;
import org.openhab.binding.netatmo.internal.weather.NetatmoUnitSystem;
import org.openhab.binding.netatmo.internal.weather.NetatmoWeatherBinding;
import org.openhab.core.binding.AbstractActiveBinding;
import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.cm.ManagedService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Binding that gets measurements from the Netatmo API every couple of minutes.
 *
 * @author <NAME>
 * @author Thomas.Eichstaedt-Engelen
 * @author <NAME>
 * @author <NAME>
 * @author Ing. <NAME>
 * @since 1.4.0
 */
public class NetatmoBinding extends AbstractActiveBinding<NetatmoBindingProvider>implements ManagedService {

    private static final String DEFAULT_USER_ID = "DEFAULT_USER";

    private static final Logger logger = LoggerFactory.getLogger(NetatmoBinding.class);

    protected static final String CONFIG_CLIENT_ID = "clientid";
    protected static final String CONFIG_CLIENT_SECRET = "clientsecret";
    protected static final String CONFIG_REFRESH = "refresh";
    protected static final String CONFIG_REFRESH_TOKEN = "refreshtoken";
    protected static final String CONFIG_PRESSURE_UNIT = "pressureunit";
    protected static final String CONFIG_UNIT_SYSTEM = "unitsystem";

    /**
     * The refresh interval which is used to poll values from the Netatmo server
     * (optional, defaults to 300000ms)
     */
    private long refreshInterval = 300000;

    private static Map<String, OAuthCredentials> credentialsCache = new HashMap<String, OAuthCredentials>();

    private final NetatmoCameraBinding cameraBinding = new NetatmoCameraBinding();
    private final NetatmoWeatherBinding weatherBinding = new NetatmoWeatherBinding();

    /**
     * {@inheritDoc}
     */
    @Override
    protected String getName() {
        return "Netatmo Refresh Service";
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected long getRefreshInterval() {
        return this.refreshInterval;
    }

    /**
     * /**
     * {@inheritDoc}
     */
    @SuppressWarnings("incomplete-switch")
    @Override
    protected void execute() {
        logger.debug("Querying Netatmo API");
        for (String userid : credentialsCache.keySet()) {

            OAuthCredentials oauthCredentials = getOAuthCredentials(userid);
            if (oauthCredentials.noAccessToken()) {
                // initial run after a restart, so get an access token first
                oauthCredentials.refreshAccessToken();
            }

            // 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;
                    }
                }
            }

            // 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);
            }
        }
    }

    /**
     * Returns the cached {@link OAuthCredentials} for the given {@code userid}.
     * If their is no such cached {@link OAuthCredentials} element, the cache is
     * searched with the {@code DEFAULT_USER}. If there is still no cached
     * element found {@code NULL} is returned.
     *
     * @param userid
     *            the userid to find the {@link OAuthCredentials}
     * @return the cached {@link OAuthCredentials} or {@code NULL}
     */
    public static OAuthCredentials getOAuthCredentials(String userid) {
        if (credentialsCache.containsKey(userid)) {
            return credentialsCache.get(userid);
        } else {
            return credentialsCache.get(DEFAULT_USER_ID);
        }
    }

    protected void addBindingProvider(NetatmoBindingProvider bindingProvider) {
        super.addBindingProvider(bindingProvider);
    }

    protected void removeBindingProvider(NetatmoBindingProvider bindingProvider) {
        super.removeBindingProvider(bindingProvider);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void updated(final Dictionary<String, ?> config) throws ConfigurationException {
        if (config != null) {

            final String refreshIntervalString = (String) config.get(CONFIG_REFRESH);
            if (isNotBlank(refreshIntervalString)) {
                this.refreshInterval = Long.parseLong(refreshIntervalString);
            }
{'fraction_non_alphanumeric': 0.08710680954026961, 'fraction_numerical': 0.005012098167991704, 'mean_word_length': 2.756247971437845, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3529549', 'n_tokens_mistral': 3123, 'n_tokens_neox': 3043, 'n_words': 792}