Search in sources :

Example 46 with OnOffType

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

the class TinkerforgeBinding method internalReceiveCommand.

/**
     * {@inheritDoc}
     *
     * Searches the item with the given {@code itemName} in the {@link TinkerforgeBindingProvider}
     * collection and gets the uid and subid of the device. The appropriate device is searched in the
     * ecosystem and the command is executed on the device.
     *
     * {@code OnOffType} commands are executed on {@link MInSwitchActor} objects. {@code StringType}
     * commands are executed on {@link MTextActor} objects.
     *
     */
@Override
protected void internalReceiveCommand(String itemName, Command command) {
    logger.debug("received command {} for item {}", command, itemName);
    for (TinkerforgeBindingProvider provider : providers) {
        for (String itemNameP : provider.getItemNames()) {
            if (itemNameP.equals(itemName)) {
                String deviceUid = provider.getUid(itemName);
                String deviceSubId = provider.getSubId(itemName);
                String deviceName = provider.getName(itemName);
                if (deviceName != null) {
                    String[] ids = getDeviceIdsForDeviceName(deviceName);
                    deviceUid = ids[0];
                    deviceSubId = ids[1];
                }
                logger.trace("{} found item for command: uid: {}, subid: {}", LoggerConstants.COMMAND, deviceUid, deviceSubId);
                MBaseDevice mDevice = tinkerforgeEcosystem.getDevice(deviceUid, deviceSubId);
                if (mDevice != null && mDevice.getEnabledA().get()) {
                    if (command instanceof OnOffType) {
                        logger.trace("{} found onoff command", LoggerConstants.COMMAND);
                        OnOffType cmd = (OnOffType) command;
                        if (mDevice instanceof MSwitchActor) {
                            OnOffValue state = cmd == OnOffType.OFF ? OnOffValue.OFF : OnOffValue.ON;
                            ((MSwitchActor) mDevice).turnSwitch(state);
                        } else if (mDevice instanceof DigitalActor) {
                            HighLowValue state = cmd == OnOffType.OFF ? HighLowValue.LOW : HighLowValue.HIGH;
                            ((DigitalActor) mDevice).turnDigital(state);
                        } else if (mDevice instanceof ProgrammableSwitchActor) {
                            OnOffValue state = cmd == OnOffType.OFF ? OnOffValue.OFF : OnOffValue.ON;
                            ((ProgrammableSwitchActor) mDevice).turnSwitch(state, provider.getDeviceOptions(itemName));
                        } else {
                            logger.error("{} received OnOff command for non-SwitchActor", LoggerConstants.COMMAND);
                        }
                    } else if (command instanceof StringType) {
                        logger.trace("{} found string command", LoggerConstants.COMMAND);
                        if (mDevice instanceof MTextActor) {
                            ((MTextActor) mDevice).write(command.toString());
                        }
                    } else if (command instanceof DecimalType) {
                        logger.debug("{} found number command", LoggerConstants.COMMAND);
                        if (command instanceof HSBType) {
                            logger.debug("{} found HSBType command", LoggerConstants.COMMAND);
                            if (mDevice instanceof ProgrammableColorActor) {
                                logger.debug("{} found ProgrammableColorActor {}", itemName);
                                ((ProgrammableColorActor) mDevice).setSelectedColor((HSBType) command, provider.getDeviceOptions(itemName));
                            } else if (mDevice instanceof SimpleColorActor) {
                                logger.debug("{} found SimpleColorActor {}", itemName);
                                ((SimpleColorActor) mDevice).setSelectedColor((HSBType) command);
                            }
                        } else if (command instanceof PercentType) {
                            if (mDevice instanceof SetPointActor) {
                                ((SetPointActor<?>) mDevice).setValue(((PercentType) command), provider.getDeviceOptions(itemName));
                                logger.debug("found SetpointActor");
                            } else if (mDevice instanceof PercentTypeActor) {
                                ((PercentTypeActor) mDevice).setValue(((PercentType) command), provider.getDeviceOptions(itemName));
                                logger.debug("found PercentType actor");
                            } else {
                                logger.error("found no percenttype actor");
                            }
                        } else {
                            if (mDevice instanceof NumberActor) {
                                ((NumberActor) mDevice).setNumber(((DecimalType) command).toBigDecimal());
                            } else if (mDevice instanceof SetPointActor) {
                                ((SetPointActor<?>) mDevice).setValue(((DecimalType) command).toBigDecimal(), provider.getDeviceOptions(itemName));
                            } else {
                                logger.error("found no number actor");
                            }
                        }
                    } else if (command instanceof UpDownType) {
                        UpDownType cmd = (UpDownType) command;
                        logger.debug("{} UpDownType command {}", itemName, cmd);
                        if (mDevice instanceof MoveActor) {
                            ((MoveActor) mDevice).move((UpDownType) command, provider.getDeviceOptions(itemName));
                        }
                    } else if (command instanceof StopMoveType) {
                        StopMoveType cmd = (StopMoveType) command;
                        if (mDevice instanceof MoveActor) {
                            if (cmd == StopMoveType.STOP) {
                                ((MoveActor) mDevice).stop();
                            } else {
                                ((MoveActor) mDevice).moveon(provider.getDeviceOptions(itemName));
                            }
                        }
                        logger.debug("{} StopMoveType command {}", itemName, cmd);
                    } else if (command instanceof IncreaseDecreaseType) {
                        IncreaseDecreaseType cmd = (IncreaseDecreaseType) command;
                        if (mDevice instanceof DimmableActor) {
                            ((DimmableActor<?>) mDevice).dimm((IncreaseDecreaseType) command, provider.getDeviceOptions(itemName));
                        }
                        logger.debug("{} IncreaseDecreaseType command {}", itemName, cmd);
                    } else {
                        logger.error("{} got unknown command type: {}", LoggerConstants.COMMAND, command.toString());
                    }
                } else {
                    logger.error("{} no tinkerforge device found for command for item uid: {} subId: {}", LoggerConstants.COMMAND, deviceUid, deviceSubId);
                }
            }
        }
    }
}
Also used : MSwitchActor(org.openhab.binding.tinkerforge.internal.model.MSwitchActor) StringType(org.openhab.core.library.types.StringType) PercentTypeActor(org.openhab.binding.tinkerforge.internal.model.PercentTypeActor) MBaseDevice(org.openhab.binding.tinkerforge.internal.model.MBaseDevice) ProgrammableColorActor(org.openhab.binding.tinkerforge.internal.model.ProgrammableColorActor) StopMoveType(org.openhab.core.library.types.StopMoveType) TinkerforgeBindingProvider(org.openhab.binding.tinkerforge.TinkerforgeBindingProvider) HighLowValue(org.openhab.binding.tinkerforge.internal.types.HighLowValue) HSBType(org.openhab.core.library.types.HSBType) ProgrammableSwitchActor(org.openhab.binding.tinkerforge.internal.model.ProgrammableSwitchActor) OnOffValue(org.openhab.binding.tinkerforge.internal.types.OnOffValue) SetPointActor(org.openhab.binding.tinkerforge.internal.model.SetPointActor) UpDownType(org.openhab.core.library.types.UpDownType) PercentType(org.openhab.core.library.types.PercentType) DigitalActor(org.openhab.binding.tinkerforge.internal.model.DigitalActor) OnOffType(org.openhab.core.library.types.OnOffType) MoveActor(org.openhab.binding.tinkerforge.internal.model.MoveActor) DimmableActor(org.openhab.binding.tinkerforge.internal.model.DimmableActor) MTextActor(org.openhab.binding.tinkerforge.internal.model.MTextActor) DecimalType(org.openhab.core.library.types.DecimalType) NumberActor(org.openhab.binding.tinkerforge.internal.model.NumberActor) IncreaseDecreaseType(org.openhab.core.library.types.IncreaseDecreaseType) SimpleColorActor(org.openhab.binding.tinkerforge.internal.model.SimpleColorActor)

