/
FakeLocationMessenger.cs
132 lines (120 loc) · 5.09 KB
/
FakeLocationMessenger.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Windows.Resources;
using System.IO;
using System.ComponentModel;
using System.Threading;
using Newtonsoft.Json;
using Rider.Persistent;
namespace Rider.Location
{
public class FakeLocationMessenger : FakeLocation
{
Stack<FakeGeoCoordinateObject> events;
LocationService.Mode mode;
public FakeLocationMessenger(IEnumerable<FakeGeoCoordinateObject> events)
{
this.events = new Stack<FakeGeoCoordinateObject>(events);
}
public FakeLocationMessenger(LocationService.Mode mode = LocationService.Mode.JsonFile)
{
this.events = new Stack<FakeGeoCoordinateObject>();
this.mode = mode;
asyncLocationWorker = new BackgroundWorker();
asyncLocationWorker.WorkerSupportsCancellation = true;
asyncLocationWorker.DoWork += (s, args) => { AsyncLocationWorkerCallback(s, args); };
asyncLocationWorker.RunWorkerCompleted += (s, args) => { AsyncLocationWorkerEnded(s, args); };
asyncLocationWorker.RunWorkerAsync();
}
private void AsyncLocationWorkerEnded(object s, RunWorkerCompletedEventArgs args)
{
//sif (asyncLocationWorker.IsBusy) asyncLocationWorker.CancelAsync();
}
private void ReadJsonFile(object s, DoWorkEventArgs args)
{
BackgroundWorker worker = s as BackgroundWorker;
bool exit = false;
string road = string.Format("Resources/JsonRoads/{0}.json", Configuration.FAKE_JSON_ROAD);
using (Stream stream = Application.GetResourceStream(new Uri(road, UriKind.Relative)).Stream)
{
using (TextReader textReader = new StreamReader(stream))
{
using (var jsonReader = new JsonTextReader(textReader))
{
while (jsonReader.Read() && !exit)
{
FakeGeoCoordinateObject loc = new FakeGeoCoordinateObject();
loc.Course = double.NaN;
if (jsonReader.TokenType == JsonToken.StartObject)
{
do
{
if (worker.CancellationPending == true) {
args.Cancel = true;
exit = true;
}
if (jsonReader.TokenType == JsonToken.PropertyName && jsonReader.Value.Equals("lat"))
{
jsonReader.Read();
loc.Latitude = Convert.ToDouble(jsonReader.Value);
}
else if (jsonReader.TokenType == JsonToken.PropertyName && jsonReader.Value.Equals("lng"))
{
jsonReader.Read();
loc.Longitude = Convert.ToDouble(jsonReader.Value);
}
else if (jsonReader.TokenType == JsonToken.PropertyName && jsonReader.Value.Equals("bearing"))
{
jsonReader.Read();
loc.Course = Convert.ToDouble(jsonReader.Value);
}
} while (jsonReader.TokenType != JsonToken.EndObject && jsonReader.Read() && !exit);
if (!exit) {
this.events.Push(loc);
Thread.Sleep(Configuration.FAKE_LOCATION_DELAY);
}
}
}
}
}
}
}
private void AsyncLocationWorkerCallback(object s, DoWorkEventArgs args)
{
if (mode == LocationService.Mode.JsonFile)
{
ReadJsonFile(s, args);
}
else
{
// TODO : Wikansit
}
}
private FakeGeoCoordinateObject Current
{
get
{
return (events.Count > 0) ? events.Pop() : null;
}
}
public void Close() {
if (asyncLocationWorker != null && asyncLocationWorker.WorkerSupportsCancellation)
asyncLocationWorker.CancelAsync();
this.Stop();
}
protected override void StartGetCurrentPosition()
{
FakeGeoCoordinateObject loc = Current;
if (loc != null) this.UpdateLocation(loc);
}
}
}