Skip to content

Commit

Permalink
V2 fully event driven implementation is now working, removed all conv…
Browse files Browse the repository at this point in the history
…ersion mechanisms and they are now based on event receivers
  • Loading branch information
renarj committed Apr 5, 2015
1 parent 3f88116 commit 252cc85
Show file tree
Hide file tree
Showing 65 changed files with 561 additions and 736 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
Expand Down Expand Up @@ -99,7 +99,7 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.oberasoftware.home.zwave;

import com.oberasoftware.base.event.EventBus;
import com.oberasoftware.base.event.EventHandler;
import com.oberasoftware.base.event.EventSubscribe;
import com.oberasoftware.home.zwave.api.ZWaveAction;
import com.oberasoftware.home.zwave.api.ZWaveDeviceAction;
import com.oberasoftware.home.zwave.api.events.controller.ControllerEvent;
Expand All @@ -9,6 +11,8 @@
import com.oberasoftware.home.zwave.core.NodeManager;
import com.oberasoftware.home.zwave.core.ZWaveNode;
import com.oberasoftware.home.zwave.exceptions.HomeAutomationException;
import com.oberasoftware.home.zwave.messages.ZWaveRawMessage;
import com.oberasoftware.home.zwave.threading.ActionConvertedEvent;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand All @@ -22,7 +26,7 @@
* @author renarj
*/
@Component
public class TransactionManagerImpl implements TransactionManager {
public class TransactionManagerImpl implements TransactionManager, EventHandler {
private static final Logger LOG = getLogger(TransactionManagerImpl.class);

@Autowired
Expand All @@ -49,12 +53,12 @@ public int startAction(ZWaveAction action) throws HomeAutomationException {
} else {
int callbackId = getCallbackId();

if (isDeviceReady(action)) {
// if (isDeviceReady(action)) {
eventBus.publish(action, callbackId);
} else {
LOG.debug("Starting action on battery device: {} that could be asleep", action);
eventBus.publish(new WaitForWakeUpAction((ZWaveDeviceAction) action, callbackId));
}
// } else {
// LOG.debug("Starting action on battery device: {} that could be asleep", action);
// eventBus.publish(new WaitForWakeUpAction((ZWaveDeviceAction) action, callbackId));
// }

return callbackId;
}
Expand All @@ -73,15 +77,14 @@ public int startAction(ZWaveAction action) throws HomeAutomationException {
// }
// }
//
// @EventSubscribe
// public void receiveSendEvent(SendDataCommand sendDataCommand) throws HomeAutomationException {
//
// ZWaveRawMessage rawMessage = sendDataCommand.getRawMessage();
// rawMessage.setCallbackId(callbackId);
// rawMessage.setTransmitOptions(0x01 | 0x04 | 0x20);
//
// connector.send(sendDataCommand.getRawMessage());
// }
@EventSubscribe
public void receiveSendEvent(ActionConvertedEvent convertedAction) throws HomeAutomationException {
LOG.debug("Converted action: {} sending to controller", convertedAction);
ZWaveRawMessage rawMessage = convertedAction.getRawMessage();
rawMessage.setTransmitOptions(0x01 | 0x04 | 0x20);

connector.send(rawMessage);
}

private boolean isDeviceReady(ZWaveAction action) {
if(action instanceof ZWaveDeviceAction) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.oberasoftware.home.zwave;

import com.oberasoftware.base.BaseConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
* @author renarj
*/
@Configuration
@ComponentScan
@Import(BaseConfiguration.class)
public class ZWaveConfiguration {
}
19 changes: 10 additions & 9 deletions src/main/java/com/oberasoftware/home/zwave/ZWaveController.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.oberasoftware.home.zwave;

import com.oberasoftware.base.event.EventBus;
import com.oberasoftware.base.event.EventHandler;
import com.oberasoftware.base.event.EventSubscribe;
import com.oberasoftware.home.zwave.api.Controller;
import com.oberasoftware.home.zwave.api.ZWaveAction;
import com.oberasoftware.home.zwave.api.actions.controller.ControllerCapabilitiesAction;
import com.oberasoftware.home.zwave.api.actions.controller.ControllerInitialDataAction;
import com.oberasoftware.home.zwave.api.actions.controller.GetControllerIdAction;
import com.oberasoftware.home.zwave.api.events.EventListener;
import com.oberasoftware.home.zwave.api.events.EventBus;
import com.oberasoftware.home.zwave.exceptions.HomeAutomationException;
import com.oberasoftware.home.zwave.api.Controller;
import com.oberasoftware.home.zwave.api.ZWaveAction;
import com.oberasoftware.home.zwave.api.events.controller.ControllerIdEvent;
import com.oberasoftware.home.zwave.connector.ControllerConnector;
import com.oberasoftware.home.zwave.core.NodeManager;
import com.oberasoftware.home.zwave.core.NodeStatus;
import com.oberasoftware.home.zwave.exceptions.HomeAutomationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -23,7 +24,7 @@
* @author renarj
*/
@Component
public class ZWaveController implements Controller, EventListener<ControllerIdEvent> {
public class ZWaveController implements Controller, EventHandler {
private static final Logger LOG = LoggerFactory.getLogger(ZWaveController.class);

@Autowired
Expand Down Expand Up @@ -65,8 +66,8 @@ public void initializeNetwork() {
}

@Override
public <T> void subscribe(EventListener<T> eventListener) {
eventBus.addListener(eventListener);
public <T> void subscribe(EventHandler eventListener) {
eventBus.registerHandler(eventListener);
}

@Override
Expand All @@ -84,7 +85,7 @@ public int getControllerId() {
return controllerIdEvent != null ? controllerIdEvent.getControllerId() : -1;
}

@Override
@EventSubscribe
public void receive(ControllerIdEvent event) throws Exception {
LOG.info("Received controller information: {}", event);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.oberasoftware.home.zwave;

import com.oberasoftware.base.event.EventHandler;
import com.oberasoftware.home.zwave.api.ZWaveAction;
import com.oberasoftware.home.zwave.api.ZWaveIntervalAction;
import com.oberasoftware.home.zwave.api.ZWaveScheduler;
import com.oberasoftware.home.zwave.api.ZWaveSession;
import com.oberasoftware.home.zwave.api.events.EventListener;
import com.oberasoftware.home.zwave.core.NodeManager;
import com.oberasoftware.home.zwave.exceptions.HomeAutomationException;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -33,7 +33,7 @@ public boolean isNetworkReady() {
}

@Override
public void subscribe(EventListener eventListener) {
public void subscribe(EventHandler eventListener) {
zWaveController.subscribe(eventListener);
}

Expand Down
8 changes: 0 additions & 8 deletions src/main/java/com/oberasoftware/home/zwave/api/Action.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.oberasoftware.home.zwave.api;

import com.oberasoftware.home.zwave.api.events.EventListener;
import com.oberasoftware.base.event.EventHandler;
import com.oberasoftware.home.zwave.exceptions.HomeAutomationException;

/**
Expand All @@ -11,7 +11,7 @@ public interface Controller {

int getControllerId();

<T> void subscribe(EventListener<T> topicListener);
<T> void subscribe(EventHandler topicListener);

int send(ZWaveAction message) throws HomeAutomationException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
/**
* @author renarj
*/
public interface ZWaveAction extends ZWaveMessage, Action {
public interface ZWaveAction extends ZWaveMessage {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.oberasoftware.home.zwave.api;

import com.oberasoftware.home.zwave.api.events.EventListener;
import com.oberasoftware.base.event.EventHandler;
import com.oberasoftware.home.zwave.core.NodeManager;
import com.oberasoftware.home.zwave.exceptions.HomeAutomationException;

Expand All @@ -13,7 +13,7 @@ public interface ZWaveSession {

boolean isNetworkReady();

void subscribe(EventListener eventListener);
void subscribe(EventHandler eventListener);

NodeManager getDeviceManager();

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.oberasoftware.home.zwave.converter;

import com.oberasoftware.home.zwave.messages.ZWaveRawMessage;
import com.oberasoftware.home.zwave.messages.types.CommandClass;
import com.oberasoftware.home.zwave.messages.types.ControllerMessageType;
import com.oberasoftware.home.zwave.messages.types.MessageType;
import com.oberasoftware.home.zwave.threading.ActionConvertedEvent;
import org.slf4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static org.slf4j.LoggerFactory.getLogger;

/**
* @author renarj
*/
public class ActionConverterBuilder {
private static final Logger LOG = getLogger(ActionConverterBuilder.class);

private final List<Byte> payload = new ArrayList<>();
private final Optional<Integer> nodeId;
private final ControllerMessageType controllerMessageType;
private final MessageType messageType;

private Optional<Integer> callbackId = Optional.empty();

public ActionConverterBuilder(Optional<Integer> nodeId, ControllerMessageType controllerMessageType, MessageType messageType) {
this.nodeId = nodeId;
this.controllerMessageType = controllerMessageType;
this.messageType = messageType;
}

public static ActionConverterBuilder create(ControllerMessageType controllerMessageType, MessageType messageType) {
return new ActionConverterBuilder(Optional.empty(), controllerMessageType, messageType);
}

public static ActionConverterBuilder create(ControllerMessageType controllerMessageType, MessageType messageType, int nodeId) {
return new ActionConverterBuilder(Optional.of(nodeId), controllerMessageType, messageType);
}

public ActionConverterBuilder callback(int callbackId) {
this.callbackId = Optional.of(callbackId);
return this;
}

public ActionConverterBuilder addByte(byte b) {
payload.add(b);

return this;
}

public ActionConverterBuilder addInt(int i) {
payload.add((byte)i);
return this;
}

public ActionConverterBuilder addCommandClass(CommandClass commandClass) {
addInt(commandClass.getClassCode());
return this;
}

public ActionConvertedEvent construct() {
ZWaveRawMessage message;
//we get the initial payload lenght without any potential nodeId
int payloadLenght = payload.size();

if(nodeId.isPresent()) {
message = new ZWaveRawMessage(nodeId.get(), controllerMessageType, messageType);
//in case a nodeId is known this will always be the first byte
payload.add(0, nodeId.get().byteValue());
} else {
message = new ZWaveRawMessage(controllerMessageType, messageType);
}
if(callbackId.isPresent()) {
message.setCallbackId(callbackId.get());
}

if(payloadLenght != 0) {
//when there is a payload we specify the length, this is always the 2nd byte
payload.add(1, (byte) payloadLenght);
}

//It could be an empty payload in case of controller messages
if(!payload.isEmpty()) {
byte[] buffer = new byte[payload.size()];
for (int i = 0; i < payload.size(); i++) {
buffer[i] = payload.get(i);
}
message.setMessage(buffer);
}

LOG.trace("Message converted: {}", message);

return new ActionConvertedEvent(message);
}
}

This file was deleted.

Loading

0 comments on commit 252cc85

Please sign in to comment.