Search in sources :

Example 21 with RefreshType

use of org.openhab.core.types.RefreshType in project openhab-addons by openhab.

the class SmartHomeDeviceHandler method handleCommand.

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
    AccountHandler accountHandler = getAccountHandler();
    if (accountHandler == null) {
        logger.debug("accountHandler is null in {}", thing.getUID());
        return;
    }
    Connection connection = accountHandler.findConnection();
    if (connection == null) {
        logger.debug("connection is null in {}", thing.getUID());
        return;
    }
    try {
        if (command instanceof RefreshType) {
            accountHandler.forceDelayedSmartHomeStateUpdate(getId());
            return;
        }
        SmartHomeBaseDevice smartHomeBaseDevice = this.smartHomeBaseDevice;
        if (smartHomeBaseDevice == null) {
            logger.debug("smarthomeBaseDevice is null in {}", thing.getUID());
            return;
        }
        Set<SmartHomeDevice> devices = getSupportedSmartHomeDevices(smartHomeBaseDevice, accountHandler.getLastKnownSmartHomeDevices());
        String channelId = channelUID.getId();
        for (String interfaceName : handlers.keySet()) {
            HandlerBase handlerBase = handlers.get(interfaceName);
            if (handlerBase == null || !handlerBase.hasChannel(channelId)) {
                continue;
            }
            for (SmartHomeDevice shd : devices) {
                String entityId = shd.entityId;
                if (entityId == null) {
                    continue;
                }
                // block updates
                accountHandler.forceDelayedSmartHomeStateUpdate(getId());
                if (handlerBase.handleCommand(connection, shd, entityId, shd.getCapabilities(), channelUID.getId(), command)) {
                    // force update again to restart
                    accountHandler.forceDelayedSmartHomeStateUpdate(getId());
                    // update timer
                    logger.debug("Command {} sent to {}", command, shd.findId());
                }
            }
        }
    } catch (Exception e) {
        logger.warn("Handle command failed", e);
    }
}
Also used : SmartHomeDevice(org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeDevices.SmartHomeDevice) Connection(org.openhab.binding.amazonechocontrol.internal.Connection) HandlerBase(org.openhab.binding.amazonechocontrol.internal.smarthome.HandlerBase) RefreshType(org.openhab.core.types.RefreshType) SmartHomeBaseDevice(org.openhab.binding.amazonechocontrol.internal.jsons.SmartHomeBaseDevice)

Example 22 with RefreshType

use of org.openhab.core.types.RefreshType in project openhab-addons by openhab.

