diff --git a/Pilz.Net/Api/Client/IJobsHandler.cs b/Pilz.Net/Api/Client/IJobsHandler.cs new file mode 100644 index 0000000..e0d3ad4 --- /dev/null +++ b/Pilz.Net/Api/Client/IJobsHandler.cs @@ -0,0 +1,10 @@ +using Pilz.Net.Api.Entities; + +namespace Pilz.Net.Api.Client; + +public interface IJobsHandler +{ + Task Execute(string name); + Task Get(string name); + Task> GetAll(); +} diff --git a/Pilz.Net/Api/Client/JobsHandler.cs b/Pilz.Net/Api/Client/JobsHandler.cs new file mode 100644 index 0000000..b66c177 --- /dev/null +++ b/Pilz.Net/Api/Client/JobsHandler.cs @@ -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> GetAll() + { + return (await client.SendRequest.Items>("/jobs", HttpMethod.Get)).EnsureOk().Items; + } + + public async Task Get(string name) + { + return (await client.SendRequest.Item>($"/jobs/{name}", HttpMethod.Get)).EnsureOk().Item; + } + + public async Task Execute(string name) + { + return (await client.SendRequest.Item>($"/jobs/{name}", HttpMethod.Post)).EnsureOk().Item; + } +} diff --git a/Pilz.Net/Api/Entities/JobInfo.cs b/Pilz.Net/Api/Entities/JobInfo.cs new file mode 100644 index 0000000..3cadfb4 --- /dev/null +++ b/Pilz.Net/Api/Entities/JobInfo.cs @@ -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; } +} diff --git a/Pilz.Net/Api/Server/JobsHandler.cs b/Pilz.Net/Api/Server/JobsHandler.cs new file mode 100644 index 0000000..a663c74 --- /dev/null +++ b/Pilz.Net/Api/Server/JobsHandler.cs @@ -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, + }; + } +} diff --git a/Pilz.Net/Pilz.Net.csproj b/Pilz.Net/Pilz.Net.csproj index abf4685..3f7fab1 100644 --- a/Pilz.Net/Pilz.Net.csproj +++ b/Pilz.Net/Pilz.Net.csproj @@ -8,7 +8,7 @@ - 2.8.2 + 2.9.0 diff --git a/Pilz/Jobs/Job.cs b/Pilz/Jobs/Job.cs index 6be1da7..7b341e0 100644 --- a/Pilz/Jobs/Job.cs +++ b/Pilz/Jobs/Job.cs @@ -5,7 +5,7 @@ public class Job private readonly Action? handler; public virtual string Name { get; } - public virtual DateTime LastExecution { get; internal set; } + public virtual DateTime LastExecution { get; set; } public virtual TimeSpan Interval { get; } public Job() diff --git a/Pilz/Jobs/JobCenter.cs b/Pilz/Jobs/JobCenter.cs index fbfc7b5..d766dc5 100644 --- a/Pilz/Jobs/JobCenter.cs +++ b/Pilz/Jobs/JobCenter.cs @@ -11,6 +11,8 @@ public class JobCenter public bool Enabled { get; protected set; } + public IReadOnlyCollection Jobs => jobs; + public JobCenter() { timerRepeat.Elapsed += TimerRepeat_Elapsed; diff --git a/Pilz/Pilz.csproj b/Pilz/Pilz.csproj index d6a4e2e..e7ec605 100644 --- a/Pilz/Pilz.csproj +++ b/Pilz/Pilz.csproj @@ -5,7 +5,7 @@ latest enable annotations - 2.5.2 + 2.5.3