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 PilightBinding method createUpdateCommand.
private Action createUpdateCommand(Command command, PilightBindingConfig config) {
Action action = new Action(Action.ACTION_CONTROL);
Code code = new Code();
code.setDevice(config.getDevice());
if (command instanceof OnOffType) {
setOnOffValue((OnOffType) command, code);
} else if (command instanceof PercentType) {
setDimmerValue((PercentType) command, code);
} else {
logger.error("Only OnOffType and PercentType can be changed by the pilight binding");
return null;
}
action.setCode(code);
return action;
}
use of org.openhab.core.library.types.OnOffType in project openhab1-addons by openhab.
the class ZWaveIndicatorConverter method receiveCommand.
/**
* {@inheritDoc}
*
* The node's indicator status can change at any time, and without the knowledge of the Z-Wave binding so
* before any changes are made to the indicator we need to get its actual value from the device.
* Hence the receiveCommand method adds the desired command to a "pending" list, and issues an INDICATOR_GET
* it does not change the actual device Indicator value.
*
* When the node responds with the INDICATOR_REPORT pending commands are applied to the indicator value
* and the result is then sent to the node. This is performed in the handleEvent() method.
*
* Pending changes are stored in a hash of hashes with the following keys "ItemName" and "bit" value is 1
* for turn bit ON, -1 to turn the bound bit OFF, 0 no change.
*
* HashMap<ItemName, HashMap<bit, pendingOperationCode>>
*/
@Override
public void receiveCommand(Item item, Command command, ZWaveNode node, ZWaveIndicatorCommandClass commandClass, int endpointId, Map<String, String> arguments) {
ZWaveCommandConverter<?, ?> converter = this.getCommandConverter(command.getClass());
if (converter == null) {
logger.warn("NODE {}: No converter found for item = {}, endpoint = {}, ignoring command.", node.getNodeId(), item.getName(), endpointId);
return;
}
if (!arguments.containsKey("bit")) {
logger.error("NODE {}: You must specify an Indicator bit for item = {}, endpoint = {}, ignoring command.", node.getNodeId(), item.getName(), endpointId);
return;
}
int bit = Integer.parseInt(arguments.get("bit"));
if (!(command instanceof OnOffType)) {
return;
}
if (command == OnOffType.ON) {
HashMap<Integer, Integer> h = new HashMap<Integer, Integer>();
h.put(bit, PENDING_ON);
pending.put(item.getName(), h);
logger.warn(String.format("NODE %d: Item %s Set bit = %d to ON - PENDING", node.getNodeId(), item.getName(), bit));
} else {
logger.warn(String.format("NODE %d: Item %s Set bit = %d to OFF - PENDING", node.getNodeId(), item.getName(), bit));
HashMap<Integer, Integer> h = new HashMap<Integer, Integer>();
h.put(bit, PENDING_OFF);
pending.put(item.getName(), h);
}
// Issue a INDICATOR_GET message to the node to get the updated indicator value
SerialMessage serialMessage = node.encapsulate(commandClass.getValueMessage(), commandClass, endpointId);
if (serialMessage == null) {
logger.warn("NODE {}: Generating message failed for command class = {}, endpoint = {}", node.getNodeId(), commandClass.getCommandClass().getLabel(), endpointId);
return;
}
this.getController().sendData(serialMessage);
logger.warn(String.format("NODE %d: Item %s bit = %d Get current Indicator State", node.getNodeId(), item.getName(), bit));
if (command instanceof State) {
this.getEventPublisher().postUpdate(item.getName(), (State) command);
}
}
use of org.openhab.core.library.types.OnOffType in project openhab1-addons by openhab.
the class ZWaveMultiLevelSwitchConverter method receiveCommand.
/**
* {@inheritDoc}
*/
@Override
public void receiveCommand(Item item, Command command, ZWaveNode node, ZWaveMultiLevelSwitchCommandClass commandClass, int endpointId, Map<String, String> arguments) {
SerialMessage serialMessage = null;
String restoreLastValue = null;
if (command instanceof StopMoveType && (StopMoveType) command == StopMoveType.STOP) {
// special handling for the STOP command
serialMessage = commandClass.stopLevelChangeMessage();
} else {
ZWaveCommandConverter<?, ?> converter = null;
if (command instanceof OnOffType) {
restoreLastValue = arguments.get("restore_last_value");
if ("true".equalsIgnoreCase(restoreLastValue)) {
converter = this.restoreValueOnOffConverter;
} else {
converter = this.normalOnOffConverter;
}
} else {
converter = this.getCommandConverter(command.getClass());
}
if (converter == null) {
logger.warn("NODE {}: No converter found for item = {}, endpoint = {}, ignoring command.", node.getNodeId(), item.getName(), endpointId);
return;
}
// Allow inversion of roller shutter UP/DOWN
if (converter instanceof MultiLevelUpDownCommandConverter) {
logger.debug("Multilevel Switch MultiLevelUpDownCommandConverter");
if ("true".equalsIgnoreCase(arguments.get("invert_state"))) {
logger.trace("Multilevel Switch MultiLevelUpDownCommandConverter - invert");
if (command == UpDownType.UP) {
command = UpDownType.DOWN;
} else {
command = UpDownType.UP;
}
logger.trace("Multilevel Switch MultiLevelUpDownCommandConverter - inverted: {}", command);
}
}
// Allow inversion of roller shutter PERCENT value
if (converter instanceof MultiLevelPercentCommandConverter) {
logger.debug("Multilevel Switch MultiLevelPercentCommandConverter");
if ("true".equalsIgnoreCase(arguments.get("invert_percent"))) {
logger.trace("Multilevel Switch MultiLevelPercentCommandConverter - invert");
command = new PercentType(100 - ((DecimalType) command).intValue());
logger.trace("Multilevel Switch MultiLevelPercentCommandConverter - inverted: {}", command);
}
}
Integer value = (Integer) converter.convertFromCommandToValue(item, command);
logger.trace("NODE {}: Converted command '{}' to value {} for item = {}, endpoint = {}.", node.getNodeId(), command.toString(), value, item.getName(), endpointId);
serialMessage = commandClass.setValueMessage(value);
}
// encapsulate the message in case this is a multi-instance node
serialMessage = node.encapsulate(serialMessage, commandClass, endpointId);
if (serialMessage == null) {
logger.warn("Generating message failed for command class = {}, node = {}, endpoint = {}", commandClass.getCommandClass().getLabel(), node.getNodeId(), endpointId);
return;
}
this.getController().sendData(serialMessage);
// update the bus in case of normal dimming. schedule refresh in case of restore to last value dimming.
if (!"true".equalsIgnoreCase(restoreLastValue) && command instanceof OnOffType && (OnOffType) command == OnOffType.ON) {
executeRefresh(node, commandClass, endpointId, arguments);
} else if (command instanceof State) {
this.getEventPublisher().postUpdate(item.getName(), (State) command);
}
}
use of org.openhab.core.library.types.OnOffType in project openhab1-addons by openhab.
the class XplBinding method handleXPLMessage.
@Override
public void handleXPLMessage(xPL_MessageI theMessage) {
for (XplBindingProvider provider : providers) {
List<String> matchingItems = provider.hasMessage(theMessage);
for (String itemName : matchingItems) {
XplBindingConfig config = provider.getConfig(itemName);
if (config == null) {
continue;
}
String current = theMessage.getNamedValue(config.NamedParameter);
Item item = provider.getItem(itemName);
if (item != null) {
if (item instanceof SwitchItem) {
OnOffType status = (current.equalsIgnoreCase("on") || current.equalsIgnoreCase("true") || current.equalsIgnoreCase("1") || current.equalsIgnoreCase("open") || current.equalsIgnoreCase("high")) ? OnOffType.ON : OnOffType.OFF;
synchronized (item) {
if (!item.getState().equals(status)) {
eventPublisher.postUpdate(itemName, status);
((SwitchItem) item).setState(status);
}
}
} else if (item instanceof ContactItem) {
OpenClosedType status = (current.equalsIgnoreCase("on") || current.equalsIgnoreCase("true") || current.equalsIgnoreCase("1") || current.equalsIgnoreCase("open") || current.equalsIgnoreCase("high")) ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
synchronized (item) {
if (!item.getState().equals(status)) {
eventPublisher.postUpdate(itemName, status);
((ContactItem) item).setState(status);
}
}
} else if (item instanceof NumberItem) {
DecimalType value = new DecimalType(current);
synchronized (item) {
if (!item.getState().equals(value)) {
eventPublisher.postUpdate(itemName, value);
((NumberItem) item).setState(value);
}
}
} else if (item instanceof StringItem) {
StringType value = new StringType(current);
synchronized (item) {
if (!item.getState().equals(value)) {
eventPublisher.postUpdate(itemName, value);
((StringItem) item).setState(value);
}
}
}
}
}
}
}
Aggregations