package net.sf.openrocket.android.thrustcurve;\r
\r
+import java.util.ArrayList;\r
import java.util.HashMap;\r
+import java.util.List;\r
import java.util.Map;\r
\r
\r
public class DownloadResponse {\r
\r
- private Map<Integer,MotorBurnFile> data = new HashMap<Integer,MotorBurnFile>();\r
+ private Map<Integer,List<MotorBurnFile>> data = new HashMap<Integer,List<MotorBurnFile>>();\r
\r
private String error = null;\r
\r
public void add( MotorBurnFile mbd ) {\r
- MotorBurnFile currentData = data.get(mbd.getMotorId());\r
- if ( currentData == null || currentData.getThrustCurveMotor() == null ) {\r
- data.put(mbd.getMotorId(),mbd);\r
- } else {\r
- // Prefer RASP motors.\r
- if ( "RockSim".equals(mbd.getFiletype()) && !"RockSim".equals(currentData.getFiletype()) ) {\r
- data.put(mbd.getMotorId(), mbd);\r
- }\r
+ List<MotorBurnFile> currentData = data.get(mbd.getMotorId());\r
+ if ( currentData == null ) {\r
+ currentData = new ArrayList<MotorBurnFile>();\r
+ data.put(mbd.getMotorId(), currentData);\r
}\r
+ currentData.add(mbd);\r
}\r
\r
- public MotorBurnFile getData(Integer motor_id) {\r
+ public List<MotorBurnFile> getData(Integer motor_id) {\r
return data.get(motor_id);\r
}\r
\r
AndroidLogWrapper.d(DownloadResponseParser.class,"base64: " + ex.getMessage());\r
}\r
currentMotor.decodeFile( s );\r
+ ret.add((MotorBurnFile)currentMotor.clone());\r
}\r
- ret.add((MotorBurnFile)currentMotor.clone());\r
}\r
}\r
);\r
return thrustCurveMotor;\r
}\r
\r
- /**\r
- * @param thrustCurveMotor the thrustCurveMotor to set\r
- */\r
- public void setThrustCurveMotor(ThrustCurveMotor thrustCurveMotor) {\r
- this.thrustCurveMotor = thrustCurveMotor;\r
- }\r
- \r
}\r
package net.sf.openrocket.android.thrustcurve;\r
\r
+import java.util.List;\r
import java.util.Set;\r
import java.util.regex.Matcher;\r
import java.util.regex.Pattern;\r
\r
import net.sf.openrocket.android.util.AndroidLogWrapper;\r
+import net.sf.openrocket.motor.ThrustCurveMotor;\r
import net.sf.openrocket.motor.ThrustCurveMotorPlaceholder;\r
\r
public class TCMissingMotorDownloadAction extends TCQueryAction {\r
\r
handler.post( new UpdateMessage("Looking for " + motor.getManufacturer() + " " + motor.getDesignation()));\r
\r
- SearchResponse res = new ThrustCurveAPI().doSearch(request);\r
+ SearchResponse res = ThrustCurveAPI.doSearch(request);\r
\r
int total = res.getResults().size();\r
int count = 1;\r
\r
AndroidLogWrapper.d(TCQueryAction.class, mi.toString());\r
\r
- MotorBurnFile b = new ThrustCurveAPI().downloadData(mi.getMotor_id());\r
+ List<MotorBurnFile> listOfMotors = ThrustCurveAPI.downloadData(mi.getMotor_id());\r
\r
- writeMotor( mi, b);\r
+ ThrustCurveMotor bestMatch = ThrustCurveAPI.findBestMatch(motor, listOfMotors);\r
+ writeMotor( mi, bestMatch);\r
\r
}\r
}\r
import net.sf.openrocket.android.motor.ExtendedThrustCurveMotor;\r
import net.sf.openrocket.android.util.AndroidLogWrapper;\r
import net.sf.openrocket.android.util.ProgressDialogFragment;\r
+import net.sf.openrocket.motor.ThrustCurveMotor;\r
import android.app.Activity;\r
import android.os.AsyncTask;\r
import android.os.Bundle;\r
}\r
}\r
\r
- protected void writeMotor( TCMotor mi, MotorBurnFile b) throws Exception {\r
+ protected void writeMotor( TCMotor mi, ThrustCurveMotor thrustCurveMotor) throws Exception {\r
\r
DbAdapter mDbHelper = new DbAdapter(getActivity());\r
mDbHelper.open();\r
try {\r
ExtendedThrustCurveMotor m = new ExtendedThrustCurveMotor();\r
\r
- m.setThrustCurveMotor( b.getThrustCurveMotor() );\r
+ m.setThrustCurveMotor( thrustCurveMotor );\r
\r
// Convert impulse class. ThrustCurve puts mmx, 1/4a and 1/2a as A.\r
m.setImpulseClass(mi.getImpulse_class());\r
package net.sf.openrocket.android.thrustcurve;\r
\r
+import java.util.List;\r
+\r
import net.sf.openrocket.android.util.AndroidLogWrapper;\r
+import net.sf.openrocket.motor.ThrustCurveMotor;\r
\r
public class TCSearchAction extends TCQueryAction {\r
\r
protected String doInBackground(Void... params) {\r
try {\r
handler.post( new UpdateMessage("Quering Thrustcurve"));\r
- SearchResponse res = new ThrustCurveAPI().doSearch(searchRequest);\r
+ SearchResponse res = ThrustCurveAPI.doSearch(searchRequest);\r
\r
int total = res.getResults().size();\r
int count = 1;\r
\r
AndroidLogWrapper.d(TCQueryAction.class, mi.toString());\r
\r
- MotorBurnFile b = new ThrustCurveAPI().downloadData(mi.getMotor_id());\r
-\r
- writeMotor( mi, b);\r
+ List<MotorBurnFile> b = ThrustCurveAPI.downloadData(mi.getMotor_id());\r
+ List<ThrustCurveMotor> motors = ThrustCurveAPI.extractAllMotors(b);\r
+ if ( motors != null && motors.size() > 0 ) {\r
+ for( ThrustCurveMotor motor : motors ) {\r
+ writeMotor( mi, motor);\r
+ }\r
+ }\r
}\r
if ( total < res.getMatches() ) {\r
dismiss();\r
import java.net.MalformedURLException;\r
import java.net.URL;\r
import java.net.URLConnection;\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
\r
import net.sf.openrocket.android.util.AndroidLogWrapper;\r
+import net.sf.openrocket.motor.ThrustCurveMotor;\r
+import net.sf.openrocket.motor.ThrustCurveMotorPlaceholder;\r
\r
\r
-public class ThrustCurveAPI {\r
+public abstract class ThrustCurveAPI {\r
\r
- private String url_base = "http://www.thrustcurve.org/servlets/";\r
+ private static String url_base = "http://www.thrustcurve.org/servlets/";\r
\r
- public SearchResponse doSearch( SearchRequest request ) throws MalformedURLException, IOException {\r
+ public static SearchResponse doSearch( SearchRequest request ) throws MalformedURLException, IOException {\r
\r
String requestString = request.toString();\r
\r
return result;\r
}\r
\r
- public MotorBurnFile downloadData( Integer motor_id ) throws MalformedURLException, IOException {\r
+ public static List<MotorBurnFile> downloadData( Integer motor_id ) throws MalformedURLException, IOException {\r
\r
if ( motor_id == null ) {\r
return null;\r
DownloadResponse downloadResponse = DownloadResponseParser.parse(is);\r
AndroidLogWrapper.d(ThrustCurveAPI.class,downloadResponse.toString());\r
\r
- MotorBurnFile mbf = downloadResponse.getData(motor_id);\r
-\r
- return mbf;\r
+ return downloadResponse.getData(motor_id);\r
\r
}\r
+ \r
+ /**\r
+ * look through the listOfMotors to find the one which best matches the motor requested.\r
+ * \r
+ * The algorithm uses a score based method. Each entry in listOfMotors is assigned a score\r
+ * and the element with the highest score is returned. The score is computed as follows:\r
+ * \r
+ * 1) if the element matches the digest of the requested motor eactly, score += 1000\r
+ * 1) if the element matches the designation in the requested motor exactly, score = 100\r
+ * 2) if the element is a RockSim file score += 10\r
+ * \r
+ * @param motor\r
+ * @param listOfMotors\r
+ * @return\r
+ */\r
+ public static ThrustCurveMotor findBestMatch( ThrustCurveMotorPlaceholder motor, List<MotorBurnFile> listOfMotors ) {\r
+ \r
+ ThrustCurveMotor bestMatch = null;\r
+ int bestScore = -1;\r
+ \r
+ final String wantedDesignation = motor.getDesignation();\r
+ final String wantedDigest = motor.getDigest();\r
+ \r
+ for ( MotorBurnFile entry : listOfMotors ) {\r
+ int entryScore = 0;\r
+ ThrustCurveMotor entryMotor = entry.getThrustCurveMotor();\r
+ \r
+ if ("RockSim".equals(entry.getFiletype()) ) {\r
+ entryScore += 10;\r
+ }\r
+\r
+ if ( wantedDigest != null && wantedDigest.equals( entryMotor.getDigest() ) ) {\r
+ entryScore += 1000;\r
+ }\r
+ \r
+ if ( wantedDesignation != null && wantedDesignation.equals(entryMotor.getDesignation())) {\r
+ entryScore += 100;\r
+ }\r
+ \r
+ if ( entryScore > bestScore ) {\r
+ bestScore = entryScore;\r
+ bestMatch = entry.getThrustCurveMotor();\r
+ }\r
+ \r
+ }\r
+ \r
+ return bestMatch;\r
+ }\r
+ \r
+ /**\r
+ * Extract all unique motors from the list based on designation.\r
+ * @param listOfMotors\r
+ * @return\r
+ */\r
+ public static List<ThrustCurveMotor> extractAllMotors( List<MotorBurnFile> listOfMotors ) {\r
+ \r
+ Map<String, ThrustCurveMotor> motorsByDesignation = new HashMap<String,ThrustCurveMotor>();\r
+ \r
+ for( MotorBurnFile entry : listOfMotors ) {\r
+ ThrustCurveMotor motor = entry.getThrustCurveMotor();\r
+ motorsByDesignation.put( motor.getDesignation(), motor);\r
+ }\r
+ \r
+ return new ArrayList<ThrustCurveMotor>(motorsByDesignation.values());\r
+ \r
+ }\r
}\r