the class LightThingHandler method handleCommand.

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
    if (channelUID.getId().equals(CHANNEL_ONTIME)) {
        if (command instanceof QuantityType<?>) {
            QuantityType<?> onTimeSeconds = ((QuantityType<?>) command).toUnit(Units.SECOND);
            if (onTimeSeconds != null) {
                onTime = 10 * onTimeSeconds.intValue();
            } else {
                logger.warn("Channel '{}' received command '{}', could not be converted to seconds.", channelUID, command);
            }
        }
        return;
    }
    if (command instanceof RefreshType) {
        valueUpdated(channelUID.getId(), lightStateCache);
        return;
    }
    LightState newLightState = new LightState();
    Boolean currentOn = lightStateCache.on;
    Integer currentBri = lightStateCache.bri;
    switch(channelUID.getId()) {
        case CHANNEL_ALERT:
            if (command instanceof StringType) {
                newLightState.alert = command.toString();
            } else {
                return;
            }
            break;
        case CHANNEL_EFFECT:
            if (command instanceof StringType) {
                // effect command only allowed for lights that are turned on
                newLightState.on = true;
                newLightState.effect = command.toString();
            } else {
                return;
            }
            break;
        case CHANNEL_EFFECT_SPEED:
            if (command instanceof DecimalType) {
                newLightState.on = true;
                newLightState.effectSpeed = Util.constrainToRange(((DecimalType) command).intValue(), 0, 10);
            } else {
                return;
            }
            break;
        case CHANNEL_SWITCH:
        case CHANNEL_LOCK:
            if (command instanceof OnOffType) {
                newLightState.on = (command == OnOffType.ON);
            } else {
                return;
            }
            break;
        case CHANNEL_BRIGHTNESS:
        case CHANNEL_COLOR:
            if (command instanceof OnOffType) {
                newLightState.on = (command == OnOffType.ON);
            } else if (command instanceof IncreaseDecreaseType) {
                // try to get best value for current brightness
                int oldBri = currentBri != null ? currentBri : (Boolean.TRUE.equals(currentOn) ? BRIGHTNESS_MAX : BRIGHTNESS_MIN);
                if (command.equals(IncreaseDecreaseType.INCREASE)) {
                    newLightState.bri = Util.constrainToRange(oldBri + BRIGHTNESS_DIM_STEP, BRIGHTNESS_MIN, BRIGHTNESS_MAX);
                } else {
                    newLightState.bri = Util.constrainToRange(oldBri - BRIGHTNESS_DIM_STEP, BRIGHTNESS_MIN, BRIGHTNESS_MAX);
                }
            } else if (command instanceof HSBType) {
                HSBType hsbCommand = (HSBType) command;
                // is in CT mode or iii) already in XY mode. Only if the bulb is in HS mode, use this one.
                if ("hs".equals(colorMode)) {
                    newLightState.hue = (int) (hsbCommand.getHue().doubleValue() * HUE_FACTOR);
                    newLightState.sat = Util.fromPercentType(hsbCommand.getSaturation());
                } else {
                    PercentType[] xy = hsbCommand.toXY();
                    if (xy.length < 2) {
                        logger.warn("Failed to convert {} to xy-values", command);
                    }
                    newLightState.xy = new double[] { xy[0].doubleValue() / 100.0, xy[1].doubleValue() / 100.0 };
                }
                newLightState.bri = Util.fromPercentType(hsbCommand.getBrightness());
            } else if (command instanceof PercentType) {
                newLightState.bri = Util.fromPercentType((PercentType) command);
            } else if (command instanceof DecimalType) {
                newLightState.bri = ((DecimalType) command).intValue();
            } else {
                return;
            }
            // send on/off state together with brightness if not already set or unknown
            Integer newBri = newLightState.bri;
            if (newBri != null) {
                newLightState.on = (newBri > 0);
            }
            // fix sending bri=0 when light is already off
            if (newBri != null && newBri == 0 && currentOn != null && !currentOn) {
                return;
            }
            Double transitiontime = config.transitiontime;
            if (transitiontime != null) {
                // value is in 1/10 seconds
                newLightState.transitiontime = (int) Math.round(10 * transitiontime);
            }
            break;
        case CHANNEL_COLOR_TEMPERATURE:
            if (command instanceof DecimalType) {
                int miredValue = kelvinToMired(((DecimalType) command).intValue());
                newLightState.ct = constrainToRange(miredValue, ctMin, ctMax);
                newLightState.on = true;
            }
            break;
        case CHANNEL_POSITION:
            if (command instanceof UpDownType) {
                newLightState.on = (command == UpDownType.DOWN);
            } else if (command == StopMoveType.STOP) {
                if (currentOn != null && currentOn && currentBri != null && currentBri <= BRIGHTNESS_MAX) {
                    // going down or currently stop (254 because of rounding error)
                    newLightState.on = true;
                } else if (currentOn != null && !currentOn && currentBri != null && currentBri > BRIGHTNESS_MIN) {
                    // going up or currently stopped
                    newLightState.on = false;
                }
            } else if (command instanceof PercentType) {
                newLightState.bri = fromPercentType((PercentType) command);
            } else {
                return;
            }
            break;
        default:
            // no supported command
            return;
    }
    Boolean newOn = newLightState.on;
    if (newOn != null && !newOn) {
        // if light shall be off, no other commands are allowed, so reset the new light state
        newLightState.clear();
        newLightState.on = false;
    } else if (newOn != null && newOn) {
        newLightState.ontime = onTime;
    }
    sendCommand(newLightState, command, channelUID, () -> {
        Integer transitionTime = newLightState.transitiontime;
        lastCommandExpireTimestamp = System.currentTimeMillis() + (transitionTime != null ? transitionTime : DEFAULT_COMMAND_EXPIRY_TIME);
        lastCommand = newLightState;
    });
}
Also used : LightState(org.openhab.binding.deconz.internal.dto.LightState) StringType(org.openhab.core.library.types.StringType) UpDownType(org.openhab.core.library.types.UpDownType) PercentType(org.openhab.core.library.types.PercentType) RefreshType(org.openhab.core.types.RefreshType) QuantityType(org.openhab.core.library.types.QuantityType) OnOffType(org.openhab.core.library.types.OnOffType) DecimalType(org.openhab.core.library.types.DecimalType) IncreaseDecreaseType(org.openhab.core.library.types.IncreaseDecreaseType) HSBType(org.openhab.core.library.types.HSBType)

