1 package net.sf.openrocket.android.thrustcurve;
\r
3 import java.util.List;
\r
4 import java.util.Set;
\r
5 import java.util.regex.Matcher;
\r
6 import java.util.regex.Pattern;
\r
8 import net.sf.openrocket.android.util.AndroidLogWrapper;
\r
9 import net.sf.openrocket.motor.ThrustCurveMotor;
\r
10 import net.sf.openrocket.motor.ThrustCurveMotorPlaceholder;
\r
12 public class TCMissingMotorDownloadAction extends TCQueryAction {
\r
14 private final static String DESIGNATION_REGEX_STRING = "(Micro Maxx|Micro Maxx II|1/4A|1/2A|[A-O][0-9]*)";
\r
16 public static TCMissingMotorDownloadAction newInstance( Set<ThrustCurveMotorPlaceholder> missingMotors ) {
\r
17 TCMissingMotorDownloadAction frag = new TCMissingMotorDownloadAction();
\r
18 frag.task = frag.new Downloader(missingMotors);
\r
22 private class Downloader extends TCQueryAction.TCQueryTask {
\r
24 private Set<ThrustCurveMotorPlaceholder> missingMotors;
\r
25 private Pattern designation_pattern = null;
\r
27 private Downloader( Set<ThrustCurveMotorPlaceholder> missingMotors ) {
\r
28 this.missingMotors = missingMotors;
\r
30 designation_pattern = Pattern.compile(DESIGNATION_REGEX_STRING);
\r
31 } catch ( Exception ex ) {
\r
32 AndroidLogWrapper.e(TCMissingMotorDownloadAction.class, "Exception in pattern compile {}", ex);
\r
36 private void downloadMissingMotor( ThrustCurveMotorPlaceholder motor ) {
\r
39 SearchRequest request = new SearchRequest();
\r
40 request.setManufacturer(motor.getManufacturer());
\r
41 String designation = motor.getDesignation();
\r
42 if ( designation_pattern != null ) {
\r
43 Matcher m = designation_pattern.matcher(designation);
\r
45 designation = m.group();
\r
48 AndroidLogWrapper.d(TCMissingMotorDownloadAction.class, "using designation {}", designation);
\r
49 request.setCommon_name(designation);
\r
51 handler.post( new UpdateMessage("Looking for " + motor.getManufacturer() + " " + motor.getDesignation()));
\r
53 SearchResponse res = ThrustCurveAPI.doSearch(request);
\r
55 int total = res.getResults().size();
\r
57 for( TCMotor mi : res.getResults() ) {
\r
58 StringBuilder message = new StringBuilder();
\r
59 message.append("Downloading details ");
\r
61 message.append(count);
\r
62 message.append(" of " );
\r
63 message.append(total);
\r
64 message.append("\n");
\r
66 message.append(mi.getManufacturer());
\r
67 message.append(" ");
\r
68 message.append(mi.getCommon_name());
\r
69 handler.post(new UpdateMessage(message.toString()));
\r
71 if ( mi.getData_files() == null || mi.getData_files().intValue() == 0 ) {
\r
75 AndroidLogWrapper.d(TCQueryAction.class, mi.toString());
\r
77 List<MotorBurnFile> listOfMotors = ThrustCurveAPI.downloadData(mi.getMotor_id());
\r
79 ThrustCurveMotor bestMatch = ThrustCurveAPI.findBestMatch(motor, listOfMotors);
\r
80 writeMotor( mi, bestMatch);
\r
84 catch( Exception ex){
\r
85 AndroidLogWrapper.d(TCQueryAction.class,ex.toString());
\r
86 handler.post( new UpdateMessage("Failed") );
\r
93 protected String doInBackground(Void... arg0) {
\r
95 for ( ThrustCurveMotorPlaceholder motor : missingMotors ) {
\r
96 AndroidLogWrapper.d(TCMissingMotorDownloadAction.class, "Motor: {}", motor);
\r
97 downloadMissingMotor(motor);
\r