Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix orvibo allone learn process and added flags: #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions orvibo-sdk.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="EclipseModuleManager">
<conelement value="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER" />
<src_description expected_position="0">
<src_folder value="file://$MODULE_DIR$/src/main/java" expected_position="0" />
<src_folder value="file://$MODULE_DIR$/src/test/resources" expected_position="1" />
<src_folder value="file://$MODULE_DIR$/src/test/java" expected_position="2" />
</src_description>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER" level="application" />
<orderEntry type="inheritedJdk" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.2" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-collections4:4.1" level="project" />
<orderEntry type="library" name="Maven: com.google.guava:guava:19.0" level="project" />
<orderEntry type="library" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" name="Maven: org.mockito:mockito-core:2.0.52-beta" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: net.bytebuddy:byte-buddy:1.3.16" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: org.objenesis:objenesis:2.1" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.0" level="project" />
</component>
</module>
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<version>1.1.1-SNAPSHOT</version>
<name>Orvibo SDK</name>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,12 @@ private void createAllOne(Message message) {
* @throws OrviboException
*/
public void handle(Message message) throws OrviboException {
OrviboDevice device = getDevice(message.getDeviceId());
if(isValidResponse(message)) {
if (device!=null) device.setLastOperationSuccess(true);
handleInternal(message);
} else {
if (device!=null) device.setLastOperationSuccess(false);
handleInvalidResponse(message);
}
}
Expand Down Expand Up @@ -227,4 +230,4 @@ protected void handleInvalidResponse(Message message) throws OrviboException {



}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.tavalin.orvibo.commands;

import com.github.tavalin.orvibo.devices.AllOne;
import com.github.tavalin.orvibo.devices.OrviboDevice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -31,6 +33,11 @@ protected Logger getLogger() {

@Override
protected void handleInternal(Message message) {
byte[] payload = message.getCommandPayload();
String deviceId = getDeviceId(payload);
OrviboDevice device = getDevice(deviceId);
AllOne allone = (AllOne) device;
allone.setStatus(AllOne.IDLE);
logger.debug("Handling emitting response");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ protected void handleInternal(Message message) {
byte[] data = Arrays.copyOfRange(in, ALLONE_COMMAND_START, in.length);
AllOne allone = (AllOne) device;
allone.saveLearnedData(data);
//System.out.println("Memorizzato comando lungo "+in.length);
allone.setStatus(AllOne.IDLE);
}else {
AllOne allone = (AllOne) device;
allone.setStatus(AllOne.LEARNING);
}
} catch (IOException e) {
logger.error(e.getMessage());
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/github/tavalin/orvibo/devices/AllOne.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,33 @@

public class AllOne extends OrviboDevice {

public static final int IDLE = 1;
public static final int EMITTING = 2;
public static final int LEARN_INIT = 3;
public static final int LEARNING = 4;




/** The Constant logger. */
private final Logger logger = LoggerFactory.getLogger(AllOne.class);

private Path learnPath = null;
private int status=IDLE;

public AllOne() {
super(DeviceType.ALLONE);
}

public void emit(Path file) throws IOException {
this.setStatus(EMITTING);
Message message = CommandFactory.createEmitCommand(this, file);
OrviboClient orviboClient = getNetworkContext();
orviboClient.sendMessage(message);
}

public void learn(Path file) {
this.setStatus(LEARN_INIT);
Message message = CommandFactory.createLearnCommand(this);
setLearnPath(file);
OrviboClient orviboClient = getNetworkContext();
Expand Down Expand Up @@ -62,4 +73,11 @@ public void setLearnPath(Path learnPath) {
logger.debug("Learn path set to {}", learnPath.toAbsolutePath());
}

public void setStatus(int status) {
this.status=status;
}

public int getStatus() {
return status;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public abstract class OrviboDevice {
private OrviboClient orviboClient;
private String label;
private DeviceType deviceType;
protected boolean lastOperationSuccess;

public OrviboDevice(DeviceType type) {
setDeviceType(type);
Expand Down Expand Up @@ -80,9 +81,13 @@ public DeviceType getDeviceType() {
public void setDeviceType(DeviceType deviceType) {
this.deviceType = deviceType;
}




public boolean isLastOperationSuccess() {
return lastOperationSuccess;
}

public void setLastOperationSuccess(boolean lastOperationSuccess) {
this.lastOperationSuccess = lastOperationSuccess;
}
}
15 changes: 5 additions & 10 deletions src/main/java/com/github/tavalin/orvibo/network/PacketHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class PacketHandler {

private ByteBuffer byteBuffer = null;
private static final byte[] HEADER = new byte[] { 0x68, 0x64 };
private final static int MAX_SIZE = 0xFF;
private final static int MAX_SIZE = 1024;
/** The listeners. */
private List<MessageListener> listeners;
private InetAddress remote;
Expand Down Expand Up @@ -95,8 +95,9 @@ public void setRemote(InetAddress remote) {
}

public synchronized void packetReceived(DatagramPacket packet) throws OrviboException {
byte[] bytes = Arrays.copyOfRange(packet.getData(), packet.getOffset(), packet.getLength());
logger.debug("<-- {} - {}", packet.getAddress(), MessageUtils.toPrettyHexString(bytes));
byte[] bytes = Arrays.copyOfRange(packet.getData(), packet.getOffset(), packet.getLength());
//System.out.println("<-- "+packet.getAddress()+" - {"+MessageUtils.toPrettyHexString(bytes)+"}");
logger.debug("<-- {} - {}", packet.getAddress(), MessageUtils.toPrettyHexString(bytes));
if (!packetAlreadyReceived(packet)) {
previousPackets.add(packet);
processPacketBytes(bytes);
Expand Down Expand Up @@ -129,19 +130,13 @@ private void processPacketBytes(byte[] bytes) throws OrviboException {
if (bufferContains(HEADER)) {
int expectedLength = getExpectedLength();
if (bufferPosition < expectedLength) {
// we haven't yet received what we expected so wait for next
// packet
logger.debug("Waiting for further packets.");
return;
} else if (bufferPosition == expectedLength) {
} else {
logger.debug("Complete packet received.");
clearBuffer();
Message message = new Message(bufCopy);
notifyListeners(message);
} else if (bufferPosition > expectedLength) {
// somehow we've received more data that expected
clearAndThrowException(
"Invalid packet size. Discarding current buffer: " + MessageUtils.toPrettyHexString(bufCopy));
}
} else {
// we've got 4 or more bytes and it doesn't have a valid header
Expand Down