Skip to content

Commit

Permalink
Add console debugging tool
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
  • Loading branch information
jlaur committed Mar 23, 2024
1 parent 1563211 commit 62ae29d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ public NetatmoCommandExtension(final @Reference ThingRegistry thingRegistry) {

@Override
public void execute(String[] args, Console console) {
if (args.length == 2 && "sim".equals(args[0])) {
for (Thing thing : thingRegistry.getAll()) {
ThingHandler thingHandler = thing.getHandler();
if (thingHandler instanceof ApiBridgeHandler bridgeHandler) {
bridgeHandler.addSimulatedEvent(args[1]);
}
}
return;
}
if (args.length == 1 && SHOW_IDS.equals(args[0])) {
this.console = console;
for (Thing thing : thingRegistry.getAll()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -133,6 +135,12 @@ public ApiBridgeHandler(Bridge bridge, HttpClient httpClient, NADeserializer des
requestCountChannelUID = new ChannelUID(thing.getUID(), GROUP_MONITORING, CHANNEL_REQUEST_COUNT);
}

private List<String> simulatedEvents = new ArrayList<>();

public void addSimulatedEvent(String event) {
simulatedEvents.add(event);
}

@Override
public void initialize() {
logger.debug("Initializing Netatmo API bridge handler.");
Expand Down Expand Up @@ -204,6 +212,12 @@ private boolean authenticate(@Nullable String code, @Nullable String redirectUri
grantServlet.ifPresent(servlet -> servlet.dispose());
grantServlet = Optional.empty();
} else {
if (simulatedEvents.remove("oauth")) {
throw new OAuthException("Simulated OAuth failure");
}
if (simulatedEvents.remove("oauthio")) {
throw new IOException("Simulated OAuth IO failure");
}
accessTokenResponse = oAuthClientService.getAccessTokenResponse();
}
} catch (OAuthException | OAuthResponseException e) {
Expand Down Expand Up @@ -337,7 +351,15 @@ public synchronized <T> T executeUri(URI uri, HttpMethod method, Class<T> clazz,
String.join(", ", request.getHeaders().stream().map(HttpField::toString).toList()));
ContentResponse response = request.send();

Code statusCode = HttpStatus.getCode(response.getStatus());
Code statusCode;
if (simulatedEvents.remove("http503")) {
statusCode = Code.SERVICE_UNAVAILABLE;
} else if (simulatedEvents.remove("http429")) {
statusCode = Code.TOO_MANY_REQUESTS;
} else {
statusCode = HttpStatus.getCode(response.getStatus());
}

String responseBody = new String(response.getContent(), StandardCharsets.UTF_8);
logger.trace(" -returned: code {} body {}", statusCode, responseBody);

Expand Down

0 comments on commit 62ae29d

Please sign in to comment.