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 public static SearchResponse doSearch( SearchRequest request ) throws MalformedURLException, IOException {
\r
23 String requestString = request.toString();
\r
25 AndroidLogWrapper.d(ThrustCurveAPI.class, "doSearch: " + requestString);
\r
26 // Froyo has troubles resolving URLS constructed with protocols. Because of this
\r
27 // we need to do it in parts.
\r
28 URL url = new URL("http", "www.thrustcurve.org", "/servlets/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 // Froyo has troubles resolving URLS constructed with protocols. Because of this
\r
62 // we need to do it in parts.
\r
63 URL url = new URL("http", "www.thrustcurve.org", "/servlets/download");
\r
65 OutputStream stream;
\r
67 URLConnection conn = url.openConnection();
\r
68 conn.setDoInput(true);
\r
69 conn.setDoOutput(true);
\r
70 conn.setUseCaches(false);
\r
72 stream = conn.getOutputStream();
\r
74 stream.write(requestString.getBytes());
\r
76 InputStream is = conn.getInputStream();
\r
78 DownloadResponse downloadResponse = DownloadResponseParser.parse(is);
\r
79 AndroidLogWrapper.d(ThrustCurveAPI.class,downloadResponse.toString());
\r
81 return downloadResponse.getData(motor_id);
\r
86 * look through the listOfMotors to find the one which best matches the motor requested.
\r
88 * The algorithm uses a score based method. Each entry in listOfMotors is assigned a score
\r
89 * and the element with the highest score is returned. The score is computed as follows:
\r
91 * 1) if the element matches the digest of the requested motor eactly, score += 1000
\r
92 * 1) if the element matches the designation in the requested motor exactly, score = 100
\r
93 * 2) if the element is a RockSim file score += 10
\r
96 * @param listOfMotors
\r
99 public static ThrustCurveMotor findBestMatch( ThrustCurveMotorPlaceholder motor, List<MotorBurnFile> listOfMotors ) {
\r
101 ThrustCurveMotor bestMatch = null;
\r
102 int bestScore = -1;
\r
104 final String wantedDesignation = motor.getDesignation();
\r
105 final String wantedDigest = motor.getDigest();
\r
107 for ( MotorBurnFile entry : listOfMotors ) {
\r
108 int entryScore = 0;
\r
109 ThrustCurveMotor entryMotor = entry.getThrustCurveMotor();
\r
111 if ("RockSim".equals(entry.getFiletype()) ) {
\r
115 if ( wantedDigest != null && wantedDigest.equals( entryMotor.getDigest() ) ) {
\r
116 entryScore += 1000;
\r
119 if ( wantedDesignation != null && wantedDesignation.equals(entryMotor.getDesignation())) {
\r
123 if ( entryScore > bestScore ) {
\r
124 bestScore = entryScore;
\r
125 bestMatch = entry.getThrustCurveMotor();
\r
134 * Extract all unique motors from the list based on designation.
\r
135 * @param listOfMotors
\r
138 public static List<ThrustCurveMotor> extractAllMotors( List<MotorBurnFile> listOfMotors ) {
\r
140 Map<String, ThrustCurveMotor> motorsByDesignation = new HashMap<String,ThrustCurveMotor>();
\r
142 for( MotorBurnFile entry : listOfMotors ) {
\r
143 ThrustCurveMotor motor = entry.getThrustCurveMotor();
\r
144 if ( motor != null ) {
\r
145 motorsByDesignation.put( motor.getDesignation(), motor);
\r
149 return new ArrayList<ThrustCurveMotor>(motorsByDesignation.values());
\r