Search in sources :

Example 71 with StringType

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

the class PLCLogoBinding method createState.

private State createState(Item item, Object value) {
    DecimalType number = null;
    if (value instanceof Number) {
        number = new DecimalType(value.toString());
    }
    State state = null;
    if (item instanceof StringType) {
        state = new StringType((String) value);
    } else if (item instanceof NumberItem) {
        if (number != null) {
            return number;
        } else if (value instanceof String) {
            state = new DecimalType(((String) value).replaceAll("[^\\d|.]", ""));
        }
    } else if (item instanceof SwitchItem && (number != null)) {
        state = (number.intValue() > 0) ? OnOffType.ON : OnOffType.OFF;
    } else if (item instanceof ContactItem && (number != null)) {
        state = (number.intValue() > 0) ? OpenClosedType.CLOSED : OpenClosedType.OPEN;
    }
    return state;
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) StringType(org.openhab.core.library.types.StringType) State(org.openhab.core.types.State) ContactItem(org.openhab.core.library.items.ContactItem) DecimalType(org.openhab.core.library.types.DecimalType) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 72 with StringType

use of org.openhab.core.library.types.StringType 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 73 with StringType

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

the class SysteminfoBinding method getData.

private State getData(SysteminfoCommandType commandType, Class<? extends Item> itemType, String target) {
    State state = UnDefType.UNDEF;
    long pid;
    try {
        switch(commandType) {
            case LOAD_AVERAGE_1MIN:
                state = new DecimalType(sigar.getLoadAverage()[0]);
                break;
            case LOAD_AVERAGE_5MIN:
                state = new DecimalType(sigar.getLoadAverage()[1]);
                break;
            case LOAD_AVERAGE_15MIN:
                state = new DecimalType(sigar.getLoadAverage()[2]);
                break;
            case CPU_COMBINED:
                state = new DecimalType(sigar.getCpuPerc().getCombined() * 100);
                break;
            case CPU_USER:
                state = new DecimalType(sigar.getCpuPerc().getUser() * 100);
                break;
            case CPU_SYSTEM:
                state = new DecimalType(sigar.getCpuPerc().getSys() * 100);
                break;
            case CPU_NICE:
                state = new DecimalType(sigar.getCpuPerc().getNice() * 100);
                break;
            case CPU_WAIT:
                state = new DecimalType(sigar.getCpuPerc().getWait() * 100);
                break;
            case UPTIME:
                state = new DecimalType(sigar.getUptime().getUptime());
                break;
            case UPTIME_FORMATTED:
                state = new StringType(getElapsedTime((long) sigar.getUptime().getUptime()));
                break;
            case MEM_FREE_PERCENT:
                state = new DecimalType(sigar.getMem().getFreePercent());
                break;
            case MEM_USED_PERCENT:
                state = new DecimalType(sigar.getMem().getUsedPercent());
                break;
            case MEM_FREE:
                state = new DecimalType(formatBytes(sigar.getMem().getFree(), units));
                break;
            case MEM_USED:
                state = new DecimalType(formatBytes(sigar.getMem().getUsed(), units));
                break;
            case MEM_ACTUAL_FREE:
                state = new DecimalType(formatBytes(sigar.getMem().getActualFree(), units));
                break;
            case MEM_ACTUAL_USED:
                state = new DecimalType(formatBytes(sigar.getMem().getActualUsed(), units));
                break;
            case MEM_TOTAL:
                state = new DecimalType(formatBytes(sigar.getMem().getTotal(), units));
                break;
            case SWAP_FREE:
                state = new DecimalType(formatBytes(sigar.getSwap().getFree(), units));
                break;
            case SWAP_TOTAL:
                state = new DecimalType(formatBytes(sigar.getSwap().getTotal(), units));
                break;
            case SWAP_USED:
                state = new DecimalType(formatBytes(sigar.getSwap().getUsed(), units));
                break;
            case SWAP_PAGE_IN:
                state = new DecimalType(formatBytes(sigar.getSwap().getPageIn(), units));
                break;
            case SWAP_PAGE_OUT:
                state = new DecimalType(formatBytes(sigar.getSwap().getPageOut(), units));
                break;
            case NET_RX_BYTES:
                state = new DecimalType(formatBytes(sigar.getNetInterfaceStat(target).getRxBytes(), units));
                break;
            case NET_TX_BYTES:
                state = new DecimalType(formatBytes(sigar.getNetInterfaceStat(target).getTxBytes(), units));
                break;
            case DISK_READS:
                state = new DecimalType(sigar.getDiskUsage(target).getReads());
                break;
            case DISK_WRITES:
                state = new DecimalType(sigar.getDiskUsage(target).getWrites());
                break;
            case DISK_READ_BYTES:
                state = new DecimalType(formatBytes(sigar.getDiskUsage(target).getReadBytes(), units));
                break;
            case DISK_WRITE_BYTES:
                state = new DecimalType(formatBytes(sigar.getDiskUsage(target).getWriteBytes(), units));
                break;
            case FS_USED:
                state = new DecimalType(formatBytes(sigar.getFileSystemUsage(target).getUsed() * 1024, units));
                break;
            case FS_FREE:
                state = new DecimalType(formatBytes(sigar.getFileSystemUsage(target).getFree() * 1024, units));
                break;
            case FS_TOTAL:
                state = new DecimalType(formatBytes(sigar.getFileSystemUsage(target).getTotal() * 1024, units));
                break;
            case FS_USE_PERCENT:
                state = new DecimalType(sigar.getFileSystemUsage(target).getUsePercent() * 100);
                break;
            case FS_FILES:
                state = new DecimalType(sigar.getFileSystemUsage(target).getFiles());
                break;
            case FS_FREE_FILES:
                state = new DecimalType(sigar.getFileSystemUsage(target).getFreeFiles());
                break;
            case DIR_USAGE:
                state = new DecimalType(formatBytes(sigar.getDirUsage(target).getDiskUsage(), units));
                break;
            case DIR_FILES:
                state = new DecimalType(sigar.getDirUsage(target).getFiles());
                break;
            case PROCESS_REAL_MEM:
                pid = getPid(target);
                state = new DecimalType(formatBytes(sigar.getProcMem(pid).getResident(), units));
                break;
            case PROCESS_VIRTUAL_MEM:
                pid = getPid(target);
                state = new DecimalType(formatBytes(sigar.getProcMem(pid).getSize(), units));
                break;
            case PROCESS_CPU_PERCENT:
                pid = getPid(target);
                state = new DecimalType(sigar.getProcCpu(pid).getPercent() * 100);
                break;
            case PROCESS_CPU_SYSTEM:
                pid = getPid(target);
                state = new DecimalType(sigar.getProcCpu(pid).getSys());
                break;
            case PROCESS_CPU_USER:
                pid = getPid(target);
                state = new DecimalType(sigar.getProcCpu(pid).getUser());
                break;
            case PROCESS_CPU_TOTAL:
                pid = getPid(target);
                state = new DecimalType(sigar.getProcCpu(pid).getTotal());
                break;
            case PROCESS_UPTIME:
                pid = getPid(target);
                state = new DecimalType(getProcessUptime(pid));
                break;
            case PROCESS_UPTIME_FORMATTED:
                pid = getPid(target);
                state = new StringType(getElapsedTime(getProcessUptime(pid)));
                break;
            default:
                break;
        }
    } catch (SigarException e) {
        logger.error("Error occured while reading KPI's", e);
    }
    return state;
}
Also used : SigarException(org.hyperic.sigar.SigarException) StringType(org.openhab.core.library.types.StringType) State(org.openhab.core.types.State) DecimalType(org.openhab.core.library.types.DecimalType)

