Skip to content

Commit

Permalink
Allow stopping rendering thread
Browse files Browse the repository at this point in the history
Fixes #33

---------

Co-authored-by: Assaf Inbal <shmuelzon@gmail.com>
  • Loading branch information
guimatheus92 and shmuelzon authored Jul 19, 2024
1 parent 0c3ac7f commit 181eb37
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ When using the "Room overlay" light mixing mode, it's also suggested to:
- [ ] Allow selecting date/time of render
- [ ] Create multiple renders for multiple hours of the day and display in Home
Assistant according to local time
- [ ] Allow stopping rendering thread
- [x] Allow stopping rendering thread
- [ ] Allow enabling/disabling/configuring state-icon
- [x] Support including sensors state-icons/labels for other items
- [ ] Support fans with animated gif/png with css3 image rotation
Expand Down
31 changes: 23 additions & 8 deletions src/com/shmuelzon/HomeAssistantFloorPlan/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.beans.PropertyChangeSupport;
import java.io.File;
import java.io.IOException;
import java.lang.InterruptedException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.MessageDigest;
Expand Down Expand Up @@ -59,6 +60,7 @@ public enum Quality {HIGH, LOW}
private boolean useExistingRenders = true;
private Renderer renderer = Renderer.YAFARAY;
private Quality quality = Quality.HIGH;
private AbstractPhotoRenderer photoRenderer;

