Search in sources :

Example 31 with StringType

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

the class KM200Comm method sendVirtualState.

/**
     * This function sets the state of a virtual service
     *
     */
public String sendVirtualState(KM200CommObject object, Class<? extends Item> itemType, String service, Command command) {
    String dataToSend = null;
    String type = null;
    logger.debug("Check virtual state of: {} type: {} item: {}", service, type, itemType.getName());
    object = device.serviceMap.get(service);
    KM200CommObject parObject = device.serviceMap.get(object.getParent());
    type = object.getServiceType();
    /* Binding is a StringItem */
    if (itemType.isAssignableFrom(StringItem.class)) {
        String val = ((StringType) command).toString();
        switch(type) {
            case "switchProgram":
                KM200SwitchProgramService sPService = ((KM200SwitchProgramService) parObject.getValueParameter());
                String[] servicePath = service.split("/");
                String virtService = servicePath[servicePath.length - 1];
                if (virtService.equals("weekday")) {
                    /* Only parameter changing without communication to device */
                    sPService.setActiveDay(val);
                }
                break;
        }
    /* Binding is a NumberItem */
    } else if (itemType.isAssignableFrom(NumberItem.class)) {
        Integer val = ((DecimalType) command).intValue();
        switch(type) {
            case "switchProgram":
                KM200SwitchProgramService sPService = ((KM200SwitchProgramService) parObject.getValueParameter());
                String[] servicePath = service.split("/");
                String virtService = servicePath[servicePath.length - 1];
                if (virtService.equals("cycle")) {
                    /* Only parameter changing without communication to device */
                    sPService.setActiveCycle(val);
                } else if (virtService.equals(sPService.getPositiveSwitch())) {
                    sPService.setActivePositiveSwitch(val);
                    /* Create a JSON Array from current switch configuration */
                    dataToSend = sPService.getUpdatedJSONData(parObject);
                } else if (virtService.equals(sPService.getNegativeSwitch())) {
                    sPService.setActiveNegativeSwitch(val);
                    /* Create a JSON Array from current switch configuration */
                    dataToSend = sPService.getUpdatedJSONData(parObject);
                }
                break;
            case "errorList":
                KM200ErrorService eService = ((KM200ErrorService) device.serviceMap.get(object.getParent()).getValueParameter());
                String[] nServicePath = service.split("/");
                String nVirtService = nServicePath[nServicePath.length - 1];
                if (nVirtService.equals("error")) {
                    /* Only parameter changing without communication to device */
                    eService.setActiveError(val);
                }
                break;
        }
    } else if (itemType.isAssignableFrom(DateTimeItem.class)) {
        Calendar cal = ((DateTimeType) command).getCalendar();
        KM200SwitchProgramService sPService = ((KM200SwitchProgramService) parObject.getValueParameter());
        String[] servicePath = service.split("/");
        String virtService = servicePath[servicePath.length - 1];
        Integer minutes;
        if (virtService.equals(sPService.getPositiveSwitch())) {
            minutes = cal.get(Calendar.HOUR_OF_DAY) * 60 + cal.get(Calendar.MINUTE);
            minutes = (minutes % sPService.getSwitchPointTimeRaster()) * sPService.getSwitchPointTimeRaster();
            sPService.setActivePositiveSwitch(minutes);
            /* Create a JSON Array from current switch configuration */
            dataToSend = sPService.getUpdatedJSONData(parObject);
        }
        if (virtService.equals(sPService.getNegativeSwitch())) {
            minutes = cal.get(Calendar.HOUR_OF_DAY) * 60 + cal.get(Calendar.MINUTE);
            minutes = (minutes % sPService.getSwitchPointTimeRaster()) * sPService.getSwitchPointTimeRaster();
            sPService.setActiveNegativeSwitch(minutes);
            /* Create a JSON Array from current switch configuration */
            dataToSend = sPService.getUpdatedJSONData(parObject);
        }
    }
    return dataToSend;
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) DateTimeType(org.openhab.core.library.types.DateTimeType) StringType(org.openhab.core.library.types.StringType) Calendar(java.util.Calendar)

