Search in sources :

Example 41 with DecimalType

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

the class DataTypeBoolean method convertToState.

/**
     * {@inheritDoc}
     */
@Override
public State convertToState(int[] data, ComfoAirCommandType commandType) {
    int[] get_reply_data_pos = commandType.getGetReplyDataPos();
    int get_reply_data_bits = commandType.getGetReplyDataBits();
    boolean result = (data[get_reply_data_pos[0]] & get_reply_data_bits) == get_reply_data_bits;
    return (result) ? new DecimalType(1) : new DecimalType(0);
}
Also used : DecimalType(org.openhab.core.library.types.DecimalType)

Example 42 with DecimalType

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

the class ComfoAirConnector method open.

/**
     * Open and initialize a serial port.
     *
     * @param portName
     *            e.g. /dev/ttyS0
     * @param listener
     *            the listener which is informed after a successful response
     *            read
     * @throws InitializationException
     */
public void open(String portName) throws InitializationException {
    logger.debug("Open ComfoAir connection");
    port = portName;
    CommPortIdentifier portIdentifier;
    try {
        portIdentifier = CommPortIdentifier.getPortIdentifier(port);
        try {
            serialPort = portIdentifier.open("openhab", 3000);
            serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
            serialPort.enableReceiveThreshold(1);
            serialPort.enableReceiveTimeout(1000);
            // RXTX serial port library causes high CPU load
            // Start event listener, which will just sleep and slow down event loop
            serialPort.addEventListener(new CPUWorkaroundThread());
            serialPort.notifyOnDataAvailable(true);
            inputStream = new DataInputStream(new BufferedInputStream(serialPort.getInputStream()));
            outputStream = serialPort.getOutputStream();
            ComfoAirCommand command = ComfoAirCommandType.getChangeCommand(ComfoAirCommandType.ACTIVATE.key, new DecimalType(1));
            sendCommand(command);
        } catch (PortInUseException e) {
            throw new InitializationException(e);
        } catch (UnsupportedCommOperationException e) {
            throw new InitializationException(e);
        } catch (IOException e) {
            throw new InitializationException(e);
        } catch (TooManyListenersException e) {
            throw new InitializationException(e);
        }
    } catch (NoSuchPortException e) {
        StringBuilder sb = new StringBuilder();
        @SuppressWarnings("rawtypes") Enumeration portList = CommPortIdentifier.getPortIdentifiers();
        while (portList.hasMoreElements()) {
            CommPortIdentifier id = (CommPortIdentifier) portList.nextElement();
            if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                sb.append(id.getName() + "\n");
            }
        }
        throw new InitializationException("Serial port '" + port + "' could not be found. Available ports are:\n" + sb.toString());
    }
}
Also used : UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) Enumeration(java.util.Enumeration) CommPortIdentifier(gnu.io.CommPortIdentifier) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) InitializationException(org.openhab.binding.comfoair.internal.InitializationException) TooManyListenersException(java.util.TooManyListenersException) PortInUseException(gnu.io.PortInUseException) NoSuchPortException(gnu.io.NoSuchPortException) BufferedInputStream(java.io.BufferedInputStream) DecimalType(org.openhab.core.library.types.DecimalType)

Example 43 with DecimalType

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

the class ComfoAirConnector method close.

/**
     * Close the serial port.
     */
public void close() {
    logger.debug("Close ComfoAir connection");
    ComfoAirCommand command = ComfoAirCommandType.getChangeCommand(ComfoAirCommandType.ACTIVATE.key, new DecimalType(0));
    sendCommand(command);
    IOUtils.closeQuietly(inputStream);
    IOUtils.closeQuietly(outputStream);
    serialPort.close();
}
Also used : DecimalType(org.openhab.core.library.types.DecimalType)

Example 44 with DecimalType

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

the class BenqProjectorBinding method sendCommandToProjector.

/**
     * Send the command to the projector via configured transport and return the
     * response string
     *
     * @param cfg
     *            Item binding configuration
     * @param c
     *            command to be sent
     * @return Response string from projector
     */
