use of org.openhab.core.library.types.OnOffType in project openhab1-addons by openhab.
the class InsteonHubBinding method internalReceiveCommand.
@Override
protected void internalReceiveCommand(String itemName, Command command) {
// get configuration for this item
InsteonHubBindingConfig config = InsteonHubBindingConfigUtil.getConfigForItem(providers, itemName);
if (config == null) {
logger.error(BINDING_NAME + " received command for unknown item '" + itemName + "'");
return;
}
// parse info from config
BindingType type = config.getBindingType();
String hubId = config.getDeviceInfo().getHubId();
String deviceId = config.getDeviceInfo().getDeviceId();
// lookup proxy from this configuration
InsteonHubProxy proxy = proxies.get(hubId);
if (proxy == null) {
logger.error(BINDING_NAME + " received command for unknown hub id '" + hubId + "'");
return;
}
if (logger.isDebugEnabled()) {
logger.debug(BINDING_NAME + " processing command '" + command + "' of type '" + command.getClass().getSimpleName() + "' for item '" + itemName + "'");
}
try {
// process according to type
if (type == BindingType.SWITCH) {
// set value on or off
if (command instanceof OnOffType) {
proxy.setDevicePower(deviceId, command == OnOffType.ON);
}
} else if (type == BindingType.DIMMER) {
// INSTEON Dimmer supports Dimmer and RollerShutter types
if (command instanceof OnOffType) {
// ON or OFF => Set level to 255 or 0
int level = command == OnOffType.ON ? 255 : 0;
proxy.setDeviceLevel(deviceId, level);
} else if (command instanceof IncreaseDecreaseType) {
// Increase/Decrease => Incremental Brighten/Dim
InsteonHubAdjustmentType adjustmentType;
if (command == IncreaseDecreaseType.INCREASE) {
adjustmentType = InsteonHubAdjustmentType.BRIGHTEN;
} else {
adjustmentType = InsteonHubAdjustmentType.DIM;
}
if (setDimTimeout(itemName)) {
proxy.startDeviceAdjustment(deviceId, adjustmentType);
}
} else if (command instanceof UpDownType) {
// Up/Down => Start Brighten/Dim
InsteonHubAdjustmentType adjustmentType;
if (command == UpDownType.UP) {
adjustmentType = InsteonHubAdjustmentType.BRIGHTEN;
} else {
adjustmentType = InsteonHubAdjustmentType.DIM;
}
proxy.startDeviceAdjustment(deviceId, adjustmentType);
} else if (command instanceof StopMoveType) {
// Stop => Stop Brighten/Dim
if (command == StopMoveType.STOP) {
proxy.stopDeviceAdjustment(deviceId);
}
} else {
// set level from 0 to 100 percent value
byte percentByte = Byte.parseByte(command.toString());
float percent = percentByte * .01f;
int level = (int) (255 * percent);
proxy.setDeviceLevel(deviceId, level);
}
}
} catch (Throwable t) {
logger.error("Error processing command '" + command + "' for item '" + itemName + "'", t);
}
}
use of org.openhab.core.library.types.OnOffType in project openhab1-addons by openhab.
the class WemoBinding method execute.
/**
* @{inheritDoc}
*/
@Override
protected void execute() {
logger.debug("execute() method is called!");
for (WemoBindingProvider provider : providers) {
for (String itemName : provider.getItemNames()) {
logger.debug("Wemo item '{}' state will be updated", itemName);
try {
if (provider.getUDN(itemName).toLowerCase().contains("insight")) {
String insightParams = getInsightParams(itemName);
if (insightParams != null) {
String[] splitInsightParams = insightParams.split("\\|");
if (splitInsightParams[0] != null) {
if (provider.getChannelType(itemName).equals(WemoChannelType.state)) {
OnOffType binaryState = null;
binaryState = splitInsightParams[0].equals("0") ? OnOffType.OFF : OnOffType.ON;
if (binaryState != null) {
logger.trace("New InsightParam binaryState '{}' for device '{}' received", binaryState, itemName);
eventPublisher.postUpdate(itemName, binaryState);
}
}
if (provider.getChannelType(itemName).equals(WemoChannelType.lastChangedAt)) {
long lastChangedAt = 0;
try {
// convert s to ms
lastChangedAt = Long.parseLong(splitInsightParams[1]) * 1000;
} catch (NumberFormatException e) {
logger.error("Unable to parse lastChangedAt value '{}' for device '{}'; expected long", splitInsightParams[1], itemName);
}
GregorianCalendar cal = new GregorianCalendar();
cal.setTimeInMillis(lastChangedAt);
State lastChangedAtState = new DateTimeType(cal);
if (lastChangedAt != 0) {
logger.trace("New InsightParam lastChangedAt '{}' for device '{}' received", lastChangedAtState, itemName);
eventPublisher.postUpdate(itemName, lastChangedAtState);
}
}
if (provider.getChannelType(itemName).equals(WemoChannelType.lastOnFor)) {
State lastOnFor = DecimalType.valueOf(splitInsightParams[2]);
if (lastOnFor != null) {
logger.trace("New InsightParam lastOnFor '{}' for device '{}' received", lastOnFor, itemName);
eventPublisher.postUpdate(itemName, lastOnFor);
}
}
if (provider.getChannelType(itemName).equals(WemoChannelType.onToday)) {
State onToday = DecimalType.valueOf(splitInsightParams[3]);
if (onToday != null) {
logger.trace("New InsightParam onToday '{}' for device '{}' received", onToday, itemName);
eventPublisher.postUpdate(itemName, onToday);
}
}
if (provider.getChannelType(itemName).equals(WemoChannelType.onTotal)) {
State onTotal = DecimalType.valueOf(splitInsightParams[4]);
if (onTotal != null) {
logger.trace("New InsightParam onTotal '{}' for device '{}' received", onTotal, itemName);
eventPublisher.postUpdate(itemName, onTotal);
}
}
if (provider.getChannelType(itemName).equals(WemoChannelType.timespan)) {
State timespan = DecimalType.valueOf(splitInsightParams[5]);
if (timespan != null) {
logger.trace("New InsightParam timespan '{}' for device '{}' received", timespan, itemName);
eventPublisher.postUpdate(itemName, timespan);
}
}
if (provider.getChannelType(itemName).equals(WemoChannelType.averagePower)) {
// natively given
State averagePower = DecimalType.valueOf(splitInsightParams[6]);
// in W
if (averagePower != null) {
logger.trace("New InsightParam averagePower '{}' for device '{}' received", averagePower, itemName);
eventPublisher.postUpdate(itemName, averagePower);
}
}
if (provider.getChannelType(itemName).equals(WemoChannelType.currentPower)) {
BigDecimal currentMW = new BigDecimal(splitInsightParams[7]);
State currentPower = new DecimalType(// recalculate
currentMW.divide(new BigDecimal(1000), RoundingMode.HALF_UP));
// mW to W
if (currentPower != null) {
logger.trace("New InsightParam currentPower '{}' for device '{}' received", currentPower, itemName);
eventPublisher.postUpdate(itemName, currentPower);
}
}
if (provider.getChannelType(itemName).equals(WemoChannelType.energyToday)) {
BigDecimal energyTodayMWMin = new BigDecimal(splitInsightParams[8]);
// recalculate mW-mins to Wh
State energyToday = new DecimalType(energyTodayMWMin.divide(new BigDecimal(60000), RoundingMode.HALF_UP));
if (energyToday != null) {
logger.trace("New InsightParam energyToday '{}' for device '{}' received", energyToday, itemName);
eventPublisher.postUpdate(itemName, energyToday);
}
}
if (provider.getChannelType(itemName).equals(WemoChannelType.energyTotal)) {
BigDecimal energyTotalMWMin = new BigDecimal(splitInsightParams[9]);
// recalculate mW-mins to Wh
State energyTotal = new DecimalType(energyTotalMWMin.divide(new BigDecimal(60000), RoundingMode.HALF_UP));
if (energyTotal != null) {
logger.trace("New InsightParam energyTotal '{}' for device '{}' received", energyTotal, itemName);
eventPublisher.postUpdate(itemName, energyTotal);
}
}
if (provider.getChannelType(itemName).equals(WemoChannelType.standbyLimit)) {
BigDecimal standbyLimitMW = new BigDecimal(splitInsightParams[10]);
// recalculate mW to W
State standbyLimit = new DecimalType(standbyLimitMW.divide(new BigDecimal(1000), RoundingMode.HALF_UP));
if (standbyLimit != null) {
logger.trace("New InsightParam standbyLimit '{}' for device '{}' received", standbyLimit, itemName);
eventPublisher.postUpdate(itemName, standbyLimit);
}
}
}
}
} else {
String state = getWemoState(itemName);
if (state != null) {
if (provider.getUDN(itemName).toLowerCase().contains("motion")) {
State newState = state.equals("0") ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
eventPublisher.postUpdate(itemName, newState);
} else {
State itemState = state.equals("0") ? OnOffType.OFF : OnOffType.ON;
eventPublisher.postUpdate(itemName, itemState);
}
}
}
} catch (Exception e) {
logger.error("Error in execute method: " + e.getMessage(), e);
}
}
}
}
use of org.openhab.core.library.types.OnOffType in project openhab1-addons by openhab.
the class FatekColorItem method command.
@Override
public void command(FatekPLC fatekPLC, Command command) throws CommandException {
try {
final HSBType val;
if (command instanceof OnOffType) {
val = valueForOnOff(fatekPLC, command);
} else if (command instanceof IncreaseDecreaseType) {
val = valueForIncreaseDecrease(fatekPLC, command);
} else if (command instanceof HSBType) {
val = (HSBType) command;
} else {
throw new UnsupportedCommandException(this, command);
}
if (val != null) {
int v1;
int v2;
int v3;
if (isColorRGB) {
Color c = val.toColor();
v1 = c.getRed();
v2 = c.getGreen();
v3 = c.getBlue();
} else {
v1 = val.getHue().intValue();
v2 = val.getSaturation().intValue();
v3 = val.getBrightness().intValue();
}
FatekWriteMixDataCmd cmd = new FatekWriteMixDataCmd(fatekPLC);
cmd.addReg(reg1, v1);
cmd.addReg(reg2, v2);
cmd.addReg(reg3, v3);
cmd.send();
}
} catch (FatekIOException | FatekException e) {
throw new CommandException(this, command, e);
}
}
use of org.openhab.core.library.types.OnOffType in project openhab1-addons by openhab.
the class Ipx800AstableSwitch method updateStateInternal.
@Override
protected boolean updateStateInternal(Type state) {
boolean changed = false;
if (state instanceof OnOffType) {
OnOffType commandState = (OnOffType) state;
if (commandState == OnOffType.ON) {
switchState(false);
changed = true;
}
}
return changed;
}
use of org.openhab.core.library.types.OnOffType in project openhab1-addons by openhab.
the class Ipx800Mirror method updateStateInternal.
@Override
protected boolean updateStateInternal(Type state) {
boolean changed = false;
if (state instanceof OnOffType) {
OnOffType commandState = (OnOffType) state;
changed = commandState.compareTo((OnOffType) lastState) != 0;
lastState = commandState;
}
return changed;
}
Aggregations