Search in sources :

Example 1 with PointType

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

the class JpaHistoricItem method fromPersistedItem.

/**
     * Converts the string value of the persisted item to the state of a HistoricItem.
     * 
     * @param pItem the persisted JpaPersistentItem
     * @param item the source reference Item
     * @return historic item
     */
public static HistoricItem fromPersistedItem(JpaPersistentItem pItem, Item item) {
    State state;
    if (item instanceof NumberItem) {
        state = new DecimalType(Double.valueOf(pItem.getValue()));
    } else if (item instanceof DimmerItem) {
        state = new PercentType(Integer.valueOf(pItem.getValue()));
    } else if (item instanceof SwitchItem) {
        state = OnOffType.valueOf(pItem.getValue());
    } else if (item instanceof ContactItem) {
        state = OpenClosedType.valueOf(pItem.getValue());
    } else if (item instanceof RollershutterItem) {
        state = PercentType.valueOf(pItem.getValue());
    } else if (item instanceof ColorItem) {
        state = new HSBType(pItem.getValue());
    } else if (item instanceof DateTimeItem) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(Long.valueOf(pItem.getValue())));
        state = new DateTimeType(cal);
    } else if (item instanceof LocationItem) {
        PointType pType = null;
        String[] comps = pItem.getValue().split(";");
        if (comps.length >= 2) {
            pType = new PointType(new DecimalType(comps[0]), new DecimalType(comps[1]));
            if (comps.length == 3) {
                pType.setAltitude(new DecimalType(comps[2]));
            }
        }
        state = pType;
    } else if (item instanceof CallItem) {
        state = new CallType(pItem.getValue());
    } else {
        state = new StringType(pItem.getValue());
    }
    return new JpaHistoricItem(item.getName(), state, pItem.getTimestamp());
}
Also used : LocationItem(org.openhab.core.library.items.LocationItem) StringType(org.openhab.core.library.types.StringType) ContactItem(org.openhab.core.library.items.ContactItem) Calendar(java.util.Calendar) CallType(org.openhab.library.tel.types.CallType) ColorItem(org.openhab.core.library.items.ColorItem) PercentType(org.openhab.core.library.types.PercentType) DateTimeItem(org.openhab.core.library.items.DateTimeItem) Date(java.util.Date) NumberItem(org.openhab.core.library.items.NumberItem) DateTimeType(org.openhab.core.library.types.DateTimeType) State(org.openhab.core.types.State) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) DecimalType(org.openhab.core.library.types.DecimalType) PointType(org.openhab.core.library.types.PointType) CallItem(org.openhab.library.tel.items.CallItem) HSBType(org.openhab.core.library.types.HSBType) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 2 with PointType

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

the class AbstractDynamoDBItem method asHistoricItem.

/*
     * (non-Javadoc)
     *
     * @see org.openhab.persistence.dynamodb.internal.DynamoItem#asHistoricItem(org.openhab.core.items.Item)
     */
