jobs api
This commit is contained in:
10
Pilz.Net/Api/Client/IJobsHandler.cs
Normal file
10
Pilz.Net/Api/Client/IJobsHandler.cs
Normal 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();
|
||||||
|
}
|
||||||
22
Pilz.Net/Api/Client/JobsHandler.cs
Normal file
22
Pilz.Net/Api/Client/JobsHandler.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
8
Pilz.Net/Api/Entities/JobInfo.cs
Normal file
8
Pilz.Net/Api/Entities/JobInfo.cs
Normal 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; }
|
||||||
|
}
|
||||||
45
Pilz.Net/Api/Server/JobsHandler.cs
Normal file
45
Pilz.Net/Api/Server/JobsHandler.cs
Normal 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,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,7 +8,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Version>2.8.2</Version>
|
<Version>2.9.0</Version>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -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()
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -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'">
|
||||||
|
|||||||
Reference in New Issue
Block a user