Example 47 with OnOffType

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

the class TACmiBinding method internalReceiveCommand.

/**
     * @throws UnknownHostException
     * @{inheritDoc
     */
@Override
protected void internalReceiveCommand(String itemName, Command command) {
    logger.debug("internalReceiveCommand({},{}) is called!", itemName, command);
    for (TACmiBindingProvider provider : providers) {
        int canNode = provider.getCanNode(itemName);
        String portType = provider.getPortType(itemName);
        int portNumber = provider.getPortNumber(itemName);
        logger.trace("Type: {}, portNumber: {}, command: {}", portType, portNumber, command.toString());
        byte[] messageBytes;
        if (portType.equals("d") && portNumber == 1 && command instanceof OnOffType) {
            boolean state = OnOffType.ON.equals(command) ? true : false;
            DigitalMessage message = new DigitalMessage((byte) canNode, state);
            messageBytes = message.getRaw();
        } else if (portType.equals("a") && (portNumber - 1) % 4 == 0 && command instanceof DecimalType) {
            TACmiMeasureType measureType = provider.getMeasureType(itemName);
            AnalogMessage message = new AnalogMessage((byte) canNode, 1, (DecimalType) command, measureType);
            messageBytes = message.getRaw();
        } else {
            logger.info("Not sending command: portType: {}, portNumber: {}, command: {}", portType, portNumber, command.toString());
            return;
        }
        DatagramPacket packet = new DatagramPacket(messageBytes, messageBytes.length, cmiAddress, TACmiBinding.cmiPort);
        try {
            clientSocket.send(packet);
        } catch (IOException e) {
            logger.warn("Error sending message: {}, {}", e.getClass().getName(), e.getMessage());
        }
    }
}
Also used : AnalogMessage(org.openhab.binding.tacmi.internal.message.AnalogMessage) TACmiBindingProvider(org.openhab.binding.tacmi.TACmiBindingProvider) OnOffType(org.openhab.core.library.types.OnOffType) DatagramPacket(java.net.DatagramPacket) DecimalType(org.openhab.core.library.types.DecimalType) DigitalMessage(org.openhab.binding.tacmi.internal.message.DigitalMessage) IOException(java.io.IOException)

