Search in sources :

Example 11 with DateTimeType

use of org.eclipse.smarthome.core.library.types.DateTimeType in project smarthome by eclipse.

the class NtpOSGiTest method testDateTimeChannelCalendarTimeZoneUpdate.

@Test
@Ignore("https://github.com/eclipse/smarthome/issues/5224")
public void testDateTimeChannelCalendarTimeZoneUpdate() {
    Configuration configuration = new Configuration();
    configuration.put(NtpBindingConstants.PROPERTY_TIMEZONE, TEST_TIME_ZONE_ID);
    initialize(configuration, NtpBindingConstants.CHANNEL_DATE_TIME, ACCEPTED_ITEM_TYPE_DATE_TIME, null, null);
    ZonedDateTime timeZoneIdFromItemRegistry = ((DateTimeType) getItemState(ACCEPTED_ITEM_TYPE_DATE_TIME)).getZonedDateTime();
    ZoneOffset testZoneId;
    if (timeZoneIdFromItemRegistry.getZone().getRules().isDaylightSavings(timeZoneIdFromItemRegistry.toInstant())) {
        testZoneId = ZoneOffset.of("-07:00");
    } else {
        testZoneId = ZoneOffset.of("-08:00");
    }
    assertEquals(testZoneId, timeZoneIdFromItemRegistry.getOffset());
}
Also used : DateTimeType(org.eclipse.smarthome.core.library.types.DateTimeType) Configuration(org.eclipse.smarthome.config.core.Configuration) ZonedDateTime(java.time.ZonedDateTime) ZoneOffset(java.time.ZoneOffset) Ignore(org.junit.Ignore) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 12 with DateTimeType

use of org.eclipse.smarthome.core.library.types.DateTimeType in project smarthome by eclipse.

the class NtpOSGiTest method testDateTimeChannelCalendarDefaultTimeZoneUpdate.

@Test
@Ignore("https://github.com/eclipse/smarthome/issues/5224")
public void testDateTimeChannelCalendarDefaultTimeZoneUpdate() {
    Configuration configuration = new Configuration();
    // Initialize with configuration with no time zone property set.
    initialize(configuration, NtpBindingConstants.CHANNEL_DATE_TIME, ACCEPTED_ITEM_TYPE_DATE_TIME, null, null);
    ZonedDateTime timeZoneIdFromItemRegistry = ((DateTimeType) getItemState(ACCEPTED_ITEM_TYPE_DATE_TIME)).getZonedDateTime();
    ZoneOffset expectedOffset;
    if (timeZoneIdFromItemRegistry.getZone().getRules().isDaylightSavings(timeZoneIdFromItemRegistry.toInstant())) {
        expectedOffset = ZoneOffset.of("+03:00");
    } else {
        expectedOffset = ZoneOffset.of("+02:00");
    }
    assertEquals(expectedOffset, timeZoneIdFromItemRegistry.getOffset());
}
Also used : DateTimeType(org.eclipse.smarthome.core.library.types.DateTimeType) Configuration(org.eclipse.smarthome.config.core.Configuration) ZonedDateTime(java.time.ZonedDateTime) ZoneOffset(java.time.ZoneOffset) Ignore(org.junit.Ignore) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 13 with DateTimeType

use of org.eclipse.smarthome.core.library.types.DateTimeType in project smarthome by eclipse.

the class NtpHandler method refreshTimeDate.

private synchronized void refreshTimeDate() {
    if (timeZone != null && locale != null) {
        long networkTimeInMillis;
        if (refreshNtpCount <= 0) {
            networkTimeInMillis = getTime(hostname);
            timeOffset = networkTimeInMillis - System.currentTimeMillis();
            logger.debug("{} delta system time: {}", getThing().getUID(), timeOffset);
            refreshNtpCount = refreshNtp.intValue();
        } else {
            networkTimeInMillis = System.currentTimeMillis() + timeOffset;
            refreshNtpCount--;
        }
        ZonedDateTime zoned = ZonedDateTime.ofInstant(Instant.ofEpochMilli(networkTimeInMillis), timeZone.toZoneId());
        updateState(dateTimeChannelUID, new DateTimeType(zoned));
        updateState(stringChannelUID, new StringType(dateTimeFormat.format(zoned)));
    } else {
        logger.debug("Not refreshing, since we do not seem to be initialized yet");
    }
}
Also used : DateTimeType(org.eclipse.smarthome.core.library.types.DateTimeType) ZonedDateTime(java.time.ZonedDateTime) StringType(org.eclipse.smarthome.core.library.types.StringType)

