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