use of org.openhab.core.library.types.OnOffType in project openhab1-addons by openhab.
the class CULIntertechnoBinding method internalReceiveCommand.
/**
*
* @{inheritDoc
*/
@Override
protected void internalReceiveCommand(String itemName, Command command) {
IntertechnoBindingConfig config = null;
for (CULIntertechnoBindingProvider provider : providers) {
config = provider.getConfigForItemName(itemName);
if (config != null) {
break;
}
}
if (config != null && culHandlerLifecycle.isCulReady() && command instanceof OnOffType) {
OnOffType type = (OnOffType) command;
String commandValue = null;
switch(type) {
case ON:
commandValue = config.getCommandValueON();
break;
case OFF:
commandValue = config.getCommandValueOFF();
break;
}
if (commandValue != null) {
try {
culHandlerLifecycle.getCul().send("is" + config.getAddress() + commandValue);
} catch (CULCommunicationException e) {
logger.error("Can't write to CUL", e);
}
} else {
logger.error("Can't determine value to send for command " + command.toString());
}
}
}
use of org.openhab.core.library.types.OnOffType in project openhab1-addons by openhab.
the class HueBinding method computeCommandForItemOnBridge.
/**
* Checks whether the command is for one of the configured Hue bulbs. If
* this is the case, the command is translated to the corresponding action
* which is then sent to the given bulb.
*
* @param command
* The command from the openHAB bus.
* @param itemName
* The name of the targeted item.
* @param bridge
* The Hue bridge the Hue bulb is connected to
*/
private void computeCommandForItemOnBridge(Command command, String itemName, HueBridge bridge) {
HueBindingConfig deviceConfig = getConfigForItemName(itemName);
if (deviceConfig == null) {
return;
}
HueBulb bulb = bulbCache.get(deviceConfig.getDeviceId());
if (bulb == null) {
bulb = new HueBulb(bridge, deviceConfig.getDeviceId());
bulbCache.put(deviceConfig.getDeviceId(), bulb);
}
if (command instanceof OnOffType) {
bulb.switchOn(OnOffType.ON.equals(command));
}
if (command instanceof HSBType) {
HSBType hsbCommand = (HSBType) command;
DecimalType hue = hsbCommand.getHue();
PercentType sat = hsbCommand.getSaturation();
PercentType bri = hsbCommand.getBrightness();
bulb.colorizeByHSB(hue.doubleValue() / 360, sat.doubleValue() / 100, bri.doubleValue() / 100);
}
if (deviceConfig.getType().equals(BindingType.brightness) || deviceConfig.getType().equals(BindingType.rgb)) {
if (IncreaseDecreaseType.INCREASE.equals(command)) {
int resultingValue = bulb.increaseBrightness(deviceConfig.getStepSize());
eventPublisher.postUpdate(itemName, new PercentType(resultingValue));
} else if (IncreaseDecreaseType.DECREASE.equals(command)) {
int resultingValue = bulb.decreaseBrightness(deviceConfig.getStepSize());
eventPublisher.postUpdate(itemName, new PercentType(resultingValue));
} else if ((command instanceof PercentType) && !(command instanceof HSBType)) {
bulb.setBrightness((int) Math.round((double) HueBulb.MAX_BRIGHTNESS / (double) 100 * ((PercentType) command).intValue()));
}
}
if (deviceConfig.getType().equals(BindingType.colorTemperature)) {
if (IncreaseDecreaseType.INCREASE.equals(command)) {
bulb.increaseColorTemperature(deviceConfig.getStepSize());
} else if (IncreaseDecreaseType.DECREASE.equals(command)) {
bulb.decreaseColorTemperature(deviceConfig.getStepSize());
} else if (command instanceof PercentType) {
bulb.setColorTemperature((int) Math.round((((double) 346 / (double) 100) * ((PercentType) command).intValue()) + 154));
}
}
}
use of org.openhab.core.library.types.OnOffType in project openhab1-addons by openhab.
the class MpdBinding method determinePlayStateChange.
private void determinePlayStateChange(String playerId) {
MPD daemon = findMPDInstance(playerId);
if (daemon == null) {
// we give that player another chance -> try to reconnect
reconnect(playerId);
}
if (daemon != null) {
try {
MPDPlayer player = daemon.getMPDPlayer();
// get the song object here
PlayerStatus ps = player.getStatus();
PlayerStatus curPs = playerStatusCache.get(playerId);
if (curPs != null) {
if (ps != curPs) {
logger.debug("Play state of player '{}' changed", playerId);
playerStatusCache.put(playerId, ps);
PlayerCommandTypeMapping reportTo;
OnOffType reportStatus;
// trigger song update
if (ps.equals(PlayerStatus.STATUS_PAUSED) || ps.equals(PlayerStatus.STATUS_STOPPED)) {
// stopped
reportTo = PlayerCommandTypeMapping.STOP;
reportStatus = OnOffType.OFF;
} else {
// playing
reportTo = PlayerCommandTypeMapping.PLAY;
reportStatus = OnOffType.ON;
}
broadcastPlayerStateChange(playerId, reportTo, reportStatus);
} else {
// nothing, same state
}
} else {
playerStatusCache.put(playerId, ps);
}
} catch (MPDPlayerException pe) {
logger.error("Error while updating player status for player '{}': {}", playerId, pe.getMessage());
} catch (Exception e) {
logger.warn("Failed to communicate with player '{}'", playerId);
}
} else {
logger.warn("Player '{}' was not found or is not connected", playerId);
}
}
use of org.openhab.core.library.types.OnOffType in project openhab1-addons by openhab.
the class PLCLogoBinding method internalReceiveCommand.
@Override
protected void internalReceiveCommand(String itemName, Command command) {
// the code being executed when a command was sent on the openHAB
// event bus goes here. This method is only called if one of the
// BindingProviders provide a binding for the given 'itemName'.
// Note itemname is the item name not the controller name/instance!
//
super.internalReceiveCommand(itemName, command);
logger.debug("internalReceiveCommand() is called!");
for (PLCLogoBindingProvider provider : providers) {
if (!provider.providesBindingFor(itemName)) {
continue;
}
PLCLogoBindingConfig config = provider.getBindingConfig(itemName);
if (!controllers.containsKey(config.getController())) {
logger.warn("Invalid write requested for controller {}", config.getController());
continue;
}
PLCLogoConfig controller = controllers.get(config.getController());
PLCLogoMemoryConfig wr = config.getWR();
int address = -1;
try {
address = wr.getAddress(controller.getModel());
} catch (BindingConfigParseException exception) {
logger.error("Invalid address for block {} on {}", wr.getBlockName(), controller);
continue;
}
int bit = -1;
try {
bit = wr.getBit(controller.getModel());
} catch (BindingConfigParseException exception) {
logger.error("Invalid bit for block {} on {}", wr.getBlockName(), controller);
continue;
}
if (!wr.isInRange(controller.getModel())) {
logger.warn("Invalid write request for block {} at address {}", wr.getBlockName(), address);
continue;
}
// Send command to the LOGO! controller memory
S7Client LogoS7Client = controller.getS7Client();
if (LogoS7Client == null) {
logger.debug("No S7client for controller {} found", config.getController());
continue;
}
final byte[] buffer = new byte[2];
int size = wr.isDigital() ? 1 : 2;
lock.lock();
int result = LogoS7Client.ReadArea(S7.S7AreaDB, 1, address, size, buffer);
logger.debug("Read word from logo memory: at {} {} bytes, result = {}", address, size, result);
if (result == 0) {
Item item = config.getItem();
if (item instanceof NumberItem && !wr.isDigital()) {
if (command instanceof DecimalType) {
int oldValue = S7.GetShortAt(buffer, 0);
int newValue = ((DecimalType) command).intValue();
S7.SetWordAt(buffer, 0, newValue);
logger.debug("Changed word at {} from {} to {}", address, oldValue, newValue);
result = LogoS7Client.WriteArea(S7.S7AreaDB, 1, address, size, buffer);
logger.debug("Wrote to memory at {} two bytes: [{}, {}]", address, buffer[0], buffer[1]);
}
} else if (item instanceof SwitchItem && wr.isDigital()) {
if (command instanceof OnOffType) {
boolean oldValue = S7.GetBitAt(buffer, 0, bit) ? true : false;
boolean newValue = command == OnOffType.ON ? true : false;
S7.SetBitAt(buffer, 0, bit, newValue);
logger.debug("Changed bit {}.{} from {} to {}", address, bit, oldValue, newValue);
result = LogoS7Client.WriteArea(S7.S7AreaDB, 1, address, size, buffer);
logger.debug("Wrote to memory at {} one byte: [{}]", address, buffer[0]);
}
}
// If nothing was written and read was ok, nothing will happen here
if (result != 0) {
logger.warn("Failed to write memory: {}. Reconnecting...", S7Client.ErrorText(result));
ReconnectLogo(LogoS7Client);
}
} else {
logger.warn("Failed to read memory: {}. Reconnecting...", S7Client.ErrorText(result));
ReconnectLogo(LogoS7Client);
}
lock.unlock();
}
}
use of org.openhab.core.library.types.OnOffType in project openhab1-addons by openhab.
the class OpenSprinklerBinding method internalReceiveCommand.
/**
* @{inheritDoc}
*/
@Override
protected void internalReceiveCommand(String itemName, Command command) {
if (command instanceof OnOffType) {
final OnOffType switchCommand = (OnOffType) command;
final OpenSprinklerBindingProvider bindingProvider = findFirstMatchingBindingProvider(itemName, command);
final int station = bindingProvider.getStationNumber(itemName);
if (station < 0 || station >= numberOfStations) {
logger.warn("Station " + station + " is not in the valid [" + 0 + ".." + numberOfStations + "] range");
return;
}
switch(switchCommand) {
case ON:
openSprinkler.openStation(station);
break;
case OFF:
openSprinkler.closeStation(station);
break;
}
return;
}
if (command instanceof OpenClosedType) {
// abort processing
return;
}
logger.debug("Provided command " + command + " is not of type 'OnOffType' or 'OpenClosedType'");
}
Aggregations