Search in sources :

Example 26 with StringType

use of org.openhab.core.library.types.StringType 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 27 with StringType

use of org.openhab.core.library.types.StringType 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 28 with StringType

use of org.openhab.core.library.types.StringType in project openhab1-addons by openhab.

the class EpsonProjectorBinding method queryDataFromDevice.

private State queryDataFromDevice(String deviceId, EpsonProjectorCommandType commmandType, Class<? extends Item> itemType) {
    DeviceConfig device = deviceConfigCache.get(deviceId);
    if (device == null) {
        logger.error("Could not find device '{}'", deviceId);
        return null;
    }
    EpsonProjectorDevice remoteController = device.getConnection();
    if (remoteController == null) {
        logger.error("Could not find device '{}'", deviceId);
        return null;
    }
    try {
        if (remoteController.isConnected() == false) {
            remoteController.connect();
        }
        switch(commmandType) {
            case AKEYSTONE:
                int autoKeystone = remoteController.getAutoKeystone();
                return new DecimalType(autoKeystone);
            case ASPECT_RATIO:
                AspectRatio aspectRatio = remoteController.getAspectRatio();
                return new StringType(aspectRatio.toString());
            case BACKGROUND:
                Background background = remoteController.getBackground();
                return new StringType(background.toString());
            case BRIGHTNESS:
                int brightness = remoteController.getBrightness();
                return new DecimalType(brightness);
            case COLOR:
                Color color = remoteController.getColor();
                return new StringType(color.toString());
            case COLOR_MODE:
                ColorMode colorMode = remoteController.getColorMode();
                return new StringType(colorMode.toString());
            case COLOR_TEMP:
                int ctemp = remoteController.getColorTemperature();
                return new DecimalType(ctemp);
            case CONTRAST:
                int contrast = remoteController.getContrast();
                return new DecimalType(contrast);
            case DENSITY:
                int density = remoteController.getDensity();
                return new DecimalType(density);
            case DIRECT_SOURCE:
                int directSource = remoteController.getDirectSource();
                return new DecimalType(directSource);
            case ERR_CODE:
                int err = remoteController.getError();
                return new DecimalType(err);
            case ERR_MESSAGE:
                String errString = remoteController.getErrorString();
                return new StringType(errString);
            case FLESH_TEMP:
                int fleshColor = remoteController.getFleshColor();
                return new DecimalType(fleshColor);
            case GAIN_BLUE:
                int gainBlue = remoteController.getGainBlue();
                return new DecimalType(gainBlue);
            case GAIN_GREEN:
                int gainGreen = remoteController.getGainGreen();
                return new DecimalType(gainGreen);
            case GAIN_RED:
                int gainRed = remoteController.getGainRed();
                return new DecimalType(gainRed);
            case GAMMA:
                Gamma gamma = remoteController.getGamma();
                return new StringType(gamma.toString());
            case GAMMA_STEP:
                logger.warn("Get '{}' not implemented!", commmandType.toString());
                return null;
            case HKEYSTONE:
                int hKeystone = remoteController.getHorizontalKeystone();
                return new DecimalType(hKeystone);
            case HPOSITION:
                int hPosition = remoteController.getHorizontalPosition();
                return new DecimalType(hPosition);
            case HREVERSE:
                Switch hReverse = remoteController.getHorizontalReverse();
                return hReverse == Switch.ON ? OnOffType.ON : OnOffType.OFF;
            case KEY_CODE:
                break;
            case LAMP_TIME:
                int lampTime = remoteController.getLampTime();
                return new DecimalType(lampTime);
            case LUMINANCE:
                Luminance luminance = remoteController.getLuminance();
                return new StringType(luminance.toString());
            case MUTE:
                Switch mute = remoteController.getMute();
                return mute == Switch.ON ? OnOffType.ON : OnOffType.OFF;
            case OFFSET_BLUE:
                int offsetBlue = remoteController.getOffsetBlue();
                return new DecimalType(offsetBlue);
            case OFFSET_GREEN:
                int offsetGreen = remoteController.getOffsetGreen();
                return new DecimalType(offsetGreen);
            case OFFSET_RED:
                int offsetRed = remoteController.getOffsetRed();
                return new DecimalType(offsetRed);
            case POWER:
                PowerStatus powerStatus = remoteController.getPowerStatus();
                if (powerStatus == PowerStatus.ON) {
                    return OnOffType.ON;
                } else {
                    return OnOffType.OFF;
                }
            case POWER_STATE:
                PowerStatus powerStatus1 = remoteController.getPowerStatus();
                return new StringType(powerStatus1.toString());
            case SHARP:
                logger.warn("Get '{}' not implemented!", commmandType.toString());
                return null;
            case SOURCE:
                Source source = remoteController.getSource();
                return new StringType(source.toString());
            case SYNC:
                int sync = remoteController.getSync();
                return new DecimalType(sync);
            case TINT:
                int tint = remoteController.getTint();
                return new DecimalType(tint);
            case TRACKING:
                int tracking = remoteController.getTracking();
                return new DecimalType(tracking);
            case VKEYSTONE:
                int vKeystone = remoteController.getVerticalKeystone();
                return new DecimalType(vKeystone);
            case VPOSITION:
                int vPosition = remoteController.getVerticalPosition();
                return new DecimalType(vPosition);
            case VREVERSE:
                Switch vReverse = remoteController.getVerticalReverse();
                return vReverse == Switch.ON ? OnOffType.ON : OnOffType.OFF;
            default:
                logger.warn("Unknown '{}' command!", commmandType);
                return null;
        }
    } catch (EpsonProjectorException e) {
        logger.warn("Couldn't execute command '{}', {}", commmandType.toString(), e);
    } catch (Exception e) {
        logger.warn("Couldn't create state of type '{}'", itemType);
        return null;
    }
    return null;
}
Also used : Background(org.openhab.binding.epsonprojector.internal.EpsonProjectorDevice.Background) StringType(org.openhab.core.library.types.StringType) Color(org.openhab.binding.epsonprojector.internal.EpsonProjectorDevice.Color) ColorMode(org.openhab.binding.epsonprojector.internal.EpsonProjectorDevice.ColorMode) Source(org.openhab.binding.epsonprojector.internal.EpsonProjectorDevice.Source) ConfigurationException(org.osgi.service.cm.ConfigurationException) AspectRatio(org.openhab.binding.epsonprojector.internal.EpsonProjectorDevice.AspectRatio) Gamma(org.openhab.binding.epsonprojector.internal.EpsonProjectorDevice.Gamma) Luminance(org.openhab.binding.epsonprojector.internal.EpsonProjectorDevice.Luminance) Switch(org.openhab.binding.epsonprojector.internal.EpsonProjectorDevice.Switch) PowerStatus(org.openhab.binding.epsonprojector.internal.EpsonProjectorDevice.PowerStatus) DecimalType(org.openhab.core.library.types.DecimalType)