Example 23 with RefreshType

use of org.openhab.core.types.RefreshType in project openhab-addons by openhab.

the class GroupThingHandler method handleCommand.

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
    String channelId = channelUID.getId();
    GroupAction newGroupAction = new GroupAction();
    switch(channelId) {
        case CHANNEL_ALL_ON:
        case CHANNEL_ANY_ON:
            if (command instanceof RefreshType) {
                valueUpdated(channelUID.getId(), groupStateCache);
                return;
            }
            break;
        case CHANNEL_ALERT:
            if (command instanceof StringType) {
                newGroupAction.alert = command.toString();
            } else {
                return;
            }
            break;
        case CHANNEL_COLOR:
            if (command instanceof HSBType) {
                HSBType hsbCommand = (HSBType) command;
                // is in CT mode or iii) already in XY mode. Only if the bulb is in HS mode, use this one.
                if ("hs".equals(colorMode)) {
                    newGroupAction.hue = (int) (hsbCommand.getHue().doubleValue() * HUE_FACTOR);
                    newGroupAction.sat = Util.fromPercentType(hsbCommand.getSaturation());
                } else {
                    PercentType[] xy = hsbCommand.toXY();
                    if (xy.length < 2) {
                        logger.warn("Failed to convert {} to xy-values", command);
                    }
                    newGroupAction.xy = new double[] { xy[0].doubleValue() / 100.0, xy[1].doubleValue() / 100.0 };
                }
            } else if (command instanceof PercentType) {
                newGroupAction.bri = Util.fromPercentType((PercentType) command);
            } else if (command instanceof DecimalType) {
                newGroupAction.bri = ((DecimalType) command).intValue();
            } else if (command instanceof OnOffType) {
                newGroupAction.on = OnOffType.ON.equals(command);
            } else {
                return;
            }
            break;
        case CHANNEL_COLOR_TEMPERATURE:
            if (command instanceof DecimalType) {
                int miredValue = Util.kelvinToMired(((DecimalType) command).intValue());
                newGroupAction.ct = Util.constrainToRange(miredValue, ZCL_CT_MIN, ZCL_CT_MAX);
            } else {
                return;
            }
            break;
        case CHANNEL_SCENE:
            if (command instanceof StringType) {
                String sceneId = scenes.get(command.toString());
                if (sceneId != null) {
                    sendCommand(null, command, channelUID, "scenes/" + sceneId + "/recall", null);
                } else {
                    logger.debug("Ignoring command {} for {}, scene is not found in available scenes: {}", command, channelUID, scenes);
                }
            }
            return;
        default:
            return;
    }
    Integer bri = newGroupAction.bri;
    if (bri != null) {
        newGroupAction.on = (bri > 0);
    }
    sendCommand(newGroupAction, command, channelUID, null);
}
Also used : GroupAction(org.openhab.binding.deconz.internal.dto.GroupAction) StringType(org.openhab.core.library.types.StringType) OnOffType(org.openhab.core.library.types.OnOffType) DecimalType(org.openhab.core.library.types.DecimalType) PercentType(org.openhab.core.library.types.PercentType) RefreshType(org.openhab.core.types.RefreshType) HSBType(org.openhab.core.library.types.HSBType)

Example 24 with RefreshType

use of org.openhab.core.types.RefreshType in project openhab-addons by openhab.

