use of org.openhab.core.library.types.StringType in project openhab1-addons by openhab.
the class MiosBindingConfig method transformIn.
/**
* Transform data from a MiOS Unit into a form suitable for use in an openHAB Item.
* <p>
*
* Data received from a MiOS unit is in a number of different formats (String, Boolean, DataTime, etc). These values
* may need to be transformed from their original format prior to being pushed into the corresponding openHAB Item.
* <p>
* This method is used internally within the MiOS Binding to perform that transformation.
* <p>
* This method is responsible for transforming the inbound value from the MiOS Unit, into the form required by the
* openHAB Item.
* <p>
* metadata supplied by the user, via the <code>in:</code> parameter, in the Binding Configuration is used to define
* the transformation that must be performed.
* <p>
* If the <code>in:</code> parameter is missing, then no transformation will occur, and the source-value will be
* returned (as a <code>StringType</code>).
* <p>
* If the <code>in:</code> parameter is present, then its value is used to determine which openHAB
* TransformationService should be used to transform the value.
*
* @return the transformed value, or the input (<code>value</code>) if no transformation has been specified in the
* Binding Configuration.
*
* @throws TransformationException
* if the underlying Transformation fails in any manner.
*/
public State transformIn(State value) throws TransformationException {
TransformationService ts = getInTransformationService();
if (ts != null) {
return createState(ts.transform(getInTransformParam(), value.toString()));
} else {
if (value instanceof StringType) {
value = createState(value.toString());
logger.trace("transformIn: Converted value '{}' from StringType to more scoped type '{}'", value, value.getClass());
}
return value;
}
}
use of org.openhab.core.library.types.StringType in project openhab1-addons by openhab.
the class PrimareResponse method openHabState.
/**
* Convert received response message containing a Primare device variable value
* to suitable OpenHAB state for the given itemType
*
* @param itemType
*
* @return openHAB state
*/
public State openHabState(Class<? extends Item> itemType) {
State state = UnDefType.UNDEF;
try {
int index;
String s;
if (itemType == SwitchItem.class) {
index = message[2];
state = index == 0 ? OnOffType.OFF : OnOffType.ON;
} else if (itemType == NumberItem.class) {
index = message[2];
state = new DecimalType(index);
} else if (itemType == DimmerItem.class) {
index = message[2];
state = new PercentType(index);
} else if (itemType == RollershutterItem.class) {
index = message[2];
state = new PercentType(index);
} else if (itemType == StringItem.class) {
s = new String(Arrays.copyOfRange(message, 2, message.length - 2));
state = new StringType(s);
}
} catch (Exception e) {
logger.debug("Cannot convert value '{}' to data type {}", message[1], itemType);
}
return state;
}
use of org.openhab.core.library.types.StringType in project openhab1-addons by openhab.
the class PulseaudioBinding method internalReceiveCommand.
@Override
public void internalReceiveCommand(String itemName, Command command) {
PulseaudioBindingProvider provider = findFirstMatchingBindingProvider(itemName, command);
if (provider == null) {
logger.warn("doesn't find matching binding provider [itemName={}, command={}]", itemName, command);
return;
}
String audioItemName = provider.getItemName(itemName);
String serverId = provider.getServerId(itemName);
// Item item = provider.getItem(itemName);
String paCommand = provider.getCommand(itemName);
PulseaudioCommandTypeMapping pulseaudioCommandType = null;
if (paCommand != null && !paCommand.isEmpty()) {
try {
pulseaudioCommandType = PulseaudioCommandTypeMapping.valueOf(paCommand.toUpperCase());
} catch (IllegalArgumentException e) {
logger.warn("unknown command specified for the given itemName [itemName={}, audio-item-name={}, serverId={}, command={}] => querying for values aborted!", new Object[] { itemName, audioItemName, serverId, command });
}
}
PulseaudioClient client = clients.get(serverId);
if (client == null) {
// try to reconnect if the server is configured
if (serverConfigCache.containsKey(serverId)) {
connect(serverId, serverConfigCache.get(serverId));
client = clients.get(serverId);
}
}
if (client == null) {
logger.warn("does't find matching pulseaudio client [itemName={}, serverId={}]", itemName, serverId);
return;
}
if (audioItemName != null && !audioItemName.isEmpty()) {
AbstractAudioDeviceConfig audioItem = client.getGenericAudioItem(audioItemName);
if (audioItem == null) {
logger.warn("no corresponding audio-item found [audioItemName={}]", audioItemName);
return;
}
State updateState = UnDefType.UNDEF;
if (command instanceof IncreaseDecreaseType) {
int volume = audioItem.getVolume();
logger.debug(audioItemName + " volume is " + volume);
if (command.equals(IncreaseDecreaseType.INCREASE)) {
volume = Math.min(100, volume + 5);
}
if (command.equals(IncreaseDecreaseType.DECREASE)) {
volume = Math.max(0, volume - 5);
}
logger.debug("setting " + audioItemName + " volume to " + volume);
client.setVolumePercent(audioItem, volume);
updateState = new PercentType(volume);
} else if (command instanceof PercentType) {
client.setVolumePercent(audioItem, Integer.valueOf(command.toString()));
updateState = (PercentType) command;
} else if (command instanceof DecimalType) {
if (pulseaudioCommandType == null || pulseaudioCommandType.equals(PulseaudioCommandTypeMapping.VOLUME)) {
// set volume
client.setVolume(audioItem, Integer.valueOf(command.toString()));
updateState = (DecimalType) command;
}
// all other pulseaudioCommandType's for DecimalTypes are
// read-only and
// therefore we do nothing here
} else if (command instanceof OnOffType) {
if (pulseaudioCommandType == null) {
// Default behaviour when no command is specified => mute
client.setMute(audioItem, ((OnOffType) command).equals(OnOffType.ON));
updateState = (OnOffType) command;
} else {
switch(pulseaudioCommandType) {
case EXISTS:
// we better do nothing here
break;
case MUTED:
client.setMute(audioItem, ((OnOffType) command).equals(OnOffType.ON));
updateState = (OnOffType) command;
break;
case RUNNING:
case CORKED:
case SUSPENDED:
case IDLE:
// the state of an audio-item cannot be changed
break;
case ID:
case MODULE_ID:
// changed
break;
case VOLUME:
if (((OnOffType) command).equals(OnOffType.ON)) {
// Set Volume to 100%
client.setVolume(audioItem, 100);
} else {
// set volume to 0
client.setVolume(audioItem, 100);
}
updateState = (OnOffType) command;
break;
case SLAVE_SINKS:
// also an read-only field
break;
}
}
} else if (command instanceof StringType) {
if (pulseaudioCommandType != null) {
switch(pulseaudioCommandType) {
case CORKED:
case EXISTS:
case ID:
case IDLE:
case MODULE_ID:
case MUTED:
case RUNNING:
case SUSPENDED:
case VOLUME:
// no action here
break;
case SLAVE_SINKS:
if (audioItem instanceof Sink && ((Sink) audioItem).isCombinedSink()) {
// change the slave sinks of the given combined sink
// to the new value
Sink mainSink = (Sink) audioItem;
ArrayList<Sink> slaveSinks = new ArrayList<Sink>();
for (String slaveSinkName : StringUtils.split(command.toString(), ",")) {
Sink slaveSink = client.getSink(slaveSinkName);
if (slaveSink != null) {
slaveSinks.add(slaveSink);
}
}
logger.debug(slaveSinks.size() + " slave sinks");
if (slaveSinks.size() > 0) {
client.setCombinedSinkSlaves(mainSink, slaveSinks);
}
}
break;
}
}
}
if (!updateState.equals(UnDefType.UNDEF)) {
eventPublisher.postUpdate(itemName, updateState);
}
} else if (command instanceof StringType) {
// send the command directly to the pulseaudio server
client.sendCommand(command.toString());
eventPublisher.postUpdate(itemName, (StringType) command);
}
}
use of org.openhab.core.library.types.StringType in project openhab1-addons by openhab.
the class CalDavBinding method updateItem.
private synchronized void updateItem(String itemName, CalDavConfig config, List<CalDavEvent> events) {
if (config.getType() == Type.PRESENCE) {
List<CalDavEvent> subList = getActiveEvents(events);
subList = this.removeWithMatchingPlace(subList);
if (subList.size() == 0) {
eventPublisher.sendCommand(itemName, OnOffType.OFF);
} else {
eventPublisher.sendCommand(itemName, OnOffType.ON);
}
} else {
List<CalDavEvent> subList = new ArrayList<CalDavEvent>();
if (config.getType() == Type.EVENT) {
subList = getAllEvents(events);
} else if (config.getType() == Type.ACTIVE) {
subList = getActiveEvents(events);
} else if (config.getType() == Type.UPCOMING) {
subList = getUpcomingEvents(events);
}
if (config.getEventNr() > subList.size()) {
logger.debug("no event found for {}, setting to UNDEF", itemName);
eventPublisher.postUpdate(itemName, org.openhab.core.types.UnDefType.UNDEF);
return;
}
logger.debug("found {} events for config: {}", subList.size(), config);
CalDavEvent event = subList.get(config.getEventNr() - 1);
logger.trace("found event {} for config {}", event.getShortName(), config);
State command = null;
switch(config.getValue()) {
case NAME:
command = new StringType(event.getName());
break;
case DESCRIPTION:
command = new StringType(event.getContent());
break;
case PLACE:
command = new StringType(event.getLocation());
break;
case START:
command = new DateTimeType(FORMATTER.print(event.getStart()));
break;
case END:
command = new DateTimeType(FORMATTER.print(event.getEnd()));
break;
case TIME:
String startEnd = DF.print(event.getStart()) + " - " + DF.print(event.getEnd());
command = new StringType(startEnd);
break;
case NAMEANDTIME:
String startEnd2 = DF.print(event.getStart()) + " - " + DF.print(event.getEnd());
String name = event.getName();
command = new StringType(name + " @ " + startEnd2);
}
logger.debug("sending command {} for item {}", command, itemName);
eventPublisher.postUpdate(itemName, command);
logger.trace("command {} successfully sent", command);
}
}
use of org.openhab.core.library.types.StringType 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;
}
Aggregations