Example 14 with DateTimeType

use of org.eclipse.smarthome.core.library.types.DateTimeType in project smarthome by eclipse.

the class WemoCoffeeHandler method getDateTimeState.

public State getDateTimeState(String attributeValue) {
    if (attributeValue != null) {
        long value = 0;
        try {
            // convert s to ms
            value = Long.parseLong(attributeValue) * 1000;
        } catch (NumberFormatException e) {
            logger.error("Unable to parse attributeValue '{}' for device '{}'; expected long", attributeValue, getThing().getUID());
            return null;
        }
        ZonedDateTime zoned = ZonedDateTime.ofInstant(Instant.ofEpochMilli(value), TimeZone.getDefault().toZoneId());
        State dateTimeState = new DateTimeType(zoned);
        if (dateTimeState != null) {
            logger.trace("New attribute brewed '{}' received", dateTimeState);
            return dateTimeState;
        }
    }
    return null;
}
Also used : DateTimeType(org.eclipse.smarthome.core.library.types.DateTimeType) ZonedDateTime(java.time.ZonedDateTime) State(org.eclipse.smarthome.core.types.State)

Example 15 with DateTimeType

use of org.eclipse.smarthome.core.library.types.DateTimeType in project smarthome by eclipse.

the class WemoHandler method onValueReceived.

