Search in sources :

Example 21 with OnOffType

use of org.eclipse.smarthome.core.library.types.OnOffType 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

OnOffType (org.eclipse.smarthome.core.library.types.OnOffType)21 PercentType (org.eclipse.smarthome.core.library.types.PercentType)10 RefreshType (org.eclipse.smarthome.core.types.RefreshType)8 IncreaseDecreaseType (org.eclipse.smarthome.core.library.types.IncreaseDecreaseType)7 State (org.eclipse.smarthome.core.types.State)7 DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)6 ValueSet (org.eclipse.smarthome.binding.dmx.internal.ValueSet)4 FadeAction (org.eclipse.smarthome.binding.dmx.internal.action.FadeAction)4 StringType (org.eclipse.smarthome.core.library.types.StringType)4 HSBType (org.eclipse.smarthome.core.library.types.HSBType)3 ZonedDateTime (java.time.ZonedDateTime)2 BaseDmxChannel (org.eclipse.smarthome.binding.dmx.internal.multiverse.BaseDmxChannel)2 DmxChannel (org.eclipse.smarthome.binding.dmx.internal.multiverse.DmxChannel)2 OpenClosedType (org.eclipse.smarthome.core.library.types.OpenClosedType)2 FilterCriteria (org.eclipse.smarthome.core.persistence.FilterCriteria)2 HistoricItem (org.eclipse.smarthome.core.persistence.HistoricItem)2 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)2 Test (org.junit.Test)2 BasicStroke (java.awt.BasicStroke)1 Color (java.awt.Color)1