-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTelemetryJsonService.cs
169 lines (143 loc) · 6.06 KB
/
TelemetryJsonService.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using Newtonsoft.Json;
using SCSSdkClient;
using SCSSdkClient.Object;
using static SCSSdkClient.Object.SCSTelemetry;
namespace TelemetryJsonService
{
public partial class TelemetryJsonService : Form
{
private SCSSdkTelemetry Telemetry { get; set; }
private static string baseDir { get; set; }
public TelemetryJsonService()
{
baseDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
InitializeComponent();
try
{
this.InitializeTelemetry();
DataService.StartWebServer();
this.UpdateSharedJs();
this.tbUrl.Text = $"http://localhost:{DataService.Port}/";
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private void Form_Closing(object sender, FormClosingEventArgs e)
{
Telemetry.Dispose();
}
private void Form_Resize(object sender, EventArgs e)
{
this.ShowInTaskbar = this.WindowState != FormWindowState.Minimized;
this.notifyIcon1.Visible = !this.ShowInTaskbar;
}
private void notifyIcon_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.WindowState = FormWindowState.Normal;
}
}
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Close();
}
/// <summary>
/// Keep port of shared javascript up to date
/// </summary>
private void UpdateSharedJs()
{
var assembly = Assembly.GetExecutingAssembly();
string resourcePath = assembly.GetManifestResourceNames().Single(str => str.EndsWith("shared-template.js"));
string resourceContent;
using (Stream stream = assembly.GetManifestResourceStream(resourcePath))
using (StreamReader reader = new StreamReader(stream))
{
resourceContent = reader.ReadToEnd();
}
resourceContent = resourceContent.Replace("{{port}}", DataService.Port.ToString());
File.WriteAllText(Path.Combine(baseDir, "overlays/js", "shared.js"), resourceContent);
}
private void InitializeTelemetry()
{
Telemetry = new SCSSdkTelemetry();
Telemetry.Data += Telemetry_Data;
Telemetry.JobStarted += TelemetryOnJobStarted;
Telemetry.JobCancelled += TelemetryJobCancelled;
Telemetry.JobDelivered += TelemetryJobDelivered;
Telemetry.Ferry += TelemetryFerry;
Telemetry.Train += TelemetryTrain;
}
private void Telemetry_Data(SCSTelemetry data, bool updated)
{
try
{
// Pause the Telemetry while processing data
Telemetry.pause();
// convert data back and forth and save populate json
var jsonString = JsonConvert.SerializeObject(data);
var jsonObj = JsonConvert.DeserializeObject<CustomTelemetry>(jsonString);
// Do some basic transformation
switch (data.Game)
{
case SCSGame.Ets2:
jsonObj.Game = "ETS2";
break;
case SCSGame.Ats:
jsonObj.Game = "ATS";
break;
default:
jsonObj.Game = "n.a.";
break;
}
#if DEBUG
// Simple mocking
var mockData = File.OpenText(Path.Combine(baseDir, "../../mock", "data.json")).ReadToEnd();
jsonObj = JsonConvert.DeserializeObject<CustomTelemetry>(mockData);
//jsonObj.Game = "ATS";
//jsonObj.SdkActive = false;
//jsonObj.Paused = false;
//jsonObj.SpecialEventsValues.OnJob = false;
#endif
// Display some data on UI
this.BeginInvoke((MethodInvoker)delegate ()
{
lbGame.Text = jsonObj.Game;
lbGameVersion.Text = $"v{jsonObj.GameVersion.Major}.{jsonObj.GameVersion.Minor}";
lbTelemetryVersion.Text = $"v{jsonObj.TelemetryVersion.Major}.{jsonObj.TelemetryVersion.Minor}";
lbUpdateInterval.Text = Telemetry.UpdateInterval + "ms";
tbStatus.Text = jsonObj.SdkActive ? "SCS-SDK connected and running!" : "SCS-SDK currently not running... ¯\\_(ツ)_/¯ ";
});
// serialize back
var jsonData = JsonConvert.SerializeObject(jsonObj, Formatting.Indented);
// write to file to be able find data objects easily
File.WriteAllText(Path.Combine(baseDir, "overlays", "data.json"), jsonData);
DataService.JsonData = jsonData;
Telemetry.resume();
}
catch (Exception ex)
{
Console.WriteLine("Telemetry was closed: " + ex);
}
}
private void TelemetryFerry(object sender, EventArgs e) =>
Console.WriteLine("Ferry");
private void TelemetryJobCancelled(object sender, EventArgs e) =>
Console.WriteLine("Job Cancelled");
private void TelemetryJobDelivered(object sender, EventArgs e) =>
Console.WriteLine("Job Delivered");
private void TelemetryOnJobStarted(object sender, EventArgs e) =>
Console.WriteLine("Just started job OR loaded game with active.");
private void TelemetryTrain(object sender, EventArgs e) =>
Console.WriteLine("Train");
}
}
// Used icon is https://iconarchive.com/show/papirus-mimetypes-icons-by-papirus-team/app-json-icon.html under GNU General Public License v3.0
// So this app will inherit from that.