Skip to content

Commit

Permalink
Tweaked hoppie timer and added stop function
Browse files Browse the repository at this point in the history
  • Loading branch information
blt950 committed Jul 26, 2023
1 parent 2d72259 commit 4aa761c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
29 changes: 25 additions & 4 deletions Acars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ internal class Acars {
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
Expand All @@ -20,13 +22,29 @@ internal class Acars {
public void init( Main main, String logon ) {
Plugin = main;

Timer hoppieTimer = new Timer();
hoppieTimer.Elapsed += new ElapsedEventHandler(fetchHoppie);
hoppieTimer.Interval = 45 * 1000;

}

/*
*
* Start the ACARS
*
*/
public void start() {
hoppieTimer.Enabled = true;
Plugin.SendDebug("[ACARS] Starting ACARS");
}

// Run it manually once
fetchHoppie(null, null);
/*
*
* Stop the ACARS
*
*/
public void stop() {
hoppieTimer.Enabled = false;
Plugin.SendDebug("[ACARS] Stopping ACARS");
}

/*
Expand All @@ -46,7 +64,7 @@ private async void fetchHoppie( object source, ElapsedEventArgs e ) {

// Build the complete URL with GET variables
string fullUrl = $"{baseUrl}?logon={logon}&from={from}&type={type}&to={to}";
Plugin.SendDebug("[ACARS] Fetching Hoppie data");
Plugin.SendDebug($"[ACARS] Fetching Hoppie data with callsign {from}");

try {
HttpResponseMessage response = await httpClient.GetAsync(fullUrl);
Expand Down Expand Up @@ -106,6 +124,9 @@ private void parseHoppie( String response ) {
if (cacheLoaded == true && message != "") {
Plugin.SendPushover($"[{type.ToUpper()}] {from}: {message}");
}

Plugin.SendDebug($"Received message with key {key} with msg: {message}");

}


Expand Down
18 changes: 18 additions & 0 deletions Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public void Initialize( IBroker broker ) {
if (settingsLoaded) {
// Subscribe to events according to settings
vPilot.NetworkConnected += OnNetworkConnectedHandler;
vPilot.NetworkDisconnected += OnNetworkDisconnectedHandler;
if (settingPrivateEnabled) vPilot.PrivateMessageReceived += OnPrivateMessageReceivedHandler;
if (settingRadioEnabled) vPilot.RadioMessageReceived += OnRadioMessageReceivedHandler;

Expand Down Expand Up @@ -97,6 +98,23 @@ public async void SendPushover( String text ) {
*/
private void OnNetworkConnectedHandler( object sender, NetworkConnectedEventArgs e ) {
connectedCallsign = e.Callsign;

if (settingHoppieEnabled) {
acars.start();
}
}

/*
*
* Hook: Network disconnected
*
*/
private void OnNetworkDisconnectedHandler( object sender, EventArgs e ) {
connectedCallsign = null;

if (settingHoppieEnabled) {
acars.stop();
}
}

/*
Expand Down

0 comments on commit 4aa761c

Please sign in to comment.