Example 48 with OnOffType

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

the class YamahaReceiverBinding method internalReceiveCommand.

@Override
protected void internalReceiveCommand(String itemName, Command command) {
    YamahaReceiverBindingConfig config = getConfigForItemName(itemName);
    if (config == null) {
        logger.error("Received command for unknown item '" + itemName + "'");
        return;
    }
    YamahaReceiverProxy proxy = proxies.get(config.getDeviceUid());
    if (proxy == null) {
        logger.error("Received command for unknown device uid '" + config.getDeviceUid() + "'");
        return;
    }
    if (logger.isDebugEnabled()) {
        logger.debug(BINDING_NAME + " processing command '" + command + "' of type '" + command.getClass().getSimpleName() + "' for item '" + itemName + "'");
    }
    try {
        Zone zone = config.getZone();
        BindingType type = config.getBindingType();
        if (type == BindingType.power) {
            if (command instanceof OnOffType) {
                proxy.setPower(zone, command == OnOffType.ON);
            }
        } else if (type == BindingType.volumePercent || type == BindingType.volumeDb) {
            if (command instanceof IncreaseDecreaseType || command instanceof UpDownType) {
                // increase/decrease dB by .5 dB
                float db = proxy.getState(zone).getVolume();
                float adjAmt;
                if (command == IncreaseDecreaseType.INCREASE || command == UpDownType.UP) {
                    adjAmt = .5f;
                } else {
                    adjAmt = -.5f;
                }
                float newDb = db + adjAmt;
                proxy.setVolume(zone, newDb);
                // send new value as update
                State newState = new DecimalType(newDb);
                eventPublisher.postUpdate(itemName, newState);
            } else if (command instanceof PercentType) {
                // set dB from percent
                byte percent = ((PercentType) command).byteValue();
                int db = percentToDB(percent);
                proxy.setVolume(zone, db);
            } else {
                // set dB from value
                float db = Float.parseFloat(command.toString());
                proxy.setVolume(zone, db);
            }
            // Volume updates multiple values => send update now
            sendUpdates(proxy, config.getDeviceUid());
        } else if (type == BindingType.mute) {
            if (command instanceof OnOffType) {
                proxy.setMute(zone, command == OnOffType.ON);
            }
        } else if (type == BindingType.input) {
            proxy.setInput(zone, parseString(command.toString()));
        } else if (type == BindingType.surroundProgram) {
            proxy.setSurroundProgram(zone, parseString(command.toString()));
        } else if (type == BindingType.netRadio) {
            if (command instanceof DecimalType) {
                proxy.setNetRadio(((DecimalType) command).intValue());
            }
        }
    } catch (IOException e) {
        logger.warn("Cannot communicate with " + proxy.getHost() + " (uid: " + config.getDeviceUid() + ")");
    } catch (Throwable t) {
        logger.error("Error processing command '" + command + "' for item '" + itemName + "'", t);
    }
}
Also used : Zone(org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConfig.Zone) UpDownType(org.openhab.core.library.types.UpDownType) PercentType(org.openhab.core.library.types.PercentType) IOException(java.io.IOException) OnOffType(org.openhab.core.library.types.OnOffType) BindingType(org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConfig.BindingType) YamahaReceiverState(org.openhab.binding.yamahareceiver.internal.hardware.YamahaReceiverState) State(org.openhab.core.types.State) DecimalType(org.openhab.core.library.types.DecimalType) IncreaseDecreaseType(org.openhab.core.library.types.IncreaseDecreaseType) YamahaReceiverProxy(org.openhab.binding.yamahareceiver.internal.hardware.YamahaReceiverProxy)

