use of org.openhab.core.library.types.StringType in project openhab1-addons by openhab.
the class FrontierSiliconRadioBinding method updateProvider.
/**
* Update all items of the given item provider.
*/
private void updateProvider(FrontierSiliconRadioBindingProvider provider) {
for (String itemName : provider.getItemNames()) {
// find the matching device config for this item
final String deviceId = provider.getDeviceID(itemName);
if (deviceId == null) {
logger.error("could not find deviceId of item: " + itemName);
continue;
} else {
final FrontierSiliconRadioBindingConfig deviceConf = deviceConfigCache.get(deviceId);
if (deviceConf != null) {
// get the assigned radio and its state (on or off)
final FrontierSiliconRadio radio = deviceConf.getRadio();
final boolean powerState = radio.getPower();
// get the assigned property of this item
final String property = provider.getProperty(itemName);
// if radio is OFF, set all values (except of power item) to uninitialized
if (!powerState && !"POWER".equals(property)) {
if (stateChanged(deviceId, property, null)) {
eventPublisher.postUpdate(itemName, UnDefType.UNDEF);
}
// continue with next item
continue;
}
// depending on the selected property, poll the property from the radio and update the item
switch(property) {
case "POWER":
if (stateChanged(deviceId, property, powerState)) {
logger.debug("powerState changed to " + powerState);
eventPublisher.postUpdate(itemName, powerState ? OnOffType.ON : OnOffType.OFF);
}
break;
case "MODE":
final int mode = radio.getMode();
if (stateChanged(deviceId, property, mode)) {
logger.debug("powerState changed to " + mode);
eventPublisher.postUpdate(itemName, new DecimalType(mode));
}
break;
case "VOLUME":
final int volume = radio.getVolume();
if (stateChanged(deviceId, property, volume)) {
final int percent = radio.convertVolumeToPercent(volume);
logger.debug("volume changed to " + volume + " (" + percent + "%)");
// based on the item type, either set absolue value or percent value
if (provider.getItemType(itemName) == DimmerItem.class) {
eventPublisher.postUpdate(itemName, new PercentType(percent));
} else {
eventPublisher.postUpdate(itemName, new DecimalType(volume));
}
}
break;
case "PLAYINFONAME":
final String playInfoName = radio.getPlayInfoName();
if (stateChanged(deviceId, property, playInfoName)) {
logger.debug("play info name changed to " + playInfoName);
eventPublisher.postUpdate(itemName, new StringType(playInfoName));
}
break;
case "PLAYINFOTEXT":
final String playInfoText = radio.getPlayInfoText();
if (stateChanged(deviceId, property, playInfoText)) {
logger.debug("play info text changed to " + playInfoText);
eventPublisher.postUpdate(itemName, new StringType(playInfoText));
}
break;
case "PRESET":
// preset is write-only, ignore
break;
case "MUTE":
final boolean muteState = radio.getMuted();
if (stateChanged(deviceId, property, muteState)) {
logger.debug("mute state changed to " + muteState);
eventPublisher.postUpdate(itemName, muteState ? OnOffType.ON : OnOffType.OFF);
}
break;
default:
logger.error("unknown property: '" + property + "'");
}
} else {
logger.error("deviceConf is null, no config found for deviceId: '" + deviceId + "'. Check binding config.");
}
}
}
}
use of org.openhab.core.library.types.StringType in project openhab1-addons by openhab.
the class NestBinding method createState.
/**
* Creates an openHAB {@link State} in accordance to the class of the given {@code propertyValue}. Currently
* {@link Date}, {@link BigDecimal}, {@link Temperature} and {@link Boolean} are handled explicitly. All other
* {@code dataTypes} are mapped to {@link StringType}.
* <p>
* If {@code propertyValue} is {@code null}, {@link UnDefType#NULL} will be returned.
*
* Copied/adapted from the Koubachi binding.
*
* @param propertyValue
*
* @return the new {@link State} in accordance with {@code dataType}. Will never be {@code null}.
*/
private State createState(Object propertyValue) {
if (propertyValue == null) {
return UnDefType.NULL;
}
Class<?> dataType = propertyValue.getClass();
if (Date.class.isAssignableFrom(dataType)) {
Calendar calendar = Calendar.getInstance();
calendar.setTime((Date) propertyValue);
return new DateTimeType(calendar);
} else if (Integer.class.isAssignableFrom(dataType)) {
return new DecimalType((Integer) propertyValue);
} else if (BigDecimal.class.isAssignableFrom(dataType)) {
return new DecimalType((BigDecimal) propertyValue);
} else if (Boolean.class.isAssignableFrom(dataType)) {
if ((Boolean) propertyValue) {
return OnOffType.ON;
} else {
return OnOffType.OFF;
}
} else {
return new StringType(propertyValue.toString());
}
}
use of org.openhab.core.library.types.StringType in project openhab1-addons by openhab.
the class MyqBinding method poll.
/**
* Poll for device changes
*/
private void poll() {
if (invalidCredentials || this.myqOnlineData == null) {
logger.trace("Invalid Account Credentials");
return;
}
try {
// Get myQ Data
MyqDeviceData myqStatus = myqOnlineData.getMyqData();
for (MyqBindingProvider provider : providers) {
for (String mygItemName : provider.getInBindingItemNames()) {
MyqBindingConfig deviceConfig = getConfigForItemName(mygItemName);
if (deviceConfig != null) {
MyqDevice device = myqStatus.getDevice(deviceConfig.deviceIndex);
if (device != null) {
if (device instanceof GarageDoorDevice) {
GarageDoorDevice garageopener = (GarageDoorDevice) device;
State newState = UnDefType.UNDEF;
if (!deviceConfig.attribute.isEmpty() && garageopener.hasAttribute(deviceConfig.attribute)) {
newState = TypeParser.parseState(deviceConfig.acceptedDataTypes, garageopener.getAttribute(deviceConfig.attribute));
if (newState == null) {
newState = UnDefType.UNDEF;
}
} else {
for (Class<? extends State> type : deviceConfig.acceptedDataTypes) {
if (OpenClosedType.class == type) {
if (garageopener.getStatus().isClosed()) {
newState = OpenClosedType.CLOSED;
break;
} else {
newState = OpenClosedType.OPEN;
break;
}
} else if (UpDownType.class == type) {
if (garageopener.getStatus().isClosed()) {
newState = UpDownType.DOWN;
break;
} else if (garageopener.getStatus().isOpen()) {
newState = UpDownType.UP;
break;
}
} else if (OnOffType.class == type) {
if (garageopener.getStatus().isClosed()) {
newState = OnOffType.OFF;
break;
} else {
newState = OnOffType.ON;
break;
}
} else if (PercentType.class == type) {
if (garageopener.getStatus().isClosed()) {
newState = PercentType.HUNDRED;
break;
} else if (garageopener.getStatus().isOpen()) {
newState = PercentType.ZERO;
break;
} else if (garageopener.getStatus().inMotion()) {
newState = new PercentType(50);
break;
}
} else if (StringType.class == type) {
newState = new StringType(garageopener.getStatus().getLabel());
break;
}
}
}
eventPublisher.postUpdate(mygItemName, newState);
// make sure we are polling frequently
if (garageopener.getStatus().inMotion()) {
beginRapidPoll(false);
}
} else if (device instanceof LampDevice) {
LampDevice lampDevice = (LampDevice) device;
State newState = UnDefType.UNDEF;
if (!deviceConfig.attribute.isEmpty() && lampDevice.hasAttribute(deviceConfig.attribute)) {
newState = TypeParser.parseState(deviceConfig.acceptedDataTypes, lampDevice.getAttribute(deviceConfig.attribute));
if (newState == null) {
newState = UnDefType.UNDEF;
}
} else {
for (Class<? extends State> type : deviceConfig.acceptedDataTypes) {
if (OnOffType.class == type) {
newState = lampDevice.getState();
break;
}
}
}
eventPublisher.postUpdate(mygItemName, newState);
}
}
}
}
}
} catch (InvalidLoginException e) {
logger.error("Could not log in, please check your credentials.", e);
invalidCredentials = true;
} catch (IOException e) {
logger.error("Could not connect to MyQ service", e);
}
}
use of org.openhab.core.library.types.StringType in project openhab1-addons by openhab.
the class SqueezeboxBinding method internalReceiveCommand.
/**
* @{inheritDoc}
*/
@Override
public void internalReceiveCommand(String itemName, Command command) {
if (squeezeServer == null) {
logger.warn("Squeeze Server not initialised or configured yet, ignoring command '{}' for item '{}'", command.toString(), itemName);
return;
}
logger.trace("internalReceiveCommand(itemname = {}, command = {})", itemName, command.toString());
for (SqueezeboxBindingProvider provider : providers) {
SqueezeboxBindingConfig bindingConfig = provider.getSqueezeboxBindingConfig(itemName);
String playerId = bindingConfig.getPlayerId();
try {
switch(bindingConfig.getCommandType()) {
case POWER:
if (command.equals(OnOffType.ON)) {
squeezeServer.powerOn(playerId);
} else if (command.equals(OnOffType.OFF)) {
squeezeServer.powerOff(playerId);
}
break;
case MUTE:
if (command.equals(OnOffType.ON)) {
squeezeServer.mute(playerId);
} else if (command.equals(OnOffType.OFF)) {
squeezeServer.unMute(playerId);
}
break;
case VOLUME:
if (command.equals(IncreaseDecreaseType.INCREASE)) {
squeezeServer.volumeUp(playerId);
} else if (command.equals(IncreaseDecreaseType.DECREASE)) {
squeezeServer.volumeDown(playerId);
} else if (command.equals(UpDownType.UP)) {
squeezeServer.volumeUp(playerId);
} else if (command.equals(UpDownType.DOWN)) {
squeezeServer.volumeDown(playerId);
} else if (command instanceof DecimalType) {
squeezeServer.setVolume(playerId, ((DecimalType) command).intValue());
}
break;
case PLAY:
if (command.equals(OnOffType.ON)) {
squeezeServer.play(playerId);
} else if (command.equals(OnOffType.OFF)) {
squeezeServer.stop(playerId);
}
break;
case PAUSE:
if (command.equals(OnOffType.ON)) {
squeezeServer.pause(playerId);
} else if (command.equals(OnOffType.OFF)) {
squeezeServer.unPause(playerId);
}
break;
case STOP:
if (command.equals(OnOffType.ON)) {
squeezeServer.stop(playerId);
} else if (command.equals(OnOffType.OFF)) {
squeezeServer.play(playerId);
}
break;
case NEXT:
if (command.equals(OnOffType.ON)) {
squeezeServer.next(playerId);
}
break;
case PREV:
if (command.equals(OnOffType.ON)) {
squeezeServer.prev(playerId);
}
break;
case HTTP:
if (command.equals(OnOffType.ON)) {
squeezeServer.playUrl(playerId, "http://" + bindingConfig.getExtra());
} else if (command.equals(OnOffType.OFF)) {
squeezeServer.stop(playerId);
}
break;
case FILE:
if (command.equals(OnOffType.ON)) {
squeezeServer.playUrl(playerId, "file://" + bindingConfig.getExtra());
} else if (command.equals(OnOffType.OFF)) {
squeezeServer.stop(playerId);
}
break;
case SYNC:
if (command.equals(OnOffType.ON)) {
squeezeServer.syncPlayer(playerId, bindingConfig.getExtra());
} else if (command.equals(OnOffType.OFF)) {
squeezeServer.unSyncPlayer(bindingConfig.getExtra());
}
break;
case COMMAND:
if (command instanceof StringType) {
squeezeServer.playerCommand(playerId, command.toString());
} else {
squeezeServer.playerCommand(playerId, bindingConfig.getExtra());
}
break;
default:
logger.warn("Unsupported command type '{}'", bindingConfig.getCommandType());
}
} catch (Exception e) {
logger.warn("Error executing command type '{}'", bindingConfig.getCommandType(), e);
}
}
}
use of org.openhab.core.library.types.StringType in project openhab1-addons by openhab.
the class EpsonProjectorBinding method queryDataFromDevice.
private State queryDataFromDevice(String deviceId, EpsonProjectorCommandType commmandType, Class<? extends Item> itemType) {
DeviceConfig device = deviceConfigCache.get(deviceId);
if (device == null) {
logger.error("Could not find device '{}'", deviceId);
return null;
}
EpsonProjectorDevice remoteController = device.getConnection();
if (remoteController == null) {
logger.error("Could not find device '{}'", deviceId);
return null;
}
try {
if (remoteController.isConnected() == false) {
remoteController.connect();
}
switch(commmandType) {
case AKEYSTONE:
int autoKeystone = remoteController.getAutoKeystone();
return new DecimalType(autoKeystone);
case ASPECT_RATIO:
AspectRatio aspectRatio = remoteController.getAspectRatio();
return new StringType(aspectRatio.toString());
case BACKGROUND:
Background background = remoteController.getBackground();
return new StringType(background.toString());
case BRIGHTNESS:
int brightness = remoteController.getBrightness();
return new DecimalType(brightness);
case COLOR:
Color color = remoteController.getColor();
return new StringType(color.toString());
case COLOR_MODE:
ColorMode colorMode = remoteController.getColorMode();
return new StringType(colorMode.toString());
case COLOR_TEMP:
int ctemp = remoteController.getColorTemperature();
return new DecimalType(ctemp);
case CONTRAST:
int contrast = remoteController.getContrast();
return new DecimalType(contrast);
case DENSITY:
int density = remoteController.getDensity();
return new DecimalType(density);
case DIRECT_SOURCE:
int directSource = remoteController.getDirectSource();
return new DecimalType(directSource);
case ERR_CODE:
int err = remoteController.getError();
return new DecimalType(err);
case ERR_MESSAGE:
String errString = remoteController.getErrorString();
return new StringType(errString);
case FLESH_TEMP:
int fleshColor = remoteController.getFleshColor();
return new DecimalType(fleshColor);
case GAIN_BLUE:
int gainBlue = remoteController.getGainBlue();
return new DecimalType(gainBlue);
case GAIN_GREEN:
int gainGreen = remoteController.getGainGreen();
return new DecimalType(gainGreen);
case GAIN_RED:
int gainRed = remoteController.getGainRed();
return new DecimalType(gainRed);
case GAMMA:
Gamma gamma = remoteController.getGamma();
return new StringType(gamma.toString());
case GAMMA_STEP:
logger.warn("Get '{}' not implemented!", commmandType.toString());
return null;
case HKEYSTONE:
int hKeystone = remoteController.getHorizontalKeystone();
return new DecimalType(hKeystone);
case HPOSITION:
int hPosition = remoteController.getHorizontalPosition();
return new DecimalType(hPosition);
case HREVERSE:
Switch hReverse = remoteController.getHorizontalReverse();
return hReverse == Switch.ON ? OnOffType.ON : OnOffType.OFF;
case KEY_CODE:
break;
case LAMP_TIME:
int lampTime = remoteController.getLampTime();
return new DecimalType(lampTime);
case LUMINANCE:
Luminance luminance = remoteController.getLuminance();
return new StringType(luminance.toString());
case MUTE:
Switch mute = remoteController.getMute();
return mute == Switch.ON ? OnOffType.ON : OnOffType.OFF;
case OFFSET_BLUE:
int offsetBlue = remoteController.getOffsetBlue();
return new DecimalType(offsetBlue);
case OFFSET_GREEN:
int offsetGreen = remoteController.getOffsetGreen();
return new DecimalType(offsetGreen);
case OFFSET_RED:
int offsetRed = remoteController.getOffsetRed();
return new DecimalType(offsetRed);
case POWER:
PowerStatus powerStatus = remoteController.getPowerStatus();
if (powerStatus == PowerStatus.ON) {
return OnOffType.ON;
} else {
return OnOffType.OFF;
}
case POWER_STATE:
PowerStatus powerStatus1 = remoteController.getPowerStatus();
return new StringType(powerStatus1.toString());
case SHARP:
logger.warn("Get '{}' not implemented!", commmandType.toString());
return null;
case SOURCE:
Source source = remoteController.getSource();
return new StringType(source.toString());
case SYNC:
int sync = remoteController.getSync();
return new DecimalType(sync);
case TINT:
int tint = remoteController.getTint();
return new DecimalType(tint);
case TRACKING:
int tracking = remoteController.getTracking();
return new DecimalType(tracking);
case VKEYSTONE:
int vKeystone = remoteController.getVerticalKeystone();
return new DecimalType(vKeystone);
case VPOSITION:
int vPosition = remoteController.getVerticalPosition();
return new DecimalType(vPosition);
case VREVERSE:
Switch vReverse = remoteController.getVerticalReverse();
return vReverse == Switch.ON ? OnOffType.ON : OnOffType.OFF;
default:
logger.warn("Unknown '{}' command!", commmandType);
return null;
}
} catch (EpsonProjectorException e) {
logger.warn("Couldn't execute command '{}', {}", commmandType.toString(), e);
} catch (Exception e) {
logger.warn("Couldn't create state of type '{}'", itemType);
return null;
}
return null;
}
Aggregations