This commit is contained in:
Pilzinsel64
2025-10-31 09:29:28 +01:00
parent e2a337a7aa
commit 6ae5cd51b0
8 changed files with 90 additions and 3 deletions

View File

@@ -0,0 +1,10 @@
using Pilz.Net.Api.Entities;
namespace Pilz.Net.Api.Client;
public interface IJobsHandler
{
Task<JobInfo> Execute(string name);
Task<JobInfo> Get(string name);
Task<IEnumerable<JobInfo>> GetAll();
}

View File

@@ -0,0 +1,22 @@
using Pilz.Net.Api.Entities;
using Pilz.Net.Api.Messages;
namespace Pilz.Net.Api.Client;
public class JobsHandler(IApiClient client) : IJobsHandler
{
public async Task<IEnumerable<JobInfo>> GetAll()
{
return (await client.SendRequest<ItemMessages<JobInfo>.Items>("/jobs", HttpMethod.Get)).EnsureOk().Items;
}
public async Task<JobInfo> Get(string name)
{
return (await client.SendRequest<ItemMessages<JobInfo>.Item>($"/jobs/{name}", HttpMethod.Get)).EnsureOk().Item;
}
public async Task<JobInfo> Execute(string name)
{
return (await client.SendRequest<ItemMessages<JobInfo>.Item>($"/jobs/{name}", HttpMethod.Post)).EnsureOk().Item;
}
}

View File

@@ -0,0 +1,8 @@
namespace Pilz.Net.Api.Entities;
public class JobInfo
{
public string? Name { get; set; }
public DateTime LastExecution { get; set; }
public TimeSpan Interval { get; set; }
}

View File

@@ -0,0 +1,45 @@
using Pilz.Jobs;
using Pilz.Net.Api.Entities;
using Pilz.Net.Extensions;
namespace Pilz.Net.Api.Server;
public class JobsHandler(IApiServer server)
{
[ApiMessageHandler("/jobs", "GET")]
public virtual ApiResult GetAll()
{
return server.Jobs.Jobs.Select(ToClient).ToItemsResult();
}
[ApiMessageHandler("/jobs/{name}", "GET")]
public virtual ApiResult Get(string name)
{
if (server.Jobs.Jobs.FirstOrDefault(n => n.Name == name) is not Job job)
return ApiResult.BadRequest();
return ToClient(job).ToItemResult();
}
[ApiMessageHandler("/jobs/{name}", "POST")]
public virtual ApiResult Execute(string name)
{
if (server.Jobs.Jobs.FirstOrDefault(n => n.Name == name) is not Job job)
return ApiResult.BadRequest();
var now = DateTime.Now;
job.Execute(new(server.Jobs));
job.LastExecution = now;
return ApiResult.Ok();
}
protected virtual JobInfo ToClient(Job job)
{
return new JobInfo
{
Name = job.Name,
Interval = job.Interval,
LastExecution = job.LastExecution,
};
}
}

View File

@@ -8,7 +8,7 @@
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>
<Version>2.8.2</Version> <Version>2.9.0</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@@ -5,7 +5,7 @@ public class Job
private readonly Action<JobContext>? handler; private readonly Action<JobContext>? handler;
public virtual string Name { get; } public virtual string Name { get; }
public virtual DateTime LastExecution { get; internal set; } public virtual DateTime LastExecution { get; set; }
public virtual TimeSpan Interval { get; } public virtual TimeSpan Interval { get; }
public Job() public Job()

View File

@@ -11,6 +11,8 @@ public class JobCenter
public bool Enabled { get; protected set; } public bool Enabled { get; protected set; }
public IReadOnlyCollection<Job> Jobs => jobs;
public JobCenter() public JobCenter()
{ {
timerRepeat.Elapsed += TimerRepeat_Elapsed; timerRepeat.Elapsed += TimerRepeat_Elapsed;

View File

@@ -5,7 +5,7 @@
<LangVersion>latest</LangVersion> <LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>annotations</Nullable> <Nullable>annotations</Nullable>
<Version>2.5.2</Version> <Version>2.5.3</Version>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0'"> <PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">