Example 49 with OnOffType

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

the class BticinoDevice method receiveCommand.

public void receiveCommand(String itemName, Command command, BticinoBindingConfig itemBindingConfig) {
    try {
        synchronized (m_lock) {
            // A command is received from the openHab system;
            // analyse it and execute it
            logger.debug("Gateway [" + m_gateway_id + "], Command '{}' received for item {}", (Object[]) new String[] { command.toString(), itemName });
            ProtocolRead l_pr = new ProtocolRead(itemBindingConfig.who + "*" + itemBindingConfig.where);
            l_pr.addProperty("who", itemBindingConfig.who);
            l_pr.addProperty("address", itemBindingConfig.where);
            int l_who = Integer.parseInt(itemBindingConfig.who);
            switch(l_who) {
                // Lights
                case 1:
                    {
                        if (command instanceof IncreaseDecreaseType) {
                            if (IncreaseDecreaseType.INCREASE.equals(command)) {
                                logger.debug("Light received INCREASE command.");
                                l_pr.addProperty("what", "30");
                            } else {
                                logger.debug("Light received DECREASE command.");
                                l_pr.addProperty("what", "31");
                            }
                        } else if (command instanceof PercentType) {
                            PercentType pType = (PercentType) command;
                            //take percentage and divide by 10, round 1 (ie 0 to 10 is the result, nothing else)
                            int percentValue = (int) (Math.floor(pType.intValue() / 10F));
                            logger.debug("Set light value to {}", percentValue);
                            l_pr.addProperty("what", String.valueOf(percentValue));
                        } else if (command instanceof OnOffType) {
                            if (OnOffType.ON.equals(command)) {
                                l_pr.addProperty("what", "1");
                            } else {
                                l_pr.addProperty("what", "0");
                            }
                        } else {
                            logger.warn("Received unknown command type for lighting: '{}'", command.getClass().getName());
                        }
                        break;
                    }
                // Shutter
                case 2:
                    {
                        if (UpDownType.UP.equals(command)) {
                            l_pr.addProperty("what", "1");
                        } else if (UpDownType.DOWN.equals(command)) {
                            l_pr.addProperty("what", "2");
                        } else if (StopMoveType.STOP.equals(command)) {
                            l_pr.addProperty("what", "0");
                        }
                        break;
                    }
                // CEN Basic & Evolved
                case 15:
                    {
                        // device
                        if (OnOffType.ON.equals(command)) {
                            l_pr.addProperty("what", itemBindingConfig.what);
                        }
                        break;
                    }
            }
            m_open_web_net.onCommand(l_pr);
        }
    } catch (Exception e) {
        logger.error("Gateway [" + m_gateway_id + "], Error processing receiveCommand '{}'", (Object[]) new String[] { e.getMessage() });
    }
}
Also used : OnOffType(org.openhab.core.library.types.OnOffType) IncreaseDecreaseType(org.openhab.core.library.types.IncreaseDecreaseType) PercentType(org.openhab.core.library.types.PercentType) ProtocolRead(be.devlaminck.openwebnet.ProtocolRead)

