IW4M-Admin/Application/API/Master/Heartbeat.cs
RaidMax 438718507b more api tweaks
removed WebfrontSettings
IW4MAdminSettings are only generated when the file does not exist, placeholder values stored in DefaultSettings.json
2018-04-19 00:48:14 -05:00

59 lines
1.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RestEase;
using SharedLibraryCore;
namespace IW4MAdmin.Application.API.Master
{
public class Heartbeat
{
public static async Task Send(ApplicationManager mgr, bool firstHeartbeat = false)
{
var api = Endpoint.Get();
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(),
Gametype = s.Gametype,
Hostname = s.Hostname,
Map = s.CurrentMap.Name,
MaxClientNum = s.MaxClients,
Id = s.GetHashCode(),
Port = (short)s.GetPort()
}).ToList()
};
if (firstHeartbeat)
{
var message = await api.AddInstance(instance);
}
else
{
var message = await api.UpdateInstance(instance.Id, instance);
}
}
}
}