Skip to content

Commit

Permalink
Refactor LabSettings. #520
Browse files Browse the repository at this point in the history
  • Loading branch information
t-oster committed Aug 9, 2019
1 parent 0788f22 commit a030e4e
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 37 deletions.
54 changes: 17 additions & 37 deletions src/com/t_oster/visicut/gui/MainView.java
Expand Up @@ -72,6 +72,7 @@
import com.frochr123.periodictasks.RefreshQRCodesTask; import com.frochr123.periodictasks.RefreshQRCodesTask;
import com.t_oster.visicut.Preferences; import com.t_oster.visicut.Preferences;
import com.t_oster.visicut.misc.Homography; import com.t_oster.visicut.misc.Homography;
import com.t_oster.visicut.misc.LabSettings;
import java.awt.Desktop; import java.awt.Desktop;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.FileDialog; import java.awt.FileDialog;
Expand Down Expand Up @@ -110,6 +111,7 @@
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Optional;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
Expand Down Expand Up @@ -3472,29 +3474,28 @@ private String getRecommendedLab() {


// Otherwise: // Otherwise:
// Get localhost FQDN for auto-detecting the lab, at least on computers owned by the lab. // Get localhost FQDN for auto-detecting the lab, at least on computers owned by the lab.
String hostname = "";
try try
{ {
hostname = InetAddress.getLocalHost().getCanonicalHostName(); final String hostname = InetAddress.getLocalHost().getCanonicalHostName();
Optional<LabSettings> r = LabSettings.get().stream().filter(s -> s.acceptsHostname(hostname)).findFirst();
if (r.isPresent()) {
return r.get().name;
}
} }
catch (UnknownHostException ex) catch (UnknownHostException ex)
{ {
// Cannot get local hostname -- ignore exception // Cannot get local hostname -- ignore exception
} }


// initial guess from local hostname. Will be used if the URL is not known
// (first startup, or preferences from before December 2018 when
// remembering the URL was implemented)
if (hostname.endsWith(".fau.de") || hostname.endsWith(".uni-erlangen.de")) {
return "Germany, Erlangen: FAU FabLab";
}

// Guess the lab based on wifi ssid. // Guess the lab based on wifi ssid.
String wirelessSsid = Helper.getWifiSSID(); String wirelessSsid = Helper.getWifiSSID();
if (wirelessSsid.equals("WL-seattle-maker-space")) { if (wirelessSsid != null) {
return "United States, Seattle: Fremont Hangar"; Optional<LabSettings> r = LabSettings.get().stream().filter(s -> s.acceptsSSID(wirelessSsid)).findFirst();
if (r.isPresent()) {
return r.get().name;
}
} }

return ""; // unknown return ""; // unknown
} }


Expand All @@ -3518,30 +3519,9 @@ private void jmDownloadSettingsActionPerformed(java.awt.event.ActionEvent evt) {
} }
choices.put(bundle.getString("EMPTY_SETTINGS"), "__EMPTY__"); choices.put(bundle.getString("EMPTY_SETTINGS"), "__EMPTY__");
choices.put(bundle.getString("IMPORT_SETTINGS_FROM_FILE"), "__FILE__"); choices.put(bundle.getString("IMPORT_SETTINGS_FROM_FILE"), "__FILE__");

