use of org.openhab.binding.zwave.internal.protocol.event.ZWaveNodeInfoEvent in project openhab1-addons by openhab.
the class ApplicationUpdateMessageClass method handleRequest.
@Override
public boolean handleRequest(ZWaveController zController, SerialMessage lastSentMessage, SerialMessage incomingMessage) {
int nodeId;
boolean result = true;
UpdateState updateState = UpdateState.getUpdateState(incomingMessage.getMessagePayloadByte(0));
switch(updateState) {
case NODE_INFO_RECEIVED:
// We've received a NIF, and this contains the node ID.
nodeId = incomingMessage.getMessagePayloadByte(1);
logger.debug("NODE {}: Application update request. Node information received.", nodeId);
int length = incomingMessage.getMessagePayloadByte(2);
ZWaveNode node = zController.getNode(nodeId);
if (node == null) {
logger.debug("NODE {}: Application update request. Node not known!", nodeId);
// This allows everyone to be notified.
if (nodeId > 0 && nodeId <= 232) {
zController.notifyEventListeners(new ZWaveInclusionEvent(ZWaveInclusionEvent.Type.IncludeDone, incomingMessage.getMessagePayloadByte(2)));
}
break;
}
node.resetResendCount();
// Remember that we've received this so we can continue initialisation
node.setApplicationUpdateReceived(true);
// If we're finished initialisation, then we can treat this like a HAIL
if (node.getNodeInitializationStage() == ZWaveNodeInitStage.DONE) {
// If this node supports associations, then assume this should be handled through that mechanism
if (node.getCommandClass(CommandClass.ASSOCIATION) == null) {
// If we receive an Application Update Request and the node is already
// fully initialised we assume this is a request to the controller to
// re-get the current node values
logger.debug("NODE {}: Application update request. Requesting node state.", nodeId);
zController.pollNode(node);
}
} else {
List<Integer> nif = new ArrayList<Integer>();
for (int i = 6; i < length + 3; i++) {
int data = incomingMessage.getMessagePayloadByte(i);
if (data == 0xef) {
// TODO: Implement control command classes
break;
}
logger.trace(String.format("NODE %d: Command class 0x%02X is supported.", nodeId, data));
// See if the command class already exists on the node
CommandClass commandClass = CommandClass.getCommandClass(data);
if (node.getCommandClass(commandClass) == null) {
// add it
ZWaveCommandClass zwaveCommandClass = ZWaveCommandClass.getInstance(data, node, zController);
if (zwaveCommandClass != null) {
logger.trace(String.format("NODE %d: Application update request. Adding Command class %s.", nodeId, commandClass));
node.addCommandClass(zwaveCommandClass);
}
}
}
node.updateNIF(nif);
}
// Notify we received an info frame
zController.notifyEventListeners(new ZWaveNodeInfoEvent(nodeId));
// Treat the node information frame as a wakeup
ZWaveWakeUpCommandClass wakeUp = (ZWaveWakeUpCommandClass) node.getCommandClass(ZWaveCommandClass.CommandClass.WAKE_UP);
if (wakeUp != null) {
wakeUp.setAwake(true);
}
break;
case NODE_INFO_REQ_FAILED:
// Make sure we can correlate the request before we use the nodeId
if (lastSentMessage.getMessageClass() != SerialMessageClass.RequestNodeInfo) {
logger.warn("Got ApplicationUpdateMessage without request, ignoring. Last message was {}.", lastSentMessage.getMessageClass());
return false;
}
// The failed message doesn't contain the node number, so use the info from the request.
nodeId = lastSentMessage.getMessageNode();
logger.debug("NODE {}: Application update request. Node Info Request Failed.", nodeId);
// Handle retries
if (--lastSentMessage.attempts >= 0) {
logger.error("NODE {}: Got Node Info Request Failed. Requeueing", nodeId);
zController.enqueue(lastSentMessage);
} else {
logger.warn("NODE {}: Node Info Request Failed 3x. Discarding message: {}", nodeId, lastSentMessage.toString());
}
// Transaction is not successful
incomingMessage.setTransactionCanceled();
result = false;
break;
default:
logger.warn("TODO: Implement Application Update Request Handling of {} ({}).", updateState.getLabel(), updateState.getKey());
}
// Check if this completes the transaction
checkTransactionComplete(lastSentMessage, incomingMessage);
return result;
}
use of org.openhab.binding.zwave.internal.protocol.event.ZWaveNodeInfoEvent in project openhab1-addons by openhab.
the class ZWaveNodeStageAdvancer method ZWaveIncomingEvent.
@Override
public void ZWaveIncomingEvent(ZWaveEvent event) {
// If we've already completed initialisation, then we're done!
if (currentStage == ZWaveNodeInitStage.DONE) {
return;
}
// Process transaction complete events
if (event instanceof ZWaveTransactionCompletedEvent) {
ZWaveTransactionCompletedEvent completeEvent = (ZWaveTransactionCompletedEvent) event;
SerialMessage serialMessage = completeEvent.getCompletedMessage();
byte[] payload = serialMessage.getMessagePayload();
// Make sure this is addressed to us
if (payload.length == 0 || node.getNodeId() != (payload[0] & 0xFF)) {
// This will allow battery devices stuck in WAIT state to get moving.
if (controller.getSendQueueLength() < 2 && currentStage == ZWaveNodeInitStage.WAIT) {
logger.debug("NODE {}: Node advancer - WAIT: The WAIT is over!", node.getNodeId());
currentStage = currentStage.getNextStage();
handleNodeQueue(null);
}
return;
}
switch(serialMessage.getMessageClass()) {
case SendData:
case IdentifyNode:
case RequestNodeInfo:
case GetRoutingInfo:
case IsFailedNodeID:
logger.debug("NODE {}: Node advancer - {}: Transaction complete ({}:{}) success({})", node.getNodeId(), currentStage.toString(), serialMessage.getMessageClass(), serialMessage.getMessageType(), completeEvent.getState());
// If this frame was successfully sent, then handle the stage advancer
if (((ZWaveTransactionCompletedEvent) event).getState() == true) {
handleNodeQueue(serialMessage);
}
break;
default:
break;
}
} else if (event instanceof ZWaveWakeUpCommandClass.ZWaveWakeUpEvent) {
// WAKEUP EVENT - only act if this is a wakeup notification
if (((ZWaveWakeUpCommandClass.ZWaveWakeUpEvent) event).getEvent() != ZWaveWakeUpCommandClass.WAKE_UP_NOTIFICATION) {
return;
}
// A wakeup event is received. Make sure it's for this node
if (node.getNodeId() != event.getNodeId()) {
return;
}
logger.debug("NODE {}: Wakeup during initialisation.", node.getNodeId());
wakeupCount++;
advanceNodeStage(null);
} else if (event instanceof ZWaveNodeStatusEvent) {
ZWaveNodeStatusEvent statusEvent = (ZWaveNodeStatusEvent) event;
// A network status event is received. Make sure it's for this node.
if (node.getNodeId() != event.getNodeId()) {
return;
}
logger.debug("NODE {}: Node Status event during initialisation - Node is {}", statusEvent.getNodeId(), statusEvent.getState());
switch(statusEvent.getState()) {
case DEAD:
case FAILED:
break;
case ALIVE:
advanceNodeStage(null);
break;
}
logger.trace("NODE {}: Node Status event during initialisation processed", statusEvent.getNodeId());
} else if (event instanceof ZWaveNodeInfoEvent) {
logger.debug("NODE {}: {} NIF event during initialisation stage {}", event.getNodeId(), node.getNodeId(), currentStage);
if (node.getNodeId() != event.getNodeId()) {
return;
}
if (currentStage == ZWaveNodeInitStage.PING) {
logger.debug("NODE {}: NIF event during initialisation stage PING - advancing", event.getNodeId());
setCurrentStage(currentStage.getNextStage());
}
logger.debug("NODE {}: NIF event during initialisation stage {}", event.getNodeId(), currentStage);
advanceNodeStage(null);
/*
* } else if (event instanceof ZWaveCommandClassValueEvent) {
* // This code is used to detect an event during the IDLE stage.
* // This is used to kick start the initialisation for battery nodes that do not support
* // the WAKE_UP class and don't send the ApplicationUpdateMessage when they are initialised.
* logger.debug("NODE {}: {} CC event during initialisation stage {}", event.getNodeId(), node.getNodeId(),
* currentStage);
* // A command class event is received. Make sure it's for this node.
* if (node.getNodeId() != event.getNodeId() || currentStage != ZWaveNodeInitStage.PING) {
* return;
* }
* logger.debug("NODE {}: CC event during initialisation stage PING - advancing", event.getNodeId());
* setCurrentStage(currentStage.getNextStage());
* logger.debug("NODE {}: CC event during initialisation stage PING - now {} - next", event.getNodeId(),
* currentStage);
* advanceNodeStage(null);
*/
}
}
Aggregations