Skip to content

Commit

Permalink
v1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bionicl authored Jul 5, 2018
2 parents 2d36705 + 9286a32 commit e03babe
Show file tree
Hide file tree
Showing 8 changed files with 749 additions and 6 deletions.
9 changes: 9 additions & 0 deletions Arc-app-export-converter/Arc-app-export-converter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,19 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="BurnedCalCalculator.cs" />
<Compile Include="XmlReader.cs" />
<Compile Include="JsonParser.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
31 changes: 31 additions & 0 deletions Arc-app-export-converter/BurnedCalCalculator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

[JsonConverter(typeof(StringEnumConverter))]
public enum ActivityType {
walking,
cycling,
running,
car,
transport,
train,
bus,
motorcycle,
airplane,
boat
}

public static class BurnedCalCalculator {
public static float[] activityTypeMultiplayer = { 0.79f, 0.37f, 1.03f };
// multiplayer values based on https://www.topendsports.com/weight-loss/energy-met.htm
// and Moves app data

public static int Calcualate(ActivityType type, float time, float avgSpeed, float weight) {
if ((int)type > 2)
return 0;
float value = activityTypeMultiplayer[(int)type] * time / 3600 * avgSpeed * weight;
return (int)Math.Round(value);
}

}
25 changes: 25 additions & 0 deletions Arc-app-export-converter/BurningCal/BurnMETValuesImporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.IO;

namespace ArcAppExportConverter.BurningCal {

public class BurnMETValue {
public ActivityType activity;
public float minAvgSpeed; //in km/s
public float metValue;

public BurnMETValue (string activity, float minAvgSpeed, float metValue) {
Enum.TryParse(activity, out this.activity);
this.minAvgSpeed = minAvgSpeed;
this.metValue = metValue;
}
public BurnMETValue(string[] array) : this(array[0], float.Parse(array[1]), float.Parse(array[2])) {

}
}

public class BurnMETValuesImporter {

}
}
223 changes: 223 additions & 0 deletions Arc-app-export-converter/JsonParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

public class JsonMoves {
[JsonConverter(typeof(StringEnumConverter))]
public enum SegmentType {
place,
move
}

[JsonConverter(typeof(StringEnumConverter))]
public enum ActivityGroup {
walking,
cycling,
running,
transport
}

public class Day {
public class Summary {
public ActivityType activity;
public ActivityGroup group;
public double duration;
public double distance;
public double? calories;

public Summary(ActivityType activity, float duration, double distance, float calories) {
this.activity = activity;
group = HelpMethods.ReturnGroup(activity);
this.duration = duration;
this.distance = Math.Round(distance * 1000);
if (calories > 0)
this.calories = calories;
}
}
public class Segment {
public class Place {
public class Location {
public double lat;
public double lon;

public Location(double lat, double lon) {
this.lat = lat;
this.lon = lon;
}
}
public long id = 1234567;
public string name;
public string type = "user";
public Location location;

public Place(string name, Location location) {
this.name = name;
this.location = location;
}

}
public class Activity {
public class TrackPoint {
public double lat;
public double lon;
public string time;

public TrackPoint(double lat, double lon, DateTime time) {
this.lat = lat;
this.lon = lon;
this.time = HelpMethods.ConvertToIso1601(time);
}

}

public ActivityType activity;
public ActivityGroup group;
public bool manual = false;
public string startTime;
public string endTime;
public double duration;
public double distance;
public double calories;
public TrackPoint[] trackPoints;

public Activity(ActivityType type, ActivityGroup group, DateTime startTime,
DateTime endTime, double duration, double distance,
double calories, List<TrackPoint> trackpoints) {
activity = type;
this.group = group;
this.startTime = HelpMethods.ConvertToIso1601(startTime);
this.endTime = HelpMethods.ConvertToIso1601(endTime);
this.duration = duration;
this.distance = Math.Round(distance * 1000);
if (calories > 0)
this.calories = calories;
this.trackPoints = trackpoints.ToArray();
}

}

public SegmentType type;
public string startTime;
public string endTime;
public Place place;
public Activity[] activities;
public string lastUpdate;

public Segment(SegmentType type, DateTime startTime, DateTime endTime) {
this.type = type;
this.startTime = HelpMethods.ConvertToIso1601(startTime);
this.endTime = HelpMethods.ConvertToIso1601(endTime);
lastUpdate = HelpMethods.ConvertToIso1601(DateTime.Now);
}

}
public string date;
public Summary[] summary;
public Segment[] segments;
public string caloriesIdle;
public string lastUpdate;

public Day(DateTime date) {
this.date = date.ToString("yyyyMMdd");
lastUpdate = HelpMethods.ConvertToIso1601(DateTime.Now);
}
}
public Day[] day;
}



