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;
}
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);
}
}
}
}
}
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;
}
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);
}
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"));
}
Aggregations