Example 32 with StringType

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

the class JointSpaceBinding method updateItemState.

/**
     * Polls the TV for the values specified in @see tvCommand and posts state
     * update for @see itemName Currently only the following commands are
     * available - "ambilight[...]" returning a HSBType state for the given
     * ambilight pixel specified in [...] - "volume" returning a DecimalType -
     * "volume.mute" returning 'On' or 'Off' - "source" returning a String with
     * selected source (e.g. "hdmi1", "tv", etc)
     *
     * @param itemName
     * @param tvCommand
     */
private void updateItemState(String itemName, String tvCommand) {
    if (tvCommand.contains("ambilight")) {
        String[] layer = command2LayerString(tvCommand);
        HSBType state = new HSBType(getAmbilightColor(ip + ":" + port, layer));
        eventPublisher.postUpdate(itemName, state);
    } else if (tvCommand.contains("volume")) {
        if (tvCommand.contains("mute")) {
            eventPublisher.postUpdate(itemName, getTVVolume(ip + ":" + port).mute ? OnOffType.ON : OnOffType.OFF);
        } else {
            eventPublisher.postUpdate(itemName, new DecimalType(getTVVolume(ip + ":" + port).volume));
        }
    } else if (tvCommand.contains("source")) {
        eventPublisher.postUpdate(itemName, new StringType(getSource(ip + ":" + port)));
    } else {
        logger.error("Could not parse item state\"" + tvCommand + "\" for polling");
        return;
    }
}
Also used : StringType(org.openhab.core.library.types.StringType) DecimalType(org.openhab.core.library.types.DecimalType) HSBType(org.openhab.core.library.types.HSBType)

Example 33 with StringType

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

the class SerialDevice method serialEvent.

@Override
public void serialEvent(SerialPortEvent event) {
    switch(event.getEventType()) {
        case SerialPortEvent.BI:
        case SerialPortEvent.OE:
        case SerialPortEvent.FE:
        case SerialPortEvent.PE:
        case SerialPortEvent.CD:
        case SerialPortEvent.CTS:
        case SerialPortEvent.DSR:
        case SerialPortEvent.RI:
        case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
            break;
        case SerialPortEvent.DATA_AVAILABLE:
            // we get here if data has been received
            StringBuilder sb = new StringBuilder();
            byte[] readBuffer = new byte[20];
            try {
                do {
                    // read data from serial device
                    while (inputStream.available() > 0) {
                        int bytes = inputStream.read(readBuffer);
                        sb.append(new String(readBuffer, 0, bytes));
                    }
                    try {
                        // add wait states around reading the stream, so that interrupted transmissions are merged
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                    // ignore interruption
                    }
                } while (inputStream.available() > 0);
                // sent data
                String result = sb.toString();
                // send data to the bus
                logger.debug("Received message '{}' on serial port {}", new String[] { result, port });
                if (eventPublisher != null) {
                    if (configMap != null && !configMap.isEmpty()) {
                        for (Entry<String, ItemType> entry : configMap.entrySet()) {
                            // use pattern
                            if (entry.getValue().pattern != null) {
                                if (transformationService == null) {
                                    logger.error("No transformation service available!");
                                } else {
                                    try {
                                        String value = transformationService.transform(entry.getValue().pattern, result);
                                        if (entry.getValue().type.equals(NumberItem.class)) {
                                            try {
                                                eventPublisher.postUpdate(entry.getKey(), new DecimalType(value));
                                            } catch (NumberFormatException e) {
                                                logger.warn("Unable to convert regex result '{}' for item {} to number", new String[] { result, entry.getKey() });
                                            }
                                        } else {
                                            eventPublisher.postUpdate(entry.getKey(), new StringType(value));
                                        }
                                    } catch (TransformationException e) {
                                        logger.error("Unable to transform!", e);
                                    }
                                }
                            } else if (entry.getValue().type == StringItem.class) {
                                if (entry.getValue().base64) {
                                    result = Base64.encodeBase64String(result.getBytes());
                                }
                                eventPublisher.postUpdate(entry.getKey(), new StringType(result));
                            } else if (entry.getValue().type == SwitchItem.class && result.trim().isEmpty()) {
                                eventPublisher.postUpdate(entry.getKey(), OnOffType.ON);
                                eventPublisher.postUpdate(entry.getKey(), OnOffType.OFF);
                            }
                        }
                    }
                }
            } catch (IOException e) {
                logger.debug("Error receiving data on serial port {}: {}", new String[] { port, e.getMessage() });
            }
            break;
    }
}
Also used : TransformationException(org.openhab.core.transform.TransformationException) StringType(org.openhab.core.library.types.StringType) IOException(java.io.IOException) StringItem(org.openhab.core.library.items.StringItem) DecimalType(org.openhab.core.library.types.DecimalType) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 34 with StringType

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