private String sendCommandToProjector(BenqProjectorBindingConfig cfg, Command c) {
    Boolean cmdSent = false;
    String response = "";
    switch(cfg.mode) {
        case POWER:
        case MUTE:
            if (c instanceof OnOffType) {
                if ((OnOffType) c == OnOffType.ON) {
                    response = transport.sendCommandExpectResponse(cfg.mode.getItemModeCommandSetString("ON"));
                    cmdSent = true;
                } else if ((OnOffType) c == OnOffType.OFF) {
                    response = transport.sendCommandExpectResponse(cfg.mode.getItemModeCommandSetString("OFF"));
                    cmdSent = true;
                }
            }
            break;
        case VOLUME:
            if (c instanceof DecimalType) {
                /* get current volume */
                State currentVolState = queryProjector(cfg);
                int currentVol = ((DecimalType) currentVolState).intValue();
                int volLevel = ((DecimalType) c).intValue();
                if (volLevel > this.MAX_VOLUME) {
                    volLevel = this.MAX_VOLUME;
                } else if (volLevel < this.MIN_VOLUME) {
                    volLevel = this.MIN_VOLUME;
                }
                if (currentVol == volLevel) {
                    cmdSent = true;
                }
                while (currentVol != volLevel) {
                    if (currentVol < volLevel) {
                        transport.sendCommandExpectResponse(cfg.mode.getItemModeCommandSetString("+"));
                        currentVol++;
                        cmdSent = true;
                    } else {
                        transport.sendCommandExpectResponse(cfg.mode.getItemModeCommandSetString("-"));
                        currentVol--;
                        cmdSent = true;
                    }
                }
            } else if (c instanceof IncreaseDecreaseType) {
                if ((IncreaseDecreaseType) c == IncreaseDecreaseType.INCREASE) {
                    transport.sendCommandExpectResponse(cfg.mode.getItemModeCommandSetString("+"));
                    cmdSent = true;
                } else if ((IncreaseDecreaseType) c == IncreaseDecreaseType.DECREASE) {
                    transport.sendCommandExpectResponse(cfg.mode.getItemModeCommandSetString("-"));
                    cmdSent = true;
                }
            }
            /* get final volume */
            response = transport.sendCommandExpectResponse(cfg.mode.getItemModeCommandQueryString());
            break;
        case LAMP_HOURS:
            logger.warn("Cannot send command to set lamp hours - not a valid operation!");
            break;
        case SOURCE_NUMBER:
            if (c instanceof DecimalType) {
                DecimalType sourceIdx = (DecimalType) c;
                String cmd = BenqProjectorSourceMapping.getStringFromMapping(sourceIdx.intValue());
                if (cmd.isEmpty() == false) {
                    response = transport.sendCommandExpectResponse(cfg.mode.getItemModeCommandSetString(cmd));
                    cmdSent = true;
                }
            }
            break;
        case SOURCE_STRING:
            if (c instanceof StringType) {
                StringType sourceStr = (StringType) c;
                int mappingIdx = BenqProjectorSourceMapping.getMappingFromString(sourceStr.toString());
                if (// double check this is a valid mapping
                mappingIdx != -1) {
                    response = transport.sendCommandExpectResponse(cfg.mode.getItemModeCommandSetString(sourceStr.toString()));
                    cmdSent = true;
                }
            }
            break;
        default:
            logger.error("Unexpected Item Mode!");
            break;
    }
    if (cmdSent == false) {
        logger.error("Unable to convert item command to projector state: Command=" + c);
    }
    return response;
}
Also used : OnOffType(org.openhab.core.library.types.OnOffType) StringType(org.openhab.core.library.types.StringType) State(org.openhab.core.types.State) DecimalType(org.openhab.core.library.types.DecimalType) IncreaseDecreaseType(org.openhab.core.library.types.IncreaseDecreaseType)

Example 45 with DecimalType

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

the class BluetoothBinding method handleAllDevicesInRange.

/**
     * {@inheritDoc}
     */
@Override
public void handleAllDevicesInRange(Iterable<BluetoothDevice> devices) {
    if (eventPublisher != null) {
        // build a comma separated list of all devices in range
        StringBuilder authSb = new StringBuilder();
        StringBuilder unauthSb = new StringBuilder();
        int noOfAuthDevices = 0;
        int noOfUnauthDevices = 0;
        for (BluetoothDevice device : devices) {
            handleDeviceInRange(device);
            if (device.isPaired()) {
                authSb.append(getName(device));
                authSb.append(", ");
                noOfAuthDevices++;
            } else {
                unauthSb.append(getName(device));
                unauthSb.append(", ");
                noOfUnauthDevices++;
            }
        }
        String authDeviceList = authSb.length() > 0 ? authSb.substring(0, authSb.length() - 2) : "";
        String unauthDeviceList = unauthSb.length() > 0 ? unauthSb.substring(0, unauthSb.length() - 2) : "";
        String allDeviceList = unauthDeviceList.isEmpty() ? authDeviceList : authSb.append(unauthDeviceList).toString();
        for (String itemName : authStringItems.values()) {
            eventPublisher.postUpdate(itemName, StringType.valueOf(authDeviceList));
        }
        for (String itemName : unauthStringItems.values()) {
            eventPublisher.postUpdate(itemName, StringType.valueOf(unauthDeviceList));
        }
        for (String itemName : allStringItems.values()) {
            eventPublisher.postUpdate(itemName, StringType.valueOf(allDeviceList));
        }
        for (String itemName : authMeasurementItems.values()) {
            eventPublisher.postUpdate(itemName, new DecimalType(noOfAuthDevices));
        }
        for (String itemName : unauthMeasurementItems.values()) {
            eventPublisher.postUpdate(itemName, new DecimalType(noOfUnauthDevices));
        }
        for (String itemName : allMeasurementItems.values()) {
            eventPublisher.postUpdate(itemName, new DecimalType(noOfAuthDevices + noOfUnauthDevices));
        }
    }
}
Also used : DecimalType(org.openhab.core.library.types.DecimalType)

Aggregations

DecimalType (org.openhab.core.library.types.DecimalType)222 Test (org.junit.Test)70 StringType (org.openhab.core.library.types.StringType)69 OnOffType (org.openhab.core.library.types.OnOffType)63 PercentType (org.openhab.core.library.types.PercentType)43 State (org.openhab.core.types.State)40 Type (org.openhab.core.types.Type)39 NumberItem (org.openhab.core.library.items.NumberItem)38 SHCMessage (org.openhab.binding.smarthomatic.internal.SHCMessage)37 DateTimeType (org.openhab.core.library.types.DateTimeType)29 BigDecimal (java.math.BigDecimal)23 Calendar (java.util.Calendar)23 HSBType (org.openhab.core.library.types.HSBType)20 SwitchItem (org.openhab.core.library.items.SwitchItem)18 IOException (java.io.IOException)16 StringItem (org.openhab.core.library.items.StringItem)16 ConfigurationException (org.osgi.service.cm.ConfigurationException)16 RollershutterItem (org.openhab.core.library.items.RollershutterItem)15 ContactItem (org.openhab.core.library.items.ContactItem)14 DimmerItem (org.openhab.core.library.items.DimmerItem)14