Skip to content

Commit

Permalink
Fixed a bug in the group creator that corrupted the properties file
Browse files Browse the repository at this point in the history
  • Loading branch information
amybytes committed May 30, 2021
1 parent aefd89a commit 38b5622
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 63 deletions.
24 changes: 21 additions & 3 deletions src/main/java/io/github/rowak/nanoleafdesktop/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -245,26 +246,43 @@ public void run() {
regEffectsPanel.clearEffects();
rhythEffectsPanel.clearEffects();
new Thread(() -> {
List<String> regEffects = new ArrayList<String>();
List<String> rhythmEffects = new ArrayList<String>();
try {
for (NanoleafDevice device : devices) {
for (Effect effect : device.getAllEffects()) {
if (effect.getEffectType().equals("plugin")) {
if (((PluginEffect)effect).getPlugin().getType().equals("rhythm")) {
rhythEffectsPanel.addEffect(effect.getName());
rhythmEffects.add(effect.getName());
}
else {
regEffectsPanel.addEffect(effect.getName());
regEffects.add(effect.getName());
}
}
else {
regEffectsPanel.addEffect(effect.getName());
regEffects.add(effect.getName());
}
}
}
}
catch (NanoleafException | IOException e) {
e.printStackTrace();
}

regEffects.sort(new Comparator<String>() {
@Override
public int compare(String ef1, String ef2) {
return ef1.compareTo(ef2);
}
});
rhythmEffects.sort(new Comparator<String>() {
@Override
public int compare(String ef1, String ef2) {
return ef1.compareTo(ef2);
}
});
regEffectsPanel.addEffects(regEffects);
rhythEffectsPanel.addEffects(rhythmEffects);

BasicEffects.initializeBasicEffects();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public DeviceChangerDialog(Main parent) {
devices = new ArrayList<NanoleafDeviceMeta>();
listModel = new DefaultListModel<String>();
initUI(parent);

findGroups();
findAuroras();
}
Expand Down Expand Up @@ -95,8 +95,7 @@ public void onDeviceFound(NanoleafDeviceMeta meta) {
public void onTimeout() {
if (devices != null && devices.isEmpty()) {
new TextDialog(DeviceChangerDialog.this, "Couldn't locate any devices. " +
"Please try again or create an issue on GitHub.")
.setVisible(true);
"Please try again.").setVisible(true);
}
lblTitle.setText("Select a Device or Group");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import io.github.rowak.nanoleafapi.Aurora;
import io.github.rowak.nanoleafapi.NanoleafDevice;
import io.github.rowak.nanoleafapi.NanoleafSearchCallback;
import io.github.rowak.nanoleafdesktop.Main;
import io.github.rowak.nanoleafdesktop.models.DeviceGroup;
import io.github.rowak.nanoleafdesktop.models.DeviceInfo;
Expand All @@ -42,73 +43,44 @@

public class GroupCreatorDialog extends JDialog {

private static List<NanoleafDeviceMeta> devices;
private final int SEARCH_TIMEOUT = 10000;

private List<NanoleafDeviceMeta> devices;
private DefaultListModel<String> devicesModel;
private DefaultListModel<String> groupDevicesModel;
private JLabel lblTitle;

public GroupCreatorDialog(Component parent) {
devices = new ArrayList<NanoleafDeviceMeta>();
initUI(parent);
new Thread(() -> {
getDevices();
}).start();
getDevices();
}

private void getDevices() {
findMethod1();

for (NanoleafDeviceMeta metadata : devices) {
addDeviceToList(metadata);
}

if (devices.isEmpty()) {
new TextDialog(this, "Couldn't locate any devices. " +
"Please try again or create an issue on GitHub.")
.setVisible(true);
}
lblTitle.setText("Create a Group");
}

private boolean findMethod1() {
devices = new ArrayList<NanoleafDeviceMeta>();
try {
devices = NanoleafSetup.findNanoleafDevices(5000);
NanoleafSetup.findNanoleafDevicesAsync(new NanoleafSearchCallback() {

@Override
public void onDeviceFound(NanoleafDeviceMeta meta) {
System.out.println("FOUND: " + meta.getDeviceName() + " " + meta.getHostName() + " " + meta.getPort());
addDeviceToList(meta);
}

@Override
public void onTimeout() {
if (devices != null && devices.isEmpty()) {
new TextDialog(GroupCreatorDialog.this, "Couldn't locate any devices. " +
"Please try again.").setVisible(true);
}
lblTitle.setText("Create a Group");
}
}, SEARCH_TIMEOUT);
}
catch (Exception e) {
// do nothing
catch (java.net.UnknownHostException e) {
e.printStackTrace();
}
return !devices.isEmpty();
}

// private boolean findMethod2()
// {
// devices = new ArrayList<NanoleafDeviceMeta>();
// try
// {
// List<InetSocketAddress> devicesOld = NanoleafSetup.quickFindAuroras();
// for (InetSocketAddress addr : devicesOld)
// {
// AuroraMetadata metadata = new AuroraMetadata(addr.getHostName(),
// addr.getPort(), "", "");
// devices.add(metadata);
// }
// }
// catch (Exception e)
// {
// // do nothing
// }
// return !devices.isEmpty();
// }

// private void fillDevicesModel()
// {
// for (AuroraMetadata metadata : devices)
// {
// addDeviceToList(metadata);
// System.out.println(metadata.getHostName());
// }
// }

private void addDeviceToList(NanoleafDeviceMeta metadata) {
Map<String, Object> savedDevices = getLocalDeviceData();
if (savedDevices.containsKey(metadata.getHostName())) {
Expand All @@ -126,6 +98,7 @@ private void addDeviceToList(NanoleafDeviceMeta metadata) {
deviceName, metadata.getHostName());
devicesModel.addElement(name);
}
devices.add(metadata);
}

private Map<String, Object> getLocalDeviceData() {
Expand Down Expand Up @@ -164,8 +137,7 @@ private void createGroup(String name) {
new Thread(() -> {
NanoleafDevice[] connectedDevices = new NanoleafDevice[groupDevicesModel.size()];
for (int i = 0; i < groupDevicesModel.size(); i++) {
connectedDevices[i] = connectToDevice(
groupDevicesModel.getElementAt(i));
connectedDevices[i] = connectToDevice(groupDevicesModel.getElementAt(i));
}

PropertyManager manager = new PropertyManager(Main.PROPERTIES_FILEPATH);
Expand All @@ -178,8 +150,9 @@ private void createGroup(String name) {
deviceGroups.add(new DeviceGroup(name, info));
manager.setProperty("deviceGroups",
new JSONArray(deviceGroups).toString());

new TextDialog(GroupCreatorDialog.this, name + " was created.").setVisible(true);
manager.setProperty("lastSession", "GROUP: " + name);
new TextDialog(GroupCreatorDialog.this, name + " was created." +
"Restart the app to connect to the group.").setVisible(true);
}).start();
}

Expand Down

0 comments on commit 38b5622

Please sign in to comment.