class StateIcon {
public String name;
Expand Down Expand Up @@ -177,11 +179,18 @@ public void setQuality(Quality quality) {
this.quality = quality;
}

public void stop() {
if (photoRenderer != null) {
photoRenderer.stop();
photoRenderer = null;
}
}

public boolean isProjectEmpty() {
return home == null || home.getFurniture().isEmpty();
}

public void render() throws Exception {
public void render() throws IOException, InterruptedException {
propertyChangeSupport.firePropertyChange(Property.COMPLETED_RENDERS.name(), numberOfCompletedRenders, 0);
numberOfCompletedRenders = 0;

Expand All @@ -205,7 +214,7 @@ public void render() throws Exception {
} catch (IOException e) {
throw e;
} finally {
restorLightsPower(lightsPower);
restoreLightsPower(lightsPower);
}
}

Expand Down Expand Up @@ -346,15 +355,15 @@ private void build3dProjection() {
perspectiveTransform.mul(yawRotation);
}

private String generateBaseRender() throws IOException {
private String generateBaseRender() throws IOException, InterruptedException {
generateImage(new ArrayList<String>(), outputFloorplanDirectoryName + File.separator + "base.png");
return String.format(
"type: picture-elements\n" +
"image: /local/floorplan/base.png?version=%s\n" +
"elements:\n", renderHash("base.png"));
}

private String generateGroupRenders(String group, BufferedImage baseImage) throws IOException {
private String generateGroupRenders(String group, BufferedImage baseImage) throws IOException, InterruptedException {
List<String> groupLights = new ArrayList<String>(lightsGroups.get(group).keySet());

List<List<String>> lightCombinations = getCombinations(groupLights);
Expand All @@ -368,7 +377,7 @@ private String generateGroupRenders(String group, BufferedImage baseImage) throw
return yaml;
}

private BufferedImage generateImage(List<String> onLights, String fileName) throws IOException {
private BufferedImage generateImage(List<String> onLights, String fileName) throws IOException, InterruptedException {
if (useExistingRenders && Files.exists(Paths.get(fileName))) {
propertyChangeSupport.firePropertyChange(Property.COMPLETED_RENDERS.name(), numberOfCompletedRenders, ++numberOfCompletedRenders);
return ImageIO.read(Files.newInputStream(Paths.get(fileName)));
Expand All @@ -390,16 +399,22 @@ private void prepareScene(List<String> onLights) {
}
}

private BufferedImage renderScene() throws IOException {
private BufferedImage renderScene() throws IOException, InterruptedException {
Map<Renderer, String> rendererToClassName = new HashMap<Renderer, String>() {{
put(Renderer.SUNFLOW, "com.eteks.sweethome3d.j3d.PhotoRenderer");
put(Renderer.YAFARAY, "com.eteks.sweethome3d.j3d.YafarayRenderer");
}};
AbstractPhotoRenderer photoRenderer = AbstractPhotoRenderer.createInstance(
photoRenderer = AbstractPhotoRenderer.createInstance(
rendererToClassName.get(renderer),
home, null, this.quality == Quality.LOW ? AbstractPhotoRenderer.Quality.LOW : AbstractPhotoRenderer.Quality.HIGH);
BufferedImage image = new BufferedImage(renderWidth, renderHeight, BufferedImage.TYPE_INT_RGB);
photoRenderer.render(image, camera, null);
if (photoRenderer != null) {
photoRenderer.dispose();
photoRenderer = null;
}
if (Thread.interrupted())
throw new InterruptedException();

return image;
}
Expand Down Expand Up @@ -488,7 +503,7 @@ conditions, entities, fileName, renderHash(fileName),
lightMixingMode == LightMixingMode.CSS ? " mix-blend-mode: lighten\n" : "");
}

private void restorLightsPower(Map<HomeLight, Float> lightsPower) {
private void restoreLightsPower(Map<HomeLight, Float> lightsPower) {
for(HomeLight light : lightsPower.keySet()) {
light.setPower(lightsPower.get(light));
}
Expand Down
30 changes: 27 additions & 3 deletions src/com/shmuelzon/HomeAssistantFloorPlan/Panel.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@

@SuppressWarnings("serial")
public class Panel extends JPanel implements DialogView {
private enum ActionType {BROWSE, START, CLOSE}
private enum ActionType {BROWSE, START, STOP, CLOSE}

private static Panel currentPanel;
private Controller controller;
Expand Down Expand Up @@ -119,17 +119,26 @@ public void run() {
try {
controller.render();
JOptionPane.showMessageDialog(null, resource.getString("HomeAssistantFloorPlan.Panel.info.finishedRendering.text"));
} catch (InterruptedException e) {
} catch (Exception e) {
JOptionPane.showMessageDialog(null, resource.getString("HomeAssistantFloorPlan.Panel.error.failedRendering.text") + " " + e);
}
setComponentsEnabled(true);
renderExecutor = null;
}
});
}
});
actions.put(ActionType.STOP, new ResourceAction(preferences, Panel.class, ActionType.STOP.name(), true) {
@Override
public void actionPerformed(ActionEvent ev) {
stop();
}
});
actions.put(ActionType.CLOSE, new ResourceAction(preferences, Panel.class, ActionType.CLOSE.name(), true) {
@Override
public void actionPerformed(ActionEvent ev) {
stop();
close();
}
});
Expand Down Expand Up @@ -330,8 +339,13 @@ private void setComponentsEnabled(boolean enabled) {
qualityComboBox.setEnabled(enabled);
outputDirectoryTextField.setEnabled(enabled);
outputDirectoryBrowseButton.setEnabled(enabled);
startButton.setEnabled(enabled);
closeButton.setEnabled(enabled);
if (enabled) {
startButton.setAction(getActionMap().get(ActionType.START));
startButton.setText(resource.getString("HomeAssistantFloorPlan.Panel.startButton.text"));
} else {
startButton.setAction(getActionMap().get(ActionType.STOP));
startButton.setText(resource.getString("HomeAssistantFloorPlan.Panel.stopButton.text"));
}
}

private void layoutComponents() {
Expand Down Expand Up @@ -441,6 +455,10 @@ public void displayView(View parentView) {
public void windowClosed(WindowEvent ev) {
currentPanel = null;
}
@Override
public void windowClosing(WindowEvent ev) {
stop();
}
});

dialog.setVisible(true);
Expand Down Expand Up @@ -469,6 +487,12 @@ private void buildLightsGroupsTree(Map<String, Map<String, List<HomeLight>>> lig
}
}

private void stop() {
if (renderExecutor != null)
renderExecutor.shutdownNow();
controller.stop();
}

private void close() {
Window window = SwingUtilities.getWindowAncestor(this);
if (window.isDisplayable())
Expand Down

0 comments on commit 181eb37

Please sign in to comment.