the class JpaHistoricItem method fromPersistedItem.

/**
     * Converts the string value of the persisted item to the state of a HistoricItem.
     * 
     * @param pItem the persisted JpaPersistentItem
     * @param item the source reference Item
     * @return historic item
     */
public static HistoricItem fromPersistedItem(JpaPersistentItem pItem, Item item) {
    State state;
    if (item instanceof NumberItem) {
        state = new DecimalType(Double.valueOf(pItem.getValue()));
    } else if (item instanceof DimmerItem) {
        state = new PercentType(Integer.valueOf(pItem.getValue()));
    } else if (item instanceof SwitchItem) {
        state = OnOffType.valueOf(pItem.getValue());
    } else if (item instanceof ContactItem) {
        state = OpenClosedType.valueOf(pItem.getValue());
    } else if (item instanceof RollershutterItem) {
        state = PercentType.valueOf(pItem.getValue());
    } else if (item instanceof ColorItem) {
        state = new HSBType(pItem.getValue());
    } else if (item instanceof DateTimeItem) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(Long.valueOf(pItem.getValue())));
        state = new DateTimeType(cal);
    } else if (item instanceof LocationItem) {
        PointType pType = null;
        String[] comps = pItem.getValue().split(";");
        if (comps.length >= 2) {
            pType = new PointType(new DecimalType(comps[0]), new DecimalType(comps[1]));
            if (comps.length == 3) {
                pType.setAltitude(new DecimalType(comps[2]));
            }
        }
        state = pType;
    } else if (item instanceof CallItem) {
        state = new CallType(pItem.getValue());
    } else {
        state = new StringType(pItem.getValue());
    }
    return new JpaHistoricItem(item.getName(), state, pItem.getTimestamp());
}
Also used : LocationItem(org.openhab.core.library.items.LocationItem) StringType(org.openhab.core.library.types.StringType) ContactItem(org.openhab.core.library.items.ContactItem) Calendar(java.util.Calendar) CallType(org.openhab.library.tel.types.CallType) ColorItem(org.openhab.core.library.items.ColorItem) PercentType(org.openhab.core.library.types.PercentType) DateTimeItem(org.openhab.core.library.items.DateTimeItem) Date(java.util.Date) NumberItem(org.openhab.core.library.items.NumberItem) DateTimeType(org.openhab.core.library.types.DateTimeType) State(org.openhab.core.types.State) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) DecimalType(org.openhab.core.library.types.DecimalType) PointType(org.openhab.core.library.types.PointType) CallItem(org.openhab.library.tel.items.CallItem) HSBType(org.openhab.core.library.types.HSBType) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 35 with StringType

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

the class AbstractDynamoDBItem method asHistoricItem.

/*
     * (non-Javadoc)
     *
     * @see org.openhab.persistence.dynamodb.internal.DynamoItem#asHistoricItem(org.openhab.core.items.Item)
     */