Example 29 with StringType

use of org.openhab.core.library.types.StringType in project openhab1-addons by openhab.

the class FritzboxTr064Binding method execute.

/**
     * @{inheritDoc}
     */
@Override
protected void execute() {
    logger.trace("FritzboxTr064 executing...");
    for (FritzboxTr064BindingProvider provider : providers) {
        for (String itemName : provider.getItemNames()) {
            // check each item relevant for this binding
            // extract itemconfig
            FritzboxTr064BindingConfig conf = provider.getBindingConfigByItemName(itemName);
            // for current item
            if (conf.getConfigString().startsWith("callmonitor")) {
                // check if we need to start call monitor
                if (_callMonitor == null) {
                    // not started yet
                    logger.debug("call monitor is not running. Configured items require call monitor -> Starting call monitor...");
                    if (_pbm == null) {
                        logger.debug("Downloading phonebooks");
                        _pbm = new PhonebookManager(_fboxComm);
                        _pbm.downloadPhonebooks();
                    }
                    _callMonitor = new CallMonitor(_url, eventPublisher, providers, _pbm);
                    _callMonitor.setupReconnectJob();
                    _callMonitor.startThread();
                }
                // make sure, no callmonitor items are processed by tr064
                continue;
            }
            // TR064 protocol usage
            // try to get value for this item
            String tr064result = _fboxComm.getTr064Value(conf.getConfigString());
            // config string from fbox
            if (tr064result == null) {
                // if value cannot be read
                tr064result = "ERR";
            }
            Class<? extends Item> itemType = conf.getItemType();
            if (itemType.isAssignableFrom(StringItem.class)) {
                eventPublisher.postUpdate(itemName, new StringType(tr064result));
            } else if (itemType.isAssignableFrom(ContactItem.class)) {
                State newState = tr064result.equals("1") ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
                eventPublisher.postUpdate(itemName, newState);
            } else if (itemType.isAssignableFrom(SwitchItem.class)) {
                State newState = tr064result.equals("1") ? OnOffType.ON : OnOffType.OFF;
                eventPublisher.postUpdate(itemName, newState);
            } else if (itemType.isAssignableFrom(NumberItem.class)) {
                // number items e.g. TAM messages
                // tr064 retrieves only Strings, trying to parse value returned
                int val = 0;
                try {
                    val = Integer.parseInt(tr064result);
                } catch (NumberFormatException ex) {
                    // indicate error as -1
                    val = -1;
                }
                State newState = new DecimalType(val);
                eventPublisher.postUpdate(itemName, newState);
            }
        }
    }
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) StringType(org.openhab.core.library.types.StringType) FritzboxTr064BindingProvider(org.openhab.binding.fritzboxtr064.FritzboxTr064BindingProvider) State(org.openhab.core.types.State) ContactItem(org.openhab.core.library.items.ContactItem) DecimalType(org.openhab.core.library.types.DecimalType) FritzboxTr064BindingConfig(org.openhab.binding.fritzboxtr064.internal.FritzboxTr064GenericBindingProvider.FritzboxTr064BindingConfig)