@Override
public HistoricItem asHistoricItem(final Item item) {
    final State[] state = new State[1];
    accept(new DynamoDBItemVisitor() {

        @Override
        public void visit(DynamoDBStringItem dynamoStringItem) {
            if (item instanceof ColorItem) {
                state[0] = new HSBType(dynamoStringItem.getState());
            } else if (item instanceof LocationItem) {
                state[0] = new PointType(dynamoStringItem.getState());
            } else if (item instanceof DateTimeItem) {
                Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
                try {
                    cal.setTime(DATEFORMATTER.parse(dynamoStringItem.getState()));
                } catch (ParseException e) {
                    LOGGER.error("Failed to parse {} as date. Outputting UNDEF instead", dynamoStringItem.getState());
                    state[0] = UnDefType.UNDEF;
                }
                state[0] = new DateTimeType(cal);
            } else if (dynamoStringItem.getState().equals(UNDEFINED_PLACEHOLDER)) {
                state[0] = UnDefType.UNDEF;
            } else if (item instanceof CallItem) {
                String parts = dynamoStringItem.getState();
                String[] strings = parts.split("##");
                String dest = strings[0];
                String orig = strings[1];
                state[0] = new CallType(orig, dest);
            } else {
                state[0] = new StringType(dynamoStringItem.getState());
            }
        }

        @Override
        public void visit(DynamoDBBigDecimalItem dynamoBigDecimalItem) {
            if (item instanceof NumberItem) {
                state[0] = new DecimalType(dynamoBigDecimalItem.getState());
            } else if (item instanceof DimmerItem) {
                state[0] = new PercentType(dynamoBigDecimalItem.getState());
            } else if (item instanceof SwitchItem) {
                state[0] = dynamoBigDecimalItem.getState().compareTo(BigDecimal.ONE) == 0 ? OnOffType.ON : OnOffType.OFF;
            } else if (item instanceof ContactItem) {
                state[0] = dynamoBigDecimalItem.getState().compareTo(BigDecimal.ONE) == 0 ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
            } else if (item instanceof RollershutterItem) {
                state[0] = new PercentType(dynamoBigDecimalItem.getState());
            } else {
                LOGGER.warn("Not sure how to convert big decimal item {} to type {}. Using StringType as fallback", dynamoBigDecimalItem.getName(), item.getClass());
                state[0] = new StringType(dynamoBigDecimalItem.getState().toString());
            }
        }
    });
    return new DynamoDBHistoricItem(getName(), state[0], getTime());
}
Also used : LocationItem(org.openhab.core.library.items.LocationItem) StringType(org.openhab.core.library.types.StringType) CallType(org.openhab.library.tel.types.CallType) ColorItem(org.openhab.core.library.items.ColorItem) DateTimeItem(org.openhab.core.library.items.DateTimeItem) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) HSBType(org.openhab.core.library.types.HSBType) SwitchItem(org.openhab.core.library.items.SwitchItem) Calendar(java.util.Calendar) ContactItem(org.openhab.core.library.items.ContactItem) PercentType(org.openhab.core.library.types.PercentType) NumberItem(org.openhab.core.library.items.NumberItem) DateTimeType(org.openhab.core.library.types.DateTimeType) State(org.openhab.core.types.State) PointType(org.openhab.core.library.types.PointType) DecimalType(org.openhab.core.library.types.DecimalType) CallItem(org.openhab.library.tel.items.CallItem) ParseException(java.text.ParseException)

Example 3 with PointType

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

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

the class AbstractDynamoDBItemSerializationTest method testPointTypeWithLocationItem.

@Test
public void testPointTypeWithLocationItem() throws IOException {
    final PointType point = new PointType(new DecimalType(60.3), new DecimalType(30.2), new DecimalType(510.90));
    String expected = StringUtils.join(new String[] { point.getLatitude().toBigDecimal().toString(), point.getLongitude().toBigDecimal().toString(), point.getAltitude().toBigDecimal().toString() }, ",");
    DynamoDBItem<?> dbitem = testStateGeneric(point, expected);
    testAsHistoricGeneric(dbitem, new LocationItem("foo"), point);
}
Also used : LocationItem(org.openhab.core.library.items.LocationItem) PointType(org.openhab.core.library.types.PointType) DecimalType(org.openhab.core.library.types.DecimalType) Test(org.junit.Test)

Aggregations

DecimalType (org.openhab.core.library.types.DecimalType)4 PointType (org.openhab.core.library.types.PointType)4 Calendar (java.util.Calendar)3 LocationItem (org.openhab.core.library.items.LocationItem)3 DateTimeType (org.openhab.core.library.types.DateTimeType)3 StringType (org.openhab.core.library.types.StringType)3 State (org.openhab.core.types.State)3 ColorItem (org.openhab.core.library.items.ColorItem)2 ContactItem (org.openhab.core.library.items.ContactItem)2 DateTimeItem (org.openhab.core.library.items.DateTimeItem)2 DimmerItem (org.openhab.core.library.items.DimmerItem)2 NumberItem (org.openhab.core.library.items.NumberItem)2 RollershutterItem (org.openhab.core.library.items.RollershutterItem)2 SwitchItem (org.openhab.core.library.items.SwitchItem)2 HSBType (org.openhab.core.library.types.HSBType)2 PercentType (org.openhab.core.library.types.PercentType)2 CallItem (org.openhab.library.tel.items.CallItem)2 CallType (org.openhab.library.tel.types.CallType)2 BigDecimal (java.math.BigDecimal)1 ParseException (java.text.ParseException)1