use of org.openhab.core.library.types.PercentType in project openhab1-addons by openhab.
the class MilightBinding method sendDecrease.
private PercentType sendDecrease(int bulb, int rgbwSteps, String bridgeId) {
logger.debug("milight: sendDecrease");
String messageBytes = null;
switch(bulb) {
// decrease brightness of white bulbs
case 0:
case 1:
case 2:
case 3:
case 4:
messageBytes = "34:00:55";
break;
// decrease brightness of rgb bulbs
case 5:
messageBytes = "24:00:55";
break;
}
int newPercent = getCurrentState(bulb, bridgeId, BindingType.brightness).intValue() - 10;
if (newPercent < 0) {
newPercent = 0;
}
PercentType newValue = new PercentType(newPercent);
if (newValue.equals(PercentType.ZERO)) {
sendOff(bulb, bridgeId);
} else {
if (bulb > 5) {
int decreasePercent = newPercent * (rgbwSteps - 2) / 100 + 2;
messageBytes = "4E:" + Integer.toHexString(decreasePercent) + ":55";
logger.debug("Bulb '{}' set to '{}' dimming Steps", bulb, rgbwSteps);
} else if (bulb < 5) {
newPercent = (int) Math.round((Math.round(getCurrentState(bulb, bridgeId, BindingType.brightness).intValue() / 9.090909090909091) - 1) * 9.090909090909091);
newValue = new PercentType(newPercent);
logger.debug("milight: Bulb '{}' getting decreased to '{}'", bulb, newPercent);
}
sendMessage(messageBytes, bridgeId);
}
setCurrentState(bulb, bridgeId, newValue, BindingType.brightness);
return newValue;
}
use of org.openhab.core.library.types.PercentType in project openhab1-addons by openhab.
the class MilightBinding method sendPercent.
private void sendPercent(int bulb, int rgbwSteps, String bridgeId, PercentType command, MilightBindingConfig.BindingType type) {
logger.debug("milight: sendPercent");
if (BindingType.brightness.equals(type) && command.equals(PercentType.ZERO)) {
sendOff(bulb, bridgeId);
return;
}
if (BindingType.brightness.equals(type) && command.equals(PercentType.HUNDRED)) {
sendFull(bulb, rgbwSteps, bridgeId);
return;
}
PercentType oldPercent = getCurrentState(bulb, bridgeId, type);
// Make sure lights are on and engage current bulb via a preceding ON command:
sendOn(bulb, bridgeId);
try {
// White Bulbs: 10 levels of brightness + Off.
if (bulb < 5) {
double stepSize = 9.090909090909091;
// Assume lowest brightness level (about 9%) if just powered on.
if (oldPercent.equals(PercentType.ZERO)) {
oldPercent = new PercentType(9);
}
int repeatCount = Math.abs((int) Math.round(command.intValue() / stepSize) - (int) Math.round(oldPercent.intValue() / stepSize));
logger.debug("milight: dim from '{}' with command '{}' via '{}' steps.", oldPercent.toString(), command.toString(), repeatCount);
if (command.compareTo(oldPercent) > 0) {
for (int i = 0; i < repeatCount; i++) {
Thread.sleep(100);
if (BindingType.brightness.equals(type)) {
sendIncrease(bulb, rgbwSteps, bridgeId);
} else if (BindingType.colorTemperature.equals(type)) {
sendWarmer(bulb, bridgeId);
}
}
} else if (command.compareTo(oldPercent) < 0) {
for (int i = 0; i < repeatCount; i++) {
Thread.sleep(100);
if (BindingType.brightness.equals(type)) {
sendDecrease(bulb, rgbwSteps, bridgeId);
} else if (BindingType.colorTemperature.equals(type)) {
sendCooler(bulb, bridgeId);
}
}
}
// Old RGB Bulbs: 9 levels of brightness + Off.
} else if (bulb == 5) {
if (command.compareTo(oldPercent) > 0) {
int repeatCount = (command.intValue() - oldPercent.intValue()) / 10;
for (int i = 0; i < repeatCount; i++) {
Thread.sleep(100);
if (BindingType.brightness.equals(type) && bulb < 6) {
sendIncrease(bulb, rgbwSteps, bridgeId);
} else if (BindingType.colorTemperature.equals(type)) {
sendWarmer(bulb, bridgeId);
} else if (BindingType.discoSpeed.equals(type)) {
sendIncreaseSpeed(bulb, bridgeId);
} else if (BindingType.discoMode.equals(type)) {
sendDiscoModeUp(bulb, bridgeId);
}
}
} else if (command.compareTo(oldPercent) < 0) {
int repeatCount = (oldPercent.intValue() - command.intValue()) / 10;
for (int i = 0; i < repeatCount; i++) {
Thread.sleep(100);
if (BindingType.brightness.equals(type) && bulb < 6) {
sendDecrease(bulb, rgbwSteps, bridgeId);
} else if (BindingType.colorTemperature.equals(type)) {
sendCooler(bulb, bridgeId);
} else if (BindingType.discoSpeed.equals(type)) {
sendDecreaseSpeed(bulb, bridgeId);
} else if (BindingType.discoMode.equals(type)) {
sendDiscoModeDown(bulb, bridgeId);
}
}
}
// RGBW Bulbs:
} else if (bulb > 5) {
if (command.intValue() > 0 && command.intValue() < 100) {
int newCommand = (command.intValue() * (rgbwSteps - 2) / 100 + 2);
Thread.sleep(100);
String messageBytes = "4E:" + Integer.toHexString(newCommand) + ":55";
logger.debug("milight: send dimming packet '{}' to RGBW bulb channel '{}'", messageBytes, bulb);
sendMessage(messageBytes, bridgeId);
} else if (command.intValue() > 99) {
sendFull(bulb, rgbwSteps, bridgeId);
} else if (command.intValue() < 1) {
sendOff(bulb, bridgeId);
}
}
// store dimmerValue
setCurrentState(bulb, bridgeId, command, type);
} catch (InterruptedException e) {
logger.debug("Sleeping thread has been interrupted.");
}
}
use of org.openhab.core.library.types.PercentType 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.PercentType 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.PercentType in project openhab1-addons by openhab.
the class PowerDogLocalApiBinding method internalReceiveCommand.
/**
* @{inheritDoc
*/
@Override
protected void internalReceiveCommand(String itemName, Command command) {
logger.debug("internalReceiveCommand({},{}) is called!", itemName, command);
State newState = null;
// cast Interfaces
if (command instanceof OnOffType) {
newState = (OnOffType) command;
} else if (command instanceof OpenClosedType) {
newState = (OpenClosedType) command;
} else if (command instanceof PercentType) {
newState = (PercentType) command;
} else if (command instanceof DecimalType) {
newState = (DecimalType) command;
}
if (newState != null) {
eventPublisher.postUpdate(itemName, newState);
}
}
Aggregations