Skip to content

Commit

Permalink
Merge pull request #77 from Smarteon/triv
Browse files Browse the repository at this point in the history
Improve logging of unexpected messages
  • Loading branch information
PetrZufan authored Feb 11, 2020
2 parents 4be6df1 + d785afb commit ab955c0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
20 changes: 14 additions & 6 deletions src/main/java/cz/smarteon/loxone/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class Command<T> {
private final boolean httpSupported;
private final boolean wsSupported;

private final String toMatch;
private final String shouldContain;

protected Command(final String command, final Type type, final Class<T> responseType, final boolean httpSupported,
final boolean wsSupported) {
Expand All @@ -52,9 +52,9 @@ protected Command(final String command, final Type type, final Class<T> response
this.wsSupported = wsSupported;

if (Type.JSON.equals(type)) {
this.toMatch = command.substring(1);
this.shouldContain = command.substring(1);
} else {
this.toMatch = command;
this.shouldContain = command;
}
}

Expand Down Expand Up @@ -88,7 +88,7 @@ static Command<Void> keyExchange(final String sessionKey) {
* @return true when the argument contains this command identifier, false otherwise
*/
public boolean is(final String toCompare) {
return toCompare != null && toCompare.contains(this.toMatch);
return toCompare != null && toCompare.contains(this.shouldContain);
}

/**
Expand Down Expand Up @@ -131,6 +131,14 @@ public boolean isWsSupported() {
return wsSupported;
}

/**
* String which should be contained in the argument passed to {@link #is(String)} to return true.
* @return string should be contained when comparing this command
*/
public String getShouldContain() {
return shouldContain;
}

/**
* Ensures the given response being the type of this command's compatible response.
* Returns the ensured response or throws the exception.
Expand Down Expand Up @@ -165,11 +173,11 @@ public boolean equals(final Object o) {
Objects.equals(command, command1.command) &&
type == command1.type &&
Objects.equals(responseType, command1.responseType) &&
Objects.equals(toMatch, command1.toMatch);
Objects.equals(shouldContain, command1.shouldContain);
}

@Override
public int hashCode() {
return Objects.hash(command, type, responseType, httpSupported, wsSupported, toMatch);
return Objects.hash(command, type, responseType, httpSupported, wsSupported, shouldContain);
}
}
3 changes: 2 additions & 1 deletion src/main/java/cz/smarteon/loxone/LoxoneWebSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,8 @@ private boolean checkLoxoneMessage(final Command command, final LoxoneMessage lo
if (command.is(loxoneMessage.getControl())) {
return true;
} else {
log.error("Unexpected message with control " + loxoneMessage.getControl());
log.error("Expected message with control containing " + command.getShouldContain()
+ " but " + loxoneMessage.getControl() + " received");
return false;
}
case HTTP_AUTH_TOO_LONG:
Expand Down
14 changes: 13 additions & 1 deletion src/test/groovy/cz/smarteon/loxone/CommandTest.groovy
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cz.smarteon.loxone

import cz.smarteon.loxone.message.IntValue

import nl.jqno.equalsverifier.EqualsVerifier
import spock.lang.Specification

Expand All @@ -14,6 +14,18 @@ class CommandTest extends Specification {

then:
cmd.command == 'CMD/aabb'
cmd.shouldContain == 'CMD/aabb'
}

def "test json command"() {
when:
def cmd = new Command('jdev/test', Command.Type.JSON, String.class, false, true)

then:
cmd.command == 'jdev/test'
cmd.shouldContain == 'dev/test'
cmd.is('dev/test/something')
!cmd.is('something')
}

def "should ensure response"() {
Expand Down

0 comments on commit ab955c0

Please sign in to comment.