Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse symbole and type #19

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ Android App to export GPX and FIT to garmin devices
* see Wiki: https://github.com/gimportexportdevs/gexporter/wiki/Help

## HOWTO Develop
* create directory app/libs
* copy fit.jar from the [Garmin FitSDK](https://developer.garmin.com/fit/download/) to app/libs
* Download the [Connect IQ Mobile SDK (Android BLE/ADB)](https://developer.garmin.com/connect-iq/sdk/) and copy the .aar file to app/libs
Comment on lines -12 to -14
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the dependencies so one does not have to download oneself.

I don't know if you need the files locally for some other kind of debugging. If so I can leave the documentation unchanged.

* Android Studio -> Settings -> System Settings -> Android SDK -> "SDK Tools" Tab -> Check "Support Repository/Constraint Layout"
* start TestServer.main() (this fires up the webserver on localhost)
* develop with the ConnectIQ simulator (connects to the webserver on localhost)
Expand Down
12 changes: 7 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ dependencies {

implementation 'org.nanohttpd:nanohttpd:2.3.1'
implementation 'org.gavaghan:geodesy:1.1.3'
implementation 'org.slf4j:slf4j-api:1.7.30'
implementation 'org.slf4j:slf4j-api:2.0.9'
runtimeOnly 'org.slf4j:slf4j-android:1.7.30'

implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.google.code.gson:gson:2.8.9'

implementation files('libs/fit.jar')
implementation files('libs/connectiq-mobile-sdk-android-1.5.aar')
implementation 'org.apache.commons:commons-lang3:3.13.0'

implementation 'com.garmin:fit:21.120.0'
implementation 'com.garmin.connectiq:ciq-companion-app-sdk:2.0.3@aar'

// androidTestImplementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
// androidTestImplementation group: 'xpp3', name: 'xpp3_min', version: '1.1.3.4.O'
Expand All @@ -54,7 +56,7 @@ dependencies {
// androidTestImplementation 'xmlpull:xmlpull:1.1.3.1'
// androidTestImplementation 'org.mockito:mockito-core:1.10.19'
testImplementation 'net.sf.kxml:kxml2:2.3.0'
testImplementation 'org.slf4j:slf4j-api:1.7.30'
testImplementation 'org.slf4j:slf4j-api:2.0.9'

//testImplementation 'xmlpull:xmlpull:1.1.3.1'
}
98 changes: 48 additions & 50 deletions app/src/main/java/org/surfsite/gexporter/Gpx2Fit.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.surfsite.gexporter;

import static org.apache.commons.lang3.StringUtils.defaultString;

import android.annotation.SuppressLint;

import com.garmin.fit.CourseMesg;
Expand All @@ -26,20 +28,16 @@
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class Gpx2Fit {
private static final Logger Log = LoggerFactory.getLogger(Gpx2Fit.class);
Expand Down Expand Up @@ -168,13 +166,25 @@ private String readTrkName(XmlPullParser parser) throws IOException, XmlPullPars
return txt;
}

private String readName(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "name");
private String readSimpleTextTag(XmlPullParser parser, String tagName) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, tagName);
String txt = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "name");
parser.require(XmlPullParser.END_TAG, ns, tagName);
return txt;
}

private String readName(XmlPullParser parser) throws IOException, XmlPullParserException {
return readSimpleTextTag(parser, "name");
}

private String readType(XmlPullParser parser) throws IOException, XmlPullParserException {
return readSimpleTextTag(parser, "type");
}

private String readSymbol(XmlPullParser parser) throws IOException, XmlPullParserException {
return readSimpleTextTag(parser, "sym");
}

private void readTrkSeg(XmlPullParser parser) throws XmlPullParserException, IOException, ParseException {
parser.require(XmlPullParser.START_TAG, ns, "trkseg");
while (parser.next() != XmlPullParser.END_TAG) {
Expand Down Expand Up @@ -238,7 +248,7 @@ private void readTrkPt(XmlPullParser parser) throws XmlPullParserException, IOEx
break;
}
}
trkPoints.add(new WayPoint(name, lat, lon, ele, time));
trkPoints.add(new WayPoint(name, lat, lon, ele, time, null, null));
}

