Search in sources :

Example 1 with NetatmoBindingProvider

use of org.openhab.binding.netatmo.NetatmoBindingProvider 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 NetatmoBindingProvider

use of org.openhab.binding.netatmo.NetatmoBindingProvider in project openhab1-addons by openhab.

the class NetatmoWeatherBinding method execute.

/**
     * Execute the weather binding from Netatmo Binding Class
     *
     * @param oauthCredentials
     * @param providers
     * @param eventPublisher
     */
public void execute(OAuthCredentials oauthCredentials, Collection<NetatmoBindingProvider> providers, EventPublisher eventPublisher) {
    logger.debug("Querying Netatmo Weather API");
    try {
        GetStationsDataResponse stationsDataResponse = processGetStationsData(oauthCredentials, providers, eventPublisher);
        if (stationsDataResponse == null) {
            return;
        }
        DeviceMeasureValueMap deviceMeasureValueMap = processMeasurements(oauthCredentials, providers, eventPublisher);
        if (deviceMeasureValueMap == null) {
            return;
        }
        for (final NetatmoBindingProvider provider : providers) {
            for (final String itemName : provider.getItemNames()) {
                final String deviceId = provider.getDeviceId(itemName);
                final String moduleId = provider.getModuleId(itemName);
                final NetatmoMeasureType measureType = provider.getMeasureType(itemName);
                final NetatmoScale scale = provider.getNetatmoScale(itemName);
                State state = null;
                if (measureType != null) {
                    switch(measureType) {
                        case MODULENAME:
                            if (moduleId == null) {
                                for (Device device : stationsDataResponse.getDevices()) {
                                    if (device.getId().equals(deviceId)) {
                                        state = new StringType(device.getModuleName());
                                        break;
                                    }
                                }
                            } else {
                                for (Device device : stationsDataResponse.getDevices()) {
                                    for (Module module : device.getModules()) {
                                        if (module.getId().equals(moduleId)) {
                                            state = new StringType(module.getModuleName());
                                            break;
                                        }
                                    }
                                }
                            }
                            break;
                        case TIMESTAMP:
                            state = deviceMeasureValueMap.timeStamp;
                            break;
                        case TEMPERATURE:
                        case CO2:
                        case HUMIDITY:
                        case NOISE:
                        case PRESSURE:
                        case RAIN:
                        case MIN_TEMP:
                        case MAX_TEMP:
                        case MIN_HUM:
                        case MAX_HUM:
                        case MIN_PRESSURE:
                        case MAX_PRESSURE:
                        case MIN_NOISE:
                        case MAX_NOISE:
                        case MIN_CO2:
                        case MAX_CO2:
                        case SUM_RAIN:
                        case WINDSTRENGTH:
                        case WINDANGLE:
                        case GUSTSTRENGTH:
                        case GUSTANGLE:
                            {
                                BigDecimal value = getValue(deviceMeasureValueMap, measureType, createKey(deviceId, moduleId, scale));
                                // numeric value is awaited (issue #1848)
                                if (value != null) {
                                    if (NetatmoMeasureType.isTemperature(measureType)) {
                                        value = unitSystem.convertTemp(value);
                                    } else if (NetatmoMeasureType.isRain(measureType)) {
                                        value = unitSystem.convertRain(value);
                                    } else if (NetatmoMeasureType.isPressure(measureType)) {
                                        value = pressureUnit.convertPressure(value);
                                    } else if (NetatmoMeasureType.isWind(measureType)) {
                                        value = unitSystem.convertWind(value);
                                    }
                                    state = new DecimalType(value);
                                }
                            }
                            break;
                        case DATE_MIN_TEMP:
                        case DATE_MAX_TEMP:
                        case DATE_MIN_HUM:
                        case DATE_MAX_HUM:
                        case DATE_MIN_PRESSURE:
                        case DATE_MAX_PRESSURE:
                        case DATE_MIN_NOISE:
                        case DATE_MAX_NOISE:
                        case DATE_MIN_CO2:
                        case DATE_MAX_CO2:
                        case DATE_MAX_GUST:
                            {
                                final BigDecimal value = getValue(deviceMeasureValueMap, measureType, createKey(deviceId, moduleId, scale));
                                if (value != null) {
                                    final Calendar calendar = Calendar.getInstance();
                                    calendar.setTimeInMillis(value.longValue() * 1000);
                                    state = new DateTimeType(calendar);
                                }
                            }
                            break;
                        case BATTERYPERCENT:
                        case BATTERYSTATUS:
                        case BATTERYVP:
                        case RFSTATUS:
                            for (Device device : stationsDataResponse.getDevices()) {
                                for (Module module : device.getModules()) {
                                    if (module.getId().equals(moduleId)) {
                                        switch(measureType) {
                                            case BATTERYPERCENT:
                                            case BATTERYVP:
                                                state = new DecimalType(module.getBatteryPercentage());
                                                break;
                                            case BATTERYSTATUS:
                                                state = new DecimalType(module.getBatteryLevel());
                                                break;
                                            case RFSTATUS:
                                                state = new DecimalType(module.getRfLevel());
                                                break;
                                            case MODULENAME:
                                                state = new StringType(module.getModuleName());
                                                break;
                                            default:
                                                break;
                                        }
                                    }
                                }
                            }
                            break;
                        case ALTITUDE:
                        case LATITUDE:
                        case LONGITUDE:
                        case WIFISTATUS:
                        case COORDINATE:
                        case STATIONNAME:
                            for (Device device : stationsDataResponse.getDevices()) {
                                if (device.getId().equals(deviceId)) {
                                    if (stationPositions.get(device) == null) {
                                        DecimalType altitude = DecimalType.ZERO;
                                        if (device.getAltitude() != null) {
                                            altitude = new DecimalType(device.getAltitude());
                                        }
                                        stationPositions.put(device, new PointType(new DecimalType(new BigDecimal(device.getLatitude()).setScale(6, BigDecimal.ROUND_HALF_UP)), new DecimalType(new BigDecimal(device.getLongitude()).setScale(6, BigDecimal.ROUND_HALF_UP)), altitude));
                                    }
                                    switch(measureType) {
                                        case LATITUDE:
                                            state = stationPositions.get(device).getLatitude();
                                            break;
                                        case LONGITUDE:
                                            state = stationPositions.get(device).getLongitude();
                                            break;
                                        case ALTITUDE:
                                            state = new DecimalType(Math.round(unitSystem.convertAltitude(stationPositions.get(device).getAltitude().doubleValue())));
                                            break;
                                        case WIFISTATUS:
                                            state = new DecimalType(device.getWifiLevel());
                                            break;
                                        case COORDINATE:
                                            state = stationPositions.get(device);
                                            break;
                                        case STATIONNAME:
                                            state = new StringType(device.getStationName());
                                            break;
                                        default:
                                            break;
                                    }
                                }
                            }
                            break;
                    }
                }
                if (state != null) {
                    eventPublisher.postUpdate(itemName, state);
                }
            }
        }
    } catch (NetatmoException ne) {
        logger.error(ne.getMessage());
    }
}
Also used : NetatmoBindingProvider(org.openhab.binding.netatmo.NetatmoBindingProvider) StringType(org.openhab.core.library.types.StringType) Device(org.openhab.binding.netatmo.internal.weather.GetStationsDataResponse.Device) Calendar(java.util.Calendar) BigDecimal(java.math.BigDecimal) DateTimeType(org.openhab.core.library.types.DateTimeType) State(org.openhab.core.types.State) DecimalType(org.openhab.core.library.types.DecimalType) PointType(org.openhab.core.library.types.PointType) Module(org.openhab.binding.netatmo.internal.weather.GetStationsDataResponse.Module) NetatmoException(org.openhab.binding.netatmo.internal.NetatmoException)

