use of org.openhab.core.library.types.PercentType in project openhab1-addons by openhab.
the class ModuleChannelGroupTest method canSendGroup1DimmerUpdate.
@Test
public void canSendGroup1DimmerUpdate() throws Exception {
ModuleChannel item = group1.addChannel("test4", 4, new ArrayList<Class<? extends Command>>());
item.setState(new PercentType(25));
group1.publishStateToNikobus(item, binding);
Mockito.verify(binding, Mockito.times(1)).sendCommand(command.capture());
NikobusCommand cmd = command.getAllValues().get(0);
assertEquals("$1E156C94000000400000FF45DE7B", cmd.getCommand());
}
use of org.openhab.core.library.types.PercentType in project openhab1-addons by openhab.
the class AudioZone method updateItem.
@Override
public void updateItem(Item item, OmniLinkBindingConfig config, EventPublisher publisher) {
int num = 0;
String str = "";
int source = new Integer(properties.getSource());
switch(config.getObjectType()) {
case AUDIOZONE_MUTE:
num = properties.isMute() ? 1 : 0;
break;
case AUDIOZONE_POWER:
num = properties.isOn() ? 1 : 0;
break;
case AUDIOZONE_SOURCE:
num = properties.getSource();
break;
case AUDIOZONE_VOLUME:
num = properties.getVolume();
break;
case AUDIOZONE_TEXT:
if (sourceValid(source)) {
str = audioSources.get(source).formatAudioText();
}
break;
case AUDIOZONE_TEXT_FIELD1:
if (sourceValid(source)) {
str = audioSources.get(source).getAudioText(0);
}
break;
case AUDIOZONE_TEXT_FIELD2:
if (sourceValid(source)) {
str = audioSources.get(source).getAudioText(1);
}
break;
case AUDIOZONE_TEXT_FIELD3:
if (sourceValid(source)) {
str = audioSources.get(source).getAudioText(2);
}
break;
case AUDIOZONE_KEY:
num = -1;
break;
default:
return;
}
if (item instanceof DimmerItem) {
logger.debug("updating percent type {}", num);
publisher.postUpdate(item.getName(), new PercentType(num));
} else if (item instanceof NumberItem) {
logger.debug("updating number type {}", num);
publisher.postUpdate(item.getName(), new DecimalType(num));
} else if (item instanceof SwitchItem) {
logger.debug("updating switch type {}", num > 0 ? OnOffType.ON : OnOffType.OFF);
publisher.postUpdate(item.getName(), num > 0 ? OnOffType.ON : OnOffType.OFF);
} else if (item instanceof StringItem) {
logger.debug("updating string type {}", str);
publisher.postUpdate(item.getName(), new StringType(str));
}
}
use of org.openhab.core.library.types.PercentType in project openhab1-addons by openhab.
the class PowerDogLocalApiBinding method internalReceiveUpdate.
/**
* @{inheritDoc
*/
@Override
protected void internalReceiveUpdate(String itemName, State newState) {
logger.debug("internalReceiveUpdate({},{}) is called!", itemName, newState);
// cycle on all available powerdogs
for (PowerDogLocalApiBindingProvider provider : providers) {
if (!provider.providesBindingFor(itemName)) {
continue;
}
// only in case of an outbinding, this need to be handled
if (provider.getOutBindingItemNames().contains(itemName)) {
// check if item may send update already now
// time indicated in config is the minimum time between two
// updates
Long lastUpdateTimeStamp = lastUpdateMap.get(itemName);
if (lastUpdateTimeStamp == null) {
lastUpdateTimeStamp = 0L;
}
long age = System.currentTimeMillis() - lastUpdateTimeStamp;
boolean itemMayUpdate = (age >= provider.getRefreshInterval(itemName));
if (itemMayUpdate) {
// Convert new State to PowerDog set Current_Value string
String value = "0";
if (newState instanceof OnOffType) {
if (newState == OnOffType.ON) {
value = "1";
}
} else // the best solution, but it is sufficient
if (newState instanceof OpenClosedType) {
if (newState == OpenClosedType.OPEN) {
value = "1";
}
} else // see comment above
if (newState instanceof PercentType) {
value = newState.toString();
} else if (newState instanceof DecimalType) {
value = newState.toString();
}
// Get the unit serverId from the binding, and relate that
// to the config
String unit = provider.getServerId(itemName);
PowerDogLocalApiServerConfig server = serverList.get(unit);
try {
logger.debug("PowerDogLocalApi sending to PowerDog");
// Perform XML RPC call
PowerDog powerdog = (PowerDog) XmlRpcProxy.createProxy(server.url(), "", new Class[] { PowerDog.class }, false);
XmlRpcStruct response = powerdog.setLinearSensorDevice(server.password, provider.getValueId(itemName), value);
lastUpdateMap.put(itemName, System.currentTimeMillis());
logger.debug("PowerDog.setLinearSensorDevice() result: {}", response.toString());
} catch (Exception e) {
logger.warn("PowerDogLocalApi sending to PowerDog failed");
logger.warn(e.getMessage());
}
}
}
}
}
use of org.openhab.core.library.types.PercentType in project openhab1-addons by openhab.
the class PrimareSPA20Response 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
*/
@Override
public State openHabState(Class<? extends Item> itemType) {
State state = UnDefType.UNDEF;
try {
int index;
String s;
if (itemType == SwitchItem.class) {
index = message[2];
// observed with SPA20, software version 1.50 Nov 2 2003
if (message[1] == 9) {
logger.trace("MUTE (variable 9) converted opposite to documentation");
state = index == 0 ? OnOffType.ON : OnOffType.OFF;
} else {
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 execute.
/**
* @{inheritDoc
*/
@Override
@SuppressWarnings("incomplete-switch")
public void execute() {
List<PulseaudioClient> updatedClients = new ArrayList<PulseaudioClient>();
for (PulseaudioBindingProvider provider : providers) {
for (String itemName : provider.getItemNames()) {
String audioItemName = provider.getItemName(itemName);
String serverId = provider.getServerId(itemName);
Class<? extends Item> itemType = provider.getItemType(itemName);
String command = provider.getCommand(itemName);
PulseaudioCommandTypeMapping commandType = null;
if (command != null && !command.isEmpty()) {
try {
commandType = PulseaudioCommandTypeMapping.valueOf(command.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 });
continue;
}
}
if (itemType.isAssignableFrom(GroupItem.class) || itemType.isAssignableFrom(StringItem.class)) {
// directly
continue;
}
PulseaudioClient client = clients.get(serverId);
if (client == null) {
logger.warn("connection to pulseaudio server in not available " + "for the given itemName [itemName={}, audio-item-name={}, serverId={}, command={}] => querying for values aborted!", new Object[] { itemName, audioItemName, serverId, command });
continue;
}
if (audioItemName == null) {
logger.warn("audio-item-name isn't configured properly " + "for the given itemName [itemName={}, audio-item-name={}, serverId={}, command={}] => querying for values aborted!", new Object[] { itemName, audioItemName, serverId, command });
continue;
}
if (!updatedClients.contains(client)) {
// update the clients data structure to avoid
// inconsistencies
client.update();
updatedClients.add(client);
}
State value = UnDefType.UNDEF;
AbstractAudioDeviceConfig audioItem = client.getGenericAudioItem(audioItemName);
if (audioItem != null) {
// item found
if (itemType.isAssignableFrom(SwitchItem.class)) {
if (commandType == null) {
// Check if item is unmuted and running
if (!audioItem.isMuted() && audioItem.getState() != null && audioItem.getState().equals(AbstractAudioDeviceConfig.State.RUNNING)) {
value = OnOffType.ON;
} else {
value = OnOffType.OFF;
}
} else {
switch(commandType) {
case EXISTS:
value = OnOffType.ON;
break;
case MUTED:
value = audioItem.isMuted() ? OnOffType.ON : OnOffType.OFF;
break;
case RUNNING:
case CORKED:
case SUSPENDED:
case IDLE:
try {
value = audioItem.getState() != null && audioItem.getState().equals(AbstractAudioDeviceConfig.State.valueOf(commandType.name())) ? OnOffType.ON : OnOffType.OFF;
} catch (IllegalArgumentException e) {
logger.warn("no corresponding AbstractAudioDeviceConfig.State found for " + commandType.name());
}
break;
}
}
} else if (itemType.isAssignableFrom(DimmerItem.class)) {
value = new PercentType(audioItem.getVolume());
} else if (itemType.isAssignableFrom(NumberItem.class)) {
if (commandType == null) {
// when no other pulseaudioCommand specified, we use
// VOLUME
value = new DecimalType(audioItem.getVolume());
} else {
// ID command types
switch(commandType) {
case VOLUME:
value = new DecimalType(audioItem.getVolume());
break;
case ID:
value = new DecimalType(audioItem.getId());
break;
case MODULE_ID:
if (audioItem.getModule() != null) {
value = new DecimalType(audioItem.getModule().getId());
}
break;
}
}
} else if (itemType.isAssignableFrom(StringItem.class)) {
if (commandType == null) {
value = new StringType(audioItem.toString());
} else if (audioItem instanceof Sink) {
Sink sink = (Sink) audioItem;
switch(commandType) {
case SLAVE_SINKS:
if (sink.isCombinedSink()) {
value = new StringType(StringUtils.join(sink.getCombinedSinkNames(), ","));
}
break;
}
}
} else {
logger.debug("unhandled item type [type={}, name={}]", itemType.getClass(), audioItemName);
}
} else if (itemType.isAssignableFrom(SwitchItem.class)) {
value = OnOffType.OFF;
}
eventPublisher.postUpdate(itemName, value);
}
}
}
Aggregations