Search in sources :

Example 1 with PortData

use of org.openhab.binding.gce.internal.model.PortData in project openhab-addons by openhab.

the class Ipx800v3Handler method dataReceived.

@Override
public void dataReceived(String port, double value) {
    updateStatus(ThingStatus.ONLINE);
    Channel channel = thing.getChannel(PortDefinition.asChannelId(port));
    if (channel != null) {
        String channelId = channel.getUID().getId();
        String groupId = channel.getUID().getGroupId();
        PortData portData = portDatas.get(channelId);
        if (portData != null && groupId != null) {
            ZonedDateTime now = ZonedDateTime.now(ZoneId.systemDefault());
            long sinceLastChange = Duration.between(portData.getTimestamp(), now).toMillis();
            Configuration configuration = channel.getConfiguration();
            PortDefinition portDefinition = PortDefinition.fromGroupId(groupId);
            if (ignoreCondition(value, portData, configuration, portDefinition, now)) {
                logger.debug("Ignore condition met for port '{}' with data '{}'", port, value);
                return;
            }
            logger.debug("About to update port '{}' with data '{}'", port, value);
            State state = UnDefType.UNDEF;
            switch(portDefinition) {
                case COUNTER:
                    state = new DecimalType(value);
                    break;
                case RELAY:
                    state = value == 1 ? OnOffType.ON : OnOffType.OFF;
                    break;
                case ANALOG:
                    state = new DecimalType(value);
                    updateState(channelId + PROPERTY_SEPARATOR + CHANNEL_VOLTAGE, new QuantityType<>(value * ANALOG_SAMPLING, Units.VOLT));
                    break;
                case CONTACT:
                    DigitalInputConfiguration config = configuration.as(DigitalInputConfiguration.class);
                    portData.cancelPulsing();
                    state = value == 1 ? OpenClosedType.CLOSED : OpenClosedType.OPEN;
                    switch((OpenClosedType) state) {
                        case CLOSED:
                            if (config.longPressTime != 0 && !portData.isInitializing()) {
                                scheduler.schedule(new LongPressEvaluator(channel, port, portData), config.longPressTime, TimeUnit.MILLISECONDS);
                            } else if (config.pulsePeriod != 0) {
                                portData.setPulsing(scheduler.scheduleWithFixedDelay(() -> {
                                    triggerPushButtonChannel(channel, EVENT_PULSE);
                                }, config.pulsePeriod, config.pulsePeriod, TimeUnit.MILLISECONDS));
                                if (config.pulseTimeout != 0) {
                                    scheduler.schedule(portData::cancelPulsing, config.pulseTimeout, TimeUnit.MILLISECONDS);
                                }
                            }
                            break;
                        case OPEN:
                            if (!portData.isInitializing() && config.longPressTime != 0 && sinceLastChange < config.longPressTime) {
                                triggerPushButtonChannel(channel, EVENT_SHORT_PRESS);
                            }
                            break;
                    }
                    if (!portData.isInitializing()) {
                        triggerPushButtonChannel(channel, value == 1 ? EVENT_PRESSED : EVENT_RELEASED);
                    }
                    break;
            }
            updateState(channelId, state);
            if (!portData.isInitializing()) {
                updateState(channelId + PROPERTY_SEPARATOR + CHANNEL_LAST_STATE_DURATION, new QuantityType<>(sinceLastChange / 1000, Units.SECOND));
            }
            portData.setData(value, now);
        } else {
            logger.debug("Received data '{}' for not configured port '{}'", value, port);
        }
    } else {
        logger.debug("Received data '{}' for not configured channel '{}'", value, port);
    }
}
Also used : PortDefinition(org.openhab.binding.gce.internal.model.PortDefinition) AnalogInputConfiguration(org.openhab.binding.gce.internal.config.AnalogInputConfiguration) Configuration(org.openhab.core.config.core.Configuration) Ipx800Configuration(org.openhab.binding.gce.internal.config.Ipx800Configuration) DigitalInputConfiguration(org.openhab.binding.gce.internal.config.DigitalInputConfiguration) RelayOutputConfiguration(org.openhab.binding.gce.internal.config.RelayOutputConfiguration) Channel(org.openhab.core.thing.Channel) DigitalInputConfiguration(org.openhab.binding.gce.internal.config.DigitalInputConfiguration) ZonedDateTime(java.time.ZonedDateTime) PortData(org.openhab.binding.gce.internal.model.PortData) State(org.openhab.core.types.State) OpenClosedType(org.openhab.core.library.types.OpenClosedType) DecimalType(org.openhab.core.library.types.DecimalType)

Example 2 with PortData

use of org.openhab.binding.gce.internal.model.PortData in project openhab-addons by openhab.

the class Ipx800v3Handler method channelUnlinked.

@Override
public void channelUnlinked(ChannelUID channelUID) {
    super.channelUnlinked(channelUID);
    PortData portData = portDatas.remove(channelUID.getId());
    if (portData != null) {
        portData.destroy();
    }
}
Also used : PortData(org.openhab.binding.gce.internal.model.PortData)

Example 3 with PortData

use of org.openhab.binding.gce.internal.model.PortData in project openhab-addons by openhab.

the class Ipx800v3Handler method channelLinked.

@Override
public void channelLinked(ChannelUID channelUID) {
    logger.debug("channelLinked: {}", channelUID);
    final String channelId = channelUID.getId();
    if (isValidPortId(channelUID)) {
        Channel channel = thing.getChannel(channelUID);
        if (channel != null) {
            PortData data = new PortData();
            portDatas.put(channelId, data);
        }
    }
}
Also used : PortData(org.openhab.binding.gce.internal.model.PortData) Channel(org.openhab.core.thing.Channel)

Aggregations

PortData (org.openhab.binding.gce.internal.model.PortData)3 Channel (org.openhab.core.thing.Channel)2 ZonedDateTime (java.time.ZonedDateTime)1 AnalogInputConfiguration (org.openhab.binding.gce.internal.config.AnalogInputConfiguration)1 DigitalInputConfiguration (org.openhab.binding.gce.internal.config.DigitalInputConfiguration)1 Ipx800Configuration (org.openhab.binding.gce.internal.config.Ipx800Configuration)1 RelayOutputConfiguration (org.openhab.binding.gce.internal.config.RelayOutputConfiguration)1 PortDefinition (org.openhab.binding.gce.internal.model.PortDefinition)1 Configuration (org.openhab.core.config.core.Configuration)1 DecimalType (org.openhab.core.library.types.DecimalType)1 OpenClosedType (org.openhab.core.library.types.OpenClosedType)1 State (org.openhab.core.types.State)1