Example 50 with OnOffType

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

the class FreeswitchBinding method endCallItemUpdate.

/**
     * update items on call end
     *
     * @param config
     */
private void endCallItemUpdate(FreeswitchBindingConfig config) {
    OnOffType activeState = OnOffType.OFF;
    ;
    CallType callType = (CallType) CallType.EMPTY;
    StringType callerId = StringType.EMPTY;
    /*
         * A channel has ended that has this item associated with it
         * We still need to check if this item is associated with other
         * channels.
         * We are going to iterate backwards to get the last added channel;
         */
    ListIterator<String> it = new ArrayList<String>(itemMap.keySet()).listIterator(itemMap.size());
    // if we get a match we will stop processing
    boolean match = false;
    while (it.hasPrevious()) {
        String uuid = it.previous();
        for (FreeswitchBindingConfig c : itemMap.get(uuid)) {
            if (c.getItemName().equals(config.getItemName())) {
                Channel channel = eventCache.get(uuid);
                activeState = OnOffType.ON;
                callType = channel.getCall();
                callerId = new StringType(String.format("%s : %s", channel.getEventHeader(CID_NAME), channel.getEventHeader(CID_NUMBER)));
                match = true;
                break;
            }
        }
        if (match) {
            break;
        }
    }
    if (config.getItemType().isAssignableFrom(SwitchItem.class)) {
        eventPublisher.postUpdate(config.getItemName(), activeState);
    } else if (config.getItemType().isAssignableFrom(CallItem.class)) {
        eventPublisher.postUpdate(config.getItemName(), callType);
    } else if (config.getItemType().isAssignableFrom(StringItem.class)) {
        eventPublisher.postUpdate(config.getItemName(), callerId);
    } else {
        logger.warn("handleHangupCall - postUpdate for itemType '{}' is undefined", config.getItemName());
    }
}
Also used : OnOffType(org.openhab.core.library.types.OnOffType) StringType(org.openhab.core.library.types.StringType) CallType(org.openhab.library.tel.types.CallType) CallItem(org.openhab.library.tel.items.CallItem)

Aggregations

OnOffType (org.openhab.core.library.types.OnOffType)50 DecimalType (org.openhab.core.library.types.DecimalType)22 PercentType (org.openhab.core.library.types.PercentType)15 IncreaseDecreaseType (org.openhab.core.library.types.IncreaseDecreaseType)12 OpenClosedType (org.openhab.core.library.types.OpenClosedType)11 StringType (org.openhab.core.library.types.StringType)11 UpDownType (org.openhab.core.library.types.UpDownType)9 IOException (java.io.IOException)8 State (org.openhab.core.types.State)7 StopMoveType (org.openhab.core.library.types.StopMoveType)6 DateTimeType (org.openhab.core.library.types.DateTimeType)5 HSBType (org.openhab.core.library.types.HSBType)5 UnknownHostException (java.net.UnknownHostException)4 ConfigurationException (org.osgi.service.cm.ConfigurationException)4 RFXComException (org.openhab.binding.rfxcom.internal.RFXComException)3 Item (org.openhab.core.items.Item)3 SwitchItem (org.openhab.core.library.items.SwitchItem)3 Color (java.awt.Color)2 DatagramPacket (java.net.DatagramPacket)2 SocketTimeoutException (java.net.SocketTimeoutException)2