Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions install-files/vstar.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
localJDconverter.active=n
localJDconverter.url=http://localhost:5000/convert
JDconverter.url=http://localhost:5000/convert
Binary file modified plugin/doc/BJD_TDB Converter.docx
Binary file not shown.
Binary file modified plugin/doc/BJD_TDB Converter.odt
Binary file not shown.
Binary file modified plugin/doc/BJD_TDB Converter.pdf
Binary file not shown.
Binary file modified plugin/doc/JDtoBJDTool.docx
Binary file not shown.
Binary file modified plugin/doc/JDtoBJDTool.odt
Binary file not shown.
Binary file modified plugin/doc/JDtoBJDTool.pdf
Binary file not shown.
121 changes: 22 additions & 99 deletions plugin/src/org/aavso/tools/vstar/external/lib/ConvertHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@

public class ConvertHelper {

public static final String ASTROUTILS_URL = "https://astroutils.astronomy.osu.edu";
public static final String URL_TEMPLATE = ASTROUTILS_URL + "/time/convert.php?JDS=%s&RA=%s&DEC=%s&FUNCTION=%s";
private static final String DEFAULT_TIME_SERVICE_URL = "http://localhost:5000/convert";

public static String localServiceURLstring = getLocalConvertServiceURLstring();
private static String timeServiceURLstring = initTimeServiceURLstring();

/**
* A pane for entering RA/Dec with a button that gets coordinates from the VSX server by the VSX star name
Expand Down Expand Up @@ -426,15 +425,10 @@ public static class ConfirmDialogWithHelp extends AbstractOkCancelDialog {

public ConfirmDialogWithHelp(String title, String msg, String helpTopic) {
super(title);
initDialog(title, msg, helpTopic, false);
initDialog(title, msg, helpTopic);
}

public ConfirmDialogWithHelp(String title, String msg, String helpTopic, boolean displayLocalServiceInfo) {
super(title);
initDialog(title, msg, helpTopic, displayLocalServiceInfo);
}

private void initDialog(String title, String msg, String helpTopic, boolean displayLocalServiceInfo) {
private void initDialog(String title, String msg, String helpTopic) {
this.helpTopic = helpTopic;

Container contentPane = this.getContentPane();
Expand All @@ -450,8 +444,7 @@ private void initDialog(String title, String msg, String helpTopic, boolean disp
topPane.add(buttonPane);
this.helpTopic = helpTopic;

if (displayLocalServiceInfo && localServiceURLstring != null)
topPane.add(createInfoPane("Local service: " + localServiceURLstring));
topPane.add(createInfoPane("Time service: " + (timeServiceURLstring != null ? timeServiceURLstring : "not defined")));

contentPane.add(topPane);

Expand Down Expand Up @@ -503,8 +496,12 @@ protected void okAction() {
}
}

public static String getLocalServiceURLstring() {
return localServiceURLstring;
public static String getTimeServiceURLstring() {
return timeServiceURLstring;
}

public static void setTimeServiceURLstring(String s) {
timeServiceURLstring = s;
}

/**
Expand All @@ -526,38 +523,14 @@ public static String getLocalServiceURLstring() {
public static List<Double> getConvertedListOfTimes(List<Double> times, double ra, double dec, String func)
throws Exception {

if (localServiceURLstring != null) {
//System.out.println(localServiceURLstring);
List<Double>out_times = convertWithLocalService(localServiceURLstring, times, ra, dec, func);
if (timeServiceURLstring != null) {
List<Double>out_times = convertTime(timeServiceURLstring, times, ra, dec, func);
return out_times;
}

Pair<String, String> result = getTextFromURLstring(getURLstring(times, ra, dec, func));
if (result.second != null) {
throw new Exception(result.second);
}

List<String> tempList = new ArrayList<String>(Arrays.asList(result.first.split("\n")));

if (tempList.size() != times.size()) {
throw new Exception("The server returned an invalid output:\n" + result.first);
}

List<Double>out_times = new ArrayList<Double>();
for (String s1 : tempList) {
double d;
try {
d = Double.parseDouble(s1);
} catch (NumberFormatException e) {
throw new Exception("The server returned an invalid output:\n" + result.first);
}
out_times.add(d);
}

return out_times;
} else
throw new Exception("Time service URL is not defined");
}

private static List<Double> convertWithLocalService(String localServiceURLstring, List<Double> times, double ra, double dec, String func)
private static List<Double> convertTime(String localServiceURLstring, List<Double> times, double ra, double dec, String func)
throws Exception {

JSONObject json = new JSONObject();
Expand Down Expand Up @@ -607,68 +580,18 @@ private static String getCfgName() {
}
}

private static String getLocalConvertServiceURLstring() {
//return "http://localhost:5000/convert";
private static String initTimeServiceURLstring() {
Properties props = new Properties();
try {
try (FileInputStream in = new FileInputStream(getCfgName())) {
props.load(in);
String a = props.getProperty("localJDconverter.active");
if (a != null) {
if ("Y".equals(a.trim().toUpperCase())) {
a = props.getProperty("localJDconverter.url");
if (a != null) {
a = a.trim();
if (!"".equals(a))
return a;
}
}
}
String a = props.getProperty("JDconverter.url");
if (a == null || "".equals(a.trim()))
return DEFAULT_TIME_SERVICE_URL;
return a.trim();
}
} catch (Exception e) {
return null;
return DEFAULT_TIME_SERVICE_URL;
}
return null;
}

private static String getURLstring(List<Double> times, double ra, double dec, String func) {

String s = null;

for (Double d : times) {
if (s != null) s += ","; else s = "";
s += String.valueOf(d);
}

return String.format(URL_TEMPLATE, s, String.valueOf(ra), String.valueOf(dec), func);
}

private static Pair<String, String> getTextFromURLstring(String urlString)
throws IOException, MalformedURLException, UnsupportedEncodingException {
Pair<String, String> result = new Pair<String, String>(null, null);

URL url = new URL(urlString);
URLConnection connection = url.openConnection();
if (!(connection instanceof HttpURLConnection)) {
result.second = "Not an HttpURLConnection";
return result;
}

if (((HttpURLConnection)connection).getResponseCode() != HttpURLConnection.HTTP_OK) {
result.second = ((HttpURLConnection)connection).getResponseMessage();
return result;
}

InputStream stream = connection.getInputStream();
StringBuilder textBuilder = new StringBuilder();
try (Reader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"))) {
int c = 0;
while ((c = reader.read()) != -1) {
textBuilder.append((char) c);
}
result.first = textBuilder.toString();
return result;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void invoke(ISeriesInfoProvider seriesInfo) {
}

private boolean showConfirmDialog(String title, String msg, String helpTopic) {
ConvertHelper.ConfirmDialogWithHelp dlg = new ConvertHelper.ConfirmDialogWithHelp(title, msg, helpTopic, true);
ConvertHelper.ConfirmDialogWithHelp dlg = new ConvertHelper.ConfirmDialogWithHelp(title, msg, helpTopic);
return !dlg.isCancelled();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,8 @@ public JDtoBJDToolDialog() {
topPane.add(coordPane);
topPane.add(createMainPane());
topPane.add(createButtonPane2(cancelListener));
if (ConvertHelper.getLocalServiceURLstring() != null) {
topPane.add(createInfoPane("Local service: " + ConvertHelper.getLocalServiceURLstring()));
}

String temp_str = ConvertHelper.getTimeServiceURLstring();
topPane.add(createInfoPane("Time service: " + (temp_str != null ? temp_str : "not defined")));

contentPane.add(topPane);

Expand Down Expand Up @@ -157,12 +155,12 @@ private JPanel createButtonPane() {
JPanel panel = new JPanel();

bUTCtoBJD = new JButton("UTC->BJD");
bUTCtoBJD.setToolTipText("Via " + ConvertHelper.ASTROUTILS_URL + " service");
bUTCtoBJD.setToolTipText("Via " + ConvertHelper.getTimeServiceURLstring() + " service");
bUTCtoBJD.addActionListener(createUTCtoBJDButtonListener());
panel.add(bUTCtoBJD, BorderLayout.NORTH);

bHJDtoBJD = new JButton("HJD->BJD");
bHJDtoBJD.setToolTipText("Via " + ConvertHelper.ASTROUTILS_URL + " service");
bHJDtoBJD.setToolTipText("Via " + ConvertHelper.getTimeServiceURLstring() + " service");
bHJDtoBJD.addActionListener(createHJDtoBJDButtonListener());
panel.add(bHJDtoBJD, BorderLayout.CENTER);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,20 @@
*/
public class ConvertHelperTest extends TestCase {

private String savedLocalServiceUrl;
private String savedTimeServiceUrl;

public ConvertHelperTest(String name) {
super(name);
}

@Override
protected void setUp() {
savedLocalServiceUrl = ConvertHelper.localServiceURLstring;
savedTimeServiceUrl = ConvertHelper.getTimeServiceURLstring();
}

@Override
protected void tearDown() {
ConvertHelper.localServiceURLstring = savedLocalServiceUrl;
ConvertHelper.setTimeServiceURLstring(savedTimeServiceUrl);
}

public void testGetCoordinatesWhenPresent() {
Expand All @@ -64,11 +64,6 @@ public void testGetCoordinatesWhenPresent() {
assertSame(dec, coords.second);
}

public void testAstroutilsUrlTemplate() {
assertTrue(ConvertHelper.URL_TEMPLATE.startsWith(ConvertHelper.ASTROUTILS_URL));
assertTrue(ConvertHelper.URL_TEMPLATE.contains("FUNCTION=%s"));
}

public void testGetConvertedListOfTimesLocalService() throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
int port = server.getAddress().getPort();
Expand All @@ -81,7 +76,7 @@ public void testGetConvertedListOfTimesLocalService() throws Exception {
});
server.start();
try {
ConvertHelper.localServiceURLstring = "http://127.0.0.1:" + port + "/convert";
ConvertHelper.setTimeServiceURLstring("http://127.0.0.1:" + port + "/convert");
List<Double> times = Arrays.asList(2458000.0, 2458001.0);
List<Double> out = ConvertHelper.getConvertedListOfTimes(times, 45.0, 30.0, "utc2bjd");
assertEquals(2, out.size());
Expand Down
Loading