public static class JsonParser {
public static void Parse(List<XmlReader> xml) {
JsonMoves json = new JsonMoves();
json.day = new JsonMoves.Day[xml.Count];
for (int i = 0; i < xml.Count; i++) {
// Date
json.day[i] = new JsonMoves.Day(xml[i].date);

// Summary
ParseSummary(i, json, xml);

// Segments
ParseSegments(i, json, xml);
}
string output = JsonConvert.SerializeObject(json,
Newtonsoft.Json.Formatting.None,
new JsonSerializerSettings {
NullValueHandling = NullValueHandling.Ignore
});
output = CleanUpJson(output);
WriteToFile(output);
}


static void ParseSummary(int i, JsonMoves json, List<XmlReader> xml) {
List<ActivitySummary> summaries = new List<ActivitySummary>();
for (int o = 0; o < 10; o++) {
if (xml[i].summary[o].duration > 0)
summaries.Add(xml[i].summary[o]);
}
List<JsonMoves.Day.Summary> convertedSummaries = new List<JsonMoves.Day.Summary>();
foreach (var item in summaries) {
convertedSummaries.Add(item.ToMoves());
}
json.day[i].summary = convertedSummaries.ToArray();
}

// Summary parsing
static void ParseSegments(int i, JsonMoves json, List<XmlReader> xml) {
List<JsonMoves.Day.Segment> segments = new List<JsonMoves.Day.Segment>();
foreach (var item in xml[i].timelineItems) {
if (item.type == XmlTimeline.TimelineItemType.place)
segments.Add(SegmentPlace(item.place));
else
segments.Add(SegmentMove(item.activity));
}
json.day[i].segments = segments.ToArray();
}
static JsonMoves.Day.Segment SegmentPlace(XmlTimeline.Place item) {
JsonMoves.Day.Segment output = new JsonMoves.Day.Segment(JsonMoves.SegmentType.place, item.startTime.Value, item.endTime.Value);
output.place = new JsonMoves.Day.Segment.Place(item.name, new JsonMoves.Day.Segment.Place.Location(item.position.lat, item.position.lon));
return output;
}
static JsonMoves.Day.Segment SegmentMove(XmlTimeline.Activity item) {
JsonMoves.Day.Segment output = new JsonMoves.Day.Segment(JsonMoves.SegmentType.move, item.startTime, item.endTime);

// TrackPoints
List<JsonMoves.Day.Segment.Activity.TrackPoint> trackpoints = new List<JsonMoves.Day.Segment.Activity.TrackPoint>();
foreach (var waypoint in item.waypoints) {
trackpoints.Add(new JsonMoves.Day.Segment.Activity.TrackPoint(waypoint.lat, waypoint.lon, waypoint.time.Value));
}

// Setup
List<JsonMoves.Day.Segment.Activity> activity = new List<JsonMoves.Day.Segment.Activity>();
activity.Add(new JsonMoves.Day.Segment.Activity(item.activity,
HelpMethods.ReturnGroup(item.activity),
item.startTime,
item.endTime,
item.Duration,
item.Distance,
item.Calories,
trackpoints));
output.activities = activity.ToArray();
return output;
}

// Clean up to match moves json output
static string CleanUpJson(string json) {
json = json.Substring(7);
json = json.Remove(json.Length - 1);
return json;
}

static void WriteToFile(string json) {
StreamWriter sw = new StreamWriter("file.json");
sw.Write(json);
sw.Close();
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.Write("Json file created!");
}
}

16 changes: 11 additions & 5 deletions Arc-app-export-converter/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
using System;

namespace Arcappexportconverter {
class MainClass {
public static void Main(string[] args) {
Console.WriteLine("Hello World!");
}
class MainClass {

public static float weight = 83;

[STAThread]
public static void Main() {
Console.Write("Input your weight (in kg): ");
weight = Convert.ToInt32(Console.ReadLine());
XmlReader xr = new XmlReader();
xr.LoadFile();
xr.ParseToJson();
}
}
Loading

0 comments on commit e03babe

Please sign in to comment.