Search in sources :

Example 1 with NetatmoException

use of org.openhab.binding.netatmo.internal.NetatmoException 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 2 with NetatmoException

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

the class NetatmoWeatherBinding method processGetStationsData.

private GetStationsDataResponse processGetStationsData(OAuthCredentials oauthCredentials, Collection<NetatmoBindingProvider> providers, EventPublisher eventPublisher) {
    GetStationsDataRequest stationsDataRequest = new GetStationsDataRequest(oauthCredentials.getAccessToken());
    logger.debug("Request: {}", stationsDataRequest);
    GetStationsDataResponse stationsDataResponse = stationsDataRequest.execute();
    logger.debug("Response: {}", stationsDataResponse);
    if (stationsDataResponse.isError()) {
        final NetatmoError error = stationsDataResponse.getError();
        if (error.isAccessTokenExpired() || error.isTokenNotVaid()) {
            logger.debug("Token is expired or is not valid, refreshing: code = {} message = {}", error.getCode(), error.getMessage());
            oauthCredentials.refreshAccessToken();
            execute(oauthCredentials, providers, eventPublisher);
        } else {
            logger.error("Error processing device list: code = {} message = {}", error.getCode(), error.getMessage());
            throw new NetatmoException(error.getMessage());
        }
        // abort processing
        return null;
    } else {
        processGetStationsDataResponse(stationsDataResponse, providers);
    }
    return stationsDataResponse;
}
Also used : NetatmoError(org.openhab.binding.netatmo.internal.messages.NetatmoError) NetatmoException(org.openhab.binding.netatmo.internal.NetatmoException)

Example 3 with NetatmoException

use of org.openhab.binding.netatmo.internal.NetatmoException 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 4 with NetatmoException

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

the class NetatmoCameraBinding method processGetHomeData.

/**
     * Camera Home Implementation
     *
     * @param oauthCredentials
     * @param providers
     */
private void processGetHomeData(OAuthCredentials oauthCredentials, Collection<NetatmoBindingProvider> providers, EventPublisher eventPublisher) {
    GetHomeDataRequest homeDataRequest = new GetHomeDataRequest(oauthCredentials.getAccessToken());
    logger.debug("Request: {}", homeDataRequest);
    GetHomeDataResponse homeDataResponse = homeDataRequest.execute();
    logger.debug("Response: {}", homeDataResponse);
    if (homeDataResponse.isError()) {
        final NetatmoError error = homeDataResponse.getError();
        if (error.isAccessTokenExpired() || error.isTokenNotVaid()) {
            logger.debug("Token is expired or is not valid, refreshing: code = {} message = {}", error.getCode(), error.getMessage());
            oauthCredentials.refreshAccessToken();
            execute(oauthCredentials, providers, eventPublisher);
        } else {
            logger.error("Error processing home list: code = {} message = {}", error.getCode(), error.getMessage());
            throw new NetatmoException(error.getMessage());
        }
        // abort processing
        return;
    } else {
        processGetHomeDataResponse(homeDataResponse, providers);
    }
}
Also used : NetatmoError(org.openhab.binding.netatmo.internal.messages.NetatmoError) NetatmoException(org.openhab.binding.netatmo.internal.NetatmoException)

Example 5 with NetatmoException

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

the class NetatmoWeatherBinding method processMeasurements.

private DeviceMeasureValueMap processMeasurements(OAuthCredentials oauthCredentials, Collection<NetatmoBindingProvider> providers, EventPublisher eventPublisher) {
    DeviceMeasureValueMap deviceMeasureValueMap = new DeviceMeasureValueMap();
    for (final MeasurementRequest request : createMeasurementRequests(providers)) {
        final MeasurementResponse response = request.execute();
        logger.debug("Request: {}", request);
        logger.debug("Response: {}", response);
        if (response.isError()) {
            final NetatmoError error = response.getError();
            if (error.isAccessTokenExpired() || error.isTokenNotVaid()) {
                logger.debug("Token is expired or is not valid, refreshing: code = {} message = {}", error.getCode(), error.getMessage());
                oauthCredentials.refreshAccessToken();
                execute(oauthCredentials, providers, eventPublisher);
                return null;
            } else {
                logger.error("Error sending measurement request: code = {} message = {}", error.getCode(), error.getMessage());
                throw new NetatmoException(error.getMessage());
            }
        } else {
            processMeasurementResponse(request, response, deviceMeasureValueMap);
        }
    }
    return deviceMeasureValueMap;
}
Also used : NetatmoError(org.openhab.binding.netatmo.internal.messages.NetatmoError) NetatmoException(org.openhab.binding.netatmo.internal.NetatmoException)

Aggregations

NetatmoException (org.openhab.binding.netatmo.internal.NetatmoException)5 NetatmoError (org.openhab.binding.netatmo.internal.messages.NetatmoError)3 Calendar (java.util.Calendar)2 NetatmoBindingProvider (org.openhab.binding.netatmo.NetatmoBindingProvider)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 Camera (org.openhab.binding.netatmo.internal.camera.GetHomeDataResponse.Camera)1 Home (org.openhab.binding.netatmo.internal.camera.GetHomeDataResponse.Home)1 Person (org.openhab.binding.netatmo.internal.camera.GetHomeDataResponse.Person)1 Device (org.openhab.binding.netatmo.internal.weather.GetStationsDataResponse.Device)1 Module (org.openhab.binding.netatmo.internal.weather.GetStationsDataResponse.Module)1 PointType (org.openhab.core.library.types.PointType)1