private void readWpt(XmlPullParser parser) throws XmlPullParserException, IOException, ParseException {
Expand All @@ -248,6 +258,8 @@ private void readWpt(XmlPullParser parser) throws XmlPullParserException, IOExce
double lat = Double.parseDouble(parser.getAttributeValue(null, "lat"));
double lon = Double.parseDouble(parser.getAttributeValue(null, "lon"));
double ele = Double.NaN;
String type = null;
String symbol = null;

while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
Expand All @@ -264,12 +276,18 @@ private void readWpt(XmlPullParser parser) throws XmlPullParserException, IOExce
case "name":
name = readName(parser);
break;
case "type":
type = readType(parser);
break;
case "sym":
symbol = readSymbol(parser);
break;
default:
skip(parser);
break;
}
}
wayPoints.add(new WayPoint(name, lat, lon, ele, time));
wayPoints.add(new WayPoint(name, lat, lon, ele, time, type, symbol));
}

private void readRtePt(XmlPullParser parser) throws XmlPullParserException, IOException, ParseException {
Expand Down Expand Up @@ -300,7 +318,7 @@ private void readRtePt(XmlPullParser parser) throws XmlPullParserException, IOEx
break;
}
}
rtePoints.add(new WayPoint(name, lat, lon, ele, time));
rtePoints.add(new WayPoint(name, lat, lon, ele, time, null, null));
}

