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.motor.ExtendedThrustCurveMotor;\r
import net.sf.openrocket.android.util.AndroidLogWrapper;\r
+import net.sf.openrocket.motor.ThrustCurveMotor;\r
import net.sf.openrocket.motor.ThrustCurveMotorPlaceholder;\r
-import android.app.Activity;\r
\r
public class TCMissingMotorDownloadAction extends TCQueryAction {\r
\r
- private Set<ThrustCurveMotorPlaceholder> missingMotors;\r
-\r
- public TCMissingMotorDownloadAction(Activity parent) {\r
- super(parent);\r
- }\r
-\r
- public void setMissingMotors( Set<ThrustCurveMotorPlaceholder> missingMotors ) {\r
- this.missingMotors = missingMotors;\r
- }\r
-\r
- protected Runnable getTask() {\r
- return new Downloader();\r
+ private final static String DESIGNATION_REGEX_STRING = "(Micro Maxx|Micro Maxx II|1/4A|1/2A|[A-O][0-9]*)";\r
+ \r
+ public static TCMissingMotorDownloadAction newInstance( Set<ThrustCurveMotorPlaceholder> missingMotors ) {\r
+ TCMissingMotorDownloadAction frag = new TCMissingMotorDownloadAction();\r
+ frag.task = frag.new Downloader(missingMotors);\r
+ return frag;\r
}\r
\r
- private class Downloader implements Runnable {\r
+ private class Downloader extends TCQueryAction.TCQueryTask {\r
\r
+ private Set<ThrustCurveMotorPlaceholder> missingMotors;\r
+ private Pattern designation_pattern = null;\r
+ \r
+ private Downloader( Set<ThrustCurveMotorPlaceholder> missingMotors ) {\r
+ this.missingMotors = missingMotors;\r
+ try {\r
+ designation_pattern = Pattern.compile(DESIGNATION_REGEX_STRING);\r
+ } catch ( Exception ex ) {\r
+ AndroidLogWrapper.e(TCMissingMotorDownloadAction.class, "Exception in pattern compile {}", ex);\r
+ }\r
+ }\r
+ \r
private void downloadMissingMotor( ThrustCurveMotorPlaceholder motor ) {\r
try {\r
+ \r
SearchRequest request = new SearchRequest();\r
request.setManufacturer(motor.getManufacturer());\r
- request.setDesignation(motor.getDesignation());\r
+ String designation = motor.getDesignation();\r
+ if ( designation_pattern != null ) {\r
+ Matcher m = designation_pattern.matcher(designation);\r
+ if ( m.find() ) {\r
+ designation = m.group();\r
+ }\r
+ }\r
+ AndroidLogWrapper.d(TCMissingMotorDownloadAction.class, "using designation {}", designation);\r
+ request.setCommon_name(designation);\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
continue;\r
}\r
\r
- MotorBurnFile b = new ThrustCurveAPI().downloadData(mi.getMotor_id());\r
-\r
AndroidLogWrapper.d(TCQueryAction.class, mi.toString());\r
\r
- ExtendedThrustCurveMotor m = new ExtendedThrustCurveMotor();\r
-\r
- m.setThrustCurveMotor( b.getThrustCurveMotor() );\r
+ List<MotorBurnFile> listOfMotors = ThrustCurveAPI.downloadData(mi.getMotor_id());\r
\r
- // Convert impulse class. ThrustCurve puts mmx, 1/4a and 1/2a as A.\r
- m.setImpulseClass(mi.getImpulse_class());\r
- if ( "a".equalsIgnoreCase(mi.getImpulse_class())) {\r
- if( mi.getCommon_name().startsWith("1/2A") ) {\r
- m.setImpulseClass("1/2A");\r
- } else if (mi.getCommon_name().startsWith("1/4A") ) {\r
- m.setImpulseClass("1/4A");\r
- } else if (mi.getCommon_name().startsWith("Micro") ) {\r
- m.setImpulseClass("1/8A");\r
- }\r
- }\r
-\r
- // Convert Case Info.\r
- if ( mi.getCase_info() == null\r
- || "single use".equalsIgnoreCase(mi.getCase_info())\r
- || "single-use".equalsIgnoreCase(mi.getCase_info())) {\r
- m.setCaseInfo(mi.getType()+ " " + mi.getDiameter() + "x" + mi.getLength());\r
- } else {\r
- m.setCaseInfo(mi.getCase_info());\r
- }\r
+ ThrustCurveMotor bestMatch = ThrustCurveAPI.findBestMatch(motor, listOfMotors);\r
+ writeMotor( mi, bestMatch);\r
\r
- AndroidLogWrapper.d(TCQueryAction.class,"adding motor " + m.toString());\r
- // Write motor.\r
- mDbHelper.getMotorDao().insertOrUpdateMotor(m);\r
}\r
}\r
catch( Exception ex){\r
AndroidLogWrapper.d(TCQueryAction.class,ex.toString());\r
- handler.post( new Error(ex.toString()) );\r
+ handler.post( new UpdateMessage("Failed") );\r
+\r
}\r
\r
}\r
\r
- @Override\r
- public void run() {\r
- for ( ThrustCurveMotorPlaceholder motor : missingMotors ) {\r
- AndroidLogWrapper.d(TCMissingMotorDownloadAction.class, "Motor: {}", motor);\r
- downloadMissingMotor(motor);\r
+ @Override\r
+ protected String doInBackground(Void... arg0) {\r
+ \r
+ for ( ThrustCurveMotorPlaceholder motor : missingMotors ) {\r
+ AndroidLogWrapper.d(TCMissingMotorDownloadAction.class, "Motor: {}", motor);\r
+ downloadMissingMotor(motor);\r
+ }\r
+ return null;\r
}\r
- handler.post( new Dismiss() );\r
+\r
}\r
-}\r
\r
}\r