for (LabSettings s : LabSettings.get()) {

choices.put(s.name, s.URL);
// Want your lab in this list? Look at https://github.com/t-oster/VisiCut/wiki/How-to-add-default-settings-for-your-lab ! }
// choices.put("Country, City: Institution", "https://example.org/foo.zip");
// also have a look at getRecommendedLab() if you want your lab to be suggested for all PCs in your local network.
// The list is sorted alphabetically.
choices.put("China, Hong Kong: Renaissance College Hong Kong", "https://github.com/RCHK-DT/visicut-settings/archive/master.zip");
choices.put("France, Chemillé en Anjou : FabLab le Boc@l", "https://github.com/bocal-chemille/Visicut/raw/master/config_laser_bocal.vcsettings");
choices.put("France, Le Mans: HAUM Hackerspace", "https://github.com/haum/visicut-settings/archive/master.zip");
choices.put("Germany, Aachen: FabLab RWTH Aachen", "https://github.com/renebohne/zing6030-visicut-settings/archive/master.zip");
choices.put("Germany, Berlin: Fab Lab Berlin", "https://github.com/FabLabBerlin/visicut-settings/archive/master.zip");
choices.put("Germany, Bremen: FabLab Bremen e.V.", "http://www.fablab-bremen.org/FabLab_Bremen.vcsettings");
choices.put("Germany, Dresden: Konglomerat e.V.", "https://github.com/konglomerat/visicut-settings/archive/master.zip");
choices.put("Germany, Dresden: Makerspace Dresden", "https://github.com/Makerspace-Dresden/visicut-settings/archive/master.zip");
choices.put("Germany, Erlangen: FAU FabLab", "https://github.com/fau-fablab/visicut-settings/archive/master.zip");
choices.put("Germany, Heidelberg: Heidelberg Makerspace", "https://github.com/heidelberg-makerspace/visicut-settings/archive/master.zip");
choices.put("Germany, Nuremberg: Fab lab Region Nürnberg e.V.", "https://github.com/fablabnbg/visicut-settings/archive/master.zip");
choices.put("Germany, Paderborn: FabLab Paderborn e.V.", "https://github.com/fablab-paderborn/visicut-settings/archive/master.zip");
choices.put("Germany, Veitsbronn: FabLab Landkreis Fürth e.V.", "https://github.com/falafue/visicut-settings/archive/master.zip");
choices.put("Netherlands, Amersfoort: FabLab", "https://github.com/Fablab-Amersfoort/visicut-settings/archive/master.zip");
choices.put("Netherlands, Enschede: TkkrLab", "https://github.com/TkkrLab/visicut-settings/archive/master.zip");
choices.put("United Kingdom, Leeds: Hackspace", "https://github.com/leedshackspace/visicut-settings/archive/master.zip");
choices.put("United Kingdom, Manchester: Hackspace", "https://github.com/hacmanchester/visicut-settings/archive/master.zip");
choices.put("United States, Seattle: Fremont Hangar", "https://github.com/hghile/visicut-settings/archive/master.zip");
choices.put(bundle.getString("DOWNLOAD_NOT_IN_LIST"), "__HELP__"); choices.put(bundle.getString("DOWNLOAD_NOT_IN_LIST"), "__HELP__");




Expand Down
87 changes: 87 additions & 0 deletions src/com/t_oster/visicut/misc/LabSettings.java
@@ -0,0 +1,87 @@
/**
* This file is part of VisiCut.
* Copyright (C) 2011 - 2013 Thomas Oster <thomas.oster@rwth-aachen.de>
* RWTH Aachen University - 52062 Aachen, Germany
*
* VisiCut is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* VisiCut is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with VisiCut. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.t_oster.visicut.misc;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
*
* @author Thomas Oster <thomas.oster@upstart-it.de>
*/
public class LabSettings
{

public final String name;
public final String URL;
public final String[] domain;
public final String[] ssid;

public LabSettings(String name, String URL, String[] domain, String[] ssid)
{
this.name = name;
this.URL = URL;
this.domain = domain != null ? domain : new String[0];
this.ssid = ssid != null ? ssid : new String[0];
}

public LabSettings(String name, String URL)
{
this(name, URL, null, null);
}

public boolean acceptsHostname(String hostname) {
return Arrays.stream(domain).anyMatch(hostname::endsWith);
}

public boolean acceptsSSID(String ssid) {
return Arrays.stream(this.ssid).anyMatch(ssid::equals);
}

public static List<LabSettings> get()
{
List<LabSettings> result = new LinkedList<>();
// Want your lab in this list? Look at https://github.com/t-oster/VisiCut/wiki/How-to-add-default-settings-for-your-lab !
// result.add(new LabSettings("Country, City: Institution", "https://example.org/foo.zip"));
// if you have a local domain name in your network or a wifi SSID, you can add them too, so if you are in your lab
// your settings will be suggested
// The list is sorted alphabetically.
result.add(new LabSettings("China, Hong Kong: Renaissance College Hong Kong", "https://github.com/RCHK-DT/visicut-settings/archive/master.zip"));
result.add(new LabSettings("France, Chemillé en Anjou : FabLab le Boc@l", "https://github.com/bocal-chemille/Visicut/raw/master/config_laser_bocal.vcsettings"));
result.add(new LabSettings("France, Le Mans: HAUM Hackerspace", "https://github.com/haum/visicut-settings/archive/master.zip"));
result.add(new LabSettings("Germany, Aachen: FabLab RWTH Aachen", "https://github.com/renebohne/zing6030-visicut-settings/archive/master.zip"));
result.add(new LabSettings("Germany, Berlin: Fab Lab Berlin", "https://github.com/FabLabBerlin/visicut-settings/archive/master.zip"));
result.add(new LabSettings("Germany, Bremen: FabLab Bremen e.V.", "http://www.fablab-bremen.org/FabLab_Bremen.vcsettings"));
result.add(new LabSettings("Germany, Dresden: Konglomerat e.V.", "https://github.com/konglomerat/visicut-settings/archive/master.zip"));
result.add(new LabSettings("Germany, Dresden: Makerspace Dresden", "https://github.com/Makerspace-Dresden/visicut-settings/archive/master.zip"));
result.add(new LabSettings("Germany, Erlangen: FAU FabLab", "https://github.com/fau-fablab/visicut-settings/archive/master.zip", new String[]{".fau.de", ".uni-erlangen.de"}, null));
result.add(new LabSettings("Germany, Heidelberg: Heidelberg Makerspace", "https://github.com/heidelberg-makerspace/visicut-settings/archive/master.zip"));
result.add(new LabSettings("Germany, Nuremberg: Fab lab Region Nürnberg e.V.", "https://github.com/fablabnbg/visicut-settings/archive/master.zip"));
result.add(new LabSettings("Germany, Paderborn: FabLab Paderborn e.V.", "https://github.com/fablab-paderborn/visicut-settings/archive/master.zip"));
result.add(new LabSettings("Germany, Veitsbronn: FabLab Landkreis Fürth e.V.", "https://github.com/falafue/visicut-settings/archive/master.zip"));
result.add(new LabSettings("Netherlands, Amersfoort: FabLab", "https://github.com/Fablab-Amersfoort/visicut-settings/archive/master.zip"));
result.add(new LabSettings("Netherlands, Enschede: TkkrLab", "https://github.com/TkkrLab/visicut-settings/archive/master.zip"));
result.add(new LabSettings("United Kingdom, Leeds: Hackspace", "https://github.com/leedshackspace/visicut-settings/archive/master.zip"));
result.add(new LabSettings("United Kingdom, Manchester: Hackspace", "https://github.com/hacmanchester/visicut-settings/archive/master.zip"));
result.add(new LabSettings("United States, Seattle: Fremont Hangar", "https://github.com/hghile/visicut-settings/archive/master.zip", null, new String[]{"WL-seattle-maker-space"}));
return result;
}
}

0 comments on commit a030e4e

Please sign in to comment.