private double readEle(XmlPullParser parser) throws IOException, XmlPullParserException {
Expand Down Expand Up @@ -346,8 +364,9 @@ public String getName() {
* Grade adjusted pace based on a study by Alberto E. Minetti on the energy cost of
* walking and running at extreme slopes.
* <p>
* see Minetti, A. E. et al. (2002). Energy cost of walking and running at extreme uphill and downhill slopes.
* Journal of Applied Physiology 93, 1039-1046, http://jap.physiology.org/content/93/3/1039.full
* see <a href="http://jap.physiology.org/content/93/3/1039.full">Minetti, A. E. et al. (2002).
* Energy cost of walking and running at extreme uphill and downhill slopes.
* Journal of Applied Physiology 93, 1039-1046</a>
Comment on lines -349 to +369
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removes a warning

*/
public double getWalkingGradeFactor(double g) {
return 1.0 + (g * (19.5 + g * (46.3 + g * (-43.3 + g * (-30.4 + g * 155.4))))) / 3.6;
Expand Down Expand Up @@ -539,14 +558,8 @@ public void writeFit(File outfile) {
lapMesg.setFieldValue(28, 0, (Integer) WayPoint.toSemiCircles(maxLong), '\uffff');
lapMesg.setFieldValue(29, 0, (Integer) WayPoint.toSemiCircles(minLat), '\uffff');
lapMesg.setFieldValue(30, 0, (Integer) WayPoint.toSemiCircles(minLong), '\uffff');
} catch (NoSuchMethodException e) {
;
} catch (IllegalAccessException e) {
;
} catch (InstantiationException e) {
;
} catch (InvocationTargetException e) {
;
} catch (NoSuchMethodException | IllegalAccessException | InstantiationException |
InvocationTargetException ignored) {
Comment on lines -542 to +562
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removes a warning

}

encode.write(lapMesg);
Expand All @@ -568,39 +581,11 @@ public void writeFit(File outfile) {


if (!skipExtraCP && !wayPoints.isEmpty()) {
for (WayPoint wpt : wayPoints) {
CoursePointMesg cp = new CoursePointMesg();
cp.setLocalNum(0);

cp.setPositionLat(wpt.getLatSemi());
cp.setPositionLong(wpt.getLonSemi());
String name = wpt.getName();
if (name != null) {
cp.setName(name);
} else {
cp.setName("");
}
cp.setType(CoursePoint.GENERIC);
encode.write(cp);
}
writeWayPoints(encode, wayPoints);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reduced code duplication

}

if (!skipExtraCP && !rtePoints.isEmpty()) {
for (WayPoint wpt : rtePoints) {
CoursePointMesg cp = new CoursePointMesg();
cp.setLocalNum(0);

cp.setPositionLat(wpt.getLatSemi());
cp.setPositionLong(wpt.getLonSemi());
String name = wpt.getName();
if (name != null) {
cp.setName(name);
} else {
cp.setName("");
}
cp.setType(CoursePoint.GENERIC);
encode.write(cp);
}
writeWayPoints(encode, rtePoints);
}

{
Expand Down Expand Up @@ -746,4 +731,17 @@ public void writeFit(File outfile) {

encode.close();
}

private void writeWayPoints(FileEncoder encode, List<WayPoint> points) {
for (WayPoint wpt : points) {
CoursePointMesg cp = new CoursePointMesg();
cp.setLocalNum(0);

cp.setPositionLat(wpt.getLatSemi());
cp.setPositionLong(wpt.getLonSemi());
cp.setName(defaultString(wpt.getName()));
cp.setType(wpt.getPointType());
encode.write(cp);
}
}
}
60 changes: 54 additions & 6 deletions app/src/main/java/org/surfsite/gexporter/WayPoint.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package org.surfsite.gexporter;

import static org.apache.commons.lang3.StringUtils.isNumeric;
import static org.apache.commons.lang3.math.NumberUtils.toShort;

import com.garmin.fit.CoursePoint;

import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GlobalCoordinates;
Expand All @@ -17,14 +22,17 @@ public class WayPoint {
// select a reference ellipsoid
private static final Ellipsoid reference = Ellipsoid.WGS84;

private double lat = Double.NaN;
private double lon = Double.NaN;
private double ele = Double.NaN;
private Date time = null;
private double lat;
private double lon;
private double ele;
private Date time;
Comment on lines -20 to +28
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

values are overridden in constructor so they don't have to be set here

private double totaldist = Double.NaN;
private String name = null;
private String name;

public WayPoint(String name, double lat, double lon, double ele, Date time) {
private String type;
private String symbol;

public WayPoint(String name, double lat, double lon, double ele, Date time, String type, String symbol) {
this.lat = lat;
this.lon = lon;
this.ele = ele;
Expand All @@ -33,6 +41,8 @@ public WayPoint(String name, double lat, double lon, double ele, Date time) {
if (this.time == null) {
this.time = RefDate;
}
this.type = type;
this.symbol = symbol;
}

public String getName() {
Expand Down Expand Up @@ -67,6 +77,15 @@ public double getEle() {
public Date getTime() {
return time;
}

public String getType() {
return type;
}

public String getSymbol() {
return symbol;
}

public void setLat(double lat) {
this.lat = lat;
}
Expand All @@ -83,6 +102,14 @@ public void setTime(Date time) {
this.time = time;
}

public void setType(String type) {
this.type = type;
}

public void setSymbol(String symbol) {
this.symbol = symbol;
}

public double distance(WayPoint other) {
return geoCalc.calculateGeodeticCurve(reference,
new GlobalCoordinates(getLat(), this.getLon()),
Expand Down Expand Up @@ -110,4 +137,25 @@ public void setTotaldist(double totaldist) {
this.totaldist = totaldist;
}

public CoursePoint getPointType() {
if (parseStringToCoursePoint(type) != null) {
return parseStringToCoursePoint(type);
}
if (parseStringToCoursePoint(symbol) != null) {
return parseStringToCoursePoint(symbol);
}
return CoursePoint.GENERIC;
}

private CoursePoint parseStringToCoursePoint(String s) {
if (isNumeric(s)) {
return CoursePoint.getByValue(toShort(s));
} else {
try {
return CoursePoint.valueOf(s);
} catch (IllegalArgumentException exception) {
return null;
}
}
}
}
5 changes: 2 additions & 3 deletions app/src/main/java/org/surfsite/gexporter/WebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;

import fi.iki.elonen.NanoHTTPD;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import fi.iki.elonen.NanoHTTPD;

public class WebServer extends NanoHTTPD {
private static final Logger Log = LoggerFactory.getLogger(NanoHTTPD.class);

Expand Down