IW4M-Admin/Application/API/Master/Heartbeat.cs

67 lines
2.1 KiB
C#
Raw Normal View History

2018-04-18 16:46:53 -04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
2018-04-18 16:46:53 -04:00
using System.Threading.Tasks;
using RestEase;
using SharedLibraryCore;
namespace IW4MAdmin.Application.API.Master
{
public class HeartbeatState
2018-04-18 16:46:53 -04:00
{
public bool Connected { get; set; }
public CancellationToken Token { get; set; }
}
2018-04-18 16:46:53 -04:00
public class Heartbeat
{
2018-04-18 16:46:53 -04:00
public static async Task Send(ApplicationManager mgr, bool firstHeartbeat = false)
{
var api = Endpoint.Get();
2018-04-18 16:46:53 -04:00
if (firstHeartbeat)
{
var token = await api.Authenticate(new AuthenticationId()
{
Id = mgr.GetApplicationSettings().Configuration().Id
});
api.AuthorizationToken = $"Bearer {token.AccessToken}";
}
var instance = new ApiInstance()
{
Id = mgr.GetApplicationSettings().Configuration().Id,
Uptime = (int)(DateTime.UtcNow - mgr.StartTime).TotalSeconds,
Version = (float)Program.Version,
Servers = mgr.Servers.Select(s =>
new ApiServer()
{
ClientNum = s.ClientNum,
Game = s.GameName.ToString(),
Version = s.Version,
2018-04-18 16:46:53 -04:00
Gametype = s.Gametype,
Hostname = s.Hostname,
Map = s.CurrentMap.Name,
MaxClientNum = s.MaxClients,
Id = s.EndPoint,
Port = (short)s.Port,
IPAddress = s.IP
2018-04-18 16:46:53 -04:00
}).ToList()
};
if (firstHeartbeat)
{
var message = await api.AddInstance(instance);
2018-04-18 16:46:53 -04:00
}
else
{
var message = await api.UpdateInstance(instance.Id, instance);
2018-04-18 16:46:53 -04:00
}
}
}
}