the class RoombaHandler method handleCommand.

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
    String ch = channelUID.getId();
    if (command instanceof RefreshType) {
        handleRefresh(ch);
        return;
    }
    if (ch.equals(CHANNEL_COMMAND)) {
        if (command instanceof StringType) {
            String cmd = command.toString();
            if (cmd.equals(CMD_CLEAN)) {
                cmd = isPaused ? "resume" : "start";
            }
            if (cmd.startsWith(CMD_CLEAN_REGIONS)) {
                // format: cleanRegions:<pmid>;<region_id1>,<region_id2>,...
                if (Pattern.matches("cleanRegions:[^:;,]+;.+(,[^:;,]+)*", cmd)) {
                    String[] cmds = cmd.split(":");
                    String[] params = cmds[1].split(";");
                    String mapId = params[0];
                    String userPmapvId;
                    if (params.length >= 3) {
                        userPmapvId = params[2];
                    } else {
                        userPmapvId = null;
                    }
                    String[] regions = params[1].split(",");
                    String[] regionIds = new String[regions.length];
                    String[] regionTypes = new String[regions.length];
                    for (int i = 0; i < regions.length; i++) {
                        String[] regionDetails = regions[i].split("=");
                        if (regionDetails.length >= 2) {
                            if (regionDetails[0].equals("r")) {
                                regionIds[i] = regionDetails[1];
                                regionTypes[i] = "rid";
                            } else if (regionDetails[0].equals("z")) {
                                regionIds[i] = regionDetails[1];
                                regionTypes[i] = "zid";
                            } else {
                                regionIds[i] = regionDetails[0];
                                regionTypes[i] = "rid";
                            }
                        } else {
                            regionIds[i] = regionDetails[0];
                            regionTypes[i] = "rid";
                        }
                    }
                    MQTTProtocol.Request request = new MQTTProtocol.CleanRoomsRequest("start", mapId, regionIds, regionTypes, userPmapvId);
                    connection.send(request.getTopic(), gson.toJson(request));
                } else {
                    logger.warn("Invalid request: {}", cmd);
                    logger.warn("Correct format: cleanRegions:<pmid>;<region_id1>,<region_id2>,...>");
                }
            } else {
                MQTTProtocol.Request request = new MQTTProtocol.CommandRequest(cmd);
                connection.send(request.getTopic(), gson.toJson(request));
            }
        }
    } else if (ch.startsWith(CHANNEL_SCHED_SWITCH_PREFIX)) {
        MQTTProtocol.Schedule schedule = lastSchedule;
        // schedule and modify components.
        if (command instanceof OnOffType && schedule != null && schedule.cycle != null) {
            for (int i = 0; i < CHANNEL_SCHED_SWITCH.length; i++) {
                if (ch.equals(CHANNEL_SCHED_SWITCH[i])) {
                    MQTTProtocol.Schedule newSchedule = new MQTTProtocol.Schedule(schedule.cycle);
                    newSchedule.enableCycle(i, command.equals(OnOffType.ON));
                    sendSchedule(newSchedule);
                    break;
                }
            }
        }
    } else if (ch.equals(CHANNEL_SCHEDULE)) {
        if (command instanceof DecimalType) {
            int bitmask = ((DecimalType) command).intValue();
            JsonArray cycle = new JsonArray();
            for (int i = 0; i < CHANNEL_SCHED_SWITCH.length; i++) {
                enableCycle(cycle, i, (bitmask & (1 << i)) != 0);
            }
            sendSchedule(new MQTTProtocol.Schedule(bitmask));
        }
    } else if (ch.equals(CHANNEL_EDGE_CLEAN)) {
        if (command instanceof OnOffType) {
            sendDelta(new MQTTProtocol.OpenOnly(command.equals(OnOffType.OFF)));
        }
    } else if (ch.equals(CHANNEL_ALWAYS_FINISH)) {
        if (command instanceof OnOffType) {
            sendDelta(new MQTTProtocol.BinPause(command.equals(OnOffType.OFF)));
        }
    } else if (ch.equals(CHANNEL_POWER_BOOST)) {
        sendDelta(new MQTTProtocol.PowerBoost(command.equals(BOOST_AUTO), command.equals(BOOST_PERFORMANCE)));
    } else if (ch.equals(CHANNEL_CLEAN_PASSES)) {
        sendDelta(new MQTTProtocol.CleanPasses(!command.equals(PASSES_AUTO), command.equals(PASSES_2)));
    } else if (ch.equals(CHANNEL_MAP_UPLOAD)) {
        if (command instanceof OnOffType) {
            sendDelta(new MQTTProtocol.MapUploadAllowed(command.equals(OnOffType.ON)));
        }
    }
}
Also used : MQTTProtocol(org.openhab.binding.irobot.internal.dto.MQTTProtocol) StringType(org.openhab.core.library.types.StringType) RefreshType(org.openhab.core.types.RefreshType) JsonArray(com.google.gson.JsonArray) OnOffType(org.openhab.core.library.types.OnOffType) DecimalType(org.openhab.core.library.types.DecimalType)