Example 30 with StringType

use of org.openhab.core.library.types.StringType in project openhab1-addons by openhab.

the class KNXCoreTypeMapper method toDPTValue.

/*
     * (non-Javadoc)
     *
     * @see org.openhab.binding.knx.config.KNXTypeMapper#toDPTValue(org.openhab.core.types.Type, java.lang.String)
     */
@Override
public String toDPTValue(Type type, String dptID) {
    DPT dpt;
    int mainNumber = getMainNumber(dptID);
    if (mainNumber == -1) {
        logger.error("toDPTValue couldn't identify mainnumber in dptID: {}", dptID);
        return null;
    }
    try {
        DPTXlator translator = TranslatorTypes.createTranslator(mainNumber, dptID);
        dpt = translator.getType();
    } catch (KNXException e) {
        e.printStackTrace();
        return null;
    }
    // check for HSBType first, because it extends PercentType as well
    if (type instanceof HSBType) {
        Color color = ((HSBType) type).toColor();
        return "r:" + Integer.toString(color.getRed()) + " g:" + Integer.toString(color.getGreen()) + " b:" + Integer.toString(color.getBlue());
    } else if (type instanceof OnOffType) {
        return type.equals(OnOffType.OFF) ? dpt.getLowerValue() : dpt.getUpperValue();
    } else if (type instanceof UpDownType) {
        return type.equals(UpDownType.UP) ? dpt.getLowerValue() : dpt.getUpperValue();
    } else if (type instanceof IncreaseDecreaseType) {
        DPT valueDPT = ((DPTXlator3BitControlled.DPT3BitControlled) dpt).getControlDPT();
        return type.equals(IncreaseDecreaseType.DECREASE) ? valueDPT.getLowerValue() + " 5" : valueDPT.getUpperValue() + " 5";
    } else if (type instanceof OpenClosedType) {
        return type.equals(OpenClosedType.CLOSED) ? dpt.getLowerValue() : dpt.getUpperValue();
    } else if (type instanceof StopMoveType) {
        return type.equals(StopMoveType.STOP) ? dpt.getLowerValue() : dpt.getUpperValue();
    } else if (type instanceof PercentType) {
        return type.toString();
    } else if (type instanceof DecimalType) {
        switch(mainNumber) {
            case 2:
                DPT valueDPT = ((DPTXlator1BitControlled.DPT1BitControlled) dpt).getValueDPT();
                switch(((DecimalType) type).intValue()) {
                    case 0:
                        return "0 " + valueDPT.getLowerValue();
                    case 1:
                        return "0 " + valueDPT.getUpperValue();
                    case 2:
                        return "1 " + valueDPT.getLowerValue();
                    default:
                        return "1 " + valueDPT.getUpperValue();
                }
            case 18:
                int intVal = ((DecimalType) type).intValue();
                if (intVal > 63) {
                    return "learn " + (intVal - 0x80);
                } else {
                    return "activate " + intVal;
                }
            default:
                return type.toString();
        }
    } else if (type instanceof StringType) {
        return type.toString();
    } else if (type instanceof DateTimeType) {
        return formatDateTime((DateTimeType) type, dptID);
    }
    logger.debug("toDPTValue: Couldn't get value for {} dpt id {} (no mapping).", type, dptID);
    return null;
}
Also used : KNXException(tuwien.auto.calimero.exception.KNXException) StringType(org.openhab.core.library.types.StringType) Color(java.awt.Color) DPT(tuwien.auto.calimero.dptxlator.DPT) UpDownType(org.openhab.core.library.types.UpDownType) PercentType(org.openhab.core.library.types.PercentType) Datapoint(tuwien.auto.calimero.datapoint.Datapoint) StopMoveType(org.openhab.core.library.types.StopMoveType) DateTimeType(org.openhab.core.library.types.DateTimeType) DPTXlator(tuwien.auto.calimero.dptxlator.DPTXlator) OnOffType(org.openhab.core.library.types.OnOffType) OpenClosedType(org.openhab.core.library.types.OpenClosedType) DPTXlator3BitControlled(tuwien.auto.calimero.dptxlator.DPTXlator3BitControlled) DecimalType(org.openhab.core.library.types.DecimalType) IncreaseDecreaseType(org.openhab.core.library.types.IncreaseDecreaseType) DPTXlator1BitControlled(tuwien.auto.calimero.dptxlator.DPTXlator1BitControlled) HSBType(org.openhab.core.library.types.HSBType)

Aggregations

StringType (org.openhab.core.library.types.StringType)90 DecimalType (org.openhab.core.library.types.DecimalType)69 State (org.openhab.core.types.State)30 DateTimeType (org.openhab.core.library.types.DateTimeType)28 PercentType (org.openhab.core.library.types.PercentType)27 NumberItem (org.openhab.core.library.items.NumberItem)25 Calendar (java.util.Calendar)23 StringItem (org.openhab.core.library.items.StringItem)18 OnOffType (org.openhab.core.library.types.OnOffType)15 SwitchItem (org.openhab.core.library.items.SwitchItem)12 ContactItem (org.openhab.core.library.items.ContactItem)10 DimmerItem (org.openhab.core.library.items.DimmerItem)10 RollershutterItem (org.openhab.core.library.items.RollershutterItem)10 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)8 DateTimeItem (org.openhab.core.library.items.DateTimeItem)8 HSBType (org.openhab.core.library.types.HSBType)8 ConfigurationException (org.osgi.service.cm.ConfigurationException)8 IOException (java.io.IOException)7 BigDecimal (java.math.BigDecimal)7