Example 74 with StringType

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

the class InfluxDBPersistenceService method objectToState.

/**
   * Converts a value to a {@link State} which is suitable for the given {@link Item}. This is
   * needed for querying a {@link HistoricState}.
   * 
   * @param value to be converted to a {@link State}
   * @param itemName name of the {@link Item} to get the {@link State} for
   * @return the state of the item represented by the itemName parameter, 
   *         else the string value of the Object parameter
   */
private State objectToState(Object value, String itemName) {
    String valueStr = String.valueOf(value);
    if (itemRegistry != null) {
        try {
            Item item = itemRegistry.getItem(itemName);
            if (item instanceof NumberItem) {
                return new DecimalType(valueStr);
            } else if (item instanceof ColorItem) {
                return new HSBType(valueStr);
            } else if (item instanceof DimmerItem) {
                return new PercentType(valueStr);
            } else if (item instanceof SwitchItem) {
                return string2DigitalValue(valueStr).equals(DIGITAL_VALUE_OFF) ? OnOffType.OFF : OnOffType.ON;
            } else if (item instanceof ContactItem) {
                return (string2DigitalValue(valueStr).equals(DIGITAL_VALUE_OFF)) ? OpenClosedType.CLOSED : OpenClosedType.OPEN;
            } else if (item instanceof RollershutterItem) {
                return new PercentType(valueStr);
            } else if (item instanceof DateTimeItem) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(new BigDecimal(valueStr).longValue());
                return new DateTimeType(calendar);
            } else {
                return new StringType(valueStr);
            }
        } catch (ItemNotFoundException e) {
            logger.warn("Could not find item '{}' in registry", itemName);
        }
    }
    // just return a StringType as a fallback
    return new StringType(valueStr);
}
Also used : StringType(org.openhab.core.library.types.StringType) ContactItem(org.openhab.core.library.items.ContactItem) Calendar(java.util.Calendar) ColorItem(org.openhab.core.library.items.ColorItem) PercentType(org.openhab.core.library.types.PercentType) DateTimeItem(org.openhab.core.library.items.DateTimeItem) BigDecimal(java.math.BigDecimal) NumberItem(org.openhab.core.library.items.NumberItem) DimmerItem(org.openhab.core.library.items.DimmerItem) SwitchItem(org.openhab.core.library.items.SwitchItem) ColorItem(org.openhab.core.library.items.ColorItem) DateTimeItem(org.openhab.core.library.items.DateTimeItem) HistoricItem(org.openhab.core.persistence.HistoricItem) NumberItem(org.openhab.core.library.items.NumberItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) Item(org.openhab.core.items.Item) ContactItem(org.openhab.core.library.items.ContactItem) DateTimeType(org.openhab.core.library.types.DateTimeType) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) DecimalType(org.openhab.core.library.types.DecimalType) HSBType(org.openhab.core.library.types.HSBType) SwitchItem(org.openhab.core.library.items.SwitchItem) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException)

Example 75 with StringType

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

the class AbstractDynamoDBItemSerializationTest method testDateTimeTypeWithStringItem.

@Test
public void testDateTimeTypeWithStringItem() throws IOException {
    Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
    calendar.set(2016, Calendar.MAY, 1, 13, 46, 0);
    calendar.set(Calendar.MILLISECOND, 50);
    DynamoDBItem<?> dbitem = testStateGeneric(new DateTimeType(calendar), "2016-05-01T13:46:00.050Z");
    testAsHistoricGeneric(dbitem, new StringItem("foo"), new StringType("2016-05-01T13:46:00.050Z"));
}
Also used : DateTimeType(org.openhab.core.library.types.DateTimeType) StringType(org.openhab.core.library.types.StringType) Calendar(java.util.Calendar) StringItem(org.openhab.core.library.items.StringItem) Test(org.junit.Test)

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