@Override
public HistoricItem asHistoricItem(final Item item) {
    final State[] state = new State[1];
    accept(new DynamoDBItemVisitor() {

        @Override
        public void visit(DynamoDBStringItem dynamoStringItem) {
            if (item instanceof ColorItem) {
                state[0] = new HSBType(dynamoStringItem.getState());
            } else if (item instanceof LocationItem) {
                state[0] = new PointType(dynamoStringItem.getState());
            } else if (item instanceof DateTimeItem) {
                Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
                try {
                    cal.setTime(DATEFORMATTER.parse(dynamoStringItem.getState()));
                } catch (ParseException e) {
                    LOGGER.error("Failed to parse {} as date. Outputting UNDEF instead", dynamoStringItem.getState());
                    state[0] = UnDefType.UNDEF;
                }
                state[0] = new DateTimeType(cal);
            } else if (dynamoStringItem.getState().equals(UNDEFINED_PLACEHOLDER)) {
                state[0] = UnDefType.UNDEF;
            } else if (item instanceof CallItem) {
                String parts = dynamoStringItem.getState();
                String[] strings = parts.split("##");
                String dest = strings[0];
                String orig = strings[1];
                state[0] = new CallType(orig, dest);
            } else {
                state[0] = new StringType(dynamoStringItem.getState());
            }
        }

        @Override
        public void visit(DynamoDBBigDecimalItem dynamoBigDecimalItem) {
            if (item instanceof NumberItem) {
                state[0] = new DecimalType(dynamoBigDecimalItem.getState());
            } else if (item instanceof DimmerItem) {
                state[0] = new PercentType(dynamoBigDecimalItem.getState());
            } else if (item instanceof SwitchItem) {
                state[0] = dynamoBigDecimalItem.getState().compareTo(BigDecimal.ONE) == 0 ? OnOffType.ON : OnOffType.OFF;
            } else if (item instanceof ContactItem) {
                state[0] = dynamoBigDecimalItem.getState().compareTo(BigDecimal.ONE) == 0 ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
            } else if (item instanceof RollershutterItem) {
                state[0] = new PercentType(dynamoBigDecimalItem.getState());
            } else {
                LOGGER.warn("Not sure how to convert big decimal item {} to type {}. Using StringType as fallback", dynamoBigDecimalItem.getName(), item.getClass());
                state[0] = new StringType(dynamoBigDecimalItem.getState().toString());
            }
        }
    });
    return new DynamoDBHistoricItem(getName(), state[0], getTime());
}
Also used : LocationItem(org.openhab.core.library.items.LocationItem) StringType(org.openhab.core.library.types.StringType) CallType(org.openhab.library.tel.types.CallType) ColorItem(org.openhab.core.library.items.ColorItem) DateTimeItem(org.openhab.core.library.items.DateTimeItem) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) HSBType(org.openhab.core.library.types.HSBType) SwitchItem(org.openhab.core.library.items.SwitchItem) Calendar(java.util.Calendar) ContactItem(org.openhab.core.library.items.ContactItem) PercentType(org.openhab.core.library.types.PercentType) NumberItem(org.openhab.core.library.items.NumberItem) DateTimeType(org.openhab.core.library.types.DateTimeType) State(org.openhab.core.types.State) PointType(org.openhab.core.library.types.PointType) DecimalType(org.openhab.core.library.types.DecimalType) CallItem(org.openhab.library.tel.items.CallItem) ParseException(java.text.ParseException)

Aggregations

StringType (org.openhab.core.library.types.StringType)90 DecimalType (org.openhab.core.library.types.DecimalType)69 State (org.openhab.core.types.State)30 DateTimeType (org.openhab.core.library.types.DateTimeType)28 PercentType (org.openhab.core.library.types.PercentType)27 NumberItem (org.openhab.core.library.items.NumberItem)25 Calendar (java.util.Calendar)23 StringItem (org.openhab.core.library.items.StringItem)18 OnOffType (org.openhab.core.library.types.OnOffType)15 SwitchItem (org.openhab.core.library.items.SwitchItem)12 ContactItem (org.openhab.core.library.items.ContactItem)10 DimmerItem (org.openhab.core.library.items.DimmerItem)10 RollershutterItem (org.openhab.core.library.items.RollershutterItem)10 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)8 DateTimeItem (org.openhab.core.library.items.DateTimeItem)8 HSBType (org.openhab.core.library.types.HSBType)8 ConfigurationException (org.osgi.service.cm.ConfigurationException)8 IOException (java.io.IOException)7 BigDecimal (java.math.BigDecimal)7