Example 25 with RefreshType

use of org.openhab.core.types.RefreshType in project openhab-addons by openhab.

the class EthernetBridgeHandler method handleCommand.

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
    if (!(command instanceof RefreshType)) {
        Channel channel = this.getThing().getChannel(channelUID.getId());
        if (channel != null) {
            Configuration channelConfiguration = channel.getConfiguration();
            if (channel.getChannelTypeUID() != null && channel.getChannelTypeUID().getId().equals(IRtransBindingConstants.BLASTER_CHANNEL_TYPE)) {
                if (command instanceof StringType) {
                    String remoteName = StringUtils.substringBefore(command.toString(), ",");
                    String irCommandName = StringUtils.substringAfter(command.toString(), ",");
                    IrCommand ircommand = new IrCommand();
                    ircommand.setRemote(remoteName);
                    ircommand.setCommand(irCommandName);
                    IrCommand thingCompatibleCommand = new IrCommand();
                    thingCompatibleCommand.setRemote((String) channelConfiguration.get(REMOTE));
                    thingCompatibleCommand.setCommand((String) channelConfiguration.get(COMMAND));
                    if (ircommand.matches(thingCompatibleCommand)) {
                        if (sendIRcommand(ircommand, Led.get((String) channelConfiguration.get(LED)))) {
                            logger.debug("Sent a matching infrared command '{}' for channel '{}'", command, channelUID);
                        } else {
                            logger.warn("An error occured whilst sending the infrared command '{}' for Channel '{}'", command, channelUID);
                        }
                    }
                }
            }
            if (channel.getAcceptedItemType() != null && channel.getAcceptedItemType().equals(IRtransBindingConstants.RECEIVER_CHANNEL_TYPE)) {
                logger.warn("Receivers can only receive infrared commands, not send them");
            }
        }
    }
}
Also used : Configuration(org.openhab.core.config.core.Configuration) StringType(org.openhab.core.library.types.StringType) SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Channel(org.openhab.core.thing.Channel) IrCommand(org.openhab.binding.irtrans.internal.IrCommand) RefreshType(org.openhab.core.types.RefreshType)

Aggregations

RefreshType (org.openhab.core.types.RefreshType)210 OnOffType (org.openhab.core.library.types.OnOffType)81 StringType (org.openhab.core.library.types.StringType)66 DecimalType (org.openhab.core.library.types.DecimalType)60 PercentType (org.openhab.core.library.types.PercentType)50 QuantityType (org.openhab.core.library.types.QuantityType)29 Channel (org.openhab.core.thing.Channel)26 State (org.openhab.core.types.State)26 IncreaseDecreaseType (org.openhab.core.library.types.IncreaseDecreaseType)25 IOException (java.io.IOException)22 Nullable (org.eclipse.jdt.annotation.Nullable)18 Bridge (org.openhab.core.thing.Bridge)18 ChannelUID (org.openhab.core.thing.ChannelUID)16 HSBType (org.openhab.core.library.types.HSBType)15 BigDecimal (java.math.BigDecimal)14 DateTimeType (org.openhab.core.library.types.DateTimeType)11 UpDownType (org.openhab.core.library.types.UpDownType)11 Temperature (javax.measure.quantity.Temperature)8 Thing (org.openhab.core.thing.Thing)8 Command (org.openhab.core.types.Command)8