Example 3 with NetatmoBindingProvider

use of org.openhab.binding.netatmo.NetatmoBindingProvider in project openhab1-addons by openhab.

the class NetatmoWeatherBinding method processGetStationsDataResponse.

/**
     * Processes an incoming {@link GetStationsDataResponse}.
     * <p>
     */
private void processGetStationsDataResponse(final GetStationsDataResponse response, Collection<NetatmoBindingProvider> providers) {
    // Prepare a map of all known device measurements
    final Map<String, Device> deviceMap = new HashMap<String, Device>();
    final Map<String, Set<String>> deviceMeasurements = new HashMap<String, Set<String>>();
    for (final Device device : response.getDevices()) {
        final String deviceId = device.getId();
        deviceMap.put(deviceId, device);
        for (final String measurement : device.getMeasurements()) {
            if (!deviceMeasurements.containsKey(deviceId)) {
                deviceMeasurements.put(deviceId, new HashSet<String>());
            }
            deviceMeasurements.get(deviceId).add(measurement);
        }
    }
    // Prepare a map of all known module measurements
    final Map<String, Module> moduleMap = new HashMap<String, Module>();
    final Map<String, Set<String>> moduleMeasurements = new HashMap<String, Set<String>>();
    final Map<String, String> mainDeviceMap = new HashMap<String, String>();
    for (final Device device : response.getDevices()) {
        final String deviceId = device.getId();
        for (final Module module : device.getModules()) {
            final String moduleId = module.getId();
            moduleMap.put(moduleId, module);
            for (final String measurement : module.getMeasurements()) {
                if (!moduleMeasurements.containsKey(moduleId)) {
                    moduleMeasurements.put(moduleId, new HashSet<String>());
                    mainDeviceMap.put(moduleId, deviceId);
                }
                moduleMeasurements.get(moduleId).add(measurement);
            }
        }
    }
    // Remove all configured items from the maps
    for (final NetatmoBindingProvider provider : providers) {
        for (final String itemName : provider.getItemNames()) {
            final String deviceId = provider.getDeviceId(itemName);
            final String moduleId = provider.getModuleId(itemName);
            final NetatmoMeasureType measureType = provider.getMeasureType(itemName);
            final Set<String> measurements;
            if (moduleId != null) {
                measurements = moduleMeasurements.get(moduleId);
            } else {
                measurements = deviceMeasurements.get(deviceId);
            }
            if (measurements != null) {
                String measure = measureType != NetatmoMeasureType.WINDSTRENGTH ? measureType.getMeasure() : WIND;
                measurements.remove(measure);
            }
        }
    }
    // Log all unconfigured measurements
    final StringBuilder message = new StringBuilder();
    for (Entry<String, Set<String>> entry : deviceMeasurements.entrySet()) {
        final String deviceId = entry.getKey();
        final Device device = deviceMap.get(deviceId);
        for (String measurement : entry.getValue()) {
            message.append("\t" + deviceId + "#" + measurement + " (" + device.getModuleName() + ")\n");
        }
    }
    for (Entry<String, Set<String>> entry : moduleMeasurements.entrySet()) {
        final String moduleId = entry.getKey();
        final Module module = moduleMap.get(moduleId);
        for (String measurement : entry.getValue()) {
            if (measurement.equals(WIND)) {
                measurement = NetatmoMeasureType.WINDSTRENGTH.toString().toLowerCase();
            }
            message.append("\t" + mainDeviceMap.get(moduleId) + "#" + moduleId + "#" + measurement + " (" + module.getModuleName() + ")\n");
        }
    }
    if (message.length() > 0) {
        message.insert(0, "The following Netatmo measurements are not yet configured:\n");
        logger.info(message.toString());
    }
}
Also used : NetatmoBindingProvider(org.openhab.binding.netatmo.NetatmoBindingProvider) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Device(org.openhab.binding.netatmo.internal.weather.GetStationsDataResponse.Device) Module(org.openhab.binding.netatmo.internal.weather.GetStationsDataResponse.Module)

