Skip to content

Commit

Permalink
implement cancellationtoken in directory and filehandler
Browse files Browse the repository at this point in the history
  • Loading branch information
KircMax committed Dec 2, 2023
1 parent 872adee commit 82db0c4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 45 deletions.
49 changes: 25 additions & 24 deletions src/Webserver.API/Services/FileHandling/ApiDirectoryHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Siemens.Simatic.S7.Webserver.API.Services.FileHandling
Expand Down Expand Up @@ -40,16 +41,16 @@ public ApiDirectoryHandler(IApiRequestHandler apiRequestHandler, IApiFileHandler
/// the function will only upload the resource and its direct sub-resources
/// </summary>
/// <param name="resource"><see cref="ApiFileResource"/> - e.g. from parsed directory</param>
public async Task DeployAsync(ApiFileResource resource)
public async Task DeployAsync(ApiFileResource resource, CancellationToken cancellationToken = default(CancellationToken))
{
if (resource.Type == Enums.ApiFileResourceType.File)
{
await ApiFileHandler.DeployFileAsync(resource);
await ApiFileHandler.DeployFileAsync(resource, cancellationToken);
}
else
{
var dirName = resource.GetVarNameForMethods();
await ApiRequestHandler.FilesCreateDirectoryAsync(dirName);
await ApiRequestHandler.FilesCreateDirectoryAsync(dirName, cancellationToken);
foreach (var subres in resource.Resources)
{
await DeployAsync(subres);
Expand All @@ -71,14 +72,14 @@ public async Task DeployAsync(ApiFileResource resource)
/// </summary>
/// <param name="resource">the resource to delete</param>
/// <returns>Task for deletion</returns>
public async Task DeleteAsync(ApiFileResource resource)
public async Task DeleteAsync(ApiFileResource resource, CancellationToken cancellationToken = default(CancellationToken))
{
if (resource.Type == Enums.ApiFileResourceType.File)
{
var resName = resource.GetVarNameForMethods();
try
{
await ApiRequestHandler.FilesDeleteAsync(resName);
await ApiRequestHandler.FilesDeleteAsync(resName, cancellationToken);
}
catch (ApiEntityDoesNotExistException) { }
}
Expand All @@ -88,7 +89,7 @@ public async Task DeleteAsync(ApiFileResource resource)
{
foreach (var file in resource.Resources)
{
await DeleteAsync(file);
await DeleteAsync(file, cancellationToken);
}
}
else
Expand All @@ -98,7 +99,7 @@ public async Task DeleteAsync(ApiFileResource resource)
var dirName = resource.GetVarNameForMethods();
try
{
await ApiRequestHandler.FilesDeleteDirectoryAsync(dirName);
await ApiRequestHandler.FilesDeleteDirectoryAsync(dirName, cancellationToken);
}
catch (ApiEntityDoesNotExistException) { }

Expand All @@ -117,12 +118,12 @@ public void Delete(ApiFileResource resource)
/// </summary>
/// <param name="resource">resouce to be browsed</param>
/// <returns>A resource containing everything that is present underneath</returns>
public async Task<ApiFileResource> BrowseAndBuildResourceAsync(ApiFileResource resource)
public async Task<ApiFileResource> BrowseAndBuildResourceAsync(ApiFileResource resource, CancellationToken cancellationToken = default(CancellationToken))
{
// should we clone here?
var res = (ApiFileResource)resource.Clone();
res.Resources = new List<ApiFileResource>();
var browseResponse = await ApiRequestHandler.FilesBrowseAsync(resource.GetVarNameForMethods());
var browseResponse = await ApiRequestHandler.FilesBrowseAsync(resource.GetVarNameForMethods(), cancellationToken);
if (resource.Type == Enums.ApiFileResourceType.Dir)
{
res.Resources = browseResponse.Result.Resources;
Expand All @@ -136,7 +137,7 @@ public async Task<ApiFileResource> BrowseAndBuildResourceAsync(ApiFileResource r
foreach (var subRes in res.Resources)
{
subRes.Parents = parentsForChildren;
var toAdd = await BrowseAndBuildResourceAsync(subRes);
var toAdd = await BrowseAndBuildResourceAsync(subRes, cancellationToken);
toAdd.Parents = parentsForChildren;
toSet.Add(toAdd);
}
Expand All @@ -160,16 +161,16 @@ public ApiFileResource BrowseAndBuildResource(ApiFileResource resource)
/// <param name="resource">the resource to be updated</param>
/// <param name="browsedResource">the resource returned by browsing the plc - make sure the sub-Nodes are present (!)</param>
/// <returns>Task to update the resource</returns>
public async Task UpdateResourceAsync(ApiFileResource resource, ApiFileResource browsedResource)
public async Task UpdateResourceAsync(ApiFileResource resource, ApiFileResource browsedResource, CancellationToken cancellationToken = default(CancellationToken))
{
if (browsedResource.Type != resource.Type)
{
await DeleteAsync(browsedResource);
await DeployAsync(resource);
await DeleteAsync(browsedResource, cancellationToken);
await DeployAsync(resource, cancellationToken);
}
if (resource.Type == Enums.ApiFileResourceType.File)
{
await UpdateFileResourceAsync(resource, browsedResource);
await UpdateFileResourceAsync(resource, browsedResource, cancellationToken);
}
else
{
Expand All @@ -182,9 +183,9 @@ public async Task UpdateResourceAsync(ApiFileResource resource, ApiFileResource
}
else
{
var browseResult = await ApiRequestHandler.FilesBrowseAsync(subResource.GetVarNameForMethods());
var browseResult = await ApiRequestHandler.FilesBrowseAsync(subResource.GetVarNameForMethods(), cancellationToken);
match.Resources = browseResult.Result.Resources;
await UpdateResourceAsync(subResource, match);
await UpdateResourceAsync(subResource, match, cancellationToken);
}
}
}
Expand All @@ -204,7 +205,7 @@ public void UpdateResource(ApiFileResource resource, ApiFileResource browsedReso
/// <param name="resource">the file to be updated</param>
/// <param name="browsedResource">the file returned by browsing the plc</param>
/// <returns>Task to update the File</returns>
public async Task UpdateFileResourceAsync(ApiFileResource resource, ApiFileResource browsedResource)
public async Task UpdateFileResourceAsync(ApiFileResource resource, ApiFileResource browsedResource, CancellationToken cancellationToken = default(CancellationToken))
{
if (resource.Type != Enums.ApiFileResourceType.File)
{
Expand All @@ -217,8 +218,8 @@ public async Task UpdateFileResourceAsync(ApiFileResource resource, ApiFileResou
// compare if an update is necessary
if (!resource.Equals(browsedResource))
{
await ApiRequestHandler.FilesDeleteAsync(resource.GetVarNameForMethods());
await ApiFileHandler.DeployFileAsync(resource);
await ApiRequestHandler.FilesDeleteAsync(resource.GetVarNameForMethods(), cancellationToken);
await ApiFileHandler.DeployFileAsync(resource, cancellationToken);
}
}

Expand All @@ -243,22 +244,22 @@ public void UpdateFileResource(ApiFileResource resource, ApiFileResource browsed
/// <param name="amountOfTriesForResourceDeployment">optional parameter:
/// used to determine wether the DirectoryHandler should retry a upload and compare of the resources found or give up right away (default)
/// </param>
public async Task DeployOrUpdateAsync(ApiFileResource resource, int amountOfTriesForResourceDeployment = 1)
public async Task DeployOrUpdateAsync(ApiFileResource resource, int amountOfTriesForResourceDeployment = 1, CancellationToken cancellationToken = default(CancellationToken))
{
if (amountOfTriesForResourceDeployment < 1)
{
throw new ArgumentOutOfRangeException(nameof(amountOfTriesForResourceDeployment));
}
try
{
var browsedResource = await BrowseAndBuildResourceAsync(resource);
var browsedResource = await BrowseAndBuildResourceAsync(resource, cancellationToken);
int tries = 0;
while (!browsedResource.Equals(resource) && tries < amountOfTriesForResourceDeployment)
{
await UpdateResourceAsync(resource, browsedResource);
await UpdateResourceAsync(resource, browsedResource, cancellationToken);
tries++;
// make sure the browsedResource now is Equal to the one we have - for a directory this means all subNodes are also equal, they are not browsed during the "standard" BRowse
browsedResource = await BrowseAndBuildResourceAsync(resource);
browsedResource = await BrowseAndBuildResourceAsync(resource, cancellationToken);
resource.Resources = resource.Resources.OrderBy(el => el.Type).ThenBy(el => el.Size).ToList();
browsedResource.Resources = browsedResource.Resources.OrderBy(el => el.Type).ThenBy(el => el.Size).ToList();
}
Expand Down Expand Up @@ -286,7 +287,7 @@ public async Task DeployOrUpdateAsync(ApiFileResource resource, int amountOfTrie
}
catch (ApiEntityDoesNotExistException)
{
await DeployAsync(resource);
await DeployAsync(resource, cancellationToken);
}
}
/// <summary>
Expand Down
23 changes: 12 additions & 11 deletions src/Webserver.API/Services/FileHandling/ApiFileHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Siemens.Simatic.S7.Webserver.API.Services.Ticketing;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace Siemens.Simatic.S7.Webserver.API.Services.FileHandling
Expand Down Expand Up @@ -45,14 +46,14 @@ public ApiFileHandler(IApiRequestHandler apiRequestHandler, IApiTicketHandler ap
/// <param name="pathToDownloadDirectory">will default to Downloads but will determine path from -DESKTOP-, replaced "Desktop" by "Downloads"</param>
/// <param name="overwriteExistingFile">choose wether you want to replace an existing file or add another file with that name to you download directory in case one already exists</param>
/// <returns>FileInfo</returns>
public async Task<FileInfo> DownloadFileAsync(string resource, string pathToDownloadDirectory = null, bool overwriteExistingFile = false)
public async Task<FileInfo> DownloadFileAsync(string resource, string pathToDownloadDirectory = null, bool overwriteExistingFile = false, CancellationToken cancellationToken = default(CancellationToken))
{
if (pathToDownloadDirectory != null && !Directory.Exists(pathToDownloadDirectory))
{
throw new DirectoryNotFoundException($"the given directory at {Environment.NewLine}{pathToDownloadDirectory}{Environment.NewLine} has not been found!");
}
var ticketId = (await ApiRequestHandler.FilesDownloadAsync(resource)).Result;
return (await ApiTicketHandler.HandleDownloadAsync(ticketId, pathToDownloadDirectory, overwriteExistingFile)).File_Downloaded;
var ticketId = (await ApiRequestHandler.FilesDownloadAsync(resource, cancellationToken)).Result;
return (await ApiTicketHandler.HandleDownloadAsync(ticketId, pathToDownloadDirectory, overwriteExistingFile, cancellationToken)).File_Downloaded;
}

/// <summary>
Expand All @@ -74,14 +75,14 @@ public FileInfo DownloadFile(string resource, string pathToDownloadDirectory = n
/// <param name="overwriteExistingFile">choose wether you want to replace an existing file or add another file with that name to you download directory in case one already exists</param>
/// <returns>FileInfo</returns>
/// <exception cref="DirectoryNotFoundException"></exception>
public async Task<FileInfo> DataLogs_DownloadAndClearAsync(string resource, string pathToDownloadDirectory = null, bool overwriteExistingFile = false)
public async Task<FileInfo> DataLogs_DownloadAndClearAsync(string resource, string pathToDownloadDirectory = null, bool overwriteExistingFile = false, CancellationToken cancellationToken = default(CancellationToken))
{
if (pathToDownloadDirectory != null && !Directory.Exists(pathToDownloadDirectory))
{
throw new DirectoryNotFoundException($"the given directory at {Environment.NewLine}{pathToDownloadDirectory}{Environment.NewLine} has not been found!");
}
var ticketId = (await ApiRequestHandler.DatalogsDownloadAndClearAsync(resource)).Result;
return (await ApiTicketHandler.HandleDownloadAsync(ticketId, pathToDownloadDirectory, overwriteExistingFile)).File_Downloaded;
var ticketId = (await ApiRequestHandler.DatalogsDownloadAndClearAsync(resource, cancellationToken)).Result;
return (await ApiTicketHandler.HandleDownloadAsync(ticketId, pathToDownloadDirectory, overwriteExistingFile, cancellationToken)).File_Downloaded;
}

/// <summary>
Expand All @@ -101,10 +102,10 @@ public FileInfo DataLogs_DownloadAndClear(string resource, string pathToDownload
/// <param name="resource">resource name on the PLC File API endpoint</param>
/// <param name="filePath">path to the local file to upload</param>
/// <returns>Task to upload the file</returns>
public async Task DeployFileAsync(string resource, string filePath)
public async Task DeployFileAsync(string resource, string filePath, CancellationToken cancellationToken = default(CancellationToken))
{
var ticket = await ApiRequestHandler.FilesCreateAsync(resource);
await ApiTicketHandler.HandleUploadAsync(ticket.Result, filePath);
var ticket = await ApiRequestHandler.FilesCreateAsync(resource, cancellationToken);
await ApiTicketHandler.HandleUploadAsync(ticket.Result, filePath, cancellationToken);
}
/// <summary>
/// Upload a resource to the File API
Expand All @@ -118,11 +119,11 @@ public void DeployFile(string resource, string filePath)
/// Upload a resource to the File API
/// </summary>
/// <param name="resource">resource to upload - filepath built via the ResourcePathResolver, name on PLC File Api built via GetVarNameForMethods()</param>
public async Task DeployFileAsync(ApiFileResource resource)
public async Task DeployFileAsync(ApiFileResource resource, CancellationToken cancellationToken = default(CancellationToken))
{
var varNameForMethods = resource.GetVarNameForMethods();
var accordingFile = Path.Combine(resource.PathToLocalDirectory, resource.Name);
await DeployFileAsync(varNameForMethods, accordingFile);
await DeployFileAsync(varNameForMethods, accordingFile, cancellationToken);
}


Expand Down
13 changes: 7 additions & 6 deletions src/Webserver.API/Services/FileHandling/IApiDirectoryHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
// SPDX-License-Identifier: MIT
using Siemens.Simatic.S7.Webserver.API.Models;
using System.Threading;
using System.Threading.Tasks;

namespace Siemens.Simatic.S7.Webserver.API.Services.FileHandling
Expand All @@ -22,7 +23,7 @@ public interface IApiDirectoryHandler
/// </summary>
/// <param name="resource">resouce to be browsed</param>
/// <returns>A resource containing everything that is present underneath</returns>
Task<ApiFileResource> BrowseAndBuildResourceAsync(ApiFileResource resource);
Task<ApiFileResource> BrowseAndBuildResourceAsync(ApiFileResource resource, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Delete the given resource (and all its sub-resources)
/// </summary>
Expand All @@ -33,7 +34,7 @@ public interface IApiDirectoryHandler
/// </summary>
/// <param name="resource">the resource to delete</param>
/// <returns>Task for deletion</returns>
Task DeleteAsync(ApiFileResource resource);
Task DeleteAsync(ApiFileResource resource, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// make very sure the given resource contains all the data:
/// Resources
Expand All @@ -51,7 +52,7 @@ public interface IApiDirectoryHandler
/// the function will only upload the resource and its direct sub-resources
/// </summary>
/// <param name="resource"><see cref="ApiFileResource"/> - e.g. from parsed directory</param>
Task DeployAsync(ApiFileResource resource);
Task DeployAsync(ApiFileResource resource, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// make very sure the given resource contains all the data:
/// Resources
Expand Down Expand Up @@ -79,14 +80,14 @@ public interface IApiDirectoryHandler
/// <param name="amountOfTriesForResourceDeployment">optional parameter:
/// used to determine wether the DirectoryHandler should retry a upload and compare of the resources found or give up right away (default)
/// </param>
Task DeployOrUpdateAsync(ApiFileResource resource, int amountOfTriesForResourceDeployment = 1);
Task DeployOrUpdateAsync(ApiFileResource resource, int amountOfTriesForResourceDeployment = 1, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Update the given File Resource when necessary
/// </summary>
/// <param name="resource">the file to be updated</param>
/// <param name="browsedResource">the file returned by browsing the plc</param>
/// <returns>Task to update the File</returns>
Task UpdateFileResourceAsync(ApiFileResource resource, ApiFileResource browsedResource);
Task UpdateFileResourceAsync(ApiFileResource resource, ApiFileResource browsedResource, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Update the given File Resource when necessary
/// </summary>
Expand All @@ -100,7 +101,7 @@ public interface IApiDirectoryHandler
/// <param name="resource">the resource to be updated</param>
/// <param name="browsedResource">the resource returned by browsing the plc - make sure the sub-Nodes are present (!)</param>
/// <returns>Task to update the resource</returns>
Task UpdateResourceAsync(ApiFileResource resource, ApiFileResource browsedResource);
Task UpdateResourceAsync(ApiFileResource resource, ApiFileResource browsedResource, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Update the given Resource - and SubResources, when necessary
/// </summary>
Expand Down
Loading

0 comments on commit 82db0c4

Please sign in to comment.