diff --git a/orvibo-sdk.iml b/orvibo-sdk.iml new file mode 100644 index 0000000..195aa93 --- /dev/null +++ b/orvibo-sdk.iml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index e4667ed..dbd5580 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,6 @@ 1.1.1-SNAPSHOT Orvibo SDK - src maven-compiler-plugin diff --git a/src/main/java/com/github/tavalin/orvibo/commands/AbstractCommandHandler.java b/src/main/java/com/github/tavalin/orvibo/commands/AbstractCommandHandler.java index f62b115..df514a3 100644 --- a/src/main/java/com/github/tavalin/orvibo/commands/AbstractCommandHandler.java +++ b/src/main/java/com/github/tavalin/orvibo/commands/AbstractCommandHandler.java @@ -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); } } @@ -227,4 +230,4 @@ protected void handleInvalidResponse(Message message) throws OrviboException { -} \ No newline at end of file +} diff --git a/src/main/java/com/github/tavalin/orvibo/commands/EmitHandler.java b/src/main/java/com/github/tavalin/orvibo/commands/EmitHandler.java index b1b693f..50a7798 100644 --- a/src/main/java/com/github/tavalin/orvibo/commands/EmitHandler.java +++ b/src/main/java/com/github/tavalin/orvibo/commands/EmitHandler.java @@ -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; @@ -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"); } } diff --git a/src/main/java/com/github/tavalin/orvibo/commands/LearnHandler.java b/src/main/java/com/github/tavalin/orvibo/commands/LearnHandler.java index 6dcf69b..0ec0d12 100644 --- a/src/main/java/com/github/tavalin/orvibo/commands/LearnHandler.java +++ b/src/main/java/com/github/tavalin/orvibo/commands/LearnHandler.java @@ -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()); diff --git a/src/main/java/com/github/tavalin/orvibo/devices/AllOne.java b/src/main/java/com/github/tavalin/orvibo/devices/AllOne.java index 4b712ce..84632b4 100644 --- a/src/main/java/com/github/tavalin/orvibo/devices/AllOne.java +++ b/src/main/java/com/github/tavalin/orvibo/devices/AllOne.java @@ -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(); @@ -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; + } } diff --git a/src/main/java/com/github/tavalin/orvibo/devices/OrviboDevice.java b/src/main/java/com/github/tavalin/orvibo/devices/OrviboDevice.java index 62067fc..a24345d 100644 --- a/src/main/java/com/github/tavalin/orvibo/devices/OrviboDevice.java +++ b/src/main/java/com/github/tavalin/orvibo/devices/OrviboDevice.java @@ -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); @@ -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; + } } diff --git a/src/main/java/com/github/tavalin/orvibo/network/PacketHandler.java b/src/main/java/com/github/tavalin/orvibo/network/PacketHandler.java index 8554596..dfd850f 100644 --- a/src/main/java/com/github/tavalin/orvibo/network/PacketHandler.java +++ b/src/main/java/com/github/tavalin/orvibo/network/PacketHandler.java @@ -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 listeners; private InetAddress remote; @@ -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); @@ -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