Skip to content

Commit

Permalink
feat: update nanoleaf from thread
Browse files Browse the repository at this point in the history
  • Loading branch information
tiste committed May 13, 2022
1 parent 63681ae commit b9b7cd6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 27 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
# nanoleaf-intellij-plugin Changelog

## [Unreleased]
### Fixed
- Launch Nanoleaf http calls from thread

## [1.0.0]
### Added
- Initial scaffold created from [IntelliJ Platform Plugin Template](https://github.com/JetBrains/intellij-platform-plugin-template)
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
pluginGroup = com.github.tiste.nanoleafintellijplugin
pluginName = nanoleaf-intellij-plugin
# SemVer format -> https://semver.org
pluginVersion = 1.0.0
pluginVersion = 1.0.1

# See https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
# for insight into build numbers and IntelliJ Platform versions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.intellij.execution.testframework.TestStatusListener;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

Expand All @@ -21,12 +20,7 @@ public void testSuiteFinished(@Nullable AbstractTestProxy root) {
boolean isFirstRunOrDebounce = numberOfRun == 0 || diff > 0;

if (isFirstRunOrDebounce) {
try {
NanoleafService.getInstance().setTestPassed(root.isPassed());
} catch (IOException e) {
System.out.println("Failed to update test passed");
System.out.println(e);
}
NanoleafService.getInstance().setTestPassed(root.isPassed());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.github.tiste.nanoleafintellijplugin.settings.ApplicationState;
import com.intellij.openapi.components.ServiceManager;
import okhttp3.*;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.concurrent.ScheduledThreadPoolExecutor;
Expand Down Expand Up @@ -44,27 +45,33 @@ public String getNewApiKey() throws IOException {
return mapper.readValue(response.body().string(), AuthTokenEntity.class).getAuthToken();
}

public void setTestPassed(boolean passed) throws IOException {
ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(5);
public void setTestPassed(boolean passed) {
ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);
ApplicationState settings = ApplicationState.getInstance();
String currentEffect = this.fetchCurrentEffect();

if (passed) {
this.setEffect(settings.greenEffect);
} else {
this.setEffect(settings.redEffect);
}

exec.schedule(new Runnable() {
public void run() {
try {
setEffect(currentEffect);
} catch (IOException e) {
System.out.println("Failed to update to previous effect");
System.out.println(e);

new Thread(() -> {
try {
String currentEffect = fetchCurrentEffect();

if (passed) {
setEffect(settings.greenEffect);
} else {
setEffect(settings.redEffect);
}

exec.schedule(() -> {
try {
setEffect(currentEffect);
} catch (IOException e) {
System.out.println("Failed to update to previous effect");
System.out.println(e);
}
}, 3, TimeUnit.SECONDS);
} catch (IOException e) {
System.out.println("Failed to update effect");
System.out.println(e);
}
}, 3, TimeUnit.SECONDS);
}).start();
}

private String fetchCurrentEffect() throws IOException {
Expand All @@ -81,13 +88,24 @@ private String fetchCurrentEffect() throws IOException {
}

private void setEffect(String effect) throws IOException {
System.out.println("Update to effect " + effect);
ApplicationState settings = ApplicationState.getInstance();

RequestBody body = RequestBody.create("{\"select\" : \"" + effect + "\"}", TEXT);
Request request = new Request.Builder()
.url("http://" + settings.ipAddress + "/api/v1/" + settings.apiKey + "/effects")
.put(body)
.build();
client.newCall(request).execute();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {

}

@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {

}
});
}
}

0 comments on commit b9b7cd6

Please sign in to comment.