-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathAcars.cs
147 lines (118 loc) · 4.85 KB
/
Acars.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
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Timers;
namespace vPilot_Pushover {
internal class Acars {
// Init
private static Main Plugin;
private static INotifier notifier;
private static List<Dictionary<string, string>> hoppieCache = new List<Dictionary<string, string>>();
private static Boolean cacheLoaded = false;
private readonly Timer hoppieTimer = new Timer();
/*
*
* Initilise the ACARS
*
*/
public void init( Main main, INotifier notifier, String logon ) {
Plugin = main;
Acars.notifier = notifier;
hoppieTimer.Elapsed += new ElapsedEventHandler(fetchHoppie);
hoppieTimer.Interval = 45 * 1000;
}
/*
*
* Start the ACARS
*
*/
public void start() {
hoppieTimer.Enabled = true;
Plugin.sendDebug("[ACARS] Starting ACARS");
fetchHoppie(null, null);
}
/*
*
* Stop the ACARS
*
*/
public void stop() {
hoppieTimer.Enabled = false;
Plugin.sendDebug("[ACARS] Stopping ACARS");
}
/*
*
* Fetch data from Hoppie API
*
*/
private async void fetchHoppie( object source, ElapsedEventArgs e ) {
string baseUrl = "http://www.hoppie.nl/acars/system/connect.html";
string logon = Plugin.settingHoppieLogon;
string from = Plugin.connectedCallsign;
string type = "peek";
string to = "SERVER";
if (Plugin.connectedCallsign != null) {
using (HttpClient httpClient = new HttpClient()) {
// Build the complete URL with GET variables
string fullUrl = $"{baseUrl}?logon={logon}&from={from}&type={type}&to={to}";
Plugin.sendDebug($"[ACARS] Fetching Hoppie data with callsign {from}");
try {
HttpResponseMessage response = await httpClient.GetAsync(fullUrl);
if (response.IsSuccessStatusCode) {
string responseContent = await response.Content.ReadAsStringAsync();
parseHoppie(responseContent);
} else {
Plugin.sendDebug($"[ACARS] HttpResponse request failed with status code: {response.StatusCode}");
}
} catch (Exception ex) {
Plugin.sendDebug($"[ACARS] An HttpResponse error occurred: {ex.Message}");
}
}
} else {
Plugin.sendDebug($"[ACARS] fetchHoppie aborted due to missing callsign");
}
}
/*
*
* Parse the Hoppie response
*
*/
private void parseHoppie( String response ) {
Boolean statusOk = response.StartsWith("ok");
if (statusOk) {
foreach (Match match in Regex.Matches(response, @"\{(\d+)\s(\w+)\s(\w+)\s\{([^\}]+)\}\}")) {
// Map the Regex groups
string key = match.Groups[1].Value;
string from = match.Groups[2].Value;
string type = match.Groups[3].Value;
string message = match.Groups[4].Value;
// Clean the messages
message = Regex.Replace(match.Groups[4].Value, @"\/data\d\/\d+\/\d*\/.+\/", "");
message = Regex.Replace(message, @"@", "");
// Check if key doesnt' exist, then add it
if (!hoppieCache.Exists(x => x["key"] == key)) {
// Create a dictionary for the current block and add the key-value pairs
Dictionary<string, string> dataDict = new Dictionary<string, string>
{
{ "key", key },
{ "from", from },
{ "type", type },
{ "message", message}
};
// Add the dictionary to the list
hoppieCache.Add(dataDict);
// Send the message to Pushover
if (cacheLoaded == true && message != "") {
notifier.sendMessage(message, $"{from} ({type.ToUpper()})");
}
Plugin.sendDebug($"[ACARS] Cached {key} with message: {message}");
}
}
cacheLoaded = true;
} else {
Plugin.sendDebug("[ACARS] okCheck Error: " + response);
}
}
}
}