use of org.openhab.binding.zwave.ZWaveBindingConfig in project openhab1-addons by openhab.
the class ZWaveConverterHandler method executeRefresh.
/**
* Execute refresh method. This method is called every time a binding item
* is refreshed and the corresponding node should be sent a message.
*
* @param provider
* the {@link ZWaveBindingProvider} that provides the item
* @param itemName
* the name of the item to poll.
* @param forceRefresh
* indicates that a polling refresh should be forced.
*/
@SuppressWarnings("unchecked")
public void executeRefresh(ZWaveBindingProvider provider, String itemName, boolean forceRefresh) {
ZWaveBindingConfig bindingConfiguration = provider.getZwaveBindingConfig(itemName);
ZWaveCommandClass commandClass;
String commandClassName = bindingConfiguration.getArguments().get("command");
// this binding is configured not to poll.
if (!forceRefresh && bindingConfiguration.getRefreshInterval() != null && 0 == bindingConfiguration.getRefreshInterval()) {
return;
}
ZWaveNode node = this.controller.getNode(bindingConfiguration.getNodeId());
// ignore nodes that are not initialized.
if (node == null) {
return;
}
if (commandClassName != null) {
// this is a report item, handle it with the report info converter.
if (commandClassName.equalsIgnoreCase("info")) {
infoConverter.executeRefresh(provider.getItem(itemName), node, bindingConfiguration.getEndpoint(), bindingConfiguration.getArguments());
return;
}
// ignore nodes that are not initialized or dead.
if (node.getNodeState() != ZWaveNodeState.ALIVE || node.isInitializationComplete() == false) {
return;
}
commandClass = node.resolveCommandClass(CommandClass.getCommandClass(commandClassName), bindingConfiguration.getEndpoint());
if (commandClass == null) {
logger.warn("No command class found for item = {}, command class name = {}, ignoring execute refresh.", itemName, commandClassName);
return;
}
} else {
commandClass = resolveConverter(provider.getItem(itemName), node, bindingConfiguration.getEndpoint());
}
if (commandClass == null) {
logger.warn("No converter found for item = {}, ignoring execute refresh.", itemName);
return;
}
ZWaveCommandClassConverter<ZWaveCommandClass> converter = (ZWaveCommandClassConverter<ZWaveCommandClass>) getConverter(commandClass.getCommandClass());
if (converter == null) {
logger.warn("No converter found for item = {}, ignoring execute refresh.", itemName);
return;
}
if (bindingConfiguration.getRefreshInterval() == null) {
bindingConfiguration.setRefreshInterval(converter.getRefreshInterval());
// this binding is configured not to poll.
if (!forceRefresh && 0 == bindingConfiguration.getRefreshInterval()) {
return;
}
}
// not enough time has passed to refresh the item.
if (!forceRefresh && bindingConfiguration.getLastRefreshed() != null && (bindingConfiguration.getLastRefreshed().getTime() + (bindingConfiguration.getRefreshInterval() * 1000) > Calendar.getInstance().getTimeInMillis())) {
return;
}
bindingConfiguration.setLastRefreshed(Calendar.getInstance().getTime());
SerialMessage serialMessage = converter.executeRefresh(node, commandClass, bindingConfiguration.getEndpoint(), bindingConfiguration.getArguments());
if (serialMessage == null) {
logger.warn("NODE {}: Generating message failed for command class = {}", node.getNodeId(), commandClass.getCommandClass().getLabel());
return;
}
// This is a poll - treat it as a low priority!
serialMessage.setPriority(SerialMessagePriority.Poll);
// Queue the message
this.controller.sendData(serialMessage);
}
use of org.openhab.binding.zwave.ZWaveBindingConfig in project openhab1-addons by openhab.
the class ZWaveConverterHandler method receiveCommand.
/**
* Receives a command from openHAB and translates it to an operation on the
* Z-Wave network.
*
* @param provider
* the {@link ZWaveBindingProvider} that provides the item
* @param itemName
* the name of the item that will receive the event.
* @param command
* the received {@link Command}
*/
@SuppressWarnings("unchecked")
public void receiveCommand(ZWaveBindingProvider provider, String itemName, Command command) {
ZWaveBindingConfig bindingConfiguration = provider.getZwaveBindingConfig(itemName);
ZWaveNode node = this.controller.getNode(bindingConfiguration.getNodeId());
if (node == null) {
logger.error("Item {} has non existant node {}", itemName, bindingConfiguration.getNodeId());
return;
}
ZWaveCommandClass commandClass;
String commandClassName = bindingConfiguration.getArguments().get("command");
if (commandClassName != null) {
if (node.getNodeId() == this.controller.getOwnNodeId() && commandClassName.equalsIgnoreCase("switch_all")) {
commandClass = ZWaveCommandClass.getInstance(0x27, node, this.controller);
} else {
commandClass = node.resolveCommandClass(CommandClass.getCommandClass(commandClassName), bindingConfiguration.getEndpoint());
if (commandClass == null) {
logger.warn("NODE {}: No command class found for item = {}. Class = {}({}), endpoint = {}. Ignoring command.", node.getNodeId(), itemName, commandClassName, CommandClass.getCommandClass(commandClassName).toString(), bindingConfiguration.getEndpoint());
return;
}
}
} else {
commandClass = resolveConverter(provider.getItem(itemName), node, bindingConfiguration.getEndpoint());
}
if (commandClass == null) {
logger.warn("NODE {}: No converter found for item = {}, ignoring command.", node.getNodeId(), itemName);
return;
}
ZWaveCommandClassConverter<ZWaveCommandClass> converter = (ZWaveCommandClassConverter<ZWaveCommandClass>) getConverter(commandClass.getCommandClass());
if (converter == null) {
logger.warn("NODE {}: No converter found for item = {}, ignoring command.", node.getNodeId(), itemName);
return;
}
converter.receiveCommand(provider.getItem(itemName), command, node, commandClass, bindingConfiguration.getEndpoint(), bindingConfiguration.getArguments());
}
use of org.openhab.binding.zwave.ZWaveBindingConfig in project openhab1-addons by openhab.
the class ZWaveActiveBinding method handleZWaveCommandClassValueEvent.
/**
* Handle an incoming Command class value event
*
* @param event the incoming Z-Wave event.
*/
private void handleZWaveCommandClassValueEvent(ZWaveCommandClassValueEvent event) {
boolean handled = false;
logger.debug("NODE {}: Got a value event from Z-Wave network, endpoint = {}, command class = {}, value = {}", new Object[] { event.getNodeId(), event.getEndpoint(), event.getCommandClass().getLabel(), event.getValue() });
for (ZWaveBindingProvider provider : providers) {
for (String itemName : provider.getItemNames()) {
ZWaveBindingConfig bindingConfig = provider.getZwaveBindingConfig(itemName);
if (bindingConfig.getNodeId() != event.getNodeId() || bindingConfig.getEndpoint() != event.getEndpoint()) {
continue;
}
converterHandler.handleEvent(provider, itemName, event);
handled = true;
}
}
if (!handled) {
logger.warn("NODE {}: No item bound for event, endpoint = {}, command class = {}, value = {}, ignoring.", new Object[] { event.getNodeId(), event.getEndpoint(), event.getCommandClass().getLabel(), event.getValue() });
}
}
use of org.openhab.binding.zwave.ZWaveBindingConfig in project openhab1-addons by openhab.
the class ZWaveActiveBinding method bindingChanged.
/**
* Called, if a single binding has changed. The given item could have been
* added or removed. We refresh the binding in case it's in the done stage.
*
* @param provider the binding provider where the binding has changed
* @param itemName the item name for which the binding has changed
*/
@Override
public void bindingChanged(BindingProvider provider, String itemName) {
logger.trace("bindingChanged {}", itemName);
ZWaveBindingProvider zProvider = (ZWaveBindingProvider) provider;
if (zProvider != null) {
ZWaveBindingConfig bindingConfig = zProvider.getZwaveBindingConfig(itemName);
if (bindingConfig != null && converterHandler != null) {
converterHandler.executeRefresh(zProvider, itemName, true);
}
}
// Bindings have changed - rebuild the polling table
rebuildPollingTable();
super.bindingChanged(provider, itemName);
}
use of org.openhab.binding.zwave.ZWaveBindingConfig in project openhab1-addons by openhab.
the class ZWaveActiveBinding method rebuildPollingTable.
/**
* This method rebuilds the polling table. The polling table is a list of items that have
* polling enabled (ie a refresh interval is set). This list is then checked periodically
* and any item that has passed its polling interval will be polled.
*/
private void rebuildPollingTable() {
// Rebuild the polling table
pollingList.clear();
if (converterHandler == null) {
logger.debug("ConverterHandler not initialised. Polling disabled.");
return;
}
// Loop all binding providers for the Z-wave binding.
for (ZWaveBindingProvider eachProvider : providers) {
// Loop all bound items for this provider
for (String name : eachProvider.getItemNames()) {
// Find the node and check if it's completed initialisation.
ZWaveBindingConfig cfg = eachProvider.getZwaveBindingConfig(name);
ZWaveNode node = this.zController.getNode(cfg.getNodeId());
if (node == null) {
logger.debug("NODE {}: Polling list: can't get node for item {}", cfg.getNodeId(), name);
continue;
}
if (node.getNodeInitializationStage() != ZWaveNodeInitStage.DONE) {
logger.debug("NODE {}: Polling list: item {} is not completed initialisation", cfg.getNodeId(), name);
continue;
}
logger.trace("Polling list: Checking {} == {}", name, converterHandler.getRefreshInterval(eachProvider, name));
// If this binding is configured to poll - add it to the list
if (converterHandler.getRefreshInterval(eachProvider, name) > 0) {
ZWavePollItem item = new ZWavePollItem();
item.item = name;
item.provider = eachProvider;
pollingList.add(item);
logger.trace("Polling list added {}", name);
}
}
}
pollingIterator = null;
}
Aggregations