Example 4 with NetatmoBindingProvider

use of org.openhab.binding.netatmo.NetatmoBindingProvider in project openhab1-addons by openhab.

the class NetatmoCameraBinding method execute.

/**
     * Execute the weather binding from Netatmo Binding Class
     *
     * @param oauthCredentials
     * @param providers
     * @param eventPublisher
     */
public void execute(OAuthCredentials oauthCredentials, Collection<NetatmoBindingProvider> providers, EventPublisher eventPublisher) {
    logger.debug("Querying Netatmo camera API");
    try {
        oauthCredentials.refreshAccessToken();
        configuredHomeKnownPersons.clear();
        configuredHomeUnKnownPersons.clear();
        configuredHomeMap.clear();
        configuredhomeCameras.clear();
        processGetHomeData(oauthCredentials, providers, eventPublisher);
        for (final NetatmoBindingProvider provider : providers) {
            for (final String itemName : provider.getItemNames()) {
                final String homeId = provider.getHomeId(itemName);
                final NetatmoCameraAttributes attribute = provider.getAttribute(itemName);
                State state = null;
                if (homeId != null) {
                    final String personId = provider.getPersonId(itemName);
                    final String cameraId = provider.getCameraId(itemName);
                    if (personId != null) {
                        if ("UNKNOWN".equals(personId)) {
                            final StringBuilder message = new StringBuilder();
                            int i = 0;
                            switch(attribute) {
                                case HOME_UNKNWOWN_HOME_COUNT:
                                    if (configuredHomeUnKnownPersons.containsKey(homeId)) {
                                        for (Entry<String, Person> entry : configuredHomeUnKnownPersons.get(homeId).entrySet()) {
                                            final Person person = entry.getValue();
                                            if (!person.getOut_of_sight()) {
                                                i++;
                                            }
                                        }
                                    }
                                    state = new DecimalType(i);
                                    break;
                                case HOME_UNKNWOWN_AWAY_COUNT:
                                    if (configuredHomeUnKnownPersons.containsKey(homeId)) {
                                        for (Entry<String, Person> entry : configuredHomeUnKnownPersons.get(homeId).entrySet()) {
                                            final Person person = entry.getValue();
                                            if (person.getOut_of_sight()) {
                                                i++;
                                            }
                                        }
                                    }
                                    state = new DecimalType(i);
                                    break;
                                case HOME_UNKNWOWN_OUTOFSIGHT_LIST:
                                    if (configuredHomeUnKnownPersons.containsKey(homeId)) {
                                        for (Entry<String, Person> entry : configuredHomeUnKnownPersons.get(homeId).entrySet()) {
                                            final Person person = entry.getValue();
                                            if (message.length() > 1) {
                                                message.append("#");
                                            }
                                            message.append(person.getOut_of_sight());
                                        }
                                    }
                                    state = new StringType(message.toString());
                                    break;
                                case HOME_UNKNWOWN_LASTSEEN_LIST:
                                    if (configuredHomeUnKnownPersons.containsKey(homeId)) {
                                        for (Entry<String, Person> entry : configuredHomeUnKnownPersons.get(homeId).entrySet()) {
                                            final Person person = entry.getValue();
                                            if (message.length() > 1) {
                                                message.append("#");
                                            }
                                            final Calendar calendar = Calendar.getInstance();
                                            calendar.setTimeInMillis(person.getLastSeen().getTime() * 1000);
                                            message.append(calendar);
                                        }
                                    }
                                    state = new StringType(message.toString());
                                    break;
                                case HOME_UNKNWOWN_FACE_ID_LIST:
                                    if (configuredHomeUnKnownPersons.containsKey(homeId)) {
                                        for (Entry<String, Person> entry : configuredHomeUnKnownPersons.get(homeId).entrySet()) {
                                            final Person person = entry.getValue();
                                            if (message.length() > 1) {
                                                message.append("#");
                                            }
                                            message.append(person.getFace().getId());
                                        }
                                    }
                                    state = new StringType(message.toString());
                                    break;
                                case HOME_UNKNWOWN_FACE_KEY_LIST:
                                    if (configuredHomeUnKnownPersons.containsKey(homeId)) {
                                        for (Entry<String, Person> entry : configuredHomeUnKnownPersons.get(homeId).entrySet()) {
                                            final Person person = entry.getValue();
                                            if (message.length() > 1) {
                                                message.append("#");
                                            }
                                            message.append(person.getFace().getKey());
                                        }
                                    }
                                    state = new StringType(message.toString());
                                    break;
                                default:
                                    break;
                            }
                        } else {
                            Person person = null;
                            if (configuredHomeKnownPersons.containsKey(homeId)) {
                                person = configuredHomeKnownPersons.get(homeId).get(personId);
                            }
                            if (person != null) {
                                switch(attribute) {
                                    case HOME_PERSON_OUTOFSIGHT:
                                        if (person.getOut_of_sight()) {
                                            state = OnOffType.OFF;
                                        } else {
                                            state = OnOffType.ON;
                                        }
                                        break;
                                    case HOME_PERSON_PSEUDO:
                                        state = new StringType(person.getPseudo());
                                        break;
                                    case HOME_PERSON_LASTSEEN:
                                        final Calendar calendar = Calendar.getInstance();
                                        calendar.setTimeInMillis(person.getLastSeen().getTime() * 1000);
                                        state = new DateTimeType(calendar);
                                        break;
                                    case HOME_PERSON_FACE_ID:
                                        state = new StringType(person.getFace().getId());
                                        break;
                                    case HOME_PERSON_FACE_KEY:
                                        state = new StringType(person.getFace().getKey());
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }
                    } else if (cameraId != null) {
                        Camera camera = null;
                        if (configuredhomeCameras.containsKey(homeId)) {
                            camera = configuredhomeCameras.get(homeId).get(cameraId);
                        }
                        if (camera != null) {
                            switch(attribute) {
                                case HOME_CAMERA_NAME:
                                    state = new StringType(camera.getName());
                                    break;
                                case HOME_CAMERA_STATUS:
                                    state = new StringType(camera.getStatus());
                                    break;
                                case HOME_CAMERA_SD_STATUS:
                                    state = new StringType(camera.getSd_status());
                                    break;
                                case HOME_CAMERA_ALIM_STATUS:
                                    state = new StringType(camera.getAlim_status());
                                    break;
                                default:
                                    break;
                            }
                        }
                    } else {
                        Home home = configuredHomeMap.get(homeId);
                        if (home != null) {
                            switch(attribute) {
                                case HOME_NAME:
                                    state = new StringType(home.getName());
                                    break;
                                case HOME_PLACE_COUNTRY:
                                    state = new StringType(home.getPlace().getCountry());
                                    break;
                                case HOME_PLACE_TIMEZONE:
                                    state = new StringType(home.getPlace().getTimezone());
                                    break;
                                default:
                                    break;
                            }
                        }
                    }
                }
                if (state != null) {
                    eventPublisher.postUpdate(itemName, state);
                }
            }
        }
    } catch (NetatmoException ne) {
        logger.error(ne.getMessage());
    }
}
Also used : NetatmoBindingProvider(org.openhab.binding.netatmo.NetatmoBindingProvider) StringType(org.openhab.core.library.types.StringType) Calendar(java.util.Calendar) DateTimeType(org.openhab.core.library.types.DateTimeType) State(org.openhab.core.types.State) DecimalType(org.openhab.core.library.types.DecimalType) Camera(org.openhab.binding.netatmo.internal.camera.GetHomeDataResponse.Camera) Person(org.openhab.binding.netatmo.internal.camera.GetHomeDataResponse.Person) Home(org.openhab.binding.netatmo.internal.camera.GetHomeDataResponse.Home) NetatmoException(org.openhab.binding.netatmo.internal.NetatmoException)

Example 5 with NetatmoBindingProvider

use of org.openhab.binding.netatmo.NetatmoBindingProvider in project openhab1-addons by openhab.

the class NetatmoWeatherBinding method createMeasurementRequests.

/**
     * Creates the necessary requests to query the Netatmo API for all measures
     * that have a binding. One request can query all measures of a single
     * device or module.
     */
private Collection<MeasurementRequest> createMeasurementRequests(Collection<NetatmoBindingProvider> providers) {
    final Map<String, MeasurementRequest> requests = new HashMap<String, MeasurementRequest>();
    for (final NetatmoBindingProvider provider : providers) {
        for (final String itemName : provider.getItemNames()) {
            final NetatmoMeasureType measureType = provider.getMeasureType(itemName);
            if (measureType != null) {
                switch(measureType) {
                    case TEMPERATURE:
                    case CO2:
                    case HUMIDITY:
                    case NOISE:
                    case PRESSURE:
                    case RAIN:
                    case MIN_TEMP:
                    case MAX_TEMP:
                    case MIN_HUM:
                    case MAX_HUM:
                    case MIN_PRESSURE:
                    case MAX_PRESSURE:
                    case MIN_NOISE:
                    case MAX_NOISE:
                    case MIN_CO2:
                    case MAX_CO2:
                    case SUM_RAIN:
                    case DATE_MIN_TEMP:
                    case DATE_MAX_TEMP:
                    case DATE_MIN_HUM:
                    case DATE_MAX_HUM:
                    case DATE_MIN_PRESSURE:
                    case DATE_MAX_PRESSURE:
                    case DATE_MIN_NOISE:
                    case DATE_MAX_NOISE:
                    case DATE_MIN_CO2:
                    case DATE_MAX_CO2:
                    case WINDSTRENGTH:
                    case WINDANGLE:
                    case GUSTSTRENGTH:
                    case GUSTANGLE:
                    case DATE_MAX_GUST:
                        final NetatmoScale scale = provider.getNetatmoScale(itemName);
                        addMeasurement(requests, provider, itemName, measureType, scale);
                        break;
                    default:
                        break;
                }
            }
        }
    }
    return requests.values();
}
Also used : NetatmoBindingProvider(org.openhab.binding.netatmo.NetatmoBindingProvider) HashMap(java.util.HashMap)

Aggregations

NetatmoBindingProvider (org.openhab.binding.netatmo.NetatmoBindingProvider)6 HashMap (java.util.HashMap)3 Calendar (java.util.Calendar)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 NetatmoException (org.openhab.binding.netatmo.internal.NetatmoException)2 Camera (org.openhab.binding.netatmo.internal.camera.GetHomeDataResponse.Camera)2 Home (org.openhab.binding.netatmo.internal.camera.GetHomeDataResponse.Home)2 Person (org.openhab.binding.netatmo.internal.camera.GetHomeDataResponse.Person)2 Device (org.openhab.binding.netatmo.internal.weather.GetStationsDataResponse.Device)2 Module (org.openhab.binding.netatmo.internal.weather.GetStationsDataResponse.Module)2 DateTimeType (org.openhab.core.library.types.DateTimeType)2 DecimalType (org.openhab.core.library.types.DecimalType)2 StringType (org.openhab.core.library.types.StringType)2 State (org.openhab.core.types.State)2 BigDecimal (java.math.BigDecimal)1 Map (java.util.Map)1 OAuthCredentials (org.openhab.binding.netatmo.internal.authentication.OAuthCredentials)1 Event (org.openhab.binding.netatmo.internal.camera.GetHomeDataResponse.Event)1 PointType (org.openhab.core.library.types.PointType)1