@Override
public void onValueReceived(String variable, String value, String service) {
    logger.debug("Received pair '{}':'{}' (service '{}') for thing '{}'", new Object[] { variable, value, service, this.getThing().getUID() });
    updateStatus(ThingStatus.ONLINE);
    this.stateMap.put(variable, value);
    if (getThing().getThingTypeUID().getId().equals("insight")) {
        String insightParams = stateMap.get("InsightParams");
        if (insightParams != null) {
            String[] splitInsightParams = insightParams.split("\\|");
            if (splitInsightParams[0] != null) {
                OnOffType binaryState = null;
                binaryState = splitInsightParams[0].equals("0") ? OnOffType.OFF : OnOffType.ON;
                if (binaryState != null) {
                    logger.trace("New InsightParam binaryState '{}' for device '{}' received", binaryState, getThing().getUID());
                    updateState(CHANNEL_STATE, binaryState);
                }
            }
            long lastChangedAt = 0;
            try {
                // convert s to ms
                lastChangedAt = Long.parseLong(splitInsightParams[1]) * 1000;
            } catch (NumberFormatException e) {
                logger.error("Unable to parse lastChangedAt value '{}' for device '{}'; expected long", splitInsightParams[1], getThing().getUID());
            }
            ZonedDateTime zoned = ZonedDateTime.ofInstant(Instant.ofEpochMilli(lastChangedAt), TimeZone.getDefault().toZoneId());
            State lastChangedAtState = new DateTimeType(zoned);
            if (lastChangedAt != 0) {
                logger.trace("New InsightParam lastChangedAt '{}' for device '{}' received", lastChangedAtState, getThing().getUID());
                updateState(CHANNEL_LASTCHANGEDAT, lastChangedAtState);
            }
            State lastOnFor = DecimalType.valueOf(splitInsightParams[2]);
            if (lastOnFor != null) {
                logger.trace("New InsightParam lastOnFor '{}' for device '{}' received", lastOnFor, getThing().getUID());
                updateState(CHANNEL_LASTONFOR, lastOnFor);
            }
            State onToday = DecimalType.valueOf(splitInsightParams[3]);
            if (onToday != null) {
                logger.trace("New InsightParam onToday '{}' for device '{}' received", onToday, getThing().getUID());
                updateState(CHANNEL_ONTODAY, onToday);
            }
            State onTotal = DecimalType.valueOf(splitInsightParams[4]);
            if (onTotal != null) {
                logger.trace("New InsightParam onTotal '{}' for device '{}' received", onTotal, getThing().getUID());
                updateState(CHANNEL_ONTOTAL, onTotal);
            }
            State timespan = DecimalType.valueOf(splitInsightParams[5]);
            if (timespan != null) {
                logger.trace("New InsightParam timespan '{}' for device '{}' received", timespan, getThing().getUID());
                updateState(CHANNEL_TIMESPAN, timespan);
            }
            // natively given in W
            State averagePower = DecimalType.valueOf(splitInsightParams[6]);
            if (averagePower != null) {
                logger.trace("New InsightParam averagePower '{}' for device '{}' received", averagePower, getThing().getUID());
                updateState(CHANNEL_AVERAGEPOWER, averagePower);
            }
            BigDecimal currentMW = new BigDecimal(splitInsightParams[7]);
            // recalculate
            State currentPower = new DecimalType(currentMW.divide(new BigDecimal(1000), RoundingMode.HALF_UP));
            // mW to W
            if (currentPower != null) {
                logger.trace("New InsightParam currentPower '{}' for device '{}' received", currentPower, getThing().getUID());
                updateState(CHANNEL_CURRENTPOWER, currentPower);
            }
            BigDecimal energyTodayMWMin = new BigDecimal(splitInsightParams[8]);
            // recalculate mW-mins to Wh
            State energyToday = new DecimalType(energyTodayMWMin.divide(new BigDecimal(60000), RoundingMode.HALF_UP));
            if (energyToday != null) {
                logger.trace("New InsightParam energyToday '{}' for device '{}' received", energyToday, getThing().getUID());
                updateState(CHANNEL_ENERGYTODAY, energyToday);
            }
            BigDecimal energyTotalMWMin = new BigDecimal(splitInsightParams[9]);
            // recalculate mW-mins to Wh
            State energyTotal = new DecimalType(energyTotalMWMin.divide(new BigDecimal(60000), RoundingMode.HALF_UP));
            if (energyTotal != null) {
                logger.trace("New InsightParam energyTotal '{}' for device '{}' received", energyTotal, getThing().getUID());
                updateState(CHANNEL_ENERGYTOTAL, energyTotal);
            }
            BigDecimal standByLimitMW = new BigDecimal(splitInsightParams[10]);
            // recalculate
            State standByLimit = new DecimalType(standByLimitMW.divide(new BigDecimal(1000), RoundingMode.HALF_UP));
            // mW to W
            if (standByLimit != null) {
                logger.trace("New InsightParam standByLimit '{}' for device '{}' received", standByLimit, getThing().getUID());
                updateState(CHANNEL_STANDBYLIMIT, standByLimit);
            }
        }
    } else {
        State state = stateMap.get("BinaryState").equals("0") ? OnOffType.OFF : OnOffType.ON;
        logger.debug("State '{}' for device '{}' received", state, getThing().getUID());
        if (state != null) {
            if (getThing().getThingTypeUID().getId().equals("motion")) {
                updateState(CHANNEL_MOTIONDETECTION, state);
                if (state.equals(OnOffType.ON)) {
                    State lastMotionDetected = new DateTimeType();
                    updateState(CHANNEL_LASTMOTIONDETECTED, lastMotionDetected);
                }
            } else {
                updateState(CHANNEL_STATE, state);
            }
        }
    }
}
Also used : DateTimeType(org.eclipse.smarthome.core.library.types.DateTimeType) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) ZonedDateTime(java.time.ZonedDateTime) State(org.eclipse.smarthome.core.types.State) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) BigDecimal(java.math.BigDecimal)

Aggregations

DateTimeType (org.eclipse.smarthome.core.library.types.DateTimeType)16 Test (org.junit.Test)10 ZonedDateTime (java.time.ZonedDateTime)7 State (org.eclipse.smarthome.core.types.State)6 ZoneOffset (java.time.ZoneOffset)4 Configuration (org.eclipse.smarthome.config.core.Configuration)4 StringType (org.eclipse.smarthome.core.library.types.StringType)4 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 Ignore (org.junit.Ignore)3 Item (org.eclipse.smarthome.core.items.Item)2 ColorItem (org.eclipse.smarthome.core.library.items.ColorItem)2 DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)2 Widget (org.eclipse.smarthome.model.sitemap.Widget)2 BigDecimal (java.math.BigDecimal)1 LinkedList (java.util.LinkedList)1 Temperature (javax.measure.quantity.Temperature)1 WeatherUndergroundJsonForecastDay (org.eclipse.smarthome.binding.weatherunderground.internal.json.WeatherUndergroundJsonForecastDay)1 HSBType (org.eclipse.smarthome.core.library.types.HSBType)1 OnOffType (org.eclipse.smarthome.core.library.types.OnOffType)1