]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.support.gis/src/main/java/org/argeo/slc/gpsbabel/GpsBabelPositionProvider.java
GisFieldViewer before leaving to field trip
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.gis / src / main / java / org / argeo / slc / gpsbabel / GpsBabelPositionProvider.java
index 83aca1d845dc770348765a22c4761309604367fb..36d5d21361ffd7d3cfa8dac1919f5ddb2b5c1788 100644 (file)
@@ -2,7 +2,9 @@ package org.argeo.slc.gpsbabel;
 
 import java.util.StringTokenizer;
 
-import org.argeo.slc.gis.model.Position;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.argeo.slc.gis.model.FieldPosition;
 import org.argeo.slc.jts.PositionProvider;
 
 import com.vividsolutions.jts.geom.Coordinate;
@@ -10,6 +12,8 @@ import com.vividsolutions.jts.geom.GeometryFactory;
 import com.vividsolutions.jts.geom.Point;
 
 public class GpsBabelPositionProvider implements PositionProvider {
+       private Log log = LogFactory.getLog(GpsBabelPositionProvider.class);
+
        private GpsBabelCall gpsBabelCall;
 
        private GeometryFactory geometryFactory = new GeometryFactory();
@@ -17,22 +21,39 @@ public class GpsBabelPositionProvider implements PositionProvider {
        private String inputFormat = "garmin,get_posn";
        private String inputFile = "usb:";
 
+       private Boolean silentlyFailing = false;
+
        public void init() {
                gpsBabelCall = new GpsBabelCall(inputFormat, inputFile, "csv", "-");
        }
 
-       public Position currentPosition() {
+       public FieldPosition currentPosition() {
                // lazy init
                if (gpsBabelCall == null)
                        init();
 
-               String output = gpsBabelCall.function();
+               String output;
+               try {
+                       output = gpsBabelCall.function();
+                       silentlyFailing = false;
+               } catch (Exception e) {
+                       if (!silentlyFailing) {
+                               log.warn(e.getMessage()
+                                               + ": "
+                                               + (e.getCause() != null ? e.getCause().getMessage()
+                                                               : ""));
+                               if (log.isTraceEnabled())
+                                       e.printStackTrace();
+                               silentlyFailing = true;
+                       }
+                       return null;
+               }
                StringTokenizer st = new StringTokenizer(output, ",");
                Double latitude = Double.parseDouble(st.nextToken());
                Double longitude = Double.parseDouble(st.nextToken());
                Point position = geometryFactory.createPoint(new Coordinate(longitude,
                                latitude));
-               return new Position(position);
+               return new FieldPosition(position);
        }
 
        public void setInputFormat(String inputFormat) {