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'");
}
use of org.openhab.core.library.types.OnOffType in project openhab1-addons by openhab.
the class IntegraStateBindingConfig method convertCommand.
/**
* {@inheritDoc}
*/
@Override
public SatelCommand convertCommand(Command command, IntegraType integraType, String userCode) {
if (command instanceof OnOffType && this.objectNumbers.length == 1) {
boolean switchOn = ((OnOffType) command == OnOffType.ON);
boolean force_arm = hasOptionEnabled(Options.FORCE_ARM);
switch(this.stateType.getObjectType()) {
case OUTPUT:
byte[] outputs = getObjectBitset((integraType == IntegraType.I256_PLUS) ? 32 : 16);
boolean invertState = hasOptionEnabled(Options.INVERT_STATE);
return new ControlObjectCommand(switchOn ^ invertState ? OutputControl.ON : OutputControl.OFF, outputs, userCode);
case DOORS:
// for doors you can also control outputs of type 101, but we do not support this feature
// anyway we need to send list of outputs, so we have 'dummy' array for this purpose
byte[] doors = getObjectBitset(8);
byte[] dummy = new byte[(integraType == IntegraType.I256_PLUS) ? 32 : 16];
if (switchOn) {
return new ControlObjectCommand(DoorsControl.OPEN, ArrayUtils.addAll(dummy, doors), userCode);
} else {
return null;
}
case ZONE:
byte[] zones = getObjectBitset((integraType == IntegraType.I256_PLUS) ? 32 : 16);
switch((ZoneState) this.stateType) {
case BYPASS:
return new ControlObjectCommand(switchOn ? ZoneControl.BYPASS : ZoneControl.UNBYPASS, zones, userCode);
case ISOLATE:
if (switchOn) {
return new ControlObjectCommand(ZoneControl.ISOLATE, zones, userCode);
} else {
return null;
}
default:
// do nothing for other types of state
break;
}
break;
case PARTITION:
byte[] partitions = getObjectBitset(4);
switch((PartitionState) this.stateType) {
// clear alarms on OFF command
case ALARM:
case ALARM_MEMORY:
case FIRE_ALARM:
case FIRE_ALARM_MEMORY:
case VERIFIED_ALARMS:
case WARNING_ALARMS:
if (switchOn) {
return null;
} else {
return new ControlObjectCommand(PartitionControl.CLEAR_ALARM, partitions, userCode);
}
// arm or disarm, depending on command
case ARMED:
case REALLY_ARMED:
return new ControlObjectCommand(switchOn ? (force_arm ? PartitionControl.FORCE_ARM_MODE_0 : PartitionControl.ARM_MODE_0) : PartitionControl.DISARM, partitions, userCode);
case ARMED_MODE_1:
return new ControlObjectCommand(switchOn ? (force_arm ? PartitionControl.FORCE_ARM_MODE_1 : PartitionControl.ARM_MODE_1) : PartitionControl.DISARM, partitions, userCode);
case ARMED_MODE_2:
return new ControlObjectCommand(switchOn ? (force_arm ? PartitionControl.FORCE_ARM_MODE_2 : PartitionControl.ARM_MODE_2) : PartitionControl.DISARM, partitions, userCode);
case ARMED_MODE_3:
return new ControlObjectCommand(switchOn ? (force_arm ? PartitionControl.FORCE_ARM_MODE_3 : PartitionControl.ARM_MODE_3) : PartitionControl.DISARM, partitions, userCode);
default:
// do nothing for other types of state
break;
}
}
} else if (this.stateType.getObjectType() == ObjectType.OUTPUT && this.objectNumbers.length == 2) {
// roller shutter support
if (command == UpDownType.UP) {
byte[] outputs = getObjectBitset((integraType == IntegraType.I256_PLUS) ? 32 : 16, 0);
return new ControlObjectCommand(OutputControl.ON, outputs, userCode);
} else if (command == UpDownType.DOWN) {
byte[] outputs = getObjectBitset((integraType == IntegraType.I256_PLUS) ? 32 : 16, 1);
return new ControlObjectCommand(OutputControl.ON, outputs, userCode);
} else if (command == StopMoveType.STOP) {
byte[] outputs = getObjectBitset((integraType == IntegraType.I256_PLUS) ? 32 : 16);
return new ControlObjectCommand(OutputControl.OFF, outputs, userCode);
}
}
return null;
}
Aggregations