|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using Newtonsoft.Json; |
| 5 | +using Newtonsoft.Json.Converters; |
| 6 | + |
| 7 | +public class JsonMoves { |
| 8 | + [JsonConverter(typeof(StringEnumConverter))] |
| 9 | + public enum SegmentType { |
| 10 | + place, |
| 11 | + move |
| 12 | + } |
| 13 | + |
| 14 | + [JsonConverter(typeof(StringEnumConverter))] |
| 15 | + public enum ActivityGroup { |
| 16 | + walking, |
| 17 | + cycling, |
| 18 | + running, |
| 19 | + transport |
| 20 | + } |
| 21 | + |
| 22 | + public class Day { |
| 23 | + public class Summary { |
| 24 | + public ActivityType activity; |
| 25 | + public ActivityGroup group; |
| 26 | + public double duration; |
| 27 | + public double distance; |
| 28 | + public double? calories; |
| 29 | + |
| 30 | + public Summary(ActivityType activity, float duration, double distance, float calories) { |
| 31 | + this.activity = activity; |
| 32 | + group = HelpMethods.ReturnGroup(activity); |
| 33 | + this.duration = duration; |
| 34 | + this.distance = Math.Round(distance * 1000); |
| 35 | + if (calories > 0) |
| 36 | + this.calories = calories; |
| 37 | + } |
| 38 | + } |
| 39 | + public class Segment { |
| 40 | + public class Place { |
| 41 | + public class Location { |
| 42 | + public double lat; |
| 43 | + public double lon; |
| 44 | + |
| 45 | + public Location(double lat, double lon) { |
| 46 | + this.lat = lat; |
| 47 | + this.lon = lon; |
| 48 | + } |
| 49 | + } |
| 50 | + public long id = 1234567; |
| 51 | + public string name; |
| 52 | + public string type = "user"; |
| 53 | + public Location location; |
| 54 | + |
| 55 | + public Place(string name, Location location) { |
| 56 | + this.name = name; |
| 57 | + this.location = location; |
| 58 | + } |
| 59 | + |
| 60 | + } |
| 61 | + public class Activity { |
| 62 | + public class TrackPoint { |
| 63 | + public double lat; |
| 64 | + public double lon; |
| 65 | + public string time; |
| 66 | + |
| 67 | + public TrackPoint(double lat, double lon, DateTime time) { |
| 68 | + this.lat = lat; |
| 69 | + this.lon = lon; |
| 70 | + this.time = HelpMethods.ConvertToIso1601(time); |
| 71 | + } |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + public ActivityType activity; |
| 76 | + public ActivityGroup group; |
| 77 | + public bool manual = false; |
| 78 | + public string startTime; |
| 79 | + public string endTime; |
| 80 | + public double duration; |
| 81 | + public double distance; |
| 82 | + public double calories; |
| 83 | + public TrackPoint[] trackPoints; |
| 84 | + |
| 85 | + public Activity(ActivityType type, ActivityGroup group, DateTime startTime, |
| 86 | + DateTime endTime, double duration, double distance, |
| 87 | + double calories, List<TrackPoint> trackpoints) { |
| 88 | + activity = type; |
| 89 | + this.group = group; |
| 90 | + this.startTime = HelpMethods.ConvertToIso1601(startTime); |
| 91 | + this.endTime = HelpMethods.ConvertToIso1601(endTime); |
| 92 | + this.duration = duration; |
| 93 | + this.distance = Math.Round(distance * 1000); |
| 94 | + if (calories > 0) |
| 95 | + this.calories = calories; |
| 96 | + this.trackPoints = trackpoints.ToArray(); |
| 97 | + } |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + public SegmentType type; |
| 102 | + public string startTime; |
| 103 | + public string endTime; |
| 104 | + public Place place; |
| 105 | + public Activity[] activities; |
| 106 | + public string lastUpdate; |
| 107 | + |
| 108 | + public Segment(SegmentType type, DateTime startTime, DateTime endTime) { |
| 109 | + this.type = type; |
| 110 | + this.startTime = HelpMethods.ConvertToIso1601(startTime); |
| 111 | + this.endTime = HelpMethods.ConvertToIso1601(endTime); |
| 112 | + lastUpdate = HelpMethods.ConvertToIso1601(DateTime.Now); |
| 113 | + } |
| 114 | + |
| 115 | + } |
| 116 | + public string date; |
| 117 | + public Summary[] summary; |
| 118 | + public Segment[] segments; |
| 119 | + public string caloriesIdle; |
| 120 | + public string lastUpdate; |
| 121 | + |
| 122 | + public Day(DateTime date) { |
| 123 | + this.date = date.ToString("yyyyMMdd"); |
| 124 | + lastUpdate = HelpMethods.ConvertToIso1601(DateTime.Now); |
| 125 | + } |
| 126 | + } |
| 127 | + public Day[] day; |
| 128 | +} |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | +public static class JsonParser { |
| 133 | + public static void Parse(List<XmlReader> xml) { |
| 134 | + JsonMoves json = new JsonMoves(); |
| 135 | + json.day = new JsonMoves.Day[xml.Count]; |
| 136 | + for (int i = 0; i < xml.Count; i++) { |
| 137 | + // Date |
| 138 | + json.day[i] = new JsonMoves.Day(xml[i].date); |
| 139 | + |
| 140 | + // Summary |
| 141 | + ParseSummary(i, json, xml); |
| 142 | + |
| 143 | + // Segments |
| 144 | + ParseSegments(i, json, xml); |
| 145 | + } |
| 146 | + string output = JsonConvert.SerializeObject(json, |
| 147 | + Newtonsoft.Json.Formatting.None, |
| 148 | + new JsonSerializerSettings { |
| 149 | + NullValueHandling = NullValueHandling.Ignore |
| 150 | + }); |
| 151 | + output = CleanUpJson(output); |
| 152 | + WriteToFile(output); |
| 153 | + } |
| 154 | + |
| 155 | + |
| 156 | + static void ParseSummary(int i, JsonMoves json, List<XmlReader> xml) { |
| 157 | + List<ActivitySummary> summaries = new List<ActivitySummary>(); |
| 158 | + for (int o = 0; o < 10; o++) { |
| 159 | + if (xml[i].summary[o].duration > 0) |
| 160 | + summaries.Add(xml[i].summary[o]); |
| 161 | + } |
| 162 | + List<JsonMoves.Day.Summary> convertedSummaries = new List<JsonMoves.Day.Summary>(); |
| 163 | + foreach (var item in summaries) { |
| 164 | + convertedSummaries.Add(item.ToMoves()); |
| 165 | + } |
| 166 | + json.day[i].summary = convertedSummaries.ToArray(); |
| 167 | + } |
| 168 | + |
| 169 | + // Summary parsing |
| 170 | + static void ParseSegments(int i, JsonMoves json, List<XmlReader> xml) { |
| 171 | + List<JsonMoves.Day.Segment> segments = new List<JsonMoves.Day.Segment>(); |
| 172 | + foreach (var item in xml[i].timelineItems) { |
| 173 | + if (item.type == XmlTimeline.TimelineItemType.place) |
| 174 | + segments.Add(SegmentPlace(item.place)); |
| 175 | + else |
| 176 | + segments.Add(SegmentMove(item.activity)); |
| 177 | + } |
| 178 | + json.day[i].segments = segments.ToArray(); |
| 179 | + } |
| 180 | + static JsonMoves.Day.Segment SegmentPlace(XmlTimeline.Place item) { |
| 181 | + JsonMoves.Day.Segment output = new JsonMoves.Day.Segment(JsonMoves.SegmentType.place, item.startTime.Value, item.endTime.Value); |
| 182 | + output.place = new JsonMoves.Day.Segment.Place(item.name, new JsonMoves.Day.Segment.Place.Location(item.position.lat, item.position.lon)); |
| 183 | + return output; |
| 184 | + } |
| 185 | + static JsonMoves.Day.Segment SegmentMove(XmlTimeline.Activity item) { |
| 186 | + JsonMoves.Day.Segment output = new JsonMoves.Day.Segment(JsonMoves.SegmentType.move, item.startTime, item.endTime); |
| 187 | + |
| 188 | + // TrackPoints |
| 189 | + List<JsonMoves.Day.Segment.Activity.TrackPoint> trackpoints = new List<JsonMoves.Day.Segment.Activity.TrackPoint>(); |
| 190 | + foreach (var waypoint in item.waypoints) { |
| 191 | + trackpoints.Add(new JsonMoves.Day.Segment.Activity.TrackPoint(waypoint.lat, waypoint.lon, waypoint.time.Value)); |
| 192 | + } |
| 193 | + |
| 194 | + // Setup |
| 195 | + List<JsonMoves.Day.Segment.Activity> activity = new List<JsonMoves.Day.Segment.Activity>(); |
| 196 | + activity.Add(new JsonMoves.Day.Segment.Activity(item.activity, |
| 197 | + HelpMethods.ReturnGroup(item.activity), |
| 198 | + item.startTime, |
| 199 | + item.endTime, |
| 200 | + item.Duration, |
| 201 | + item.Distance, |
| 202 | + item.Calories, |
| 203 | + trackpoints)); |
| 204 | + output.activities = activity.ToArray(); |
| 205 | + return output; |
| 206 | + } |
| 207 | + |
| 208 | + // Clean up to match moves json output |
| 209 | + static string CleanUpJson(string json) { |
| 210 | + json = json.Substring(7); |
| 211 | + json = json.Remove(json.Length - 1); |
| 212 | + return json; |
| 213 | + } |
| 214 | + |
| 215 | + static void WriteToFile(string json) { |
| 216 | + StreamWriter sw = new StreamWriter("file.json"); |
| 217 | + sw.Write(json); |
| 218 | + sw.Close(); |
| 219 | + Console.ForegroundColor = ConsoleColor.DarkGreen; |
| 220 | + Console.Write("Json file created!"); |
| 221 | + } |
| 222 | +} |
| 223 | + |
0 commit comments