1 package net.sf.openrocket.android.thrustcurve;
\r
3 import java.io.IOException;
\r
4 import java.io.InputStream;
\r
5 import java.io.OutputStream;
\r
6 import java.net.MalformedURLException;
\r
8 import java.net.URLConnection;
\r
9 import java.util.ArrayList;
\r
10 import java.util.HashMap;
\r
11 import java.util.List;
\r
12 import java.util.Map;
\r
14 import net.sf.openrocket.android.util.AndroidLogWrapper;
\r
15 import net.sf.openrocket.motor.ThrustCurveMotor;
\r
16 import net.sf.openrocket.motor.ThrustCurveMotorPlaceholder;
\r
19 public abstract class ThrustCurveAPI {
\r
21 private static String url_base = "http://www.thrustcurve.org/servlets/";
\r
23 public static SearchResponse doSearch( SearchRequest request ) throws MalformedURLException, IOException {
\r
25 String requestString = request.toString();
\r
27 AndroidLogWrapper.d(ThrustCurveAPI.class, "doSearch: " + requestString);
\r
28 URL url = new URL(url_base + "search");
\r
30 OutputStream stream;
\r
32 URLConnection conn = url.openConnection();
\r
33 conn.setConnectTimeout(2000);
\r
34 conn.setDoInput(true);
\r
35 conn.setDoOutput(true);
\r
36 conn.setUseCaches(false);
\r
38 stream = conn.getOutputStream();
\r
40 stream.write(requestString.getBytes());
\r
42 InputStream is = conn.getInputStream();
\r
44 SearchResponse result = SearchResponseParser.parse(is);
\r
45 AndroidLogWrapper.d(ThrustCurveAPI.class,result.toString());
\r
50 public static List<MotorBurnFile> downloadData( Integer motor_id ) throws MalformedURLException, IOException {
\r
52 if ( motor_id == null ) {
\r
55 DownloadRequest dr = new DownloadRequest();
\r
58 String requestString = dr.toString();
\r
60 AndroidLogWrapper.d(ThrustCurveAPI.class, "downloadData: " + requestString);
\r
61 URL url = new URL(url_base + "download");
\r
63 OutputStream stream;
\r
65 URLConnection conn = url.openConnection();
\r
66 conn.setDoInput(true);
\r
67 conn.setDoOutput(true);
\r
68 conn.setUseCaches(false);
\r
70 stream = conn.getOutputStream();
\r
72 stream.write(requestString.getBytes());
\r
74 InputStream is = conn.getInputStream();
\r
76 DownloadResponse downloadResponse = DownloadResponseParser.parse(is);
\r
77 AndroidLogWrapper.d(ThrustCurveAPI.class,downloadResponse.toString());
\r
79 return downloadResponse.getData(motor_id);
\r
84 * look through the listOfMotors to find the one which best matches the motor requested.
\r
86 * The algorithm uses a score based method. Each entry in listOfMotors is assigned a score
\r
87 * and the element with the highest score is returned. The score is computed as follows:
\r
89 * 1) if the element matches the digest of the requested motor eactly, score += 1000
\r
90 * 1) if the element matches the designation in the requested motor exactly, score = 100
\r
91 * 2) if the element is a RockSim file score += 10
\r
94 * @param listOfMotors
\r
97 public static ThrustCurveMotor findBestMatch( ThrustCurveMotorPlaceholder motor, List<MotorBurnFile> listOfMotors ) {
\r
99 ThrustCurveMotor bestMatch = null;
\r
100 int bestScore = -1;
\r
102 final String wantedDesignation = motor.getDesignation();
\r
103 final String wantedDigest = motor.getDigest();
\r
105 for ( MotorBurnFile entry : listOfMotors ) {
\r
106 int entryScore = 0;
\r
107 ThrustCurveMotor entryMotor = entry.getThrustCurveMotor();
\r
109 if ("RockSim".equals(entry.getFiletype()) ) {
\r
113 if ( wantedDigest != null && wantedDigest.equals( entryMotor.getDigest() ) ) {
\r
114 entryScore += 1000;
\r
117 if ( wantedDesignation != null && wantedDesignation.equals(entryMotor.getDesignation())) {
\r
121 if ( entryScore > bestScore ) {
\r
122 bestScore = entryScore;
\r
123 bestMatch = entry.getThrustCurveMotor();
\r
132 * Extract all unique motors from the list based on designation.
\r
133 * @param listOfMotors
\r
136 public static List<ThrustCurveMotor> extractAllMotors( List<MotorBurnFile> listOfMotors ) {
\r
138 Map<String, ThrustCurveMotor> motorsByDesignation = new HashMap<String,ThrustCurveMotor>();
\r
140 for( MotorBurnFile entry : listOfMotors ) {
\r
141 ThrustCurveMotor motor = entry.getThrustCurveMotor();
\r
142 if ( motor != null ) {
\r
143 motorsByDesignation.put( motor.getDesignation(), motor);
\r
147 return new ArrayList<ThrustCurveMotor>(motorsByDesignation.values());
\r