-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathEts2TelemetryController.cs
More file actions
55 lines (50 loc) · 2.04 KB
/
Copy pathEts2TelemetryController.cs
File metadata and controls
55 lines (50 loc) · 2.04 KB
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
using System;
using System.Configuration;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Web.Http;
using Funbit.Ets.Telemetry.Server.Data;
using Funbit.Ets.Telemetry.Server.Helpers;
using Newtonsoft.Json;
namespace Funbit.Ets.Telemetry.Server.Controllers
{
[RoutePrefix("api")]
public class Ets2TelemetryController : ApiController
{
public const string TelemetryApiUriPath = "/api/ets2/telemetry";
const string TestTelemetryJsonFileName = "Ets2TestTelemetry.json";
static readonly bool UseTestTelemetryData = Convert.ToBoolean(
ConfigurationManager.AppSettings["UseEts2TestTelemetryData"]);
public static string GetEts2TelemetryJson()
{
// if we have test data defined in the app.config then use it
if (UseTestTelemetryData)
{
using (var file = File.Open(
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, TestTelemetryJsonFileName),
FileMode.Open,
FileAccess.Read,
FileShare.ReadWrite))
using (var reader = new StreamReader(file, Encoding.UTF8))
return reader.ReadToEnd();
}
// otherwise return real data from the simulator
return JsonConvert.SerializeObject(Ets2TelemetryDataReader.Instance.Read(), JsonHelper.RestSettings);
}
[HttpGet]
[HttpPost]
[Route("ets2/telemetry", Name = "Get")]
public HttpResponseMessage Get()
{
var telemetryJson = GetEts2TelemetryJson();
var response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent(telemetryJson, Encoding.UTF8, "application/json");
response.Headers.CacheControl = new CacheControlHeaderValue { NoCache = true };
response.Headers.Add("Access-Control-Allow-Origin", "*");
return response;
}
}
}