Skip to content

Commit

Permalink
feat: online status for homepage
Browse files Browse the repository at this point in the history
  • Loading branch information
xfoxfu committed Sep 19, 2024
1 parent f64f016 commit 1996db8
Show file tree
Hide file tree
Showing 8 changed files with 459 additions and 4 deletions.
2 changes: 0 additions & 2 deletions Net.Vatprc.Uniapi/Controllers/Discord/MetarModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ namespace Net.Vatprc.Uniapi.Controllers.Discord;

public class MetarModule(RudiMetarService MetarService, ILogger<MetarModule> Logger) : InteractionModuleBase
{


[SlashCommand("metar", "Get METAR for an airport")]
public async Task WhoAmIAsync(string icao)
{
Expand Down
97 changes: 97 additions & 0 deletions Net.Vatprc.Uniapi/Controllers/UtilController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using Net.Vatprc.Uniapi.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Net.Vatprc.Uniapi.Models;
using System.Collections;
using System.Globalization;
using System.Text.RegularExpressions;

namespace Net.Vatprc.Uniapi.Controllers;

/// <summary>
/// Operate users.
/// </summary>
[ApiController, Route("api/util")]
public partial class UtilController(VatsimService VatsimService) : ControllerBase
{
public class ControllerDto
{
public required int Cid { get; set; }
public required string Name { get; set; }
public required string Callsign { get; set; }
public required string Frequency { get; set; }
}

public class FutureControllerDto
{
public required string Callsign { get; set; }
public required string Name { get; set; }
public required string Start { get; set; }
public required string End { get; set; }
}

public class PilotDto
{
public required int Cid { get; set; }
public required string Name { get; set; }
public required string Callsign { get; set; }
public required string? Departure { get; set; }
public required string? Arrival { get; set; }
public required string? Aircraft { get; set; }
}

public class VatprcStatusDto
{
public required DateTimeOffset LastUpdated { get; set; }
public required IEnumerable<PilotDto> Pilots { get; set; }
public required IEnumerable<ControllerDto> Controllers { get; set; }
public required IEnumerable<FutureControllerDto> FutureControllers { get; set; }
}


[GeneratedRegex("^(Z[BSGUHWJPLYM][A-Z0-9]{2}(_[A-Z0-9]*)?_(DEL|GND|TWR|APP|DEP|CTR))|(PRC_FSS)$")]
protected static partial Regex vatprcControllerRegexp();
[GeneratedRegex("Z[BMSPGJYWLH][A-Z]{2}")]
protected static partial Regex vatprcAirportRegexp();

[HttpGet("online-status")]
[AllowAnonymous]
public async Task<VatprcStatusDto> Status()
{
var vatsimData = await VatsimService.GetOnlineData();
var atcSchedule = await VatsimService.GetAtcSchedule();
return new VatprcStatusDto
{
LastUpdated = vatsimData.General.UpdateTimestamp,
Pilots = vatsimData.Pilots
.Where(x =>
(x.FlightPlan?.Departure != null && vatprcAirportRegexp().IsMatch(x.FlightPlan?.Departure!)) ||
(x.FlightPlan?.Arrival != null && vatprcAirportRegexp().IsMatch(x.FlightPlan?.Arrival!)))
.Select(x => new PilotDto
{
Cid = Convert.ToInt32(x.Cid),
Name = x.Name,
Callsign = x.Callsign,
Departure = x.FlightPlan?.Departure,
Arrival = x.FlightPlan?.Arrival,
Aircraft = x.FlightPlan?.AircraftShort,
}),
Controllers = vatsimData.Controllers
.Where(x => vatprcAirportRegexp().IsMatch(x.Callsign))
.Select(x => new ControllerDto
{
Cid = Convert.ToInt32(x.Cid),
Name = x.Name,
Callsign = x.Callsign,
Frequency = x.Frequency,
}),
FutureControllers = atcSchedule.Select(x => new FutureControllerDto
{
Name = $"{x.User.FirstName} {x.User.LastName}",
Callsign = x.Callsign,
Start = x.Start.ToUniversalTime().ToString("dd HH:mm"),
End = x.Finish.ToUniversalTime().ToString("dd HH:mm"),
}),
};
}
}
2 changes: 2 additions & 0 deletions Net.Vatprc.Uniapi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
global using f64 = double;
global using Net.Vatprc.Uniapi;
global using UniApi = Net.Vatprc.Uniapi;
global using static Net.Vatprc.Uniapi.Utils.Utils;

using System.CommandLine;
using System.Text.Json.Serialization;
Expand Down Expand Up @@ -214,6 +215,7 @@ error message example.
});

RudiMetarService.ConfigureOn(builder);
VatsimService.ConfigureOn(builder);

var app = builder.Build();

Expand Down
4 changes: 2 additions & 2 deletions Net.Vatprc.Uniapi/Services/DiscordWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ IServiceProvider ServiceProvider
) : IHostedService
{
protected InteractionService Interaction { get; init; } = new(Client.Rest);
protected static ActivitySource ActivitySource =
new ActivitySource(typeof(DiscordWorker).FullName ?? throw new ArgumentNullException());
protected readonly static ActivitySource ActivitySource =
new(typeof(DiscordWorker).FullName ?? throw new ArgumentNullException());

public async Task StartAsync(CancellationToken ct)
{
Expand Down
2 changes: 2 additions & 0 deletions Net.Vatprc.Uniapi/Services/RudiMetarService.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using Flurl;
using Flurl.Http;
using Microsoft.Extensions.Options;
Expand All @@ -17,6 +18,7 @@ public async Task<string> GetMetar(string icao)
{
var response = await Options.Value.Endpoint
.SetQueryParam("id", icao)
.WithHeader("User-Agent", UniapiUserAgent)
.GetAsync();
return await response.GetStringAsync();
}
Expand Down
Loading

0 comments on commit 1996db8

Please sign in to comment.